Top Description Fields Constructors Methods
org.codehaus.groovy.runtime

public abstract Class ConversionHandler

extends Object
implements InvocationHandler, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.reflect.InvocationHandler
Known Direct Subclasses
org.codehaus.groovy.runtime.ConvertedClosure, org.codehaus.groovy.runtime.ConvertedMap
Imports
groovy.lang.GroovyObject, .GroovyRuntimeException, .GroovySystem, .MetaClass, org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl, org.codehaus.groovy.vmplugin.VMPlugin, .VMPluginFactory, java.io.Serializable, java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.util.Map, java.util.concurrent.ConcurrentHashMap

This class is a general adapter to map a call to a Java interface to a given delegate.

Field Summary

Modifier and TypeField and Description
private final Object
private final ConcurrentHashMap<Method, Object>
private MetaClass
private static final long

Constructor Summary

AccessConstructor and Description
public
ConversionHandler(Object
the delegate
delegate
)

Creates a ConversionHandler with an delegate.

Method Summary

Modifier and TypeMethod and Description
protected boolean
checkMethod(final Method method)

private boolean
defaultOverridden(final Method method)

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

Returns:

the delegate
getDelegate
()

Returns the delegate.

private MetaClass
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the delegate.
public Object

Returns:

the result of the invocation by method or delegate
invoke
(final Object
the proxy
proxy
,
final Method
the method
method
,
final Object[]
the arguments
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

This method is a default implementation for the invoke method given in InvocationHandler.
public abstract Object

Returns:

the result of the invocation of the delegate
invokeCustom
(Object
the proxy
proxy
,
Method
the method
method
,
Object[]
the arguments
args
)

This method is called for all Methods not defined on Object.

public static boolean

Returns:

true if the method is deemed to be a core method
isCoreObjectMethod
(Method
the method to check
method
)

Checks whether a method is a core method from java.lang.Object.

protected boolean
isDefaultMethod(final Method method)

private MetaClass
public String
toString()

Overrides java.lang.Object.toString.

Returns a String version of the delegate.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

delegateback to summary
private final Object delegate
handleCacheback to summary
private final ConcurrentHashMap<Method, Object> handleCache
metaClassback to summary
private MetaClass metaClass
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

ConversionHandlerback to summary
public ConversionHandler(Object delegate)

Creates a ConversionHandler with an delegate.

Parameters
delegate:Object

the delegate

Exceptions
IllegalArgumentException:
if the given delegate is null

Method Detail

checkMethodback to summary
protected boolean checkMethod(final Method method)
defaultOverriddenback to summary
private boolean defaultOverridden(final Method method)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one. The delegate is used if the class of the parameter and the current class are equal. In other cases the method will return false. The exact class is here used, if inheritance is needed, this method must be overwritten.

Parameters
obj:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

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

See Also
java.lang.Object#equals(java.lang.Object)
getDelegateback to summary
public Object getDelegate()

Returns the delegate.

Returns:Object

the delegate

getMetaClassback to summary
private MetaClass getMetaClass(Object proxy)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the delegate.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

See Also
java.lang.Object#hashCode()
invokeback to summary
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

This method is a default implementation for the invoke method given in InvocationHandler. Any call to a method with a declaring class that is not Object, excluding toString() and default methods is redirected to invokeCustom.

Methods like equals and hashcode are called on the class itself instead of the delegate because they are considered fundamental methods that should not be overwritten. The toString() method gets special treatment as it is deemed to be a method that you might wish to override when called from Groovy. Interface default methods from Java 8 on the other hand are considered being default implementations you don't normally want to change. So they are called directly too

In many scenarios, it is better to overwrite the invokeCustom method where the core Object related methods are filtered out.

Parameters
proxy:Object

the proxy

method:Method

the method

args:Object[]

the arguments

Returns:Object

the result of the invocation by method or delegate

Exceptions
Throwable:
if caused by the delegate or the method
See Also
invokeCustom(Object, Method, Object[]), InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
invokeCustomback to summary
public abstract Object invokeCustom(Object proxy, Method method, Object[] args) throws Throwable

This method is called for all Methods not defined on Object. The delegate should be called here.

Parameters
proxy:Object

the proxy

method:Method

the method

args:Object[]

the arguments

Returns:Object

the result of the invocation of the delegate

Exceptions
Throwable:
any exception causes by the delegate
See Also
invoke(Object, Method, Object[]), InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
isCoreObjectMethodback to summary
public static boolean isCoreObjectMethod(Method method)

Checks whether a method is a core method from java.lang.Object. Such methods often receive special treatment because they are deemed fundamental enough to not be tampered with.

Parameters
method:Method

the method to check

Returns:boolean

true if the method is deemed to be a core method

isDefaultMethodback to summary
protected boolean isDefaultMethod(final Method method)
setMetaClassback to summary
private MetaClass setMetaClass(MetaClass mc)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a String version of the delegate.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

See Also
java.lang.Object#toString()