Am trying to create an object using an AOP framework which uses CGLIB to create proxy objects. Strangely enough, the \"enhanced\" proxy object is devoid of ANY annotations t
Cglib is not capable of retaining annotations without changing its internal implementation. This is however quite complicated and believe me I tried. My altered version I finally came up with was however so complicated that I decided to rather implement Byte Buddy, another code generation library which is capable of such functionality.
Here is an example of how you can create subclass that
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation { }
@MyAnnotation
class MyClass { }
assertThat(new ByteBuddy()
.subclass(Object.class)
.attribute(TypeAttributeAppender.ForSuperType.INSTANCE)
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded()
.isAnnotationPresent(MyAnnotation.class), is(true));
Byte Buddy comes with an extensive full-text documentation and javadoc and it is quite extensible. Hope you make good use of the library.
CGLIB creates subclasses of given classes to generate proxies. Annotations are not preserved in subclasses unless explicitly specified in annotation definition. @Inherited annotation is used for this purpose.
You can use this annotation in the annotations you define, and make them reachable in subclasses, as following:
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
}
This isn't an issue with "retaining" the annotations. CGLIB proxies are actually generated subclasses of the target object's class. These subclasses may not have annotations, but their superclass (i.e. your own class) will still have them. Any annotation-reflecting code you use needs to be able to look back up the class hierarchy to look for annotations.
It's a valid problem (I'm running into now myself) as a) not all frameworks are smart enough to inspect parent classes b) even if they are smart enough, they may chose not to. The latter seems to be the case with Guice. FWIW, https://issues.apache.org/jira/browse/WICKET-1130 is the problem I was working on when I found this out.