bloritsch 2002/10/03 07:54:32
Modified: util/src/java/org/apache/excalibur/util Delegate.java
Log:
Prove Delegates work with statics--unfortunately it requires an instance object.
Revision Changes Path
1.4 +46 -2
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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Delegate.java 3 Oct 2002 14:08:52 -0000 1.3
+++ Delegate.java 3 Oct 2002 14:54:32 -0000 1.4
@@ -93,6 +93,35 @@
}
/**
+ * Create a new delegate instance for a static method. We use the instance
+ * with the specified method name to forward requests to the delegate
+ * interface--which can only have one method declared. We do not require an
instance
+ *
+ * @param klass The class that has the static method
+ * @param methodName The method name that implements the signature
+ * @param delegateInterface The interface that the delegate uses.
+ *
+ * @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)
+ {
+ ClassLoader loader = delegateInterface.getClassLoader();
+ Class[] publicInterface = new Class[] { delegateInterface };
+ Method[] interfaceMethods = delegateInterface.getDeclaredMethods();
+
+ if ( interfaceMethods.length != 1 )
+ {
+ throw new IllegalArgumentException("The delegate interface must have
one (1) and only one method.");
+ }
+
+ Class[] signature = interfaceMethods[0].getParameterTypes();
+ InvocationHandler handler = new DelegateHandler( klass, methodName,
signature );
+
+ return Proxy.newProxyInstance( loader, publicInterface, handler );
+ }
+
+ /**
* The invocation handler for all delegates.
*/
private static final class DelegateHandler implements InvocationHandler
@@ -115,8 +144,23 @@
{
m_instance = instance;
- Class instanceClass = m_instance.getClass();
- Method[] methods = instanceClass.getDeclaredMethods();
+ this( m_instance.getClass() );
+ }
+
+ /**
+ * 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 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
)
+ {
+ Method[] methods = klass.getDeclaredMethods();
Method delegate = null;
for ( int i = 0; i < methods.length; i++ )
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>