Dynamic class annotation
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.
精彩评论