Posted By:
David_Jung
Posted On:
Tuesday, February 1, 2005 08:01 AM
I'd like to instantiate a runtime retention annotation dynamically (i.e. programmatically at runtime). That is, suppose I have an annotation compiled: @Retention(RetentionPolicy.RUNTIME) @Inherited @Target({ElementType.METHOD}) public @interface accessor {} I can get the Class for this reflectively by calling Class.forName("accessor") [which I assume is identical to accessor.class in this case]. However, this 'Class' is actually an interface - hence cannot be instantiated. I know that if I were to statically apply the annotation to a class source that I compile, I can get an Annotation by calling .getAnnotation(accessor.class). From that calling Annotation.annotationType() will give
More>>
I'd like to instantiate a runtime retention annotation dynamically (i.e. programmatically at runtime). That is, suppose I have an annotation compiled:
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Target({ElementType.METHOD})
public @interface accessor {}
I can get the Class for this reflectively by calling Class.forName("accessor") [which I assume is identical to accessor.class in this case].
However, this 'Class' is actually an interface - hence cannot be instantiated. I know that if I were to statically apply the annotation to a class source that I compile, I can get an Annotation by calling .getAnnotation(accessor.class). From that calling Annotation.annotationType() will give me back acessor.class (right?)
So, my question is: given accessor.class, how can I instantiate an Annotation subclass object of some kind? Does the JVM cheat - doing something we can't do via reflection?
I hope I don't have to resort to dynamic bytecode generation.
In case you're wondering why, I'm writing a scripting language interpreter for a language that also has annotations - which I thought I could implement using Annotation subclasses (as the language also interoperates with native JVM Java types transparently via reflection)
Any help from a guru would be greatly appreciated as I've spent a lot of time trying to answer this question fruitlessly. Thankyou.
<<Less