Class ClosureMetaClass
java.lang.Object
groovy.lang.MetaClassImpl
org.codehaus.groovy.runtime.metaclass.ClosureMetaClass
- All Implemented Interfaces:
MetaClass,MetaObjectProtocol,MutableMetaClass
A metaclass for closures generated by the Groovy compiler. These classes
have special characteristics this MetaClass uses. One of these is that a
generated Closure has only additional doCall methods, all other methods
are in the Closure class as well. To use this fact this MetaClass uses
a MetaClass for Closure as static field And delegates calls to this
MetaClass if needed. This allows a lean implementation for this MetaClass.
Multiple generated closures will then use the same MetaClass for Closure.
For static dispatching this class uses the MetaClass of Class, again
all instances of this class will share that MetaClass. The Class MetaClass
is initialized lazy, because most operations do not need this MetaClass.
The Closure and Class metaclasses are not replaceable.
This MetaClass is for internal usage only!
- Since:
- 1.5
-
Nested Class Summary
Nested classes/interfaces inherited from class groovy.lang.MetaClassImpl
MetaClassImpl.MetaConstructor -
Field Summary
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
ConstructorsConstructorDescriptionClosureMetaClass(MetaClassRegistry registry, Class theClass) Constructs a new ClosureMetaClass for the given closure class. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a meta bean property to this metaclass.voidaddMetaMethod(MetaMethod method) Adds a meta method to this metaclass.voidaddNewInstanceMethod(Method method) Adds a new instance method to this metaclass.voidaddNewStaticMethod(Method method) Adds a new static method to this metaclass.protected voidapplyPropertyDescriptors(PropertyDescriptor[] propertyDescriptors) Turns JavaBeans property descriptors into Groovy meta properties.createPogoCallCurrentSite(CallSite site, Class sender, Object[] args) Creates a POGO call site for the current class context.createPogoCallSite(CallSite site, Object[] args) Creates a POGO (Plain Old Groovy Object) call site for this metaclass.createPojoCallSite(CallSite site, Object receiver, Object[] args) Creates a POJO (Plain Old Java Object) call site for this metaclass.getAttribute(Class sender, Object object, String attribute, boolean useSuper, boolean fromInsideClass) Gets an attribute from the closure or the object it represents.Returns a list of all metamethods in the Closure metaclass.getMetaProperty(String name) Gets the meta property for the given name from the closure metaclass.Returns a list of all methods available to closures, including both closure-specific doCall methods and inherited Closure class methods.getMethodWithoutCaching(int index, Class sender, String methodName, Class[] arguments, boolean isCallToSuper) Gets a method without using the cache.Returns a list of all properties available to closures.getProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass) Gets a property on the closure or the object it represents.getStaticMetaMethod(String name, Class[] argTypes) Gets a static meta method by name and argument types.getStaticMetaMethod(String name, Object[] args) Gets a static meta method by name and argument objects.voidInitializes the closure metaclass by discovering and indexing all closure-related methods.invokeMethod(Class sender, Object object, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass) Invokes a method on the given receiver for the specified arguments.invokeStaticMethod(Object object, String methodName, Object[] arguments) Invokes a static method on the closure class itself.pickMethod(String methodName, Class[] argumentTypes) Picks the most appropriate method from the available closure methods that matches the given method name and argument types.static voidResets the cached metaclasses for Closure and Class.respondsTo(Object obj, String name) Checks if this metaclass responds to the given method name.respondsTo(Object obj, String name, Object[] argTypes) Checks if this metaclass responds to the given method name with the specified argument types.retrieveConstructor(Class[] arguments) Retrieves a constructor matching the given argument types.retrieveStaticMethod(String methodName, Class[] arguments) Retrieves a static method with the given name and argument types.voidsetAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass) Sets an attribute on the closure or the object it represents.voidsetProperties(Object bean, Map map) Sets multiple properties on a bean.voidsetProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass) Sets a property on the closure or the object it represents.Methods inherited from class groovy.lang.MetaClassImpl
addMetaMethodToIndex, checkIfGroovyObjectMethod, checkInitalised, chooseMethod, clearInvocationCaches, createConstructorSite, createErrorMessageForAmbiguity, createStaticSite, createTransformMetaMethod, doChooseMostSpecificParams, dropMethodCache, dropStaticMethodCache, findMethodInClassHierarchy, findMixinMethod, findOwnMethod, findPropertyInClassHierarchy, getAdditionalMetaMethods, getAttribute, getAttribute, getClassInfo, getClassNode, getEffectiveGetMetaProperty, getMetaMethod, getMethodWithCaching, getMethodWithoutCaching, getNonClosureOuter, getProperty, getRegistry, getSubclassMetaMethods, getSuperClasses, getTheCachedClass, getTheClass, getVersion, handleMatches, hasCustomInvokeMethod, hasCustomStaticInvokeMethod, hasProperty, incVersion, invokeConstructor, invokeMethod, invokeMethod, invokeMissingMethod, invokeMissingProperty, invokeStaticMissingProperty, isGroovyObject, isInitialized, isModified, isPermissivePropertyAccess, onGetPropertyFoundInHierarchy, onInvokeMethodFoundInHierarchy, onMixinMethodFound, onSetPropertyFoundInHierarchy, onSuperMethodFoundInHierarchy, onSuperPropertyFoundInHierarchy, reinitialize, retrieveConstructor, retrieveStaticMethod, selectConstructorAndTransformArguments, setAttribute, setInitialized, setPermissivePropertyAccess, setProperty, toString
-
Constructor Details
-
ClosureMetaClass
Constructs a new ClosureMetaClass for the given closure class. This metaclass handles method invocation and property access for closure instances, providing special handling for closure-specific behavior.- Parameters:
registry- the metaclass registrytheClass- the closure class this metaclass represents
-
-
Method Details
-
resetCachedMetaClasses
public static void resetCachedMetaClasses()Resets the cached metaclasses for Closure and Class. This method is used to reinitialize the metaclass caches when the system configuration changes, ensuring that any cached metaclass information is refreshed. -
getMetaProperty
Gets the meta property for the given name from the closure metaclass.- Specified by:
getMetaPropertyin interfaceMetaObjectProtocol- Overrides:
getMetaPropertyin classMetaClassImpl- Parameters:
name- the property name- Returns:
- the meta property or null if not found
- See Also:
-
invokeMethod
public Object invokeMethod(Class sender, Object object, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass) Description copied from class:MetaClassImplInvokes a method on the given receiver for the specified arguments. The sender is the class that invoked the method on the object. The MetaClass will attempt to establish the method to invoke based on the name and arguments provided.The isCallToSuper and fromInsideClass help the Groovy runtime perform optimisations on the call to go directly to the super class if necessary
- Specified by:
invokeMethodin interfaceMetaClass- Overrides:
invokeMethodin 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:
-
initialize
public void initialize()Initializes the closure metaclass by discovering and indexing all closure-related methods.- Specified by:
initializein interfaceMetaClass- Overrides:
initializein classMetaClassImpl
-
getMethods
Returns a list of all methods available to closures, including both closure-specific doCall methods and inherited Closure class methods.- Specified by:
getMethodsin interfaceMetaClass- Specified by:
getMethodsin interfaceMetaObjectProtocol- Overrides:
getMethodsin classMetaClassImpl- Returns:
- a list of all available methods
- See Also:
-
getMetaMethods
Returns a list of all metamethods in the Closure metaclass.- Specified by:
getMetaMethodsin interfaceMetaClass- Overrides:
getMetaMethodsin classMetaClassImpl- Returns:
- a list of all metamethods
-
getProperties
Returns a list of all properties available to closures.- Specified by:
getPropertiesin interfaceMetaClass- Specified by:
getPropertiesin interfaceMetaObjectProtocol- Overrides:
getPropertiesin classMetaClassImpl- Returns:
- a list of all available properties
- See Also:
-
pickMethod
Picks the most appropriate method from the available closure methods that matches the given method name and argument types.- Specified by:
pickMethodin interfaceMetaClass- Overrides:
pickMethodin classMetaClassImpl- Parameters:
methodName- the name of the method to pick (typically "call" or "doCall" for closures)argumentTypes- the types of arguments to match against method signatures- Returns:
- the matching method, or null if no match is found
-
retrieveStaticMethod
Retrieves a static method with the given name and argument types. Closures do not support static methods, so this always returns null.- Parameters:
methodName- the name of the static method to retrievearguments- the argument types- Returns:
- null, as closures do not have static methods
-
getStaticMetaMethod
Gets a static meta method by name and argument objects. Delegates to the Closure metaclass.- Specified by:
getStaticMetaMethodin interfaceMetaObjectProtocol- Overrides:
getStaticMetaMethodin classMetaClassImpl- Parameters:
name- the name of the static methodargs- the arguments- Returns:
- the matching static meta method, or null if not found
- See Also:
-
getStaticMetaMethod
Gets a static meta method by name and argument types. Delegates to the Closure metaclass.- Parameters:
name- the name of the static methodargTypes- the argument types- Returns:
- the matching static meta method, or null if not found
-
getProperty
public Object getProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass) Gets a property on the closure or the object it represents. For Class objects, delegates to the static metaclass; otherwise delegates to Closure metaclass.- Specified by:
getPropertyin interfaceMetaClass- Overrides:
getPropertyin classMetaClassImpl- Parameters:
sender- the class that is sending this requestobject- the object to get the property fromname- the name of the propertyuseSuper- whether to use super resolutionfromInsideClass- whether the call is from inside the class- Returns:
- the property value
-
getAttribute
public Object getAttribute(Class sender, Object object, String attribute, boolean useSuper, boolean fromInsideClass) Gets an attribute from the closure or the object it represents. For Class objects, delegates to the static metaclass. For closures, checks the cached fields first before delegating to the Closure metaclass.- Overrides:
getAttributein classMetaClassImpl- Parameters:
sender- the class that is sending this requestobject- the object to get the attribute fromattribute- the name of the attributeuseSuper- whether to use super resolutionfromInsideClass- whether the call is from inside the class- Returns:
- the attribute value
-
setAttribute
public void setAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass) Sets an attribute on the closure or the object it represents. For Class objects, delegates to the static metaclass. For closures, checks the cached fields first before delegating to the Closure metaclass.- Specified by:
setAttributein interfaceMetaClass- Overrides:
setAttributein classMetaClassImpl- Parameters:
sender- the class that is sending this requestobject- the object on which to set the attributeattribute- the name of the attributenewValue- the new value for the attributeuseSuper- whether to use super resolutionfromInsideClass- whether the call is from inside the class
-
invokeStaticMethod
Invokes a static method on the closure class itself. Delegates to the static metaclass.- Specified by:
invokeStaticMethodin interfaceMetaObjectProtocol- Overrides:
invokeStaticMethodin classMetaClassImpl- Parameters:
object- the object on which to invoke the static methodmethodName- the name of the static methodarguments- the arguments to pass to the method- Returns:
- the result of the method invocation
- See Also:
-
setProperty
public void setProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass) Sets a property on the closure or the object it represents. For Class objects, delegates to the static metaclass; otherwise delegates to Closure metaclass.- Specified by:
setPropertyin interfaceMetaClass- Overrides:
setPropertyin classMetaClassImpl- Parameters:
sender- the class that is sending this requestobject- the object on which to set the propertyname- the name of the propertynewValue- the new value for the propertyuseSuper- whether to use super resolutionfromInsideClass- whether the call is from inside the class
-
getMethodWithoutCaching
public MetaMethod getMethodWithoutCaching(int index, Class sender, String methodName, Class[] arguments, boolean isCallToSuper) Gets a method without using the cache. This operation is not supported for ClosureMetaClass.- Parameters:
index- the method indexsender- the class that is sending this requestmethodName- the name of the methodarguments- the argument typesisCallToSuper- whether this is a call to a super method- Returns:
- never returns; always throws UnsupportedOperationException
- Throws:
UnsupportedOperationException- this method is not supported for closures
-
setProperties
Sets multiple properties on a bean. This operation is not supported for ClosureMetaClass.- Overrides:
setPropertiesin classMetaClassImpl- Parameters:
bean- the bean objectmap- the properties to set- Throws:
UnsupportedOperationException- this method is not supported for closures
-
addMetaBeanProperty
Adds a meta bean property to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.- Specified by:
addMetaBeanPropertyin interfaceMutableMetaClass- Overrides:
addMetaBeanPropertyin classMetaClassImpl- Parameters:
mp- the meta bean property to add- Throws:
UnsupportedOperationException- this method is not supported for closures
-
addMetaMethod
Adds a meta method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.- Specified by:
addMetaMethodin interfaceMutableMetaClass- Overrides:
addMetaMethodin classMetaClassImpl- Parameters:
method- the meta method to add- Throws:
UnsupportedOperationException- this method is not supported for closures- See Also:
-
addNewInstanceMethod
Adds a new instance method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.- Specified by:
addNewInstanceMethodin interfaceMutableMetaClass- Overrides:
addNewInstanceMethodin classMetaClassImpl- Parameters:
method- the method to add- Throws:
UnsupportedOperationException- this method is not supported for closures
-
addNewStaticMethod
Adds a new static method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.- Specified by:
addNewStaticMethodin interfaceMutableMetaClass- Overrides:
addNewStaticMethodin classMetaClassImpl- Parameters:
method- the static method to add- Throws:
UnsupportedOperationException- this method is not supported for closures
-
retrieveConstructor
Retrieves a constructor matching the given argument types. This operation is not supported for ClosureMetaClass.- Overrides:
retrieveConstructorin classMetaClassImpl- Parameters:
arguments- the argument types- Returns:
- never returns; always throws UnsupportedOperationException
- Throws:
UnsupportedOperationException- this method is not supported for closures
-
createPojoCallSite
Creates a POJO (Plain Old Java Object) call site for this metaclass. This operation is not supported for ClosureMetaClass.- Overrides:
createPojoCallSitein classMetaClassImpl- Parameters:
site- the call sitereceiver- the receiver objectargs- the arguments- Returns:
- never returns; always throws UnsupportedOperationException
- Throws:
UnsupportedOperationException- this method is not supported for closures
-
createPogoCallSite
Creates a POGO (Plain Old Groovy Object) call site for this metaclass. Provides optimized method invocation for POGO objects.- Overrides:
createPogoCallSitein classMetaClassImpl- Parameters:
site- the call siteargs- the arguments- Returns:
- a POGO-specific call site
-
createPogoCallCurrentSite
Creates a POGO call site for the current class context. Provides optimized method invocation for POGO objects within a specific class context.- Overrides:
createPogoCallCurrentSitein classMetaClassImpl- Parameters:
site- the call sitesender- the class sending the callargs- the arguments- Returns:
- a POGO-specific call site
-
respondsTo
Checks if this metaclass responds to the given method name with the specified argument types. Returns a list of methods that match the given name and signature.- Specified by:
respondsToin interfaceMetaObjectProtocol- Overrides:
respondsToin classMetaClassImpl- Parameters:
obj- the object to checkname- the method nameargTypes- the argument types- Returns:
- a list of matching methods, or an empty list if none match
- See Also:
-
respondsTo
Checks if this metaclass responds to the given method name. Returns a list of methods that match the given name.- Specified by:
respondsToin interfaceMetaObjectProtocol- Overrides:
respondsToin classMetaClassImpl- Parameters:
obj- the object to checkname- the method name- Returns:
- a list of matching methods, or an empty list if none match
- See Also:
-
applyPropertyDescriptors
Description copied from class:MetaClassImplTurns JavaBeans property descriptors into Groovy meta properties.- Overrides:
applyPropertyDescriptorsin classMetaClassImpl- Parameters:
propertyDescriptors- the descriptors to apply
-