Top Description Inners Fields Constructors Methods
org.springframework.cglib.reflect

public abstract Class MethodDelegate

extends Object
Class Inheritance
Annotations
@SuppressWarnings:rawtypes, unchecked
Imports
java.lang.reflect.Method, java.security.ProtectionDomain, org.springframework.asm.ClassVisitor, .Type, org.springframework.cglib.core.AbstractClassGenerator, .ClassEmitter, .CodeEmitter, .Constants, .EmitUtils, .KeyFactory, .MethodInfo, .ReflectUtils, .Signature, .TypeUtils

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.25 2006/03/05 02:43:19 herbyderby Exp $

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
pack-priv static interface

Field Summary

Modifier and TypeField and Description
protected String
private static final MethodDelegate.MethodDelegateKey
protected Object

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static MethodDelegate
create(Object target, String methodName, Class<T> iface)

public static MethodDelegate
createStatic(Class<T> targetClass, String methodName, Class<T> iface)

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public Object
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public abstract MethodDelegate
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

eqMethodback to summary
protected String eqMethod
KEY_FACTORYback to summary
private static final MethodDelegate.MethodDelegateKey KEY_FACTORY
targetback to summary
protected Object target

Constructor Detail

MethodDelegateback to summary
public MethodDelegate()

Method Detail

createback to summary
public static MethodDelegate create(Object target, String methodName, Class<T> iface)
createStaticback to summary
public static MethodDelegate createStatic(Class<T> targetClass, String methodName, Class<T> iface)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

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.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getTargetback to summary
public Object getTarget()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
newInstanceback to summary
public abstract MethodDelegate newInstance(Object target)
org.springframework.cglib.reflect back to summary

public Class MethodDelegate.Generator

extends AbstractClassGenerator
Class Inheritance

Field Summary

Modifier and TypeField and Description
private Class<T>
private static final Type
private String
private static final Signature
private static final AbstractClassGenerator.Source
private Object
private Class<T>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public MethodDelegate
protected Object
public void
protected ClassLoader
protected ProtectionDomain
getProtectionDomain()

Overrides org.springframework.cglib.core.AbstractClassGenerator.getProtectionDomain.

Returns the protection domain to use when defining the class.

protected Object
public void
setInterface(Class<T> iface)

public void
setMethodName(String methodName)

public void
setTarget(Object target)

public void
setTargetClass(Class<T> targetClass)

Inherited from org.springframework.cglib.core.AbstractClassGenerator:
creategenerategetAttemptLoadgetClassLoadergetClassNamegetCurrentgetNamingPolicygetStrategygetUseCachesetAttemptLoadsetClassLoadersetContextClasssetNamePrefixsetNamingPolicysetStrategysetUseCacheunwrapCachedValuewrapCachedClass

Field Detail

ifaceback to summary
private Class<T> iface
METHOD_DELEGATEback to summary
private static final Type METHOD_DELEGATE
methodNameback to summary
private String methodName
NEW_INSTANCEback to summary
private static final Signature NEW_INSTANCE
SOURCEback to summary
private static final AbstractClassGenerator.Source SOURCE
targetback to summary
private Object target
targetClassback to summary
private Class<T> targetClass

Constructor Detail

Generatorback to summary
public Generator()

Method Detail

createback to summary
public MethodDelegate create()
firstInstanceback to summary
protected Object firstInstance(Class<T> type)

Implements abstract org.springframework.cglib.core.AbstractClassGenerator.firstInstance.

Annotations
@Override
generateClassback to summary
public void generateClass(ClassVisitor v) throws NoSuchMethodException

Implements org.springframework.cglib.core.ClassGenerator.generateClass.

Annotations
@Override
getDefaultClassLoaderback to summary
protected ClassLoader getDefaultClassLoader()

Implements abstract org.springframework.cglib.core.AbstractClassGenerator.getDefaultClassLoader.

Annotations
@Override
getProtectionDomainback to summary
protected ProtectionDomain getProtectionDomain()

Overrides org.springframework.cglib.core.AbstractClassGenerator.getProtectionDomain.

Doc from org.springframework.cglib.core.AbstractClassGenerator.getProtectionDomain.

Returns the protection domain to use when defining the class.

Default implementation returns null for using a default protection domain. Sub-classes may override to use a more specific protection domain.

Returns:ProtectionDomain

the protection domain (null for using a default)

Annotations
@Override
nextInstanceback to summary
protected Object nextInstance(Object instance)

Implements abstract org.springframework.cglib.core.AbstractClassGenerator.nextInstance.

Annotations
@Override
setInterfaceback to summary
public void setInterface(Class<T> iface)
setMethodNameback to summary
public void setMethodName(String methodName)
setTargetback to summary
public void setTarget(Object target)
setTargetClassback to summary
public void setTargetClass(Class<T> targetClass)
org.springframework.cglib.reflect back to summary

pack-priv Interface MethodDelegate.MethodDelegateKey


Method Summary

Modifier and TypeMethod and Description
public Object
newInstance(Class<T> delegateClass, String methodName, Class<T> iface)

Method Detail

newInstanceback to summary
public Object newInstance(Class<T> delegateClass, String methodName, Class<T> iface)