java使用反射修改注解参数值内容

方法一

该方法不太灵活
过程:使用反射获取指定method上方的指定注解,拿到该注解的String类型的参数值,使用反射修改该String对象内字符数组的地址


import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestAnnotation {

    @MyAnnotation("test")
    public void test(){
        final Class<? extends TestAnnotation> aClass = this.getClass();
        try {
            System.out.println("运行:"+aClass.getMethod("test").getAnnotation(MyAnnotation.class).value());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @MyAnnotation("test02")
    public void test02(){
        final Class<? extends TestAnnotation> aClass = this.getClass();
        try {
            System.out.println("运行:"+aClass.getMethod("test02").getAnnotation(MyAnnotation.class).value());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        TestAnnotation testAnnotation = new TestAnnotation();
        testAnnotation.setAnnotationValue("editor");
        testAnnotation.test();
        testAnnotation.test02();
        new TestAnnotation().test();
        new TestAnnotation().test02();
        new Runnable() {
            @Override
            public void run() {
                new TestAnnotation().test();
                new TestAnnotation().test02();
            }
        };
    }
    //修改test02方法上方MyAnnotation注解的value值
    private void setAnnotationValue(String value) throws Exception {
        Method method = this.getClass().getMethod("test");
        MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
        Field field = String.class.getDeclaredField("value");
        field.setAccessible(true);
        field.set(annotation.value(), value.toCharArray());
        System.out.println("修改:"+annotation.value());
    }
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    String value();
}

方法二

改写法比较灵活
使用反射修改注解的动态代理内存储的内容

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

public class TestAnnotation02 {
    @MyAnnotation("test")
    public void test(){
        final Class<? extends TestAnnotation02> aClass = this.getClass();
        try {
            System.out.println("运行:"+aClass.getMethod("test").getAnnotation(MyAnnotation.class).value());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @MyAnnotation("test02")
    public void test02(){
        final Class<? extends TestAnnotation02> aClass = this.getClass();
        try {
            System.out.println("运行:"+aClass.getMethod("test02").getAnnotation(MyAnnotation.class).value());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        TestAnnotation02 testAnnotation = new TestAnnotation02();
        testAnnotation.setAnnotationValue("editor");
        testAnnotation.test();
        testAnnotation.test02();
        new TestAnnotation02().test();
        new TestAnnotation02().test02();
        new Runnable() {
            @Override
            public void run() {
                new TestAnnotation02().test();
                new TestAnnotation02().test02();
            }
        };
    }
    //修改test02方法上方MyAnnotation注解的value值
    private void setAnnotationValue(String value) throws Exception {
        Method method = this.getClass().getMethod("test");
        MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
        InvocationHandler handler = Proxy.getInvocationHandler(annotation);
        Field field = handler.getClass().getDeclaredField("memberValues");
        field.setAccessible(true);
        Map meberValues= (Map) field.get(handler);
        meberValues.put("value", value);
        System.out.println("修改:"+annotation.value());
    }
}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值