更改Java的8场注解注释值 [英] Change annotation value of field annotation in Java 8

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

问题描述

我觉得标题描述的问题。下面是一些code:

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?

推荐答案

注解的修饰的就像私人同步。它们是一类的不变的静态结构的一部分,并不意图进行修改。您可以攻入实施的反思,使特定的方法打印你想要什么,但除了黑客的肮脏,你根本没有改变注释,你只砍死某个库的数据结构。

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.

有其他反射或字节code操纵,这将不使用内置的反射API,但读取的字节code直接(例如通过的getResource()或通过仪表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.

进一步的注意,因为这些值都应该是嵌入在类文件中的常量,反思的实施总是可以使用延迟抓取的以及的依赖于不可控的条件下,缓存值下探这些值可以随时要重新获取。还有,反射实现使用的数据结构都无法保证;它也可能产生code返回常数值。

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.

在换句话说,如果你想可变数据与方法相关联,不使用的标注。你可以简单的使用地图<方法,MutableData> ,或者,如果你只是有一个特定的方法,声明一个很好的旧静态字段,它已经提供了所有你需要的功能和更容易处理。

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天全站免登陆