Proxy classes lose original class annotations
shamoh Dec 18, 2015 8:40 AMHello all.
I would like to ask transparent support of annotation propagated to proxy classes from original class.
(This post is originally comment on CDI-10 issue.)
I hope it will work transparently. It seams to me strange to check if some application class instance is proxy or not to get Annotation instances written on class I've injected. I know I could write utility method for my code (and hope it correctly detects proxies):
public static <A extends Annotation> A getAnnotationProxyReady(Class<?> clazz, Class<A> annotationClass) { |
final A annotation = clazz.getAnnotation(annotationClass); |
if (annotation == null |
&& (clazz.isSynthetic())) { //OK, this is probably proxy |
return getAnnotationProxyReady(clazz.getSuperclass(), annotationClass); |
} else { |
return annotation; |
} |
} |
and now I can access annotation written on bean:
@Target({ TYPE, METHOD, FIELD }) |
@Retention(RUNTIME) |
@Documented |
public @interface MyAnnotation { |
String attr1(); |
int attr2; |
} |
@MyAnnotation(attr1="aaa", attr2=42) |
public class MyBean { |
//... |
} |
@Inject |
private MyBean myBean; |
public void methodA() throws NoSuchMethodException { |
MyAnnotation myAnn = getAnnotationProxyReady(myBean.getClass(), MyAnnotation.class); |
//use my bean annotations ... |
} |
But I can not expect 3rd party libraries support such approach. 3rd party libraries just works with objects and classes and its annotations. They don't what to care about any proxy objects or different proxy implementations to do their business.
Note: It works with Annotations annotated by @Inherited what is expected.
@Target({ TYPE, METHOD, FIELD }) |
@Retention(RUNTIME) |
@Documented |
@Inherited |
public @interface MyAnnotation { |
String attr1(); |
int attr2; |
} |
So I would like to vote for: The proxy bean contains same annotations as original class. And exactly that class not its super-classes.
Note: It is necessary to say that it already works for method annotations, see WELD-1131. So the issue remains just on class itself.