2 Replies Latest reply on Nov 15, 2013 10:42 AM by Michael Musgrove

    How to detect Stereotypes from an interceptor

    Michael Musgrove Master

      I am fixing an @Transactional issue to do with Stereotypes. The current implementation of the interceptor looks for the Transactional annotation on the invocation target using getAnnotation(Transactional.class). If the target is annotated with a Stereotype (which in turn includes the Transactional annotation) then the check for the Transactional annotation fails (even thought the TransactionalInterceptor has fired so weld knows it's Transactional).

       

      When I debug the interceptor I find that there are no annotations present on the target class, not even the Stereotype I annotated the target with. Is there something particular I need to do in order to determine whether a target is a Stereotype?

        • 1. Re: How to detect Stereotypes from an interceptor
          Jozef Hartinger Master

          Unfortunately I am not aware of any easy way of doing this so you will need to implement the spec logic:

           

          The set of interceptor bindings for a method declared at class level includes those declared on stereotypes. An interceptor binding

          declared on a bean class replaces an interceptor binding of the same type declared by a stereotype that is applied to the bean class.

           

          Which means you need to check the method, the declaring type and all the stereotypes it declares in order to determine what stereotypes are effective. Note that you can use BeanManager.isStereotype() to check whether an annotation is a stereotype portable.

           

          Btw, this could make for a nice reusable utility for e.g. Apache DeltaSpike.

          1 of 1 people found this helpful
          • 2. Re: How to detect Stereotypes from an interceptor
            Michael Musgrove Master

            Thanks for the help.

             

            The main issue I had was that I was looking for the Stereotype annotation via the invocation target class using ic.getTarget().getClass() but this gives me a Weld proxy to the bean and calling getAnnotations() on the proxy does not return stereotypes (which personally I think is wrong - the proxy should give me all the direct annotations on the proxied object). So instead I got the actual (proxied) class using ic.getMethod().getDeclaringClass() which does return the stereotype annotation.

             

            Once I had the stereotype I was able to use your suggestion of using the BeanManager to see if the @Transactional interceptor was in the stereotype.

             

            Btw, this could make for a nice reusable utility for e.g. Apache DeltaSpike.

            There is an AnnotationInspector that does something similar in the http://seamframework.org/Seam3/Solder project (org.jboss.solder.reflection)