fév.
2011
Introducing AnnotationInjector
Let me introduce you to AnnotationInjector, a small library that allows to inject annotations on classes at runtime.
It began as sample code for the conference on annotations I presented at several JUGs, and was extracted later as a standalone library for general consumption.
What does it do ?
AnnotationInjector allows you to inject annotations on classes at runtime.
It works only on classes though, not on fields, methods, or parameters, due to some gory implementation details in the Class
class on the Sun/Oracle JRE.
Let's see it in action :
// Pojo does not bear the MyAnnotation annotation MyAnnotation existingAnnot = Pojo.class.getAnnotation(MyAnnotation.class); assertNull(existingAnnot); // Instanciate and inject a new MyAnnotation on Pojo MyAnnotation injectedAnnot = new MyAnnotation() { ... }; AnnotationInjector injector = new AnnotationInjector(Pojo.class); injector.injectAnnotation(injectedAnnot); // Now Pojo bears our new annotation existingAnnot = Pojo.class.getAnnotation(MyAnnotation.class); assertNotNull(existingAnnot); assertEquals(existingAnnot, injectedAnnot);