|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgroovy.lang.MetaClassImpl
public class MetaClassImpl
Allows methods to be dynamically added to existing classes at runtime
MetaClass
Nested Class Summary | |
---|---|
static class |
MetaClassImpl.Index
|
Field Summary | |
---|---|
protected static Class[] |
EMPTY_CLASS_ARRAY
|
protected boolean |
isGroovyObject
|
protected boolean |
isMap
|
protected static Logger |
LOG
|
protected static String |
METHOD_MISSING
|
protected static String |
PROPERTY_MISSING
|
protected MetaClassRegistry |
registry
|
protected static String |
STATIC_METHOD_MISSING
|
protected static String |
STATIC_PROPERTY_MISSING
|
protected CachedClass |
theCachedClass
|
protected Class |
theClass
|
Constructor Summary | |
---|---|
MetaClassImpl(Class theClass)
|
|
MetaClassImpl(MetaClassRegistry registry,
Class theClass)
|
Method Summary | |
---|---|
void |
addMetaBeanProperty(MetaBeanProperty mp)
Adds a new MetaBeanProperty to this MetaClass |
void |
addMetaMethod(MetaMethod method)
adds a MetaMethod to this class. |
void |
addNewInstanceMethod(Method method)
adds a new instance method to this MetaClass. |
void |
addNewStaticMethod(Method method)
adds a new static method to this MetaClass. |
protected void |
cacheInstanceMethod(MethodKey key,
MetaMethod method)
|
protected void |
cacheStaticMethod(MethodKey key,
MetaMethod method)
|
protected void |
checkInitalised()
checks if the initialisation of the class id complete. |
protected void |
clearInvocationCaches()
remove all method call cache entries. |
protected void |
dropMethodCache(String name)
|
protected void |
dropStaticMethodCache(String name)
|
Object |
getAttribute(Class sender,
Object receiver,
String messageName,
boolean useSuper)
Retrieves the value of an attribute (field). |
Object |
getAttribute(Class sender,
Object object,
String attribute,
boolean useSuper,
boolean fromInsideClass)
Looks up the given attribute (field) on the given object |
Object |
getAttribute(Object object,
String attribute)
Retrieves an attribute of an instance of the class returned by the getTheClass() method. |
ClassNode |
getClassNode()
Obtains a reference to the original AST for the MetaClass if it is available at runtime |
MetaMethod |
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 MetaMethod |
List |
getMetaMethods()
Retrieves a list of MetaMethod instances held by this class |
MetaProperty |
getMetaProperty(String name)
Returns a MetaProperty for the given name or null if it doesn't exist |
List |
getMethods()
Retrieves a list of MetaMethods held by the class |
MetaMethod |
getMethodWithCaching(Class sender,
String methodName,
Class[] arguments,
boolean isCallToSuper)
|
MetaMethod |
getMethodWithoutCaching(Class sender,
String methodName,
Class[] arguments,
boolean isCallToSuper)
|
List |
getProperties()
Get all the properties defined for this type |
Object |
getProperty(Class sender,
Object object,
String name,
boolean useSuper,
boolean fromInsideClass)
Retrieves a property on the given receiver for the specified arguments. |
Object |
getProperty(Object object,
String property)
Retrieves a property of an instance of the class returned by the getTheClass() method. |
MetaMethod |
getStaticMetaMethod(String name,
Object[] argTypes)
Retreives a static MetaMethod for the given name and argument values, using the types of the arguments to establish the chosen MetaMethod |
Class |
getTheClass()
Retrieves that Java Class that the attached Meta behaviours apply to |
MetaProperty |
hasProperty(Object obj,
String name)
Returns true of the implementing MetaClass has a property of the given name |
void |
initialize()
complete the initlialisation process. |
Object |
invokeConstructor(Object[] arguments)
Invokes a constructor for the given arguments. |
Object |
invokeConstructorAt(Class at,
Object[] arguments)
Deprecated. use invokeConstructor instead |
Object |
invokeMethod(Class sender,
Object object,
String methodName,
Object[] originalArguments,
boolean isCallToSuper,
boolean fromInsideClass)
Invokes the given method on the object. |
Object |
invokeMethod(Object object,
String methodName,
Object arguments)
Invokes a method on the given object, with the given name and single argument. |
Object |
invokeMethod(Object object,
String methodName,
Object[] originalArguments)
Invokes the given method on the object. |
Object |
invokeMissingMethod(Object instance,
String methodName,
Object[] arguments)
Attempts to invoke the methodMissing method otherwise throws a MissingMethodException |
Object |
invokeMissingProperty(Object instance,
String propertyName,
Object optionalValue,
boolean isGetter)
Invokes the propertyMissing method otherwise throws a MissingPropertyException |
Object |
invokeStaticMethod(Object object,
String methodName,
Object[] arguments)
Invokes a static method on the given Object with the given name and arguments. |
protected Object |
invokeStaticMissingProperty(Object instance,
String propertyName,
Object optionalValue,
boolean isGetter)
Hook to deal with the case of MissingProperty for static properties. |
boolean |
isGroovyObject()
|
protected boolean |
isInitialized()
|
boolean |
isModified()
Return whether the MetaClass has been modified or not |
MetaMethod |
pickMethod(String methodName,
Class[] arguments)
Selects a method by name and argument classes. |
List |
respondsTo(Object obj,
String name)
Returns true if the implementing MetaClass responds a method with the given name regardless of arguments. |
List |
respondsTo(Object obj,
String name,
Object[] argTypes)
Returns true if the implementing MetaClass responds a method with the given name and arguments types |
Constructor |
retrieveConstructor(Class[] arguments)
|
protected MetaMethod |
retrieveMethod(String methodName,
Class[] arguments)
Deprecated. use pickMethod instead |
MetaMethod |
retrieveStaticMethod(String methodName,
Class[] arguments)
|
int |
selectConstructorAndTransformArguments(int numberOfCosntructors,
Object[] arguments)
Internal method to support Groovy runtime. |
void |
setAttribute(Class sender,
Object object,
String attribute,
Object newValue,
boolean useSuper,
boolean fromInsideClass)
Sets the given attribute (field) on the given object |
void |
setAttribute(Object object,
String attribute,
Object newValue)
Sets an attribute of an instance of the class returned by the getTheClass() method. |
void |
setProperties(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 set |
void |
setProperty(Class sender,
Object object,
String name,
Object newValue,
boolean useSuper,
boolean fromInsideClass)
Sets the property value on an object |
void |
setProperty(Object object,
String property,
Object newValue)
Sets a property of an instance of the class returned by the getTheClass() method. |
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final String STATIC_METHOD_MISSING
protected static final String STATIC_PROPERTY_MISSING
protected static final String METHOD_MISSING
protected static final String PROPERTY_MISSING
protected static final Logger LOG
protected final Class theClass
protected final CachedClass theCachedClass
protected MetaClassRegistry registry
protected final boolean isGroovyObject
protected final boolean isMap
protected static final Class[] EMPTY_CLASS_ARRAY
Constructor Detail |
---|
public MetaClassImpl(Class theClass)
public MetaClassImpl(MetaClassRegistry registry, Class theClass)
Method Detail |
---|
public List respondsTo(Object obj, String name, Object[] argTypes)
MetaObjectProtocol
Returns true if the implementing MetaClass responds a method with the given name and arguments types
Note that this method will only return true for realised methods and does not take into account objects or classes that implement invokeMethod or methodMissing
This method is "safe" and will always return a boolean and never throw an exception
respondsTo
in interface MetaObjectProtocol
obj
- The object to inspectname
- The name of the methodargTypes
- The argument types
MetaObjectProtocol.respondsTo(Object,String, Object[])
public List respondsTo(Object obj, String name)
MetaObjectProtocol
Returns true if the implementing MetaClass responds 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 will only return true for realised methods and does not take into account objects or classes that implement invokeMethod or methodMissing
This method is "safe" and will always return a boolean and never throw an exception
respondsTo
in interface MetaObjectProtocol
obj
- The object to inspectname
- The name of the method
MetaObjectProtocol.respondsTo(Object,String, Object[])
public MetaProperty hasProperty(Object obj, String name)
MetaObjectProtocol
Returns 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
hasProperty
in interface MetaObjectProtocol
obj
- The object to inspectname
- The name of the property
MetaObjectProtocol.hasProperty(Object,String)
public MetaProperty getMetaProperty(String name)
MetaObjectProtocol
getMetaProperty
in interface MetaObjectProtocol
name
- The name of the MetaProperty
MetaObjectProtocol.getMetaProperty(String)
public MetaMethod getStaticMetaMethod(String name, Object[] argTypes)
MetaObjectProtocol
getStaticMetaMethod
in interface MetaObjectProtocol
name
- The name of the MetaMethodargTypes
- The argument types
MetaObjectProtocol.getStaticMetaMethod(String, Object[])
public MetaMethod getMetaMethod(String name, Object[] argTypes)
MetaObjectProtocol
getMetaMethod
in interface MetaObjectProtocol
name
- The name of the MetaMethodargTypes
- The argument types
MetaObjectProtocol.getMetaMethod(String, Object[])
public Class getTheClass()
MetaObjectProtocol
getTheClass
in interface MetaObjectProtocol
public boolean isGroovyObject()
public boolean isModified()
MutableMetaClass
isModified
in interface MutableMetaClass
public void addNewInstanceMethod(Method method)
MutableMetaClass
addNewInstanceMethod
in interface MutableMetaClass
method
- the method to be addedpublic void addNewStaticMethod(Method method)
MutableMetaClass
addNewStaticMethod
in interface MutableMetaClass
method
- the method to be addedpublic Object invokeMethod(Object object, String methodName, Object arguments)
MetaObjectProtocol
Invokes a method on the given object, with the given name and single argument.
invokeMethod
in interface MetaObjectProtocol
object
- The Object to invoke the method onmethodName
- The name of the methodarguments
- The argument to the method
MetaObjectProtocol.invokeMethod(Object, String, Object[])
public Object invokeMissingMethod(Object instance, String methodName, Object[] arguments)
MetaClass
Attempts to invoke the methodMissing method otherwise throws a MissingMethodException
invokeMissingMethod
in interface MetaClass
instance
- The instance to invoke methodMissing onmethodName
- The name of the methodarguments
- The arguments to the method
MissingMethodException
public Object invokeMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter)
MetaClass
invokeMissingProperty
in interface MetaClass
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
protected Object invokeStaticMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter)
instance
- The instancepropertyName
- The name of the propertyoptionalValue
- The value in the case of a setterisGetter
- True if its a getter
public Object invokeMethod(Object object, String methodName, Object[] originalArguments)
invokeMethod
in interface MetaObjectProtocol
object
- The instance which the method is invoked onmethodName
- The name of the methodoriginalArguments
- The arguments to the method
MissingMethodException
public Object invokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass)
invokeMethod
in interface MetaClass
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
public MetaMethod getMethodWithCaching(Class sender, String methodName, Class[] arguments, boolean isCallToSuper)
protected void cacheInstanceMethod(MethodKey key, MetaMethod method)
protected void cacheStaticMethod(MethodKey key, MetaMethod method)
public Constructor retrieveConstructor(Class[] arguments)
public MetaMethod retrieveStaticMethod(String methodName, Class[] arguments)
public MetaMethod getMethodWithoutCaching(Class sender, String methodName, Class[] arguments, boolean isCallToSuper)
public Object invokeStaticMethod(Object object, String methodName, Object[] arguments)
MetaObjectProtocol
Invokes 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
invokeStaticMethod
in interface MetaObjectProtocol
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
MissingMethodException
public Object invokeConstructorAt(Class at, Object[] arguments)
public Object invokeConstructor(Object[] arguments)
MetaObjectProtocol
invokeConstructor
in interface MetaObjectProtocol
arguments
- The arguments to the constructor
public int selectConstructorAndTransformArguments(int numberOfCosntructors, Object[] arguments)
MetaClass
selectConstructorAndTransformArguments
in interface MetaClass
numberOfCosntructors
- The number of constructorsarguments
- The arguments
protected void checkInitalised()
IllegalStateException
- if the initialisation is incomplete yetpublic void setProperties(Object bean, Map map)
public Object getProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass)
MetaClass
Retrieves 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
getProperty
in interface MetaClass
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
- ??
public List getProperties()
getProperties
in interface MetaClass
getProperties
in interface MetaObjectProtocol
MetaProperty
public void addMetaBeanProperty(MetaBeanProperty mp)
addMetaBeanProperty
in interface MutableMetaClass
mp
- The MetaBeanPropertypublic void setProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass)
setProperty
in interface MetaClass
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
- ??public Object getAttribute(Class sender, Object receiver, String messageName, boolean useSuper)
MetaClass
getAttribute
in interface MetaClass
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
public Object getAttribute(Class sender, Object object, String attribute, boolean useSuper, boolean fromInsideClass)
public void setAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass)
setAttribute
in interface MetaClass
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 classpublic ClassNode getClassNode()
MetaClass
getClassNode
in interface MetaClass
public String toString()
toString
in class Object
public void addMetaMethod(MetaMethod method)
addMetaMethod
in interface MutableMetaClass
method
- the MetaMethodinitialize()
protected boolean isInitialized()
public void initialize()
MetaClass
initialize
in interface MetaClass
public List getMethods()
MetaClass
getMethods
in interface MetaClass
getMethods
in interface MetaObjectProtocol
MetaMethod
public List getMetaMethods()
MetaClass
getMetaMethods
in interface MetaClass
protected void dropStaticMethodCache(String name)
protected void dropMethodCache(String name)
public Object getProperty(Object object, String property)
MetaObjectProtocol
Retrieves 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.
getProperty
in interface MetaObjectProtocol
object
- An instance of the class returned by the getTheClass() methodproperty
- The name of the property to retrieve the value for
MetaClassImpl
public void setProperty(Object object, String property, Object newValue)
MetaObjectProtocol
Sets 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.
setProperty
in interface MetaObjectProtocol
object
- An instance of the class returned by the getTheClass() methodproperty
- The name of the property to setnewValue
- The new value of the propertyMetaClassImpl
public Object getAttribute(Object object, String attribute)
MetaObjectProtocol
Retrieves 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.
getAttribute
in interface MetaObjectProtocol
object
- An instance of the class returned by the getTheClass() methodattribute
- The name of the attribute to retrieve the value for
MetaClassImpl
public void setAttribute(Object object, String attribute, Object newValue)
MetaObjectProtocol
Sets 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.
setAttribute
in interface MetaObjectProtocol
object
- An instance of the class returned by the getTheClass() methodattribute
- The name of the attribute to setnewValue
- The new value of the attributeMetaClassImpl
public MetaMethod pickMethod(String methodName, Class[] arguments)
MetaClass
pickMethod
in interface MetaClass
methodName
- the name of the method to pickarguments
- the method arguments
protected MetaMethod retrieveMethod(String methodName, Class[] arguments)
protected void clearInvocationCaches()
|
Copyright © 2003-2007 The Codehaus. All rights reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |