Top Description Inners Fields Constructors Methods
groovy.util

public Class ProxyGenerator

extends Object
Class Inheritance
Annotations
@SuppressWarnings:rawtypes
Imports
groovy.lang.Closure, .DelegatingMetaClass, .GroovyObject, .GroovySystem, .MetaClass, org.codehaus.groovy.runtime.InvokerHelper, .ProxyGeneratorAdapter, org.codehaus.groovy.runtime.memoize.LRUCache, org.codehaus.groovy.runtime.typehandling.GroovyCastException, org.codehaus.groovy.transform.trait.Traits, java.lang.ref.WeakReference, java.lang.reflect.Modifier, java.util.ArrayList, .Arrays, .Collections, .Comparator, .HashMap, .HashSet, .List, .Map, .Objects, .Set

Generates 'Proxy' objects which implement interfaces, maps of closures and/or extend classes/delegates.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final LRUCache<ProxyGenerator.CacheKey, ProxyGeneratorAdapter>
adapterCache

The adapter cache is used to cache proxy classes.

private boolean
private static final Map<Object, Object>
private boolean
public static final ProxyGenerator
private ClassLoader

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private ProxyGeneratorAdapter
createAdapter(final Map<Object, Object> closureMap, final List<Class<T>> interfaceList, final Class<T> delegateClass, final Class<T> baseClass)

public boolean
public boolean
public ClassLoader
public GroovyObject
public GroovyObject
instantiateAggregate(Map<K, V> closureMap, List<Class<T>> interfaces)

public GroovyObject
instantiateAggregate(Map<K, V> closureMap, List<Class<T>> interfaces, Class<T> clazz)

public GroovyObject
instantiateAggregate(Map<K, V> closureMap, List<Class<T>> interfaces, Class<T> clazz, Object[] constructorArgs)

public GroovyObject
public GroovyObject
public GroovyObject
public GroovyObject
instantiateAggregateFromBaseClass(Class<T> clazz, Object[] constructorArgs)

public GroovyObject
instantiateAggregateFromBaseClass(Map<K, V> map, Class<T> clazz, Object[] constructorArgs)

public GroovyObject
public GroovyObject
public GroovyObject
public GroovyObject
instantiateDelegate(List<Class<T>> interfaces, Object delegate)

public GroovyObject
instantiateDelegate(Map<K, V> closureMap, List<Class<T>> interfaces, Object delegate)

public GroovyObject
instantiateDelegateWithBaseClass(Map<K, V> closureMap, List<Class<T>> interfaces, Object delegate)

public GroovyObject
instantiateDelegateWithBaseClass(Map<K, V> closureMap, List<Class<T>> interfaces, Object delegate, Class<T> baseClass)

public GroovyObject

Returns:

a proxy object implementing the specified interfaces, and delegating to the provided object
instantiateDelegateWithBaseClass
(Map<K, V>
the closure for methods not handled by the delegate
closureMap
,
List<Class<T>>
interfaces to be implemented
interfaces
,
Object
the delegate object
delegate
,
Class<T>
the base class
baseClass
,
String
the name of the proxy, unused, but kept for compatibility with previous versions of Groovy.
name
)

Creates a proxy with a delegate object.

public void
setDebug(boolean
true if you want generated source to be printed
debug
)

Instructs ProxyGenerator to dump generated Groovy source code to standard output during construction.

public void
setEmptyMethods(boolean
true if you want generated methods to be empty
emptyMethods
)

Changes generated methods to have empty implementations.

private static void
setMetaClass(final MetaClass metaClass)

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

adapterCacheback to summary
private final LRUCache<ProxyGenerator.CacheKey, ProxyGeneratorAdapter> adapterCache

The adapter cache is used to cache proxy classes. When, for example, a call like: map as MyClass is found, then a lookup is made into the cache to find if a suitable adapter already exists. If so, then the class is reused, instead of generating a new class.

debugback to summary
private boolean debug
EMPTY_CLOSURE_MAPback to summary
private static final Map<Object, Object> EMPTY_CLOSURE_MAP
emptyMethodsback to summary
private boolean emptyMethods
INSTANCEback to summary
public static final ProxyGenerator INSTANCE
overrideback to summary
private ClassLoader override

Constructor Detail

ProxyGeneratorback to summary
public ProxyGenerator()

Method Detail

