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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义注解的实现

發布時間:2024/4/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义注解的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  注解看起來很神秘,其實看穿了就是一種標記,通過運行時獲取標記進行后續處理。說到運行時自然離不開反射,所以注解就是反射的一種應用。使用元注解就可以實現自定義注解,元注解只有4個:Retention、Target、Document和Inherited,分別用于標記注解的保留策略、應用目標、是否包含于javadoc、是否允許子類繼承。直接看代碼:

package com.wulinfeng.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Type {String name() default ""; } package com.wulinfeng.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Element {String value() default ""; } package com.wulinfeng.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodName {int version() default 0; } package com.wulinfeng.annotation;import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; import java.util.TreeMap;@Type(name = "hello") public class Annotations {Map<Integer, Method> methodMap = new TreeMap<>();@Element("world")private String name;@MethodName(version = 1)public void logs1(Class<?> clazz) {// 取類注釋值for (Annotation annotation : clazz.getAnnotations())if (annotation instanceof Type) {name = ((Type) annotation).name();break;}System.out.println("hello " + name);}@MethodName(version = 2)public void logs2(Class<?> clazz) {// 取字段name的注解值Field[] fields = Annotations.class.getDeclaredFields();for (Field field : fields) {if (field.isAnnotationPresent(Element.class)) {Element element = field.getAnnotation(Element.class);name = element.value();break;}}System.out.println("hello " + name);}// 加載方法版本號映射public void initialize(Class<?> clazz) {Method[] methods = clazz.getMethods();for (Method method : methods) {for (Annotation annotation : method.getAnnotations()) {if (annotation instanceof MethodName) {methodMap.put(((MethodName) annotation).version(), method);}}}}// 根據方法版本號獲取具體方法,通過反射執行方法public void execute(int version, Class<?> clazz)throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {Method method = methodMap.get(version);Object o = clazz.newInstance();if (method != null) {method.invoke(o, clazz);}}public static void main(String[] args)throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {// 開始測試不同版本號的方法調用Annotations annotation = new Annotations();annotation.initialize(Annotations.class);annotation.execute(1, Annotations.class);annotation.execute(2, Annotations.class);} }

 運行結果:

hello hello hello world

?

轉載于:https://www.cnblogs.com/wuxun1997/p/6722508.html

總結

以上是生活随笔為你收集整理的自定义注解的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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