bloritsch    2002/10/03 08:12:45

  Modified:    util/src/java/org/apache/excalibur/util Delegate.java
  Log:
  make delegate work a bit more nicely with static methods--i.e. no instance needed
  
  Revision  Changes    Path
  1.6       +39 -12    
jakarta-avalon-excalibur/util/src/java/org/apache/excalibur/util/Delegate.java
  
  Index: Delegate.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/util/src/java/org/apache/excalibur/util/Delegate.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Delegate.java     3 Oct 2002 15:05:54 -0000       1.5
  +++ Delegate.java     3 Oct 2002 15:12:45 -0000       1.6
  @@ -87,7 +87,7 @@
           }
   
           Class[] signature = interfaceMethods[0].getParameterTypes();
  -        InvocationHandler handler = new DelegateHandler( instance, 
instance.getClass(), methodName, signature );
  +        InvocationHandler handler = new DelegateHandler( instance, methodName, 
signature );
   
           return  Proxy.newProxyInstance( loader, publicInterface, handler );
       }
  @@ -104,7 +104,7 @@
        * @return the Delegate instance.  You have to cast it to the interface
        *         you passed in (<code>delegateInterface</code>).
        */
  -    public static Object newStaticDelegate( Class klass, String methodName, Class 
delegateInterface)
  +    public static Object newDelegate( Class klass, String methodName, Class 
delegateInterface)
       {
           ClassLoader loader = delegateInterface.getClassLoader();
           Class[] publicInterface = new Class[] { delegateInterface };
  @@ -116,7 +116,7 @@
           }
   
           Class[] signature = interfaceMethods[0].getParameterTypes();
  -        InvocationHandler handler = new DelegateHandler( null, klass, methodName, 
signature );
  +        InvocationHandler handler = new DelegateHandler( klass, methodName, 
signature );
   
           return  Proxy.newProxyInstance( loader, publicInterface, handler );
       }
  @@ -130,21 +130,48 @@
           private final Method m_delegateMethod;
   
           /**
  -         * Create a new InvocationHandler for the delegate we manufactured.
  -         * We examine the instance class passed in for the method with the
  -         * requested name.  When we find it, we save and use it every time
  -         * we have a method call on the delegate.
  +         * Create a new InvocationHandler for the delegate we manufactured,
  +         * this version is used to delegate to objects.
            *
            * @param instance    The object instance that has the method
  -         * @param klass       The class of the object containing the method
            * @param methodName  The method name that we forward messages to.
            * @param signature   The method signature used to ensure we have
            *                    a good delegate.
            */
  -        public DelegateHandler ( Object instance, Class klass, String methodName, 
Class[] signature )
  +        public DelegateHandler ( Object instance, String methodName, Class[] 
signature )
           {
               m_instance = instance;
  +            m_delegateMethod = extractMethod( m_instance.getClass(), methodName, 
signature );
  +        }
  +
  +        /**
  +         * Create a new InvocationHandler for the delegate we manufactured,
  +         * this version is used to delegate to static methods.
  +         *
  +         * @param klass       The class of the object containing the method
  +         * @param methodName  The method name that we forward messages to.
  +         * @param signature   The method signature used to ensure we have
  +         *                    a good delegate.
  +         */
  +        public DelegateHandler ( Class klass, String methodName, Class[] signature 
)
  +        {
  +            m_instance = null;
  +            m_delegateMethod = extractMethod( klass, methodName, signature );
  +        }
   
  +        /**
  +         * Extract the Method instance we need for this delegate.  We examine
  +         * the instance class passed in for the method with the requested name.
  +         * When we find it, we save and use it every time we have a method call
  +         * on the delegate.
  +         *
  +         * @param klass       The class of the object containing the method
  +         * @param methodName  The method name that we forward messages to.
  +         * @param signature   The method signature used to ensure we have
  +         *                    a good delegate.
  +         */
  +        public Method extractMethod ( Class klass, String methodName, Class[] 
signature )
  +        {
               Method[] methods = klass.getDeclaredMethods();
               Method delegate = null;
   
  @@ -174,12 +201,12 @@
                   }
               }
   
  -            m_delegateMethod = delegate;
  -
  -            if ( m_delegateMethod == null )
  +            if ( delegate == null )
               {
                   throw new IllegalArgumentException( "There is no valid method with 
the required interface" );
               }
  +
  +            return delegate;
           }
   
           /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to