createAdapterback to summary
private ProxyGeneratorAdapter createAdapter(final Map<Object, Object> closureMap, final List<Class<T>> interfaceList, final Class<T> delegateClass, final Class<T> baseClass)
getDebugback to summary
public boolean getDebug()
getEmptyMethodsback to summary
public boolean getEmptyMethods()
getOverrideback to summary
public ClassLoader getOverride()
instantiateAggregateback to summary
public GroovyObject instantiateAggregate(List<Class<T>> interfaces)
instantiateAggregateback to summary
public GroovyObject instantiateAggregate(Map<K, V> closureMap, List<Class<T>> interfaces)
instantiateAggregateback to summary
public GroovyObject instantiateAggregate(Map<K, V> closureMap, List<Class<T>> interfaces, Class<T> clazz)
instantiateAggregateback to summary
public GroovyObject instantiateAggregate(Map<K, V> closureMap, List<Class<T>> interfaces, Class<T> clazz, Object[] constructorArgs)
instantiateAggregateFromBaseClassback to summary
public GroovyObject instantiateAggregateFromBaseClass(Class<T> clazz)
instantiateAggregateFromBaseClassback to summary
public GroovyObject instantiateAggregateFromBaseClass(Map<K, V> map, Class<T> clazz)
instantiateAggregateFromBaseClassback to summary
public GroovyObject instantiateAggregateFromBaseClass(Closure<V> cl, Class<T> clazz)
instantiateAggregateFromBaseClassback to summary
public GroovyObject instantiateAggregateFromBaseClass(Class<T> clazz, Object[] constructorArgs)
instantiateAggregateFromBaseClassback to summary
public GroovyObject instantiateAggregateFromBaseClass(Map<K, V> map, Class<T> clazz, Object[] constructorArgs)
instantiateAggregateFromInterfaceback to summary
public GroovyObject instantiateAggregateFromInterface(Class<T> clazz)
instantiateAggregateFromInterfaceback to summary
public GroovyObject instantiateAggregateFromInterface(Map<K, V> map, Class<T> clazz)
instantiateDelegateback to summary
public GroovyObject instantiateDelegate(Object delegate)
instantiateDelegateback to summary
public GroovyObject instantiateDelegate(List<Class<T>> interfaces, Object delegate)
instantiateDelegateback to summary
public GroovyObject instantiateDelegate(Map<K, V> closureMap, List<Class<T>> interfaces, Object delegate)
instantiateDelegateWithBaseClassback to summary
public GroovyObject instantiateDelegateWithBaseClass(Map<K, V> closureMap, List<Class<T>> interfaces, Object delegate)
instantiateDelegateWithBaseClassback to summary
public GroovyObject instantiateDelegateWithBaseClass(Map<K, V> closureMap, List<Class<T>> interfaces, Object delegate, Class<T> baseClass)
instantiateDelegateWithBaseClassback to summary
public GroovyObject instantiateDelegateWithBaseClass(Map<K, V> closureMap, List<Class<T>> interfaces, Object delegate, Class<T> baseClass, String name)

Creates a proxy with a delegate object.

Parameters
closureMap:Map<K, V>

the closure for methods not handled by the delegate

interfaces:List<Class<T>>

interfaces to be implemented

delegate:Object

the delegate object

baseClass:Class<T>

the base class

name:String

the name of the proxy, unused, but kept for compatibility with previous versions of Groovy.

Returns:GroovyObject

a proxy object implementing the specified interfaces, and delegating to the provided object

setDebugback to summary
public void setDebug(boolean debug)

Instructs ProxyGenerator to dump generated Groovy source code to standard output during construction. This is useful for debugging purposes but should be turned off in production.

Parameters
debug:boolean

true if you want generated source to be printed

setEmptyMethodsback to summary
public void setEmptyMethods(boolean emptyMethods)

Changes generated methods to have empty implementations.

Methods in generated aggregates not supplied in a closures map or base class are given 'default' implementations. The implementation will normally throw an UnsupportedOperationException but setting this boolean will leave it empty.

Parameters
emptyMethods:boolean

true if you want generated methods to be empty

setMetaClassback to summary
private static void setMetaClass(final MetaClass metaClass)
setOverrideback to summary
public void setOverride(ClassLoader override)
groovy.util back to summary

private final Class ProxyGenerator.CacheKey

extends Object
Class Inheritance

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ProxyGenerator.CacheKey.ClassReference

A weak reference which delegates equals and hashcode to the referent.

Field Summary

Modifier and TypeField and Description
private final ProxyGenerator.CacheKey.ClassReference
private final ProxyGenerator.CacheKey.ClassReference
private final boolean
private static final Comparator<Class<T>>
private final ProxyGenerator.CacheKey.ClassReference[]
private final Set<String>
private final boolean

Constructor Summary

AccessConstructor and Description
private
CacheKey(final Class<T> baseClass, final Class<T> delegateClass, final Set<String> methods, final Class<T>[] interfaces, final boolean emptyMethods, final boolean useDelegate)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(final Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

baseClassback to summary
private final ProxyGenerator.CacheKey.ClassReference baseClass
delegateClassback to summary
private final ProxyGenerator.CacheKey.ClassReference delegateClass
emptyMethodsback to summary
private final boolean emptyMethods
INTERFACE_COMPARATORback to summary
private static final Comparator<Class<T>> INTERFACE_COMPARATOR
interfacesback to summary
private final ProxyGenerator.CacheKey.ClassReference[] interfaces
methodsback to summary
private final Set<String> methods
useDelegateback to summary
private final boolean useDelegate

Constructor Detail

CacheKeyback to summary
private CacheKey(final Class<T> baseClass, final Class<T> delegateClass, final Set<String> methods, final Class<T>[] interfaces, final boolean emptyMethods, final boolean useDelegate)

Method Detail

equalsback to summary
public boolean equals(final Object o)

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
o: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
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the 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
groovy.util back to summary

private Class ProxyGenerator.CacheKey.ClassReference

extends WeakReference<Class<T>>
Class Inheritance

A weak reference which delegates equals and hashcode to the referent.

Constructor Summary

AccessConstructor and Description
public
ClassReference(Class<T> referent)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(final Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.

Constructor Detail

ClassReferenceback to summary
public ClassReference(Class<T> referent)

Method Detail

equalsback to summary
public boolean equals(final Object o)

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
o: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
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the 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