Created
April 3, 2018 06:38
-
-
Save hengyunabc/e763297d76b8299019721a0bf95e815e to your computer and use it in GitHub Desktop.
jdk8里的 Repeatable annotation的实现,反编绎可以看到
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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