反射修改 final

记一次线上事故:

原本代码是这样的,线上application.yml中istest=true

@Value("${istest}")
private boolean istest=false;

IDEA提示将该字段用final修饰,结果导致读取到该字段为false。

原因是@Value注解的字段是在bean实例化之后,通过反射赋值的,而反射能顺利修改final字段(无报错提示),但由于字符串和基本类型的final字段会经过JVM内联优化,对变量的访问被替换成常量值的访问,导致获取不到修改后的值。即反射能修改final,但普通的方式无法获取到修改后的值。

final String s = "abc";
final int i = 3;

但引用类型final除外,普通方式能获取到反射修改后的结果

final String s1 = new String("abc");
final Object o = new Object();

完整例子

public class FinalReflectEdit {
    final String s = "abc";
    final String s1 = new String("abc");
    final int i = 3;
    final Object o = new Object();

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        FinalReflectEdit finalReflectEdit = new FinalReflectEdit();
        Field s = FinalReflectEdit.class.getDeclaredField("s");
        Field i = FinalReflectEdit.class.getDeclaredField("i");
        Field o = FinalReflectEdit.class.getDeclaredField("o");
        Field s1 = FinalReflectEdit.class.getDeclaredField("s1");
        System.out.printf("s:%s,i:%s,o:%s,s1:%s%n", finalReflectEdit.s, finalReflectEdit.i, finalReflectEdit.o, finalReflectEdit.s1);
        s.setAccessible(true);
        i.setAccessible(true);
        o.setAccessible(true);
        s1.setAccessible(true);
        s.set(finalReflectEdit, "cde");
        i.set(finalReflectEdit, 4);
        o.set(finalReflectEdit, new Object());
        s1.set(finalReflectEdit, "cde");
        System.out.printf("s:%s,i:%s,o:%s,s1:%s%n", finalReflectEdit.s, finalReflectEdit.i, finalReflectEdit.o, finalReflectEdit.s1);
        System.out.printf("s:%s,i:%s,o:%s%n", s.get(finalReflectEdit), i.get(finalReflectEdit), o.get(finalReflectEdit));
    }
}
s:abc,i:3,o:java.lang.Object@6aceb1a5,s1:abc
s:abc,i:3,o:java.lang.Object@67117f44,s1:cde
s:cde,i:4,o:java.lang.Object@67117f44
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过反射修改注解中的某个属性的值,你可以使用 `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()` 方法),如果是,则返回修改后的属性值;如果不是,则保持原样调用。 请注意,这种方法需要使用动态代理,并且可能会对性能产生一定的影响。在实际开发中,请谨慎使用反射和动态代理,并考虑其他替代方案。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值