bloritsch 2002/10/03 07:08:52
Modified: util/src/java/org/apache/excalibur/util Delegate.java
util/src/test/org/apache/excalibur/util/test
DelegateTestCase.java
Added: util/src/test/org/apache/excalibur/util/test
EchoDelegate.java
Removed: util/src/test/org/apache/excalibur/util/test
TestDelegate.java
Log:
Update Delegate to be able to be used like Leo Sutic's version
which is arguably better.
Revision Changes Path
1.3 +97 -63
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Delegate.java 3 Oct 2002 04:44:39 -0000 1.2
+++ Delegate.java 3 Oct 2002 14:08:52 -0000 1.3
@@ -49,95 +49,129 @@
*/
package org.apache.excalibur.util;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
+import java.lang.reflect.*;
/**
* Used to create a proxy for a method. It is supposed to be as close to the
* C# delegate as possible. The Delegate is only allowed to have one method,
- * and all instances of it will call that one method.
+ * and all instances of it will call that one method. You must also realize
+ * that only public methods on public classes can be turned into a Delegate.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
*/
-public abstract class Delegate
+public final class Delegate
{
- private final Method m_method;
- private final Object m_object;
-
/**
- * Pass in the delegate object that implements the same method that
- * the concrete definition does.
+ * Create a new delegate instance. We use the instance with the specified
+ * method name to forward requests to the delegate interface--which can
+ * only have one method declared.
+ *
+ * @param instance The object that has the method
+ * @param methodName The method name that implements the signature
+ * @param delegateInterface The interface that the delegate uses.
*
- * @param objDelegate the Object that implements the method.
+ * @return the Delegate instance. You have to cast it to the interface
+ * you passed in (<code>delegateInterface</code>).
*/
- public Delegate( Object objDelegate )
+ public static Object newDelegate( Object instance, String methodName, Class
delegateInterface)
{
- m_object = objDelegate;
+ ClassLoader loader = delegateInterface.getClassLoader();
+ Class[] publicInterface = new Class[] { delegateInterface };
+ Method[] interfaceMethods = delegateInterface.getDeclaredMethods();
- Method[] methods = getClass().getMethods();
- Method delegateMethod = null;
-
- for ( int i = 0; i < methods.length; i++ )
+ if ( interfaceMethods.length != 1 )
{
- if ( Modifier.isPublic( methods[i].getModifiers() ) )
- {
- // ignore all Object methods
- String name = methods[i].getName();
-
- if ( name.equals( "getClass" ) )
- continue;
-
- if ( name.equals( "equals" ) )
- continue;
-
- if ( name.equals( "hashCode" ) )
- continue;
+ throw new IllegalArgumentException("The delegate interface must have
one (1) and only one method.");
+ }
- if ( name.equals( "notify" ) )
- continue;
+ Class[] signature = interfaceMethods[0].getParameterTypes();
+ InvocationHandler handler = new DelegateHandler( instance, methodName,
signature );
- if ( name.equals( "notifyAll" ) )
- continue;
+ return Proxy.newProxyInstance( loader, publicInterface, handler );
+ }
- if ( name.equals( "toString" ) )
- continue;
+ /**
+ * The invocation handler for all delegates.
+ */
+ private static final class DelegateHandler implements InvocationHandler
+ {
+ private final Object m_instance;
+ private final Method m_delegateMethod;
- if ( name.equals( "wait" ) )
- continue;
+ /**
+ * 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.
+ *
+ * @param instance The object instance that has 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, String methodName, Class[]
signature )
+ {
+ m_instance = instance;
+
+ Class instanceClass = m_instance.getClass();
+ Method[] methods = instanceClass.getDeclaredMethods();
+ Method delegate = null;
- delegateMethod = methods[i];
+ for ( int i = 0; i < methods.length; i++ )
+ {
+ if ( methods[i].getName().equals( methodName ) )
+ {
+ Class[] parameters = methods[i].getParameterTypes();
+
+ if ( parameters.length == signature.length )
+ {
+ boolean signatureOk = true;
+
+ for ( int m = 0; m < signature.length; m++ )
+ {
+ if ( ! ( signature[m].equals( parameters[m] ) ) )
+ {
+ signatureOk = false;
+ }
+ }
+
+ if ( signatureOk )
+ {
+ delegate = methods[i];
+ }
+ }
+ }
}
- }
-
- if ( null == delegateMethod )
- {
- throw new VerifyError("A Delegate must have one method that is not an
Object method.");
- }
- Class[] argTypes = delegateMethod.getParameterTypes();
- String name = delegateMethod.getName();
+ m_delegateMethod = delegate;
- try
- {
- Class objClass = m_object.getClass();
- m_method = objClass.getMethod( name, argTypes );
- }
- catch( Exception e )
- {
- throw new IllegalArgumentException( "The class does not implement the
required method." );
+ if ( m_delegateMethod == null )
+ {
+ throw new IllegalArgumentException( "There is no valid method with
the required interface" );
+ }
}
- }
- protected final Object invoke( Object[] args )
- {
- try
- {
- return (String) m_method.invoke( m_object, args );
- }
- catch( Exception e )
+ /**
+ * Call the proxied delegate method.
+ *
+ * @param proxy The original object we are forwarding calls to.
+ * @param m The method we are ignoring( the delegated method
+ * may not have the same name )
+ * @param args The arguments passed to the method
+ *
+ * @return the results of the invocation in the form of an Object
+ *
+ * @throws IllegalAccessException When the method or proxy object
+ * is not accessible
+ * @throws InvocationTargetException When the method throws an
+ * exception
+ */
+ public Object invoke( Object proxy, Method m, Object[] args )
+ throws IllegalAccessException, InvocationTargetException
{
- throw new RuntimeException( "Bad Method" );
+ return m_delegateMethod.invoke( m_instance, args );
}
}
}
1.3 +14 -12
jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/DelegateTestCase.java
Index: DelegateTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/DelegateTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DelegateTestCase.java 3 Oct 2002 04:44:39 -0000 1.2
+++ DelegateTestCase.java 3 Oct 2002 14:08:52 -0000 1.3
@@ -50,10 +50,11 @@
package org.apache.excalibur.util.test;
import junit.framework.TestCase;
+import org.apache.excalibur.util.Delegate;
/**
- * @author <a href="${EMAIL}">bloritsch</a>
+ * @author <a href="[EMAIL PROTECTED]">Berin Loritsch</a>
*/
public class DelegateTestCase extends TestCase
{
@@ -64,39 +65,40 @@
super( name );
}
- public final class Echo
+ public static final class Echo
{
public String echo( String message )
{
return "Echo: " + message;
}
- }
- public final class OtherEcho
- {
- public String echo( String message )
+ public String otherEcho( String message )
{
return "OtherEcho: " + message;
}
}
+ private final Echo m_echo = new Echo();
+
public void testDelegate()
{
- TestDelegate delegate = new TestDelegate( new Echo() );
+ EchoDelegate delegate = (EchoDelegate) Delegate.newDelegate( m_echo,
"echo", EchoDelegate.class );
assertTrue( delegate.echo( MESSAGE ).startsWith( "Echo: " ) );
}
public void testDifferentDelegate()
{
- TestDelegate delegate = new TestDelegate( new Echo() );
- TestDelegate otherDelegate = new TestDelegate( new OtherEcho() );
+ EchoDelegate delegate =
+ (EchoDelegate) Delegate.newDelegate( m_echo, "echo",
EchoDelegate.class );
+ EchoDelegate otherDelegate =
+ (EchoDelegate) Delegate.newDelegate( m_echo, "otherEcho",
EchoDelegate.class );
assertTrue( delegate.echo( MESSAGE ).startsWith( "Echo: " ) );
assertTrue( otherDelegate.echo( MESSAGE ).startsWith( "OtherEcho: " ) );
}
- protected void checkDelegate( TestDelegate delegate )
+ protected void checkDelegate( EchoDelegate delegate )
{
String answer = delegate.echo( MESSAGE );
@@ -105,7 +107,7 @@
public void testExpectedUse()
{
- checkDelegate( new TestDelegate( new Echo() ) );
- checkDelegate( new TestDelegate( new OtherEcho() ) );
+ checkDelegate( (EchoDelegate) Delegate.newDelegate( m_echo, "echo",
EchoDelegate.class ) );
+ checkDelegate( (EchoDelegate) Delegate.newDelegate( m_echo, "otherEcho",
EchoDelegate.class ) );
}
}
1.1
jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/EchoDelegate.java
Index: EchoDelegate.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
must not be used to endorse or promote products derived from this software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.util.test;
/**
* Used in the testcase to create a Delegate.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
*/
public interface EchoDelegate
{
String echo( String message );
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>