Skip to content

Instantly share code, notes, and snippets.

@hengyunabc
Created April 3, 2018 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengyunabc/e763297d76b8299019721a0bf95e815e to your computer and use it in GitHub Desktop.
Save hengyunabc/e763297d76b8299019721a0bf95e815e to your computer and use it in GitHub Desktop.
jdk8里的 Repeatable annotation的实现,反编绎可以看到
import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import abc.Test.AAA;
// @BBB(value={@AAA(value="aaa"), @AAA(value="abc")}) // 反编绎实际上这个
@AAA(value = "aaa")
@AAA(value = "abc")
public class Test {
@Documented
@Retention(RetentionPolicy.RUNTIME)
@java.lang.annotation.Target(ElementType.TYPE)
@Repeatable(BBB.class)
public @interface AAA {
String value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@java.lang.annotation.Target(ElementType.TYPE)
public @interface BBB {
AAA[] value();
}
public static void main(String[] args) {
System.err.println(Test.class.getAnnotation(AAA.class)); // null
System.err.println(Test.class.getAnnotation(BBB.class));
System.err.println(Arrays.toString(Test.class.getAnnotationsByType(AAA.class))); // since jdk8
for(Annotation a : Test.class.getAnnotations()) {
System.err.println(a);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment