3 Replies Latest reply on Oct 25, 2016 10:51 AM by ljnelson

    Accessing class annotation on a Weld-made proxy?

    ljnelson

      (This is tangentially related to WELD-1131 and this forum thread.)

       

      When Weld hands me a bean instance, and I want to inspect that instance's class for a class-level annotation, I cannot, because the class of the instance that Weld hands me is a proxy.

       

      While I can think of lots of non-portable hacks to discover my class-level annotation, is there a sanctioned way to arrange things such that a call of Class#getAnnotation(Class) on the proxy class will work properly?

       

      The CDI 2.0 EDR2 Javadocs index includes the word "proxy" only 10 times, and only in connection with the UnproxyableResolutionException class, so I am beginning to fear that this may not be possible.

       

      For comparison purposes, HK2 (another JSR-330 framework with features comparable to CDI) allows this use case explicitly: you can always test an instance to see if it is an instanceof ProxyCtl.  I mention this because this is the mindset I'm approaching the problem with in case that helps.

       

      Thanks,

      Best,

      Laird

        • 1. Re: Accessing class annotation on a Weld-made proxy?
          ljnelson

          I see that https://issues.jboss.org/browse/CDI-10 is unresolved.  Looks like I'll need to stick with the getClass().getSuperclass() hack.

          • 2. Re: Accessing class annotation on a Weld-made proxy?
            mkouba

            Hi Laird,

            the spec calls the instance which is injected a contextual reference, for normal scoped beans you get a client proxy. Both these terms are well-defined (see also 6.5.3. Contextual reference for a bean and 5.4. Client proxies). Now back to the issue - since Weld 2.3 all the generated "proxy classes" have SYNTHETIC modifier so it's safe to walk the contextual reference class hierarchy and the first non-synthetic class is what you're looking for. The other non-portable solution would be to test instanceof org.jboss.weld.bean.proxy.ProxyObject (of course this would require compile-time dependency on Weld).

            • 3. Re: Accessing class annotation on a Weld-made proxy?
              ljnelson

              Martin Kouba wrote:

               

              Hi Laird,

              the spec calls the instance which is injected a contextual reference, for normal scoped beans you get a client proxy. Both these terms are well-defined (see also 6.5.3. Contextual reference for a bean and 5.4. Client proxies).

              Now back to the issue - since Weld 2.3 all the generated "proxy classes" have SYNTHETIC modifier so it's safe to walk the contextual reference class hierarchy and the first non-synthetic class is what you're looking for. The other non-portable solution would be to test instanceof org.jboss.weld.bean.proxy.ProxyObject (of course this would require compile-time dependency on Weld).

              (I wasn't sure what your point was when you called attention to the definitions of contextual references and client proxies.  I agree they're well-defined.)

               

              I ended up using the isSynthetic() trick, which will certainly work in Weld; I'm too lazy to test it against other CDI implementations at the moment. :-)

               

              Thanks,

              Best,

              Laird