Java Annotation的RetentionPolicy介绍
Java Annotation對(duì)應(yīng)的Retention有3種,在RetentionPolicy中定義,有3種:
RUNTIME
大部分情況下,我們都是使用RUNTIME這個(gè)Policy。
下面就是一個(gè)RUNTIME Annotation的例子。
先定義Annotation:
| 1 2 3 4 5 6 | @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyClassRuntimeAnno { ????String name(); ????int level() default 1; } |
然后在CLASS前面使用這個(gè)Annotation。
| 1 2 3 | @MyClassRuntimeAnno(name = "simple", level = 10) public class SimpleObj { } |
最后寫一個(gè)testcase通過反射可以獲取這個(gè)類的Annotation進(jìn)行后續(xù)操作。
| 1 2 3 4 5 6 7 8 | @Test public void testGetAnnotation() { ????Annotation[] annotations = SimpleObj.class.getAnnotations(); ????System.out.println(Arrays.toString(annotations)); ????MyClassRuntimeAnno myClassAnno = SimpleObj.class.getAnnotation(MyClassRuntimeAnno.class); ????System.out.println(myClassAnno.name() + ", " + myClassAnno.level()); ????System.out.println(myClassAnno == annotations[0]); } |
SOURCE
SOURCE這個(gè)policy表示注解保留在源代碼中,但是編譯的時(shí)候會(huì)被編譯器所丟棄。 由于在編譯的過程中這個(gè)注解還被保留著,所以在編譯過程中可以針對(duì)這個(gè)policy進(jìn)行一些操作。比如在自動(dòng)生成java代碼的場(chǎng)景下使用。最常見的就是lombok的使用了,可以自動(dòng)生成field的get和set方法以及toString方法,構(gòu)造器等;消除了冗長的java代碼。
SOURCE這個(gè)policy可以使用jdk中的javax.annotation.processing.*包中的processor處理器進(jìn)行注解的處理過程。
以1個(gè)編譯過程中會(huì)打印類中的方法的例子來說明SOUCRE這個(gè)policy的作用:
首先定義一個(gè)Printer注解:
| 1 2 3 4 | @Retention(RetentionPolicy.SOURCE) @Target(ElementType.METHOD) public @interface Printer { } |
然后一個(gè)類的方法使用這個(gè)注解:
| 1 2 3 4 5 6 7 8 9 10 11 12 | public class SimpleObject { ????@Printer ????public void methodA() { ????} ????public void methodB() { ????} } |
創(chuàng)建對(duì)應(yīng)的Processor:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | @SupportedAnnotationTypes({"me.format.annotaion.Printer"}) public class PrintProcessor extends AbstractProcessor { ????@Override ????public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { ????????Messager messager = processingEnv.getMessager(); ????????messager.printMessage(Diagnostic.Kind.NOTE, "start to use PrintProcessor .."); ????????Set<? extends Element> rootElements = roundEnv.getRootElements(); ????????messager.printMessage(Diagnostic.Kind.NOTE, "root classes: "); ????????for(Element root : rootElements) { ????????????messager.printMessage(Diagnostic.Kind.NOTE, ">> " + root.toString()); ????????} ????????messager.printMessage(Diagnostic.Kind.NOTE, "annotation: "); ????????for(TypeElement te : annotations) { ????????????messager.printMessage(Diagnostic.Kind.NOTE, ">>> " + te.toString()); ????????????Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(te); ????????????for(Element ele : elements) { ????????????????messager.printMessage(Diagnostic.Kind.NOTE, ">>>> " + ele.toString()); ????????????} ????????} ????????return true; ????} ????@Override ????public SourceVersion getSupportedSourceVersion() { ????????return SourceVersion.latestSupported(); ????} } |
然后先使用javac編譯Printer和PrintProcessor:
| 1 | javac -d classes src/main/java/me/format/annotation/Printer.java src/main/java/me/format/annotation/PrintProcessor.java |
最后再使用javac中的processor參數(shù)處理:
| 1 | javac -cp classes -processor me.format.annotation.PrintProcessor -d classes src/main/java/me/format/annotation/SimpleObject.java |
控制臺(tái)打印出:
| 1 2 3 4 5 6 | 注: start to use PrintProcessor .. 注: root classes: 注: >> hello.annotation.SimpleObject 注: annotation: 注: >>> hello.annotation.Printer 注: >>>> methodA() |
CLASS
CLASS和RUNTIME的唯一區(qū)別是RUNTIME在運(yùn)行時(shí)期間注解是存在的,而CLASS則不存在。
我們通過asm來獲取class文件里的annotation。
首先定義注解:
policy為CLASS的注解。
| 1 2 3 4 5 6 7 | @Retention(RetentionPolicy.CLASS) @Target(ElementType.TYPE) public @interface Meta { ????String name(); } |
policy為RUNTIME的注解。
| 1 2 3 4 5 6 7 | @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Header { ????int code(); } |
使用注解:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Meta(name = "obj") @Header(code = 200) public class AnnotationObject { ????private String val; ????public String getVal() { ????????return val; ????} ????public void setVal(String val) { ????????this.val = val; ????} } |
編譯這3個(gè)java文件得到字節(jié)碼文件AnnotationObject.class:
| 1 | javac -d classes src/main/java/me/format/annotaion/AnnotationObject.java src/main/java/me/format/annotation/Meta.java src/main/java/me/format/annotation/Header.java |
使用asm獲取字節(jié)碼文件中的注解:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ClassNode classNode = new ClassNode(); ClassReader cr = new ClassReader(new FileInputStream("classes/me/format/annotation/AnnotationObject.class")); cr.accept(classNode, 0); System.out.println("Class Name: " + classNode.name); System.out.println("Source File: " + classNode.sourceFile); System.out.println("invisible: "); AnnotationNode anNode = null; for (Object annotation : classNode.invisibleAnnotations) { ????anNode = (AnnotationNode) annotation; ????System.out.println("Annotation Descriptor : " + anNode.desc); ????System.out.println("Annotation attribute pairs : " + anNode.values); } System.out.println("visible: "); for (Object annotation : classNode.visibleAnnotations) { ????anNode = (AnnotationNode) annotation; ????System.out.println("Annotation Descriptor : " + anNode.desc); ????System.out.println("Annotation attribute pairs : " + anNode.values); } |
打印出:
| 1 2 3 4 5 6 7 8 | Class Name: me/format/annotation/AnnotationObject Source File: AnnotationObject.java invisible: Annotation Descriptor : Lme/format/annotation/Meta; Annotation attribute pairs : [name, obj] visible: Annotation Descriptor : Lme/format/annotation/Header; Annotation attribute pairs : [code, 200] |
其中policy為CLASS的注解編譯完后不可見,而policy為RUNTIME的注解編譯后可見。
同樣,我們可以使用javap查看編譯后的信息:
| 1 | javap -v me.format.annotation.AnnotationObject |
會(huì)打印出注解的visible信息:
| 1 2 3 4 5 6 7 8 9 | #16 = Utf8?????????????? AnnotationObject.java #17 = Utf8?????????????? RuntimeVisibleAnnotations #18 = Utf8?????????????? Lhello/annotation/Header; #19 = Utf8?????????????? code #20 = Integer??????????? 200 #21 = Utf8?????????????? RuntimeInvisibleAnnotations #22 = Utf8?????????????? Lhello/annotation/Meta; #23 = Utf8?????????????? name #24 = Utf8?????????????? obj |
from:?http://www.importnew.com/24051.html
總結(jié)
以上是生活随笔為你收集整理的Java Annotation的RetentionPolicy介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kafka 指南
- 下一篇: Java线程同步:synchronize