日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

自定义Java annotation及解析和使用

發布時間:2023/12/19 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义Java annotation及解析和使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用@interface定義一個annotation:

package annotationTest;import java.lang.annotation.*;@Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MethodInfo {String author() default "Jerry";String version() default "1.0";String date();String comment(); }

新建一個類,給其方法添加上剛才創建的注解:

package annotationTest;// Jerry change for demopublic class AnnotationExample {@Override@MethodInfo(author = "xxx",version = "1.0",date = "2015/03/26",comment = "override toString()")public String toString() {return "AnnotationExample{}";}@Deprecated@MethodInfo(comment = "deprecated method", date = "2015/03/26")public static void oldMethod() {System.out.println("old method, don't use it.");}@MethodInfo(author = "Pankaj", comment = "Main method", date = "Nov 17 2012", version = "1.0")public static void genericsTest() {oldMethod();} }

打印的輸出:

@annotationTest.MethodInfo(version="1.0", author="xxx", date="2015/03/26", comment="override toString()") in method:public java.lang.String annotationTest.AnnotationExample.toString()Method with revision no 1.0 = public java.lang.String annotationTest.AnnotationExample.toString() @annotationTest.MethodInfo(version="1.0", author="Pankaj", comment="Main method", date="Nov 17 2012") in method:public static void annotationTest.AnnotationExample.genericsTest()Method with revision no 1.0 = public static void annotationTest.AnnotationExample.genericsTest() @java.lang.Deprecated(forRemoval=false, since="") in method:public static void annotationTest.AnnotationExample.oldMethod() @annotationTest.MethodInfo(version="1.0", author="Jerry", comment="deprecated method", date="2015/03/26") in method:public static void annotationTest.AnnotationExample.oldMethod()Method with revision no 1.0 = public static void annotationTest.AnnotationExample.oldMethod()

同理,新建一個description注解,用來修飾People類:

package annotationTest;import java.lang.annotation.Documented; import java.lang.annotation.Inherited; import java.lang.annotation.*;@Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Description {String desc();String author();int age() default 18; } package annotationTest;import java.lang.annotation.Annotation; import java.lang.reflect.Method;@Description(author = "Jerry", desc = "Class annotation", age = 35) public class People {@Description(author = "Jerry 2", desc = "method annotation", age = 35)public void hello(){}@SuppressWarnings({ "rawtypes", "unchecked" })public static void main(String[] arg){try {// 使用類加載器加載類Class c = Class.forName("annotationTest.People");// 找到類上面的注解boolean isExist = c.isAnnotationPresent(Description.class);// 上面的這個方法是用這個類來判斷這個類是否存在Description這樣的一個注解if (isExist) {// 拿到注解實例,解析類上面的注解Description d = (Description) c.getAnnotation(Description.class);System.out.println(d.desc());}//獲取所有的方法Method[] ms = c.getMethods();// 遍歷所有的方法for (Method m : ms) {boolean isExist1 = m.isAnnotationPresent(Description.class);if (isExist1) {Description d1=m.getAnnotation(Description.class);System.out.println(d1.desc());}}//另一種解析方法for (Method m : ms) {//拿到方法上的所有的注解Annotation[] as=m.getAnnotations();for (Annotation a : as) {//用二元操作符判斷a是否是Description的實例if (a instanceof Description) {Description d=(Description) a;System.out.println(d.desc());}}}} catch (ClassNotFoundException e) {e.printStackTrace();}}}

輸出:

Class annotation
method annotation
method annotation

總結

以上是生活随笔為你收集整理的自定义Java annotation及解析和使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。