自定义注解的实现
注解看起來很神秘,其實看穿了就是一種標記,通過運行時獲取標記進行后續處理。說到運行時自然離不開反射,所以注解就是反射的一種應用。使用元注解就可以實現自定義注解,元注解只有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
總結
- 上一篇: Java虚拟机JVM简单理解
- 下一篇: 关于photoshop处理图片的自动化