Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain annotations on CGLIB proxies?

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 the previous class had!

Can anyone tell me how can I make CGLIB retain the annotations on the proxies it creates?

Cheers! Nirav

like image 760
Nirav Avatar asked Nov 10 '09 09:11

Nirav


2 Answers

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 {
}
like image 139
eaorak Avatar answered Sep 18 '22 03:09

eaorak


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.

like image 24
skaffman Avatar answered Sep 18 '22 03:09

skaffman