开发者

Dynamic class annotation

开发者 https://www.devze.com 2022-12-24 13:59 出处:网络
I want to annotate a class dynamically to make it the more generic as possible: public class Test<T> {开发者_Go百科

I want to annotate a class dynamically to make it the more generic as possible:

public class Test<T> {开发者_Go百科

    @XmlAttribute(name = dynamicvalue)
    T[] data;

    public Test(String dynamicvalue) {
    }  
}

Is there any way to achieve something like this.

TA


No. Annotations are static class-level information and they can't be influenced by the values of instance fields (no, they can't influenced by the values of static fields either).


You can create new classes at runtime by loading in new bytecode, so in theory it should be possible (remember, the annotations are associated with the class, not the object). And you can use an API like Java Assist to help create them. There's an annotation package that you could look at.

It won't be easy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号