groovy.lang
public class ExpandoMetaClass extends MetaClassImpl implements GroovyObject
// defines or replaces instance method:
metaClass.myMethod = { args -> }
// defines a new instance method
metaClass.myMethod << { args -> }
// creates multiple overloaded methods of the same name
metaClass.myMethod << { String s -> } << { Integer i -> }
// defines or replaces a static method with the 'static' qualifier
metaClass.'static'.myMethod = { args -> }
// defines a new static method with the 'static' qualifier
metaClass.'static'.myMethod << { args -> }
// defines a new constructor
metaClass.constructor << { String arg -> }
// defines or replaces a constructor
metaClass.constructor = { String arg -> }
// defines a new property with an initial value of "blah"
metaClass.myProperty = "blah"
By default methods are only allowed to be added before initialize() is called. In other words you create a new
ExpandoMetaClass, add some methods and then call initialize(). If you attempt to add new methods after initialize()
has been called an error will be thrown.This is to ensure that the MetaClass can operate appropriately in multi
threaded environments as it forces you to do all method additions at the beginning, before using the MetaClass.
ExpandoMetaClass differs here from the default in that it allows you adding methods after initialize has been called.
This is done by setting the initialize flag internally to false and then add the methods. Since this is not thread
safe it has to be done in a synchronized block. The methods to check for modification and initialization are
therefore synchronized as well. Any method call done through this meta class will first check if the it is
synchronized. Should this happen during a modification, then the method cannot be selected or called unless the
modification is completed.
WARNING: This MetaClass uses a thread-bound ThreadLocal instance to store and retrieve properties.
In addition properties stored use soft references so they are both bound by the life of the Thread and by the soft
references. The implication here is you should NEVER use dynamic properties if you want their values to stick around
for long periods because as soon as the JVM is running low on memory or the thread dies they will be garbage collected.| Modifier and Type | Class and Description |
|---|---|
protected class |
ExpandoMetaClass.ExpandoMetaConstructor
Handles the ability to use the left shift operator to append new constructors
|
protected class |
ExpandoMetaClass.ExpandoMetaProperty
Instances of this class are returned when using the << left shift operator.
|
MetaClassImpl.Index| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CONSTRUCTOR |
boolean |
inRegistry |
static java.lang.String |
STATIC_QUALIFIER |
getPropertyMethod, INVOKE_METHOD_METHOD, invokeMethodMethod, isGroovyObject, isMap, metaMethodIndex, METHOD_MISSING, PROPERTY_MISSING, registry, setPropertyMethod, STATIC_METHOD_MISSING, STATIC_PROPERTY_MISSING, theCachedClass, theClass| Constructor and Description |
|---|
ExpandoMetaClass(java.lang.Class theClass)
Constructs a new ExpandoMetaClass instance for the given class
|
ExpandoMetaClass(java.lang.Class theClass,
boolean register)
Constructs a new ExpandoMetaClass instance for the given class optionally placing the MetaClass
in the MetaClassRegistry automatically
|
ExpandoMetaClass(java.lang.Class theClass,
boolean register,
boolean allowChangesAfterInit)
Constructs a new ExpandoMetaClass instance for the given class optionally placing the MetaClass
in the MetaClassRegistry automatically
|
ExpandoMetaClass(java.lang.Class theClass,
boolean register,
MetaMethod[] add) |
ExpandoMetaClass(java.lang.Class theClass,
MetaMethod[] add) |
| Modifier and Type | Method and Description |
|---|---|
void |
addMixinClass(MixinInMetaClass mixin) |
java.lang.Object |
castToMixedType(java.lang.Object obj,
java.lang.Class type) |
CallSite |
createConstructorSite(CallSite site,
java.lang.Object[] args) |
CallSite |
createPogoCallCurrentSite(CallSite site,
java.lang.Class sender,
java.lang.String name,
java.lang.Object[] args) |
CallSite |
createPogoCallSite(CallSite site,
java.lang.Object[] args) |
CallSite |
createPojoCallSite(CallSite site,
java.lang.Object receiver,
java.lang.Object[] args) |
CallSite |
createStaticSite(CallSite site,
java.lang.Object[] args) |
ExpandoMetaClass |
define(Closure closure) |
static void |
disableGlobally()
Call to disable the global use of ExpandoMetaClass
|
static void |
enableGlobally()
Call to enable global use of global use of ExpandoMetaClass within the registry.
|
MetaMethod |
findMixinMethod(java.lang.String methodName,
java.lang.Class[] arguments) |
java.util.List<MetaMethod> |
getExpandoMethods()
Returns a list of expando MetaMethod instances added to this ExpandoMetaClass
|
java.util.Collection<MetaProperty> |
getExpandoProperties()
Returns a list of MetaBeanProperty instances added to this ExpandoMetaClass
|
java.util.Collection |
getExpandoSubclassMethods() |
java.lang.Class |
getJavaClass() |
MetaClass |
getMetaClass()
Returns the metaclass for a given class.
|
MetaProperty |
getMetaProperty(java.lang.String name)
Looks up an existing MetaProperty by name
|
java.util.List<MetaMethod> |
getMethods()
Overrides the behavior of parent getMethods() method to make MetaClass aware of added Expando methods
|
java.util.List<MetaProperty> |
getProperties()
Get all the properties defined for this type
|
java.lang.Object |
getProperty(java.lang.Class sender,
java.lang.Object object,
java.lang.String name,
boolean useSuper,
boolean fromInsideClass)
Overrides default implementation just in case getProperty method has been overridden by ExpandoMetaClass
|
java.lang.Object |
getProperty(java.lang.Object object,
java.lang.String name)
Overrides default implementation just in case getProperty method has been overriden by ExpandoMetaClass
|
java.lang.Object |
getProperty(java.lang.String property)
Retrieves a property value.
|
java.lang.String |
getPropertyForSetter(java.lang.String setterName)
Returns a property name equivalent for the given setter name or null if it is not a getter
|
protected java.lang.Object |
getSubclassMetaMethods(java.lang.String methodName) |
boolean |
hasMetaMethod(java.lang.String name,
java.lang.Class[] args)
Checks whether a MetaMethod for the given name and arguments exists
|
boolean |
hasMetaProperty(java.lang.String name)
Returns true if the MetaClass has the given property
|
void |
initialize()
complete the initlialisation process.
|
java.lang.Object |
invokeConstructor(java.lang.Object[] arguments)
Invokes a constructor for the given arguments.
|
java.lang.Object |
invokeMethod(java.lang.Class sender,
java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] originalArguments,
boolean isCallToSuper,
boolean fromInsideClass)
Overrides default implementation just in case invokeMethod has been overriden by ExpandoMetaClass
|
java.lang.Object |
invokeMethod(java.lang.String name,
java.lang.Object args)
Invokes the given method.
|
java.lang.Object |
invokeStaticMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] arguments)
Overrides default implementation just in case a static invoke method has been set on ExpandoMetaClass
|
protected boolean |
isInitialized()
Checks if the meta class is initialized.
|
boolean |
isModified()
Return whether the MetaClass has been modified or not
|
boolean |
isSetter(java.lang.String name,
CachedClass[] args) |
static boolean |
isValidExpandoProperty(java.lang.String property) |
protected void |
onGetPropertyFoundInHierarchy(MetaMethod method) |
protected void |
onInvokeMethodFoundInHierarchy(MetaMethod method) |
protected void |
onSetPropertyFoundInHierarchy(MetaMethod method) |
protected void |
onSuperMethodFoundInHierarchy(MetaMethod method) |
protected void |
onSuperPropertyFoundInHierarchy(MetaBeanProperty property) |
protected void |
performOperationOnMetaClass(groovy.lang.ExpandoMetaClass.Callable c) |
void |
refreshInheritedMethods(java.util.Set modifiedSuperExpandos)
Called from ExpandoMetaClassCreationHandle in the registry if it exists to
set up inheritance handling
|
void |
registerBeanProperty(java.lang.String property,
java.lang.Object newValue)
Registers a new bean property
|
void |
registerInstanceMethod(MetaMethod metaMethod)
Registers a new instance method for the given method name and closure on this MetaClass
|
void |
registerInstanceMethod(java.lang.String name,
Closure closure) |
protected void |
registerStaticMethod(java.lang.String name,
Closure callable) |
protected void |
registerStaticMethod(java.lang.String name,
Closure callable,
java.lang.Class[] paramTypes)
Registers a new static method for the given method name and closure on this MetaClass
|
void |
registerSubclassInstanceMethod(MetaMethod metaMethod) |
void |
registerSubclassInstanceMethod(java.lang.String name,
java.lang.Class klazz,
Closure closure) |
protected void |
setInitialized(boolean b) |
void |
setMetaClass(MetaClass metaClass)
Allows the MetaClass to be replaced with a derived implementation.
|
void |
setProperty(java.lang.Class sender,
java.lang.Object object,
java.lang.String name,
java.lang.Object newValue,
boolean useSuper,
boolean fromInsideClass)
Overrides default implementation just in case setProperty method has been overriden by ExpandoMetaClass
|
void |
setProperty(java.lang.String property,
java.lang.Object newValue)
Sets the given property to the new value.
|
addMetaBeanProperty, addMetaMethod, addMetaMethodToIndex, addNewInstanceMethod, addNewStaticMethod, checkIfGroovyObjectMethod, checkInitalised, chooseMethod, clearInvocationCaches, createPogoCallCurrentSite, dropMethodCache, dropStaticMethodCache, findMethodInClassHierarchy, findOwnMethod, findPropertyInClassHierarchy, getAdditionalMetaMethods, getAttribute, getAttribute, getAttribute, getClassInfo, getClassNode, getEffectiveGetMetaProperty, getMetaMethod, getMetaMethods, getMethodWithCaching, getMethodWithoutCaching, getStaticMetaMethod, getSuperClasses, getTheCachedClass, getTheClass, getVersion, hasProperty, incVersion, invokeConstructorAt, invokeMethod, invokeMethod, invokeMissingMethod, invokeMissingProperty, invokeStaticMissingProperty, isGroovyObject, onMixinMethodFound, pickMethod, respondsTo, respondsTo, retrieveConstructor, retrieveMethod, retrieveStaticMethod, selectConstructorAndTransformArguments, setAttribute, setAttribute, setProperties, setProperty, toStringpublic static final java.lang.String STATIC_QUALIFIER
public static final java.lang.String CONSTRUCTOR
public boolean inRegistry
public ExpandoMetaClass(java.lang.Class theClass)
theClass - The class that the MetaClass applies topublic ExpandoMetaClass(java.lang.Class theClass,
MetaMethod[] add)
public ExpandoMetaClass(java.lang.Class theClass,
boolean register)
theClass - The class that the MetaClass applies toregister - True if the MetaClass should be registered inside the MetaClassRegistry. This defaults to true and ExpandoMetaClass will effect all instances if changedpublic ExpandoMetaClass(java.lang.Class theClass,
boolean register,
MetaMethod[] add)
public ExpandoMetaClass(java.lang.Class theClass,
boolean register,
boolean allowChangesAfterInit)
theClass - The class that the MetaClass applies toregister - True if the MetaClass should be registered inside the MetaClassRegistry. This defaults to true and ExpandoMetaClass will effect all instances if changedallowChangesAfterInit - Should the meta class be modifiable after initialization. Default is false.public java.util.Collection getExpandoSubclassMethods()
public MetaMethod findMixinMethod(java.lang.String methodName, java.lang.Class[] arguments)
findMixinMethod in class MetaClassImplprotected void onInvokeMethodFoundInHierarchy(MetaMethod method)
onInvokeMethodFoundInHierarchy in class MetaClassImplprotected void onSuperMethodFoundInHierarchy(MetaMethod method)
onSuperMethodFoundInHierarchy in class MetaClassImplprotected void onSuperPropertyFoundInHierarchy(MetaBeanProperty property)
onSuperPropertyFoundInHierarchy in class MetaClassImplprotected void onSetPropertyFoundInHierarchy(MetaMethod method)
onSetPropertyFoundInHierarchy in class MetaClassImplprotected void onGetPropertyFoundInHierarchy(MetaMethod method)
onGetPropertyFoundInHierarchy in class MetaClassImplpublic boolean isModified()
MutableMetaClassisModified in interface MutableMetaClassisModified in class MetaClassImplpublic void registerSubclassInstanceMethod(java.lang.String name,
java.lang.Class klazz,
Closure closure)
public void registerSubclassInstanceMethod(MetaMethod metaMethod)
public void addMixinClass(MixinInMetaClass mixin)
public java.lang.Object castToMixedType(java.lang.Object obj,
java.lang.Class type)
public static void enableGlobally()
public static void disableGlobally()
public void initialize()
MetaClassinitialize in interface MetaClassinitialize in class MetaClassImplprotected boolean isInitialized()
isInitialized in class MetaClassImplMetaClassImpl.isInitialized()protected void setInitialized(boolean b)
public java.lang.Object invokeConstructor(java.lang.Object[] arguments)
MetaObjectProtocolinvokeConstructor in interface MetaObjectProtocolinvokeConstructor in class MetaClassImplarguments - The arguments to the constructorpublic MetaClass getMetaClass()
GroovyObjectgetMetaClass in interface GroovyObjectpublic java.lang.Object getProperty(java.lang.String property)
GroovyObjectgetProperty in interface GroovyObjectproperty - the name of the property of interestpublic static boolean isValidExpandoProperty(java.lang.String property)
public java.lang.Object invokeMethod(java.lang.String name,
java.lang.Object args)
GroovyObjectinvokeMethod in interface GroovyObjectname - the name of the method to callargs - the arguments to use for the method callpublic void setMetaClass(MetaClass metaClass)
GroovyObjectsetMetaClass in interface GroovyObjectmetaClass - the new metaclasspublic void setProperty(java.lang.String property,
java.lang.Object newValue)
GroovyObjectsetProperty in interface GroovyObjectproperty - the name of the property of interestnewValue - the new value for the propertypublic ExpandoMetaClass define(Closure closure)
protected void performOperationOnMetaClass(groovy.lang.ExpandoMetaClass.Callable c)
public void registerBeanProperty(java.lang.String property,
java.lang.Object newValue)
property - The property namenewValue - The properties initial valuepublic void registerInstanceMethod(MetaMethod metaMethod)
metaMethod - public void registerInstanceMethod(java.lang.String name,
Closure closure)
public java.util.List<MetaMethod> getMethods()
getMethods in interface MetaClassgetMethods in interface MetaObjectProtocolgetMethods in class MetaClassImplMetaObjectProtocol.getMethods()public java.util.List<MetaProperty> getProperties()
MetaClassImplgetProperties in interface MetaClassgetProperties in interface MetaObjectProtocolgetProperties in class MetaClassImplMetaPropertyprotected void registerStaticMethod(java.lang.String name,
Closure callable)
protected void registerStaticMethod(java.lang.String name,
Closure callable,
java.lang.Class[] paramTypes)
name - The method namecallable - The callable Closureprotected java.lang.Object getSubclassMetaMethods(java.lang.String methodName)
getSubclassMetaMethods in class MetaClassImplpublic java.lang.Class getJavaClass()
public void refreshInheritedMethods(java.util.Set modifiedSuperExpandos)
modifiedSuperExpandos - A list of modified super ExpandoMetaClasspublic java.util.List<MetaMethod> getExpandoMethods()
public java.util.Collection<MetaProperty> getExpandoProperties()
public java.lang.Object invokeMethod(java.lang.Class sender,
java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] originalArguments,
boolean isCallToSuper,
boolean fromInsideClass)
invokeMethod in interface MetaClassinvokeMethod in class MetaClassImplsender - The java.lang.Class instance that invoked the methodobject - The object which the method was invoked onmethodName - The name of the methodoriginalArguments - 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 classMetaClassImpl.invokeMethod(Class, Object, String, Object[], boolean, boolean)public java.lang.Object invokeStaticMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] arguments)
invokeStaticMethod in interface MetaObjectProtocolinvokeStaticMethod in class MetaClassImplobject - An instance of the class returned by the getTheClass() method or the class itselfmethodName - The name of the methodarguments - The arguments to the methodMetaClassImpl.invokeStaticMethod(Object, String, Object[])public java.lang.Object getProperty(java.lang.Class sender,
java.lang.Object object,
java.lang.String name,
boolean useSuper,
boolean fromInsideClass)
getProperty in interface MetaClassgetProperty in class MetaClassImplsender - The java.lang.Class instance that requested the propertyobject - The Object which the property is being retrieved fromname - The name of the propertyuseSuper - Whether the call is to a super class propertyfromInsideClass - ??MetaClassImpl.getProperty(Class, Object, String, boolean, boolean)public java.lang.Object getProperty(java.lang.Object object,
java.lang.String name)
getProperty in interface MetaObjectProtocolgetProperty in class MetaClassImplobject - An instance of the class returned by the getTheClass() methodname - The name of the property to retrieve the value forMetaClassImpl.getProperty(Object, String)public void setProperty(java.lang.Class sender,
java.lang.Object object,
java.lang.String name,
java.lang.Object newValue,
boolean useSuper,
boolean fromInsideClass)
setProperty in interface MetaClasssetProperty in class MetaClassImplsender - The java.lang.Class instance that is mutating the propertyobject - The Object which the property is being set onname - The name of the propertynewValue - The new value of the property to setuseSuper - Whether the call is to a super class propertyfromInsideClass - ??MetaClassImpl.setProperty(Class, Object, String, Object, boolean, boolean)public MetaProperty getMetaProperty(java.lang.String name)
getMetaProperty in interface MetaObjectProtocolgetMetaProperty in class MetaClassImplname - The name of the MetaPropertyMetaObjectProtocol.getMetaProperty(String)public boolean hasMetaProperty(java.lang.String name)
name - The name of the MetaPropertypublic boolean hasMetaMethod(java.lang.String name,
java.lang.Class[] args)
name - The name of the MetaMethodargs - The arguments to the meta methodpublic java.lang.String getPropertyForSetter(java.lang.String setterName)
setterName - The setter namepublic boolean isSetter(java.lang.String name,
CachedClass[] args)
public CallSite createPojoCallSite(CallSite site, java.lang.Object receiver, java.lang.Object[] args)
createPojoCallSite in class MetaClassImplpublic CallSite createStaticSite(CallSite site, java.lang.Object[] args)
createStaticSite in class MetaClassImplpublic CallSite createPogoCallSite(CallSite site, java.lang.Object[] args)
createPogoCallSite in class MetaClassImplpublic CallSite createPogoCallCurrentSite(CallSite site, java.lang.Class sender, java.lang.String name, java.lang.Object[] args)
public CallSite createConstructorSite(CallSite site, java.lang.Object[] args)
createConstructorSite in class MetaClassImpl