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.
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).
instance
parameter passed to the newDelegate method was the same for both. The
instances are compared with the identity equality operator, ==.
Method.equals.| Modifier and Type | Class and Description |
|---|---|
| public static class | |
| pack-priv static interface |
| Modifier and Type | Field and Description |
|---|---|
| protected String | |
| private static final MethodDelegate. | |
| protected Object |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static MethodDelegate | |
| public static MethodDelegate | |
| public boolean | equals(Object
the reference object with which to compare. obj)Overrides java. |
| public Object | |
| public int | |
| public abstract MethodDelegate |
| eqMethod | back to summary |
|---|---|
| protected String eqMethod | |
| KEY_FACTORY | back to summary |
|---|---|
| private static final MethodDelegate. | |
| target | back to summary |
|---|---|
| protected Object target | |
| MethodDelegate | back to summary |
|---|---|
| public MethodDelegate() | |
| create | back to summary |
|---|---|
| public static MethodDelegate create(Object target, String methodName, Class<T> iface) | |
| createStatic | back to summary |
|---|---|
| public static MethodDelegate createStatic(Class<T> targetClass, String methodName, Class<T> iface) | |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| getTarget | back to summary |
|---|---|
| public Object getTarget() | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| newInstance | back to summary |
|---|---|
| public abstract MethodDelegate newInstance(Object target) | |
| Modifier and Type | Field and Description |
|---|---|
| private Class | |
| private static final Type | |
| private String | |
| private static final Signature | |
| private static final AbstractClassGenerator. | |
| private Object | |
| private Class |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public MethodDelegate | |
| protected Object | firstInstance(Class<T> type)
Implements abstract org. |
| public void | |
| protected ClassLoader | |
| protected ProtectionDomain | getProtectionDomain()
Overrides org. |
| protected Object | nextInstance(Object instance)
Implements abstract org. |
| public void | |
| public void | |
| public void | |
| public void |
| iface | back to summary |
|---|---|
| private Class<T> iface | |
| METHOD_DELEGATE | back to summary |
|---|---|
| private static final Type METHOD_DELEGATE | |
| methodName | back to summary |
|---|---|
| private String methodName | |
| NEW_INSTANCE | back to summary |
|---|---|
| private static final Signature NEW_INSTANCE | |
| SOURCE | back to summary |
|---|---|
| private static final AbstractClassGenerator. | |
| target | back to summary |
|---|---|
| private Object target | |
| targetClass | back to summary |
|---|---|
| private Class<T> targetClass | |
| Generator | back to summary |
|---|---|
| public Generator() | |
| create | back to summary |
|---|---|
| public MethodDelegate create() | |
| firstInstance | back to summary |
|---|---|
| protected Object firstInstance(Class<T> type) Implements abstract org.
| |
| generateClass | back to summary |
|---|---|
| public void generateClass(ClassVisitor v) throws NoSuchMethodException Implements org.
| |
| getDefaultClassLoader | back to summary |
|---|---|
| protected ClassLoader getDefaultClassLoader() Implements abstract org.
| |
| getProtectionDomain | back to summary |
|---|---|
| protected ProtectionDomain getProtectionDomain() Overrides org. Doc from org. Returns the protection domain to use when defining the class.
Default implementation returns
| |
| nextInstance | back to summary |
|---|---|
| protected Object nextInstance(Object instance) Implements abstract org.
| |
| setInterface | back to summary |
|---|---|
| public void setInterface(Class<T> iface) | |
| setMethodName | back to summary |
|---|---|
| public void setMethodName(String methodName) | |
| setTarget | back to summary |
|---|---|
| public void setTarget(Object target) | |
| setTargetClass | back to summary |
|---|---|
| public void setTargetClass(Class<T> targetClass) | |
| Modifier and Type | Method and Description |
|---|---|
| public Object |
| newInstance | back to summary |
|---|---|
| public Object newInstance(Class<T> delegateClass, String methodName, Class<T> iface) | |