在 Java 8 中更改字段注解的注解值 [英] Change annotation value of field annotation in Java 8

查看:35
本文介绍了在 Java 8 中更改字段注解的注解值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为标题描述了问题.这是一些代码:

I think title describes the problem. Here's some code:

import static org.junit.Assert.assertEquals;
import java.lang.annotation.*;

public class Main {
    public static void main(String[] args) throws Exception {
        assertEquals("foo", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param());

        // the magic here -> set to bar

        assertEquals("bar", Main.class.getDeclaredMethod("myMethod").getAnnotation(Anno.class).param());
    }

    @Anno(param = "foo")
    public void myMethod() {}

    @Retention(RetentionPolicy.RUNTIME)
    @interface Anno {
        String param();
    }
}

到目前为止,我猜这是不可能的.似乎总是当您尝试通过反射获取方法时,您只会获得副本,并且所有值(如注释)都是从更深的 java 层重新读取的.在这些副本中,您可以更改值,但如果您重新加载,这些值就会消失.

So far I'm guessing this is not possible. It seems that always when you try to get a method via reflection you only get copies and all values (like annotations) are reread from a deeper java layer. In these copies you could change values but these are gone if you reload.

是我遗漏了什么还是真的不可能?

Is there something I missed or is it really not possible?

推荐答案

注解是修饰符,就像 privatesynchronized 一样.它们是类的不变静态结构的一部分,不打算修改.你可以破解反射实现,让特定方法打印你想要的东西,但除了黑客的肮脏之外,你根本没有改变注释,你只是破解了特定库的数据结构.

Annotations are modifiers just like private or synchronized. They are part of the invariant static structure of a class and not intended to be modified. You can hack into the Reflection implementation to make a particular method print what you want, but besides the dirtiness of the hack, you simply haven’t changed the annotation, you just hacked a data structure of a particular library.

还有其他反射或字节码操作库不会使用内置的反射 API,而是直接读取字节码(例如,通过 getResource() 或通过 Instrumentation API).这些库永远不会注意到您的操作.

There are other Reflection or byte code manipulation libraries which won’t use the built-in Reflection API but read the byte code directly (e.g. via getResource() or via the Instrumentation API). These libraries will never notice your manipulation.

进一步注意,由于这些值应该是嵌入在类文件中的常量,反射实现可以总是使用延迟获取加上缓存值的删除取决于不可控的条件,因为这些值总是可以被重新取回.也不能保证 Reflection 实现完全使用数据结构;它还可以生成返回常量值的代码.

Note further, since these values are supposed to be constants embedded in the class file, a Reflection implementation could always use lazy fetching plus dropping of the cached values depending on uncontrollable conditions as these values can always be refetched. There’s also no guaranty that the Reflection implementation uses data structures at all; it could also generate code returning the constant values.

换句话说,如果要将可变数据与方法相关联,请不要使用注解.您可以简单地使用 Map<Method,MutableData>,或者,如果您只有一个特定的方法,请声明一个旧的 static 字段,它已经提供了您的所有功能需要并且更容易处理.

In other words, if you want to associate mutable data with a method, don’t use annotations. You could simple use a Map<Method,MutableData>, or, if you just have that one particular method, declare a good old static field which already provides all the features you need and is much easier to handle.

这篇关于在 Java 8 中更改字段注解的注解值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