[jboss-cvs] JBossCache/src/org/jboss/cache/notifications ...

Manik Surtani manik at jboss.org
Mon Jul 2 06:49:03 EDT 2007


  User: msurtani
  Date: 07/07/02 06:49:03

  Modified:    src/org/jboss/cache/notifications  Notifier.java
  Log:
  Added tests to ensure listener class and methods are public
  
  Revision  Changes    Path
  1.34      +21 -5     JBossCache/src/org/jboss/cache/notifications/Notifier.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Notifier.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/notifications/Notifier.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -b -r1.33 -r1.34
  --- Notifier.java	28 Jun 2007 16:53:36 -0000	1.33
  +++ Notifier.java	2 Jul 2007 10:49:03 -0000	1.34
  @@ -20,6 +20,7 @@
   
   import javax.transaction.Transaction;
   import java.lang.reflect.Method;
  +import java.lang.reflect.Modifier;
   import java.util.Collections;
   import java.util.HashSet;
   import java.util.List;
  @@ -66,10 +67,11 @@
       */
      private void validateAndAddListenerInvocation(Object listener)
      {
  -      if (!listener.getClass().isAnnotationPresent(CacheListener.class))
  -         throw new IncorrectCacheListenerException("An Object must have the org.jboss.cache.notifications.annotation.CacheListener annotation to be considered a cache listener!");
  +      if (!isValidListenerClass(listener.getClass()))
  +         throw new IncorrectCacheListenerException("An Object must have the org.jboss.cache.notifications.annotation.CacheListener annotation to be considered a cache listener, and be publicly accesible!");
   
  -      // now try all methods on the listener for anything that we like:
  +      boolean foundMethods = false;
  +      // now try all methods on the listener for anything that we like.  Note that only PUBLIC methods are scanned.
         for (Method m : listener.getClass().getMethods())
         {
            // loop through all valid method annotations
  @@ -77,17 +79,31 @@
            {
               if (m.isAnnotationPresent(allowedMethodAnnotations[i]))
               {
  -               if (m.getParameterTypes().length == 1 && m.getParameterTypes()[0].isAssignableFrom(parameterTypes[i]) && m.getReturnType().equals(void.class))
  +               if (isValidListenerMethod(m, parameterTypes[i]))
                  {
                     addListenerInvocation(allowedMethodAnnotations[i], new ListenerInvocation(listener, m));
  +                  foundMethods = true;
                  }
                  else
                  {
  -                  throw new IncorrectCacheListenerException("Methods annotated with " + allowedMethodAnnotations[i].getName() + " need to accept one, and only one, parameter, assignable from " + parameterTypes[i].getName());
  +                  throw new IncorrectCacheListenerException("Methods annotated with " + allowedMethodAnnotations[i].getName() + " need to be publicly accessible, return void, and accept one, and only one, parameter, assignable from " + parameterTypes[i].getName());
                  }
               }
            }
         }
  +
  +      if (!foundMethods && log.isWarnEnabled())
  +         log.warn("Attempted to register listener of class " + listener.getClass() + ", but no valid, public methods annotated with method-level event annotations found! Ignoring listener.");
  +   }
  +
  +   private boolean isValidListenerClass(Class listenerClass)
  +   {
  +      return (listenerClass.isAnnotationPresent(CacheListener.class) && Modifier.isPublic(listenerClass.getModifiers()));
  +   }
  +
  +   private boolean isValidListenerMethod(Method m, Class allowedParameter)
  +   {
  +      return m.getParameterTypes().length == 1 && m.getParameterTypes()[0].isAssignableFrom(allowedParameter) && m.getReturnType().equals(void.class);
      }
   
      private void addListenerInvocation(Class annotation, ListenerInvocation li)
  
  
  



More information about the jboss-cvs-commits mailing list