groovy.lang
Class ProxyMetaClass

java.lang.Object
  extended by groovy.lang.MetaClassImpl
      extended by 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 invokations 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 threadsafe 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

Author:
Dierk Koenig, Graeme Rocher
See Also:
MetaClassRegistry

Nested Class Summary
 
Nested classes/interfaces inherited from class groovy.lang.MetaClassImpl
MetaClassImpl.Index
 
Field Summary
protected  MetaClass adaptee
           
protected  Interceptor interceptor
           
 
Fields inherited from class groovy.lang.MetaClassImpl
getPropertyMethod, INVOKE_METHOD_METHOD, invokeMethodMethod, isGroovyObject, isMap, metaMethodIndex, METHOD_MISSING, PROPERTY_MISSING, registry, setPropertyMethod, STATIC_METHOD_MISSING, STATIC_PROPERTY_MISSING, theCachedClass, theClass
 
Constructor Summary
ProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee)
           
 
Method Summary
 MetaClass getAdaptee()
          Returns the MetaClass that this adapter adapts
static ProxyMetaClass getInstance(Class theClass)
          convenience factory method for the most usual case.
 Interceptor getInterceptor()
           
 Object getProperty(Class aClass, Object object, String property, boolean b, boolean b1)
          Interceptors the call to getProperty if a PropertyAccessInterceptor is available
 void initialize()
          complete the initlialisation process.
 Object invokeConstructor(Object[] arguments)
          Call invokeConstructor 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 MetaClass
 void setInterceptor(Interceptor interceptor)
           
 void setProperty(Class aClass, Object object, String property, Object newValue, boolean b, boolean b1)
          Interceptors the call to a property setter if a PropertyAccessInterceptor is available
 Object 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, checkIfGroovyObjectMethod, checkInitalised, chooseMethod, clearInvocationCaches, createConstructorSite, createPogoCallCurrentSite, createPogoCallSite, createPojoCallSite, createStaticSite, dropMethodCache, dropStaticMethodCache, findMethodInClassHierarchy, findMixinMethod, findOwnMethod, findPropertyInClassHierarchy, getAdditionalMetaMethods, getAttribute, getAttribute, getAttribute, getClassInfo, getClassNode, getEffectiveGetMetaProperty, getMetaMethod, getMetaMethods, getMetaProperty, getMethods, getMethodWithCaching, getMethodWithoutCaching, getProperties, getProperty, getStaticMetaMethod, getSubclassMetaMethods, getSuperClasses, getTheCachedClass, getTheClass, getVersion, hasProperty, incVersion, invokeConstructorAt, invokeMethod, invokeMethod, invokeMissingMethod, invokeMissingProperty, invokeStaticMissingProperty, isGroovyObject, isInitialized, isModified, onGetPropertyFoundInHierarchy, onInvokeMethodFoundInHierarchy, onMixinMethodFound, onSetPropertyFoundInHierarchy, onSuperMethodFoundInHierarchy, onSuperPropertyFoundInHierarchy, pickMethod, respondsTo, respondsTo, retrieveConstructor, retrieveMethod, retrieveStaticMethod, selectConstructorAndTransformArguments, setAttribute, setAttribute, 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, invokeMethod, 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 Detail

adaptee

protected MetaClass adaptee

interceptor

protected Interceptor interceptor
Constructor Detail

ProxyMetaClass

public ProxyMetaClass(MetaClassRegistry registry,
                      Class theClass,
                      MetaClass adaptee)
               throws IntrospectionException
Parameters:
adaptee - the MetaClass to decorate with interceptability
Throws:
IntrospectionException
Method Detail

getInstance

public static ProxyMetaClass getInstance(Class theClass)
                                  throws IntrospectionException
convenience factory method for the most usual case.

Throws:
IntrospectionException

initialize

public void initialize()
Description copied from interface: MetaClass
complete the initlialisation process. After this method is called no methods should be added to the meta class. Invocation of methods or access to fields/proeprties 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 interface MetaClass
Overrides:
initialize in class MetaClassImpl

use

public Object use(Closure closure)
Use the ProxyMetaClass for the given Closure. Cares for balanced register/unregister.

Parameters:
closure - piece of code to be executed with registered ProxyMetaClass

use

public Object use(GroovyObject object,
                  Closure closure)
Use the ProxyMetaClass for the given Closure. Cares for balanced setting/unsetting ProxyMetaClass.

Parameters:
closure - piece of code to be executed with ProxyMetaClass

getInterceptor

public Interceptor getInterceptor()
Returns:
the interceptor in use or null if no interceptor is used

setInterceptor

public void setInterceptor(Interceptor interceptor)
Parameters:
interceptor - may be null to reset any interception

invokeMethod

public Object invokeMethod(Object object,
                           String methodName,
                           Object[] arguments)
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 interface MetaObjectProtocol
Overrides:
invokeMethod in class MetaClassImpl
Parameters:
object - The instance which the method is invoked on
methodName - The name of the method
arguments - The arguments to the method
Returns:
The return value of the method which is null if the return type is void
See Also:
MissingMethodException

invokeStaticMethod

public Object invokeStaticMethod(Object object,
                                 String methodName,
                                 Object[] arguments)
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 interface MetaObjectProtocol
Overrides:
invokeStaticMethod in class MetaClassImpl
Parameters:
object - An instance of the class returned by the getTheClass() method or the class itself
methodName - The name of the method
arguments - 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

public Object invokeConstructor(Object[] arguments)
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 interface MetaObjectProtocol
Overrides:
invokeConstructor in class MetaClassImpl
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 b,
                          boolean b1)
Interceptors the call to getProperty if a PropertyAccessInterceptor is available

Specified by:
getProperty in interface MetaClass
Overrides:
getProperty in class MetaClassImpl
Parameters:
object - the object to invoke the getter on
property - the property name
aClass - The java.lang.Class instance that requested the property
b - Whether the call is to a super class property
b1 - ??
Returns:
the value of the property

setProperty

public void setProperty(Class aClass,
                        Object object,
                        String property,
                        Object newValue,
                        boolean b,
                        boolean b1)
Interceptors the call to a property setter if a PropertyAccessInterceptor is available

Specified by:
setProperty in interface MetaClass
Overrides:
setProperty in class MetaClassImpl
Parameters:
object - The object to invoke the setter on
property - The property name to set
newValue - The new value of the property
aClass - The java.lang.Class instance that is mutating the property
b - Whether the call is to a super class property
b1 - ??

getAdaptee

public MetaClass getAdaptee()
Description copied from interface: AdaptingMetaClass
Returns the MetaClass that this adapter adapts

Specified by:
getAdaptee in interface AdaptingMetaClass
Returns:
The MetaClass instance

setAdaptee

public void setAdaptee(MetaClass metaClass)
Description copied from interface: AdaptingMetaClass
Sets the MetaClass adapted by this MetaClass

Specified by:
setAdaptee in interface AdaptingMetaClass
Parameters:
metaClass - The MetaClass to adapt

Copyright © 2003-2010 The Codehaus. All rights reserved.