標籤:
java註解是附加在代碼中的一些元資訊,用於一些工具在編譯、運行時進行解析和使用,起到說明、配置的功能。
註解不會也不能影響代碼的實際邏輯,僅僅起到輔助性的作用。包含在 java.lang.annotation 包中。
1、元註解
元註解是指註解的註解。包括 @Retention @Target @Document @Inherited四種。
1.1、@Retention: 定義註解的保留原則
@Retention(RetentionPolicy.SOURCE) //註解僅存在於源碼中,在class位元組碼檔案中不包含
@Retention(RetentionPolicy.CLASS) // 預設的保留原則,註解會在class位元組碼檔案中存在,但運行時無法獲得,
@Retention(RetentionPolicy.RUNTIME) // 註解會在class位元組碼檔案中存在,在運行時可以通過反射擷取到
1.2、@Target:定義註解的作用目標
其定義的源碼為:
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Target { ElementType[] value();}
@Target(ElementType.TYPE) //介面、類、枚舉、註解
@Target(ElementType.FIELD) //欄位、枚舉的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法參數
@Target(ElementType.CONSTRUCTOR) //建構函式
@Target(ElementType.LOCAL_VARIABLE)//局部變數
@Target(ElementType.ANNOTATION_TYPE)//註解
@Target(ElementType.PACKAGE) ///包
由以上的源碼可以知道,他的elementType 可以有多個,一個註解可以為類的,方法的,欄位的等等
1.3、@Document:說明該註解將被包含在javadoc中
1.4、@Inherited:說明子類可以繼承父類中的該註解
2、java 註解的自訂
下面是自訂註解的一個例子
@Documented@Target({ElementType.TYPE,ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public @interface Yts { public enum YtsType{util,entity,service,model} public YtsType classType() default YtsType.util;}
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@Inheritedpublic @interface HelloWorld { public String name()default "";}
@Retention(RetentionPolicy.RUNTIME)
定義的這個註解是註解會在class位元組碼檔案中存在,在運行時可以通過反射擷取到。
@Target({ElementType.TYPE,ElementType.METHOD})
因此這個註解可以是類註解,也可以是方法的註解
這樣一個註解就自訂好了,當然註解裡面的成員可以為基本的資料類型,也可以為資料,Object等等
3 註解是定義好了,那麼怎麼來得到,解析註解呢?
java的反射機制可以協助,得到註解,代碼如下:
public class ParseAnnotation { public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});for(Method method : clazz.getDeclaredMethods()){ HelloWorld say = method.getAnnotation(HelloWorld.class); String name = ""; if(say != null){ name = say.name(); method.invoke(obj, name); } Yts yts = (Yts)method.getAnnotation(Yts.class); if(yts != null){ if(YtsType.util.equals(yts.classType())){ System.out.println("this is a util method"); }else{ System.out.println("this is a other method"); } } } } @SuppressWarnings("unchecked") public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ Yts yts = (Yts) clazz.getAnnotation(Yts.class); if(yts != null){ if(YtsType.util.equals(yts.classType())){ System.out.println("this is a util class"); }else{ System.out.println("this is a other class"); } } } }
前一個方法是解析得到方法註解的,後一個方法是得到類註解的
以下是測試方法類
@Yts(classType =YtsType.util)public class SayHell { @HelloWorld(name = " 小明 ") @Yts public void sayHello(String name){ if(name == null || name.equals("")){ System.out.println("hello world!"); }else{ System.out.println(name + "say hello world!"); } }}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException { ParseAnnotation parse = new ParseAnnotation(); parse.parseMethod(SayHell.class); parse.parseType(SayHell.class); }
java自訂註解