生活随笔
收集整理的這篇文章主要介紹了
【转】 java自定义注解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?java注解是附加在代碼中的一些元信息,用于一些工具在編譯、運行時進行解析和使用,起到說明、配置的功能。
注解不會也不能影響代碼的實際邏輯,僅僅起到輔助性的作用。包含在 java.lang.annotation 包中。
1、元注解
元注解是指注解的注解。包括? @Retention @Target @Document @Inherited四種。
1.1、@Retention: 定義注解的保留策略
@Retention(RetentionPolicy.
SOURCE)??
//注解僅存在于源碼中,在class字節(jié)碼文件中不包含 @Retention(RetentionPolicy.
CLASS)
?????// 默認的保留策略,注解會在class字節(jié)碼文件中存在,但運行時無法獲得, @Retention(RetentionPolicy.RUNTIME)??
// 注解會在class字節(jié)碼文件中存在,在運行時可以通過反射獲取到 1.2、@Target:定義注解的作用目標 其定義的源碼為:?
[java] view plaincopy
@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)
//方法參數(shù) @Target(ElementType.CONSTRUCTOR)?
//構造函數(shù) @Target(ElementType.LOCAL_VARIABLE)
//局部變量 @Target(ElementType.ANNOTATION_TYPE)
//注解 @Target(ElementType.PACKAGE)?/
//包??? 由以上的源碼可以知道,他的elementType?可以有多個,一個注解可以為類的,方法的,字段的等等
1.3、@Document:說明該注解將被包含在javadoc中 1.4、@Inherited:說明子類可以繼承父類中的該注解 2、java 注解的自定義 下面是自定義注解的一個例子
[java] view plaincopy
@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;??}????? [java] view plaincopy
@Documented??@Retention(RetentionPolicy.RUNTIME)??@Target(ElementType.METHOD)??@Inherited??public?@interface?HelloWorld?{??????public?String?name()default?"";??}?? @Retention(RetentionPolicy.RUNTIME)
定義的這個注解是注解會在class字節(jié)碼文件中存在,在運行時可以通過反射獲取到。
@Target({ElementType.TYPE,ElementType.METHOD})
因此這個注解可以是類注解,也可以是方法的注解
這樣一個注解就自定義好了,當然注解里面的成員可以為基本的數(shù)據(jù)類型,也可以為數(shù)據(jù),Object等等
?
3 注解是定義好了,那么怎么來得到,解析注解呢?
java的反射機制可以幫助,得到注解,代碼如下:
[java] view plaincopy
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");??????????????}??????????}??????}????????}?? 前一個方法是解析得到方法注解的,后一個方法是得到類注解的
以下是測試方法類
[java] view plaincopy
@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!");??????????}??????}??}?? ?
[html] view plaincopy
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);??????}?
轉載于:https://www.cnblogs.com/sunson/p/3166975.html
總結
以上是生活随笔為你收集整理的【转】 java自定义注解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。