Java中自定义注解并通过反射获取注解属性值

1 篇文章 0 订阅

自定义类注解

package com.uno.ray;  
  
import java.lang.annotation.Documented;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Inherited;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
import java.net.Authenticator.RequestorType;  
  
/** 
 * 自定义注解 
 * @author Uno 
 *@Documented:指明该注解可以用于生成doc 
 *@Inherited:该注解可以被自动继承 
 *@Retention:指明在什么级别显示该注解: 
 *  RetentionPolicy.SOURCE 注解存在于源代码中,编译时会被抛弃 
    RetentionPolicy.CLASS 注解会被编译到class文件中,但是JVM会忽略 
    RetentionPolicy.RUNTIME JVM会读取注解,同时会保存到class文件中 
  @Target:指明该注解可以注解的程序范围 
    ElementType.TYPE 用于类,接口,枚举但不能是注解 
    ElementType.FIELD 作用于字段,包含枚举值 
    ElementType.METHOD 作用于方法,不包含构造方法 
    ElementType.PARAMETER 作用于方法的参数 
    ElementType.CONSTRUCTOR 作用于构造方法 
    ElementType.LOCAL_VERIABLE 作用于本地变量或者catch语句 
    ElementType.ANNOTATION_TYPE 作用于注解 
    ElementType.PACKAGE 作用于包 
 */  
@Documented  
@Inherited  
@Retention(RetentionPolicy.RUNTIME)  
@Target({ElementType.TYPE, ElementType.FIELD})//次注解作用于类和字段上  
public @interface FieldTypeAnnotation {  
    /** 
     *leip 2016年12月3日 
     *TODO 
    **/  
    String type() default "ignore";  
    int age() default 27;  
    String[] hobby(); //没有指定defalut的,需要在注解的时候显式指明  
}  

自定义方法注解

package com.uno.ray;  
  
import java.lang.annotation.Documented;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Inherited;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
  
/** 
 *  
 * @author Uno 
 * 
 */  
@Documented  
@Inherited  
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.METHOD) //次注解只能作用于方法上  
public @interface MethodAnnotation {  
  
    String desc() default "method1";  
}  

反射类测试

package com.uno.ray;  
  
import java.awt.Dialog.ModalityType;  
import java.lang.reflect.Field;  
import java.lang.reflect.Method;  
import java.util.Arrays;  
  
@FieldTypeAnnotation(type = "class", hobby = { "smoke" })  
public class ReflectAnnotation {  
    /** 
     * leip 2016年12月3日 TODO 
     **/  
    @FieldTypeAnnotation(hobby = { "sleep", "play" })  
    private String maomao;  
  
    @FieldTypeAnnotation(hobby = { "phone", "buy" }, age = 27, type = "normal")  
    private String zhangwenping;  
      
    @MethodAnnotation()  
    public void method1() {  
          
    }  
    @MethodAnnotation(desc="method2")  
    public void method2() {  
          
    }  
  
    public static void main(String[] args) {  
        // 此处要用反射将字段中的注解解析出来  
        Class<ReflectAnnotation> clz = ReflectAnnotation.class;  
        // 判断类上是否有次注解  
        boolean clzHasAnno = clz.isAnnotationPresent(FieldTypeAnnotation.class);  
        if (clzHasAnno) {  
            // 获取类上的注解  
            FieldTypeAnnotation annotation = clz.getAnnotation(FieldTypeAnnotation.class);  
            // 输出注解上的属性  
            int age = annotation.age();  
            String[] hobby = annotation.hobby();  
            String type = annotation.type();  
            System.out.println(clz.getName() + " age = " + age + ", hobby = " + Arrays.asList(hobby).toString() + " type = " + type);  
        }  
        // 解析字段上是否有注解  
        // ps:getDeclaredFields会返回类所有声明的字段,包括private、protected、public,但是不包括父类的  
        // getFields:则会返回包括父类的所有的public字段,和getMethods()一样  
        Field[] fields = clz.getDeclaredFields();  
        for(Field field : fields){  
            boolean fieldHasAnno = field.isAnnotationPresent(FieldTypeAnnotation.class);  
            if(fieldHasAnno){  
                FieldTypeAnnotation fieldAnno = field.getAnnotation(FieldTypeAnnotation.class);  
                //输出注解属性  
                int age = fieldAnno.age();  
                String[] hobby = fieldAnno.hobby();  
                String type = fieldAnno.type();  
                System.out.println(field.getName() + " age = " + age + ", hobby = " + Arrays.asList(hobby).toString() + " type = " + type);  
            }  
        }  
        //解析方法上的注解  
        Method[] methods = clz.getDeclaredMethods();  
        for(Method method : methods){  
            boolean methodHasAnno = method.isAnnotationPresent(MethodAnnotation.class);  
            if(methodHasAnno){  
                //得到注解  
                MethodAnnotation methodAnno = method.getAnnotation(MethodAnnotation.class);  
                //输出注解属性  
                String desc = methodAnno.desc();  
                System.out.println(method.getName() + " desc = " + desc);  
            }  
        }  
    }  
}  

输出

com.uno.ray.ReflectAnnotation age = 27, hobby = [smoke] type = class  
maomao age = 27, hobby = [sleep, play] type = ignore  
zhangwenping age = 27, hobby = [phone, buy] type = normal  
method2 desc = method2  
method1 desc = method1 


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要通过反射修改注解的某个属性,你可以使用 `java.lang.reflect.Proxy` 类来代理注解,并在代理对象上修改属性。下面是一个示例代码: ```java import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class Main { public static void main(String[] args) throws NoSuchFieldException { // 获取字段上的注解 Field field = MyClass.class.getDeclaredField("myField"); MyAnnotation annotation = field.getAnnotation(MyAnnotation.class); // 修改注解属性 if (annotation != null) { System.out.println("Before modification: " + annotation.value()); MyAnnotation modifiedAnnotation = modifyAnnotationValue(annotation, "new value"); System.out.println("After modification: " + modifiedAnnotation.value()); } } public static MyAnnotation modifyAnnotationValue(MyAnnotation annotation, String newValue) { return (MyAnnotation) Proxy.newProxyInstance( annotation.getClass().getClassLoader(), new Class[] { MyAnnotation.class }, new AnnotationInvocationHandler(annotation, newValue) ); } } class AnnotationInvocationHandler implements InvocationHandler { private final MyAnnotation originalAnnotation; private final String newValue; public AnnotationInvocationHandler(MyAnnotation originalAnnotation, String newValue) { this.originalAnnotation = originalAnnotation; this.newValue = newValue; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 修改属性 if (method.getName().equals("value")) { return newValue; } // 其他方法调用保持原样 return method.invoke(originalAnnotation, args); } } @MyAnnotation("old value") class MyClass { @MyAnnotation("old value") private String myField; } @interface MyAnnotation { String value(); } ``` 在上面的例子,我们定义了一个自定义注解 `MyAnnotation`,并将其应用到了 `MyClass` 类的字段 `myField` 上。通过反射和动态代理,我们创建了一个代理对象,该代理对象可以修改注解属性。 在 `modifyAnnotationValue` 方法,我们使用 `Proxy.newProxyInstance` 方法创建了一个代理对象,该代理对象会调用 `AnnotationInvocationHandler` 的 `invoke` 方法处理方法调用。在 `invoke` 方法,我们判断被调用的方法是否是注解属性方法(这里是 `value()` 方法),如果是,则返回修改后的属性;如果不是,则保持原样调用。 请注意,这种方法需要使用动态代理,并且可能会对性能产生一定的影响。在实际开发,请谨慎使用反射和动态代理,并考虑其他替代方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值