net.sf.cglib
Class MethodDelegate

java.lang.Object
  |
  +--net.sf.cglib.MethodDelegate

public abstract class MethodDelegate
extends java.lang.Object

DOCUMENTATION FROM APACHE AVALON DELEGATE CLASS

Delegates are a typesafe pointer to another method. Since Java does not have language support for such a construct, this utility will construct a proxy that forwards method calls to any method with the same signature. This utility is inspired in part by the C# delegate mechanism. We implemented it in a Java-centric manner.

Delegate

Any interface with one method can become the interface for a delegate. Consider the example below:

   public interface MainDelegate {
       int main(String[] args);
   }
 

The interface above is an example of an interface that can become a delegate. It has only one method, and the interface is public. In order to create a delegate for that method, all we have to do is call MethodDelegate.create(this, "alternateMain", MainDelegate.class). The following program will show how to use it:

   public class Main {
       public static int main( String[] args ) {
           Main newMain = new Main();
           MainDelegate start = (MainDelegate)
               MethodDelegate.create(newMain, "alternateMain", MainDelegate.class);
           return start.main( args );
       }

       public int alternateMain( String[] args ) {
           for (int i = 0; i < args.length; i++) {
               System.out.println( args[i] );
           }
           return args.length;
       }
   }
 

By themselves, delegates don't do much. Their true power lies in the fact that they can be treated like objects, and passed to other methods. In fact that is one of the key building blocks of building Intelligent Agents which in tern are the foundation of artificial intelligence. In the above program, we could have easily created the delegate to match the static main method by substituting the delegate creation call with this: MethodDelegate.createStatic(getClass(), "main", MainDelegate.class).

Another key use for Delegates is to register event listeners. It is much easier to have all the code for your events separated out into methods instead of individual classes. One of the ways Java gets around that is to create anonymous classes. They are particularly troublesome because many Debuggers do not know what to do with them. Anonymous classes tend to duplicate alot of code as well. We can use any interface with one declared method to forward events to any method that matches the signature (although the method name can be different).

Equality

The criteria that we use to test if two delegates are equal are:

Version:
$Id: MethodDelegate.java,v 1.11 2003/07/15 16:38:46 herbyderby Exp $

Field Summary
protected  java.lang.Object delegate
           
protected  java.lang.String eqMethod
           
 
Constructor Summary
protected MethodDelegate()
           
 
Method Summary
static MethodDelegate create(java.lang.Object delegate, java.lang.String methodName, java.lang.Class iface)
           
static MethodDelegate create(java.lang.Object delegate, java.lang.String methodName, java.lang.Class iface, java.lang.ClassLoader loader)
           
static MethodDelegate createStatic(java.lang.Class clazz, java.lang.String methodName, java.lang.Class iface)
           
static MethodDelegate createStatic(java.lang.Class clazz, java.lang.String methodName, java.lang.Class iface, java.lang.ClassLoader loader)
           
 boolean equals(java.lang.Object obj)
           
 java.lang.Object getInvocationTarget()
           
 int hashCode()
           
protected abstract  MethodDelegate newInstance(java.lang.Object delegate)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

delegate

protected java.lang.Object delegate

eqMethod

protected java.lang.String eqMethod
Constructor Detail

MethodDelegate

protected MethodDelegate()
Method Detail

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

getInvocationTarget

public java.lang.Object getInvocationTarget()

newInstance

protected abstract MethodDelegate newInstance(java.lang.Object delegate)

createStatic

public static MethodDelegate createStatic(java.lang.Class clazz,
                                          java.lang.String methodName,
                                          java.lang.Class iface)

createStatic

public static MethodDelegate createStatic(java.lang.Class clazz,
                                          java.lang.String methodName,
                                          java.lang.Class iface,
                                          java.lang.ClassLoader loader)

create

public static MethodDelegate create(java.lang.Object delegate,
                                    java.lang.String methodName,
                                    java.lang.Class iface)

create

public static MethodDelegate create(java.lang.Object delegate,
                                    java.lang.String methodName,
                                    java.lang.Class iface,
                                    java.lang.ClassLoader loader)


Copyright © 2002-2003 cglib. All Rights Reserved.