Class ConversionHandler
- All Implemented Interfaces:
Serializable,InvocationHandler
- Direct Known Subclasses:
ConvertedClosure,ConvertedMap
Instances are Serializable, so closure as SomeInterface survives a round trip
through ObjectOutputStream and back — behaviour distributed-compute and
caching users depend on, and deliberately not gated.
The same property means a crafted stream can rebuild one of these proxies with a delegate
it chose, and every interface call then routes into that delegate. ConvertedMap looks up
its closure by the invoked method's name, so a stream naming toString supplies code that
runs when the object is merely logged or interpolated, with no call to a declared method.
Deserializing untrusted bytes is out of scope for this project (THREAT_MODEL.md ยง3) and no
change to this class makes it safe; where an application must read semi-trusted streams, bound
them with a JDK serial filter, for example
-Djdk.serialFilter='!org.codehaus.groovy.runtime.ConversionHandler;*', which refuses these
handlers while leaving ordinary closure serialization intact.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConversionHandler(Object delegate) Creates a ConversionHandler with a delegate. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckMethod(Method method) Reports whethermethodshould be invoked on this handler rather than forwarded to the delegate.booleanIndicates whether some other object is "equal to" this one.Returns the delegate.inthashCode()Returns a hash code value for the delegate.This method is a default implementation for the invoke method given in InvocationHandler.abstract ObjectinvokeCustom(Object proxy, Method method, Object[] args) This method is called for all Methods not defined on Object.static booleanisCoreObjectMethod(Method method) Checks whether a method is a core method from java.lang.Object.protected booleanisDefaultMethod(Method method) Reports whethermethodis a Java interface default method.toString()Returns a String version of the delegate.
-
Constructor Details
-
ConversionHandler
Creates a ConversionHandler with a delegate.- Parameters:
delegate- the delegate- Throws:
IllegalArgumentException- if the given delegate is null
-
-
Method Details
-
getDelegate
Returns the delegate.- Returns:
- the delegate
-
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.
- Specified by:
invokein interfaceInvocationHandler- Parameters:
proxy- the proxymethod- the methodargs- the arguments- Returns:
- the result of the invocation by method or delegate
- Throws:
Throwable- if caused by the delegate or the method- See Also:
-
isDefaultMethod
Reports whethermethodis a Java interface default method.invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])calls such a method withinvokespecialunless the delegate map overrides that name.- Parameters:
method- method being invoked on the proxy- Returns:
truewhenmethodis a default method
-
checkMethod
Reports whethermethodshould be invoked on this handler rather than forwarded to the delegate. Methods declared onObjecttake that path.- Parameters:
method- method being invoked on the proxy- Returns:
truewhenmethodis declared onObject
-
invokeCustom
This method is called for all Methods not defined on Object. The delegate should be called here.- Parameters:
proxy- the proxymethod- the methodargs- the arguments- Returns:
- the result of the invocation of the delegate
- Throws:
Throwable- any exception causes by the delegate- See Also:
-
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. -
hashCode
public int hashCode()Returns a hash code value for the delegate. -
toString
Returns a String version of the delegate. -
isCoreObjectMethod
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- the method to check- Returns:
- true if the method is deemed to be a core method
-