[jboss-dev-forums] [Design of JBossCache] - Re: @CacheListener impl must be public?

bstansberry@jboss.com do-not-reply at jboss.com
Thu Jul 5 09:44:12 EDT 2007


I already changed the exception message bit. Do you want me to post a JIRA for that?

I wrote a little test program and it had no trouble doing inspection on a private class and then invoking a public method on it:


  | package org.jboss.cache.notifications;
  | 
  | import org.jboss.cache.notifications.annotation.CacheListener;
  | import org.jboss.cache.notifications.annotation.NodeModified;
  | import org.jboss.cache.notifications.event.NodeModifiedEvent;
  | 
  | public class Testee
  | {
  |    public Object getCacheListener()
  |    {
  |       return new InnerClass();
  |    }
  |    
  |    @CacheListener
  |    private class InnerClass
  |    {
  |       @NodeModified
  |       public void nodeModified(NodeModifiedEvent event)
  |       {
  |          System.out.println("Node modified " + event);
  |       }
  |       
  |       public long getLong()
  |       {
  |          return 0L;
  |       }
  |    }
  | }
  | 


  | package org.jboss.cache.notifications;
  | 
  | import java.lang.reflect.Method;
  | 
  | import org.jboss.cache.notifications.annotation.CacheListener;
  | import org.jboss.cache.notifications.annotation.NodeModified;
  | import org.jboss.cache.notifications.event.EventImpl;
  | 
  | public class Tester
  | {
  | 
  |    public static void main(String[] args)
  |    {
  |       Testee testee = new Testee();
  |       Object listener = testee.getCacheListener();
  |       
  |       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, and must be publicly accessible!");
  |       // 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())
  |       {
  |          if (m.isAnnotationPresent(NodeModified.class))
  |          {
  |             System.out.println("Annotation found");
  |             try
  |             {
  |                m.invoke(listener, new Object[] { new EventImpl() });
  |             }
  |             catch (Exception e)
  |             {
  |                e.printStackTrace(System.out);
  |             }
  |             System.out.println("Method invoked");
  |             return;
  |          }
  |       }
  |       System.out.println("Annotation not found");
  |    }
  | }

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4060822#4060822

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4060822



More information about the jboss-dev-forums mailing list