Class MetaClassImpl
- All Implemented Interfaces:
MetaClass,MetaObjectProtocol,MutableMetaClass
- Direct Known Subclasses:
ClosureMetaClass,ExpandoMetaClass,ProxyMetaClass
- Version:
- $Revision$
- Author:
- James Strachan, Guillaume Laforge, Jochen Theodorou, Graeme Rocher, Alex Tkachman, Roshan Dawrani
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected MetaMethodprotected static final Stringprotected MetaMethodprotected final booleanprotected final booleanprotected final MetaMethodIndexprotected static final Stringprotected static final Stringprotected MetaClassRegistryprotected MetaMethodprotected static final Stringprotected static final Stringprotected final CachedClassprotected final Class -
Constructor Summary
ConstructorsConstructorDescriptionMetaClassImpl(MetaClassRegistry registry, Class theClass) MetaClassImpl(MetaClassRegistry registry, Class theClass, MetaMethod[] add) MetaClassImpl(Class theClass) MetaClassImpl(Class theClass, MetaMethod[] add) -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new MetaBeanProperty to this MetaClassvoidaddMetaMethod(MetaMethod method) adds a MetaMethod to this class.protected voidaddMetaMethodToIndex(MetaMethod method, MetaMethodIndex.Header header) 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) protected final voidcheckIfGroovyObjectMethod(MetaMethod metaMethod) Checks if the metaMethod is a method from the GroovyObject interface such as setProperty, getProperty and invokeMethodprotected voidchecks if the initialisation of the class id complete.protected ObjectchooseMethod(String methodName, Object methodOrList, Class[] arguments) Chooses the correct method to use from a list of methods which match by name.protected voidremove all method call cache entries.createConstructorSite(CallSite site, Object[] args) createPogoCallCurrentSite(CallSite site, Class sender, Object[] args) createPogoCallSite(CallSite site, Object[] args) createPojoCallSite(CallSite site, Object receiver, Object[] args) createStaticSite(CallSite site, Object[] args) protected voiddropMethodCache(String name) protected voiddropStaticMethodCache(String name) protected static MetaMethodfindMethodInClassHierarchy(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass) protected MetaMethodfindMixinMethod(String methodName, Class[] arguments) protected static MetaMethodfindOwnMethod(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass, MetaMethod method) protected MetaBeanPropertyfindPropertyInClassHierarchy(String propertyName, CachedClass theClass) getAttribute(Class sender, Object receiver, String messageName, boolean useSuper) Retrieves the value of an attribute (field).getAttribute(Class sender, Object object, String attribute, boolean useSuper, boolean fromInsideClass) Looks up the given attribute (field) on the given objectgetAttribute(Object object, String attribute) Retrieves an attribute of an instance of the class returned by the getTheClass() method.Obtains a reference to the original AST for the MetaClass if it is available at runtimegetEffectiveGetMetaProperty(Class sender, Object object, String name, boolean useSuper) getMetaMethod(String name, Object[] argTypes) Retrieves an instance MetaMethod for the given name and argument values, using the types of the argument values to establish the chosen MetaMethodRetrieves a list of MetaMethods held by this classgetMetaProperty(String name) Returns a MetaProperty for the given name or null if it doesn't existRetrieves a list of MetaMethods held by the classgetMethodWithCaching(Class sender, String methodName, Object[] arguments, boolean isCallToSuper) getMethodWithoutCaching(Class sender, String methodName, Class[] arguments, boolean isCallToSuper) Get all the properties defined for this typegetProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass) Retrieves a property on the given receiver for the specified arguments.getProperty(Object object, String property) Retrieves a property of an instance of the class returned by the getTheClass() method.getStaticMetaMethod(String name, Object[] argTypes) Retrieves a static MetaMethod for the given name and argument values, using the types of the arguments to establish the chosen MetaMethodprotected ObjectgetSubclassMetaMethods(String methodName) protected LinkedList<CachedClass> final CachedClassRetrieves that Java Class that the attached Meta behaviours apply tointhasProperty(Object obj, String name) Returns true of the implementing MetaClass has a property of the given namevoidvoidcomplete the initialisation process.invokeConstructor(Object[] arguments) Invokes a constructor for the given arguments.invokeConstructorAt(Class at, Object[] arguments) Deprecated.use invokeConstructor insteadinvokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass) Invokes the given method on the object.invokeMethod(Object object, String methodName, Object arguments) Invokes a method on the given object, with the given name and single argument.invokeMethod(Object object, String methodName, Object[] originalArguments) Invokes the given method on the object.invokeMissingMethod(Object instance, String methodName, Object[] arguments) Attempts to invoke the methodMissing method otherwise throws a MissingMethodExceptioninvokeMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter) Invokes the propertyMissing method otherwise throws a MissingPropertyExceptioninvokeStaticMethod(Object object, String methodName, Object[] arguments) Invokes a static method on the given Object with the given name and arguments.protected ObjectinvokeStaticMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter) Hook to deal with the case of MissingProperty for static properties.booleanprotected booleanbooleanReturn whether the MetaClass has been modified or notprotected voidprotected voidprotected voidonMixinMethodFound(MetaMethod method) protected voidprotected voidprotected voidpickMethod(String methodName, Class[] arguments) Selects a method by name and argument classes.respondsTo(Object obj, String name) Returns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name regardless of arguments.respondsTo(Object obj, String name, Object[] argTypes) Returns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name and arguments types.retrieveConstructor(Class[] arguments) protected MetaMethodretrieveMethod(String methodName, Class[] arguments) Deprecated.use pickMethod insteadretrieveStaticMethod(String methodName, Object[] arguments) intselectConstructorAndTransformArguments(int numberOfConstructors, Object[] arguments) Internal method to support Groovy runtime.voidsetAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass) Sets the given attribute (field) on the given objectvoidsetAttribute(Object object, String attribute, Object newValue) Sets an attribute of an instance of the class returned by the getTheClass() method.voidsetProperties(Object bean, Map map) Sets a number of bean properties from the given Map where the keys are the String names of properties and the values are the values of the properties to setvoidsetProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass) Sets the property value on an objectvoidsetProperty(Object object, String property, Object newValue) Sets a property of an instance of the class returned by the getTheClass() method.toString()
-
Field Details
-
STATIC_METHOD_MISSING
- See Also:
-
STATIC_PROPERTY_MISSING
- See Also:
-
METHOD_MISSING
- See Also:
-
PROPERTY_MISSING
- See Also:
-
INVOKE_METHOD_METHOD
- See Also:
-
theClass
-
theCachedClass
-
getPropertyMethod
-
invokeMethodMethod
-
setPropertyMethod
-
registry
-
isGroovyObject
protected final boolean isGroovyObject -
isMap
protected final boolean isMap -
metaMethodIndex
-
-
Constructor Details
-
MetaClassImpl
-
MetaClassImpl
-
MetaClassImpl
-
MetaClassImpl
-
-
Method Details
-
getTheCachedClass
-
respondsTo
Description copied from interface:MetaObjectProtocolReturns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name and arguments types.
Note that this method's return value is based on realised methods and does not take into account objects or classes that implement invokeMethod or methodMissing
This method is "safe" in that it will always return a value and never throw an exception
- Specified by:
respondsToin interfaceMetaObjectProtocol- Parameters:
obj- The object to inspectname- The name of the method of interestargTypes- The argument types to match against- Returns:
- A List of MetaMethods matching the argument types which will be empty if no matching methods exist
- See Also:
-
respondsTo
Description copied from interface:MetaObjectProtocolReturns an object satisfying Groovy truth if the implementing MetaClass responds to a method with the given name regardless of arguments. In other words this method will return for foo() and foo(String).
Note that this method's return value is based on realised methods and does not take into account objects or classes that implement invokeMethod or methodMissing
This method is "safe" in that it will always return a value and never throw an exception
- Specified by:
respondsToin interfaceMetaObjectProtocol- Parameters:
obj- The object to inspectname- The name of the method of interest- Returns:
- A List of MetaMethods which will be empty if no methods with the given name exist
- See Also:
-
hasProperty
Description copied from interface:MetaObjectProtocolReturns true of the implementing MetaClass has a property of the given name
Note that this method will only return true for realised properties and does not take into account implementation of getProperty or propertyMissing
- Specified by:
hasPropertyin interfaceMetaObjectProtocol- Parameters:
obj- The object to inspectname- The name of the property- Returns:
- The MetaProperty or null if it doesn't exist
- See Also:
-
getMetaProperty
Description copied from interface:MetaObjectProtocolReturns a MetaProperty for the given name or null if it doesn't exist- Specified by:
getMetaPropertyin interfaceMetaObjectProtocol- Parameters:
name- The name of the MetaProperty- Returns:
- A MetaProperty or null
- See Also:
-
getStaticMetaMethod
Description copied from interface:MetaObjectProtocolRetrieves a static MetaMethod for the given name and argument values, using the types of the arguments to establish the chosen MetaMethod- Specified by:
getStaticMetaMethodin interfaceMetaObjectProtocol- Parameters:
name- The name of the MetaMethodargTypes- The argument types- Returns:
- A MetaMethod or null if it doesn't exist
- See Also:
-
getMetaMethod
Description copied from interface:MetaObjectProtocolRetrieves an instance MetaMethod for the given name and argument values, using the types of the argument values to establish the chosen MetaMethod- Specified by:
getMetaMethodin interfaceMetaObjectProtocol- Parameters:
name- The name of the MetaMethodargTypes- Array containing - 1) the argument values (using which their types are then inferred), or 2) the corresponding argument types- Returns:
- A MetaMethod or null if it doesn't exist
- See Also:
-
getTheClass
Description copied from interface:MetaObjectProtocolRetrieves that Java Class that the attached Meta behaviours apply to- Specified by:
getTheClassin interfaceMetaObjectProtocol- Returns:
- The java.lang.Class instance
-
isGroovyObject
public boolean isGroovyObject() -
getSuperClasses
-
isModified
public boolean isModified()Description copied from interface:MutableMetaClassReturn whether the MetaClass has been modified or not- Specified by:
isModifiedin interfaceMutableMetaClass- Returns:
- True if it has
-
addNewInstanceMethod
Description copied from interface:MutableMetaClassadds a new instance method to this MetaClass. Instance methods are able to overwrite the original methods of the class. Calling this method should not be done after initialise was called.- Specified by:
addNewInstanceMethodin interfaceMutableMetaClass- Parameters:
method- the method to be added
-
addNewStaticMethod
Description copied from interface:MutableMetaClassadds a new static method to this MetaClass. This is only possible as long as initialise was not called.- Specified by:
addNewStaticMethodin interfaceMutableMetaClass- Parameters:
method- the method to be added
-
invokeMethod
Description copied from interface:MetaObjectProtocolInvokes a method on the given object, with the given name and single argument.
- Specified by:
invokeMethodin interfaceMetaObjectProtocol- Parameters:
object- The Object to invoke the method onmethodName- The name of the methodarguments- The argument to the method- Returns:
- The return value of the method which is null if the return type is void
- See Also:
-
invokeMissingMethod
Description copied from interface:MetaClassAttempts to invoke the methodMissing method otherwise throws a MissingMethodException
- Specified by:
invokeMissingMethodin interfaceMetaClass- Parameters:
instance- The instance to invoke methodMissing onmethodName- The name of the methodarguments- The arguments to the method- Returns:
- The results of methodMissing or throws MissingMethodException
- See Also:
-
invokeMissingProperty
public Object invokeMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter) Description copied from interface:MetaClassInvokes the propertyMissing method otherwise throws a MissingPropertyException- Specified by:
invokeMissingPropertyin interfaceMetaClass- Parameters:
instance- The instance of the classpropertyName- The name of the propertyoptionalValue- The value of the property which could be null in the case of a getterisGetter- Whether the missing property event was the result of a getter or a setter- Returns:
- The result of the propertyMissing method or throws MissingPropertyException
-
onSuperPropertyFoundInHierarchy
-
onMixinMethodFound
-
onSuperMethodFoundInHierarchy
-
onInvokeMethodFoundInHierarchy
-
onSetPropertyFoundInHierarchy
-
onGetPropertyFoundInHierarchy
-
invokeStaticMissingProperty
protected Object invokeStaticMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter) Hook to deal with the case of MissingProperty for static properties. The method will look attempt to look up "propertyMissing" handlers and invoke them otherwise thrown a MissingPropertyException- Parameters:
instance- The instancepropertyName- The name of the propertyoptionalValue- The value in the case of a setterisGetter- True if its a getter- Returns:
- The value in the case of a getter or a MissingPropertyException
-
invokeMethod
Invokes the given method on the object. TODO: should this be deprecated? If so, we have to propogate to many places.- Specified by:
invokeMethodin interfaceMetaObjectProtocol- Parameters:
object- The instance which the method is invoked onmethodName- The name of the methodoriginalArguments- The arguments to the method- Returns:
- The return value of the method which is null if the return type is void
- See Also:
-
invokeMethod
public Object invokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass) Invokes the given method on the object.- Specified by:
invokeMethodin interfaceMetaClass- Parameters:
sender- 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 class- Returns:
- The return value of the method
-
getMethodWithCaching
public MetaMethod getMethodWithCaching(Class sender, String methodName, Object[] arguments, boolean isCallToSuper) -
retrieveConstructor
-
retrieveStaticMethod
-
getMethodWithoutCaching
public MetaMethod getMethodWithoutCaching(Class sender, String methodName, Class[] arguments, boolean isCallToSuper) -
invokeStaticMethod
Description copied from interface:MetaObjectProtocolInvokes a static method on the given Object with the given name and arguments.
The Object can either be an instance of the class that this MetaObjectProtocol instance applies to or the java.lang.Class instance itself. If a method cannot be invoked a MissingMethodException is will be thrown
- Specified by:
invokeStaticMethodin interfaceMetaObjectProtocol- 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:
-
invokeConstructorAt
Deprecated.use invokeConstructor insteadWarning, this method will be removed -
invokeConstructor
Description copied from interface:MetaObjectProtocolInvokes a constructor for the given arguments. The MetaClass will attempt to pick the best argument which matches the types of the objects passed within the arguments array- Specified by:
invokeConstructorin interfaceMetaObjectProtocol- Parameters:
arguments- The arguments to the constructor- Returns:
- An instance of the java.lang.Class that this MetaObjectProtocol object applies to
-
selectConstructorAndTransformArguments
Description copied from interface:MetaClassInternal method to support Groovy runtime. Not for client usage.- Specified by:
selectConstructorAndTransformArgumentsin interfaceMetaClass- Parameters:
numberOfConstructors- The number of constructorsarguments- The arguments- Returns:
- selected index
-
checkInitalised
protected void checkInitalised()checks if the initialisation of the class id complete. This method should be called as a form of assert, it is no way to test if there is still initialisation work to be done. Such logic must be implemented in a different way.- Throws:
IllegalStateException- if the initialisation is incomplete yet
-
setProperties
Sets a number of bean properties from the given Map where the keys are the String names of properties and the values are the values of the properties to set -
getProperty
public Object getProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass) Description copied from interface:MetaClassRetrieves a property on the given receiver for the specified arguments. The sender is the class that is requesting the property from 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:
getPropertyin interfaceMetaClass- Parameters:
sender- 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- ??- Returns:
- the given property's value on the object
-
getEffectiveGetMetaProperty
public MetaProperty getEffectiveGetMetaProperty(Class sender, Object object, String name, boolean useSuper) -
getProperties
Get all the properties defined for this type- Specified by:
getPropertiesin interfaceMetaClass- Specified by:
getPropertiesin interfaceMetaObjectProtocol- Returns:
- a list of MetaProperty objects
- See Also:
-
applyPropertyDescriptors
-
addMetaBeanProperty
Adds a new MetaBeanProperty to this MetaClass- Specified by:
addMetaBeanPropertyin interfaceMutableMetaClass- Parameters:
mp- The MetaBeanProperty
-
setProperty
public void setProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass) Sets the property value on an object- Specified by:
setPropertyin interfaceMetaClass- Parameters:
sender- 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- ??
-
getAttribute
Description copied from interface:MetaClassRetrieves the value of an attribute (field). This method is to support the Groovy runtime and not for general client API usage.- Specified by:
getAttributein interfaceMetaClass- Parameters:
sender- The class of the object that requested the attributereceiver- The instancemessageName- The name of the attributeuseSuper- Whether to look-up on the super class or not- Returns:
- The attribute value
-
getAttribute
public Object getAttribute(Class sender, Object object, String attribute, boolean useSuper, boolean fromInsideClass) Looks up the given attribute (field) on the given object -
setAttribute
public void setAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass) Sets the given attribute (field) on the given object- Specified by:
setAttributein interfaceMetaClass- Parameters:
sender- The class of the object that requested the attributeobject- The instanceattribute- The name of the attributenewValue- The value of the attributeuseSuper- Whether to look-up on the super class or notfromInsideClass- Whether the call happened from the inside or the outside of a class
-
getClassNode
Description copied from interface:MetaClassObtains a reference to the original AST for the MetaClass if it is available at runtime- Specified by:
getClassNodein interfaceMetaClass- Returns:
- The original AST or null if it cannot be returned
-
toString
-
addMetaMethod
adds a MetaMethod to this class. WARNING: this method will not do the neccessary steps for multimethod logic and using this method doesn't mean, that a method added here is replacing another method from a parent class completely. These steps are usually done by initialize, which means if you need these steps, you have to add the method before running initialize the first time.- Specified by:
addMetaMethodin interfaceMutableMetaClass- Parameters:
method- the MetaMethod- See Also:
-
addMetaMethodToIndex
-
checkIfGroovyObjectMethod
Checks if the metaMethod is a method from the GroovyObject interface such as setProperty, getProperty and invokeMethod- Parameters:
metaMethod- The metaMethod instance- See Also:
-
isInitialized
protected boolean isInitialized() -
chooseMethod
Chooses the correct method to use from a list of methods which match by name.- Parameters:
methodOrList- the possible methods to choose fromarguments-
-
initialize
public void initialize()Description copied from interface:MetaClasscomplete 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:
initializein interfaceMetaClass
-
getMethods
Description copied from interface:MetaClassRetrieves a list of MetaMethods held by the class- Specified by:
getMethodsin interfaceMetaClass- Specified by:
getMethodsin interfaceMetaObjectProtocol- Returns:
- A list of MetaMethods
- See Also:
-
getMetaMethods
Description copied from interface:MetaClassRetrieves a list of MetaMethods held by this class- Specified by:
getMetaMethodsin interfaceMetaClass- Returns:
- A list of MetaMethods
-
dropStaticMethodCache
-
dropMethodCache
-
createPojoCallSite
-
createStaticSite
-
createPogoCallSite
-
createPogoCallCurrentSite
-
createConstructorSite
-
getClassInfo
-
getVersion
public int getVersion() -
incVersion
public void incVersion() -
getAdditionalMetaMethods
-
findPropertyInClassHierarchy
-
findMixinMethod
-
findMethodInClassHierarchy
protected static MetaMethod findMethodInClassHierarchy(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass) -
findOwnMethod
protected static MetaMethod findOwnMethod(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass, MetaMethod method) -
getSubclassMetaMethods
-
getProperty
Description copied from interface:MetaObjectProtocolRetrieves a property of an instance of the class returned by the getTheClass() method.
What this means is largely down to the MetaClass implementation, however the default case would result in an attempt to invoke a JavaBean getter, or if no such getter exists a public field of the instance.
- Specified by:
getPropertyin interfaceMetaObjectProtocol- Parameters:
object- An instance of the class returned by the getTheClass() methodproperty- The name of the property to retrieve the value for- Returns:
- The properties value
- See Also:
-
setProperty
Description copied from interface:MetaObjectProtocolSets a property of an instance of the class returned by the getTheClass() method.
What this means is largely down to the MetaClass implementation, however the default case would result in an attempt to invoke a JavaBean setter, or if no such setter exists to set a public field of the instance.
- Specified by:
setPropertyin interfaceMetaObjectProtocol- Parameters:
object- An instance of the class returned by the getTheClass() methodproperty- The name of the property to setnewValue- The new value of the property- See Also:
-
getAttribute
Description copied from interface:MetaObjectProtocolRetrieves an attribute of an instance of the class returned by the getTheClass() method.
What this means is largely down to the MetaClass implementation, however the default case would result in attempt to read a field of the instance.
- Specified by:
getAttributein interfaceMetaObjectProtocol- Parameters:
object- An instance of the class returned by the getTheClass() methodattribute- The name of the attribute to retrieve the value for- Returns:
- The attribute value
- See Also:
-
setAttribute
Description copied from interface:MetaObjectProtocolSets an attribute of an instance of the class returned by the getTheClass() method.
What this means is largely down to the MetaClass implementation, however the default case would result in an attempt to set a field of the instance.
- Specified by:
setAttributein interfaceMetaObjectProtocol- Parameters:
object- An instance of the class returned by the getTheClass() methodattribute- The name of the attribute to setnewValue- The new value of the attribute- See Also:
-
pickMethod
Description copied from interface:MetaClassSelects a method by name and argument classes. This method does not search for an exact match, it searches for a compatible method. For this the method selection mechanism is used as provided by the implementation of this MetaClass. pickMethod may or may not be used during the method selection process when invoking a method. There is no warranty for that.- Specified by:
pickMethodin interfaceMetaClass- Parameters:
methodName- the name of the method to pickarguments- the method arguments- Returns:
- a matching MetaMethod or null
-
retrieveMethod
Deprecated.use pickMethod instead -
clearInvocationCaches
protected void clearInvocationCaches()remove all method call cache entries. This should be done if a method is added during runtime, but not by using a category.
-