[cdi-dev] [JBoss JIRA] (CDI-10) Add ability to access a bean instance from a proxy

Libor Kramolis (JIRA) issues at jboss.org
Fri Dec 18 08:07:00 EST 2015


    [ https://issues.jboss.org/browse/CDI-10?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13142939#comment-13142939 ] 

Libor Kramolis commented on CDI-10:
-----------------------------------

*+1 for use case (1).*

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):

{code:java}
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;
    }
}
{code}

and now I can access annotation written on bean:

{code:java}
@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 ...
}
{code}

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.

{code:java}
@Target({ TYPE, METHOD, FIELD })
@Retention(RUNTIME)
@Documented
@Inherited
public @interface MyAnnotation {
    String attr1();
    int attr2;
}
{code}

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.


> Add ability to access a bean instance from a proxy
> --------------------------------------------------
>
>                 Key: CDI-10
>                 URL: https://issues.jboss.org/browse/CDI-10
>             Project: CDI Specification Issues
>          Issue Type: Feature Request
>          Components: Beans
>    Affects Versions: 1.0
>            Reporter: Stuart Douglas
>             Fix For: 2.0 (discussion)
>
>
> There are occasions when it would be useful to access a bean instance directly from a proxy. This could be achieved by making all proxies assignable to an interface (say BeanProxy) that provides a getBeanInstance() method. 
> Client code that needs access to the actual instance can check if the object is assignable to the BeanProxy interface and then call getBeanInstance() to get the actual instance if required.
> This is something that is probably more useful to extension writers than the end user, but there have already been a few requests on the weld forum about this so it is probably worth considering.



--
This message was sent by Atlassian JIRA
(v6.4.11#64026)


More information about the cdi-dev mailing list