Java注解使用及原理解析
基本特性
1、jdk 1.5之后才引入的。
2、用來說明程序的。(注釋是給程序員看的,注解就是給電腦看的)
java注解的作用分類
1、編寫文檔:通過代碼標識的注解生成文檔?!旧蒬oc文檔】
2、代碼分析:通過代碼標識的注解對代碼進行分析?!臼褂梅瓷洹?/p>
3、編譯檢查:通過代碼標識的注解讓編譯器能夠實現基本的編譯檢查。【override】
測試類:
/** * 我的javadoc測試 */public class TestCode { /** * 計算兩個數的和 * @param a 整數a * @param b 整數b * @return 返回兩個數的和 */ public int add(int a, int b){ return a+b; }}
對于2、3兩點我們應該是知道的。盡管可能不知道里面的原理。但是是平時都在用的。但是對于1點還可以生成doc文檔?
測試操作如下:
D:softjdkbinjavadoc.exe .TestCode.java -encoding utf-8 -docEncoding utf-8 -charset utf-8
生成了一大堆的東西:
打開TestCode.html可以發現,我們的java api手冊就是這樣生產的。
注解來源分類
1、jdk自帶的注解,如常見的override(重寫校驗),deprecated(表示棄用)
2、自定義的注解
1)格式, 以override為例:
2)注解的本質
我們編寫一個簡單的注解
MyAnnotation.java
public @interface MyAnnotation {}
我們通過編譯和反編譯看下最終是什么樣的結果:
D:softjdkbinjavac.exe MyAnnotation.java
D:softjdkbinjavap.exe MyAnnotation.class
結果如下:
public interface MyAnnotation extends java.lang.annotation.Annotation {
}
可以發現注解的本質就是接口,這個接口繼承了jdk里面的Annotation接口。
3)注解的屬性
由于注解本質為接口,那么里面可以定義未實現的方法。這些稱為注解的“屬性”。
屬性的返回類型有(返回值不能為void):
基本數據類型 String 枚舉 注解 以及以上四種類型的數組例子:
public enum Person { PS;}public @interface Annotation2 {}public @interface MyAnnotation { String stringValue(); int integerValue(); Person personValue(); Annotation2 myAnnotationValue(); String[] stringArrayValue();}
屬性的使用,需要注意幾點:
定義了屬性在使用的時候就要給屬性賦值,除非設置default值。如:String stringValue() default 'aaa'; 如果屬性為value且屬性只有這一個,那么value可以省略,直接填寫屬性值。 如果是數組,需要用{}包含起來。public @interface MyAnnotation { String stringValue() default 'xxx'; int integerValue(); String[] stringArrayValue();}public @interface Annotation2 { String value();}@MyAnnotation(integerValue = 1, stringArrayValue = {'aaa', 'bbb'})@Annotation2('default')public class TestCode { /** * 計算兩個數的和 * @param a 整數a * @param b 整數b * @return 返回兩個數的和 */ public int add(int a, int b){ return a+b; } @Override public String toString() { return super.toString(); }}
元注解
元注解是你在編寫注解的時候,上面加的注解,就是注解的注解。主要有4個。
@target, 用于指定注解的使用位置。如@Target(ElementType.ANNOTATION_TYPE),@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})。 @Inherited,表示父類加了這個注解,子類也自動加上。 @Documented, 表示這個注解的信息在執行javadoc的時候是否抽取到api文檔中。 @Retention,表示注解被保留的階段,java類,class文件,以及被jvm讀取??偣踩N。RetentionPolicy.SOURCE, RetentionPolicy.CLASS, RetentionPolicy.RUNTIME元注解的內容,可以到jdk源碼里面看一下,更有利于理解。
解析注解
這個是最關鍵了,以上加了這么多的屬性,并且還為這些屬性附了值,那么是希望程序讀取這些值,進行使用的。那其實就是要看如何拿到這些注解配置的值。
測試:
MyAnnotition.java:
package annotation_;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation { String stringValue() default 'xxx'; int integerValue();}
TestCode.java:
package annotation_;@MyAnnotation(integerValue = 1)public class TestCode { public static void main(String[] args) { Class<TestCode> testCodeClass = TestCode.class; MyAnnotation myAnnotation = testCodeClass.getAnnotation(MyAnnotation.class); int i = myAnnotation.integerValue(); String s = myAnnotation.stringValue(); System.out.printf('i = %d, s = %sn', i, s); }}
輸出結果:
Connected to the target VM, address: ’127.0.0.1:49586’, transport: ’socket’i = 1, s = xxxDisconnected from the target VM, address: ’127.0.0.1:49586’, transport: ’socket’
Process finished with exit code 0
是不是感覺可以當配置文件使用。但是最主要的問題是myAnnotation.integerValue(),myAnnotation.stringValue()為什么可以拿到對應的值,這個也是最核心的問題。
那就是getAnnotation里面返回了一個實現了MyAnnotation注解(注解的本質是接口)的實例。這個類大概是長這樣的。
package annotation_;import java.lang.annotation.Annotation;public class MyAnnotationImpl implements MyAnnotation{ public String stringValue() { return 'xxx'; } public int integerValue() { return 0; } public Class<? extends Annotation> annotationType() { return null; }}
所以就可以通過抽象方法獲取到對應的值。(如何生成這樣的一個類,只是學習注解,可以不關心。要不然,只能看里面的源碼。因為如果自定義注解,你只會用到這一步,去獲取值。)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
