Package groovy.lang
Class ProxyMetaClass
java.lang.Object
groovy.lang.MetaClassImpl
groovy.lang.ProxyMetaClass
- All Implemented Interfaces:
AdaptingMetaClass
,MetaClass
,MetaObjectProtocol
,MutableMetaClass
- Direct Known Subclasses:
MockProxyMetaClass
public class ProxyMetaClass extends MetaClassImpl implements AdaptingMetaClass
As subclass of MetaClass, ProxyMetaClass manages calls from Groovy Objects to POJOs.
It enriches MetaClass with the feature of making method invocations interceptable by
an Interceptor. To this end, it acts as a decorator (decorator pattern) allowing
to add or withdraw this feature at runtime.
See groovy/lang/InterceptorTest.groovy for details.
WARNING: This implementation of ProxyMetaClass is NOT thread-safe and hence should only be used for as a per-instance MetaClass running in a single thread. Do not place this MetaClass in the MetaClassRegistry as it will result in unpredictable behaviour
- See Also:
MetaClassRegistry
-
Nested Class Summary
Nested classes/interfaces inherited from class groovy.lang.MetaClassImpl
MetaClassImpl.Index, MetaClassImpl.MetaConstructor
-
Field Summary
Fields Modifier and Type Field Description protected MetaClass
adaptee
protected Interceptor
interceptor
Fields inherited from class groovy.lang.MetaClassImpl
EMPTY_ARGUMENTS, getPropertyMethod, INVOKE_METHOD_METHOD, invokeMethodMethod, isGroovyObject, isMap, metaMethodIndex, METHOD_MISSING, PROPERTY_MISSING, registry, setPropertyMethod, STATIC_METHOD_MISSING, STATIC_PROPERTY_MISSING, theCachedClass, theClass
-
Constructor Summary
Constructors Constructor Description ProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee)
-
Method Summary
Modifier and Type Method Description MetaClass
getAdaptee()
Returns the MetaClass that this adapter adaptsstatic ProxyMetaClass
getInstance(Class theClass)
convenience factory method for the most usual case.Interceptor
getInterceptor()
Object
getProperty(Class aClass, Object object, String property, boolean useSuper, boolean fromInsideClass)
Interceptors the call to getProperty if a PropertyAccessInterceptor is availablevoid
initialize()
Complete the initialisation process.Object
invokeConstructor(Object[] arguments)
Call invokeConstructor on adaptee with logic like in MetaClass unless we have an Interceptor.Object
invokeMethod(Class sender, Object object, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass)
Call invokeMethod on adaptee with logic like in MetaClass unless we have an Interceptor.Object
invokeMethod(Object object, String methodName, Object[] arguments)
Call invokeMethod on adaptee with logic like in MetaClass unless we have an Interceptor.Object
invokeStaticMethod(Object object, String methodName, Object[] arguments)
Call invokeStaticMethod on adaptee with logic like in MetaClass unless we have an Interceptor.void
setAdaptee(MetaClass metaClass)
Sets the MetaClass adapted by this MetaClassvoid
setInterceptor(Interceptor interceptor)
void
setProperty(Class aClass, Object object, String property, Object newValue, boolean useSuper, boolean fromInsideClass)
Interceptors the call to a property setter if a PropertyAccessInterceptor is availableObject
use(Closure closure)
Use the ProxyMetaClass for the given Closure.Object
use(GroovyObject object, Closure closure)
Use the ProxyMetaClass for the given Closure.Methods inherited from class groovy.lang.MetaClassImpl
addMetaBeanProperty, addMetaMethod, addMetaMethodToIndex, addNewInstanceMethod, addNewStaticMethod, applyPropertyDescriptors, checkIfGroovyObjectMethod, checkInitalised, chooseMethod, clearInvocationCaches, createConstructorSite, createErrorMessageForAmbiguity, createPogoCallCurrentSite, createPogoCallSite, createPojoCallSite, createStaticSite, createTransformMetaMethod, doChooseMostSpecificParams, dropMethodCache, dropStaticMethodCache, findMethodInClassHierarchy, findMixinMethod, findOwnMethod, findPropertyInClassHierarchy, getAdditionalMetaMethods, getAttribute, getAttribute, getAttribute, getClassInfo, getClassNode, getEffectiveGetMetaProperty, getMetaMethod, getMetaMethods, getMetaProperty, getMethods, getMethodWithCaching, getMethodWithoutCaching, getProperties, getProperty, getRegistry, getStaticMetaMethod, getSubclassMetaMethods, getSuperClasses, getTheCachedClass, getTheClass, getVersion, handleMatches, hasCustomInvokeMethod, hasCustomStaticInvokeMethod, hasProperty, incVersion, invokeMethod, invokeMissingMethod, invokeMissingProperty, invokeStaticMissingProperty, isGroovyObject, isInitialized, isModified, isPermissivePropertyAccess, onGetPropertyFoundInHierarchy, onInvokeMethodFoundInHierarchy, onMixinMethodFound, onSetPropertyFoundInHierarchy, onSuperMethodFoundInHierarchy, onSuperPropertyFoundInHierarchy, pickMethod, reinitialize, respondsTo, respondsTo, retrieveConstructor, retrieveConstructor, retrieveStaticMethod, selectConstructorAndTransformArguments, setAttribute, setAttribute, setInitialized, setPermissivePropertyAccess, setProperties, setProperty, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface groovy.lang.MetaClass
getAttribute, getClassNode, getMetaMethods, getMethods, getProperties, invokeMissingMethod, invokeMissingProperty, pickMethod, selectConstructorAndTransformArguments, setAttribute
Methods inherited from interface groovy.lang.MetaObjectProtocol
getAttribute, getMetaMethod, getMetaProperty, getProperty, getStaticMetaMethod, getTheClass, hasProperty, invokeMethod, respondsTo, respondsTo, setAttribute, setProperty
-
Field Details
-
adaptee
-
interceptor
-
-
Constructor Details
-
ProxyMetaClass
- Parameters:
adaptee
- the MetaClass to decorate with interceptability
-
-
Method Details
-
getInstance
convenience factory method for the most usual case. -
initialize
public void initialize()Description copied from class:MetaClassImpl
Complete the initialisation process. After this method is called no methods should be added to the meta class. Invocation of methods or access to fields/properties is forbidden unless this method is called. This method should contain any initialisation code, taking a longer time to complete. An example is the creation of the Reflector. It is suggested to synchronize this method.- Specified by:
initialize
in interfaceMetaClass
- Overrides:
initialize
in classMetaClassImpl
-
getAdaptee
Description copied from interface:AdaptingMetaClass
Returns the MetaClass that this adapter adapts- Specified by:
getAdaptee
in interfaceAdaptingMetaClass
- Returns:
- The MetaClass instance
-
setAdaptee
Description copied from interface:AdaptingMetaClass
Sets the MetaClass adapted by this MetaClass- Specified by:
setAdaptee
in interfaceAdaptingMetaClass
- Parameters:
metaClass
- The MetaClass to adapt
-
getInterceptor
- Returns:
- the interceptor in use or null if no interceptor is used
-
setInterceptor
- Parameters:
interceptor
- may be null to reset any interception
-
use
Use the ProxyMetaClass for the given Closure. Cares for balanced register/unregister.- Parameters:
closure
- piece of code to be executed with registered ProxyMetaClass
-
use
Use the ProxyMetaClass for the given Closure. Cares for balanced setting/unsetting ProxyMetaClass.- Parameters:
closure
- piece of code to be executed with ProxyMetaClass
-
invokeMethod
Call invokeMethod on adaptee with logic like in MetaClass unless we have an Interceptor. With Interceptor the call is nested in its beforeInvoke and afterInvoke methods. The method call is suppressed if Interceptor.doInvoke() returns false. See Interceptor for details.- Specified by:
invokeMethod
in interfaceMetaObjectProtocol
- Overrides:
invokeMethod
in classMetaClassImpl
- Parameters:
object
- The object which the method was invoked onmethodName
- The name of the methodarguments
- The arguments to the method- Returns:
- The return value of the method
- See Also:
MetaClass.invokeMethod(Class, Object, String, Object[], boolean, boolean)
-
invokeMethod
public Object invokeMethod(Class sender, Object object, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass)Call invokeMethod on adaptee with logic like in MetaClass unless we have an Interceptor. With Interceptor the call is nested in its beforeInvoke and afterInvoke methods. The method call is suppressed if Interceptor.doInvoke() returns false. See Interceptor for details.- Specified by:
invokeMethod
in interfaceMetaClass
- Overrides:
invokeMethod
in classMetaClassImpl
- Parameters:
sender
- The java.lang.Class instance that invoked the methodobject
- The object which the method was invoked onmethodName
- The name of the methodarguments
- The arguments to the methodisCallToSuper
- Whether the method is a call to a super class methodfromInsideClass
- Whether the call was invoked from the inside or the outside of the class- Returns:
- The return value of the method
- See Also:
MetaClass.invokeMethod(Class, Object, String, Object[], boolean, boolean)
-
invokeStaticMethod
Call invokeStaticMethod on adaptee with logic like in MetaClass unless we have an Interceptor. With Interceptor the call is nested in its beforeInvoke and afterInvoke methods. The method call is suppressed if Interceptor.doInvoke() returns false. See Interceptor for details.- Specified by:
invokeStaticMethod
in interfaceMetaObjectProtocol
- Overrides:
invokeStaticMethod
in classMetaClassImpl
- Parameters:
object
- An instance of the class returned by the getTheClass() method or the class itselfmethodName
- The name of the methodarguments
- The arguments to the method- Returns:
- The return value of the method which is null if the return type is void
- See Also:
MissingMethodException
-
invokeConstructor
Call invokeConstructor on adaptee with logic like in MetaClass unless we have an Interceptor. With Interceptor the call is nested in its beforeInvoke and afterInvoke methods. The method call is suppressed if Interceptor.doInvoke() returns false. See Interceptor for details.- Specified by:
invokeConstructor
in interfaceMetaObjectProtocol
- Overrides:
invokeConstructor
in classMetaClassImpl
- Parameters:
arguments
- The arguments to the constructor- Returns:
- An instance of the java.lang.Class that this MetaObjectProtocol object applies to
-
getProperty
public Object getProperty(Class aClass, Object object, String property, boolean useSuper, boolean fromInsideClass)Interceptors the call to getProperty if a PropertyAccessInterceptor is available- Specified by:
getProperty
in interfaceMetaClass
- Overrides:
getProperty
in classMetaClassImpl
- Parameters:
object
- the object to invoke the getter onproperty
- the property nameaClass
- The java.lang.Class instance that requested the propertyuseSuper
- Whether the call is to a super class propertyfromInsideClass
- ??- Returns:
- the value of the property
-
setProperty
public void setProperty(Class aClass, Object object, String property, Object newValue, boolean useSuper, boolean fromInsideClass)Interceptors the call to a property setter if a PropertyAccessInterceptor is available- Specified by:
setProperty
in interfaceMetaClass
- Overrides:
setProperty
in classMetaClassImpl
- Parameters:
object
- The object to invoke the setter onproperty
- The property name to setnewValue
- The new value of the propertyaClass
- The java.lang.Class instance that is mutating the propertyuseSuper
- Whether the call is to a super class propertyfromInsideClass
- Whether the call was invoked from the inside or the outside of the class.
-