4 Replies Latest reply on Jan 11, 2008 7:19 AM by Pete Muir

    Custom interceptors do not work with super classes

    Tim Evers Master

      When creating my own interceptor I was hoping to have super class that all my classes could extend that would give them my interceptor.

      However, the initInterceptors method in the SEAM Component.java does not scan superclasses for annotations.

      So I have the following code.

      My Annotation

      package gekko.web.services;
      
      import static java.lang.annotation.ElementType.TYPE;
      import static java.lang.annotation.RetentionPolicy.RUNTIME;
      
      import java.lang.annotation.Retention;
      import java.lang.annotation.Target;
      
      import org.jboss.seam.annotations.intercept.Interceptors;
      
      @Target(TYPE)
      @Retention(RUNTIME)
      @Interceptors(GekkoSpringInjector.class)
      public @interface SpringAutowirable {}
      


      My Super class
      package gekko.web.actionbean;
      
      import gekko.web.services.SpringAutowirable;
      
      @SpringAutowirable
      public class AbstractSeamComponent {
      
      }
      


      And finally my sub-class
      package gekko.web.actionbean.enquiry;
      
      *imports stripped*
      
      @Name("catSubcatEnquiry")
      @Scope(PAGE)
      public class CatSubcatEnquiry extends AbstractSeamComponent {
      
      .....
      
      }
      


      If I use the extends Superclass the Interceptor never fires.

      If I change this and make an interface like
      package gekko.web.actionbean.enquiry;
      
      import gekko.web.services.SpringAutowirable;
      
      @SpringAutowirable
      public interface TestInterface {
      
      }
      


      and use
      @Name("catSubcatEnquiry")
      @Scope(PAGE)
      public class CatSubcatEnquiry implements TestInterface {
      ...
      }
      


      That works fine.

      If you have a look at the Spring AnnotationUtils.java you will find that in there findAnnotation method it does look in super classes.
      As seen here.
      public static <A extends Annotation> A findAnnotation(Method method, final Class<A> annotationType) {
      
       if (!annotationType.isAnnotation()) {
       throw new IllegalArgumentException(annotationType + " is not an annotation");
       }
       A annotation = getAnnotation(method, annotationType);
       Class<?> cl = method.getDeclaringClass();
       while (annotation == null) {
       cl = cl.getSuperclass();
       if (cl == null || cl.equals(Object.class)) {
       break;
       }
       try {
       method = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
       annotation = getAnnotation(method, annotationType);
       }
       catch (final NoSuchMethodException ex) {
       // We're done...
       }
       }
       return annotation;
       }
      


      Is there a possiblity of making Seam behave the same way?