groovy.lang
Class MetaClass

java.lang.Object
  extended bygroovy.lang.MetaClass
Direct Known Subclasses:
ProxyMetaClass

public class MetaClass
extends Object

Allows methods to be dynamically added to existing classes at runtime

Version:
$Revision: 1.114 $
Author:
James Strachan, Guillaume Laforge, Jochen Theodorou

Field Summary
protected static Object[] ARRAY_WITH_NULL
           
static Object[] EMPTY_ARRAY
           
static Class[] EMPTY_TYPE_ARRAY
           
protected  MetaClassRegistry registry
           
protected  Class theClass
           
 
Constructor Summary
MetaClass(MetaClassRegistry registry, Class theClass)
           
 
Method Summary
protected  void addMethod(MetaMethod method, boolean forceOverwrite)
           
protected  void addMethods(Class theClass, boolean forceOverwrite)
          Adds all the methods declared in the given class to the metaclass ignoring any matching methods already defined by a derived class
protected  void addNewInstanceMethod(Method method)
          Allows static method definitions to be added to a meta class as if it was an instance method
protected  void addNewStaticMethod(Method method)
           
protected  void addNewStaticMethodsFrom(Class theClass)
          Adds all of the newly defined methods from the given class to this metaclass
protected  Object[] asArray(Object arguments)
          Converts the given object into an array; if its an array then just cast otherwise wrap it in an array
protected  String capitalize(String property)
           
protected  void checkForInvalidOverloading(String name, Class[] baseTypes, Class[] derivedTypes)
          Checks that one of the parameter types is a superset of the other and that the two lists of types don't conflict.
protected  void checkInitialised()
           
protected  Object chooseEmptyMethodParams(List methods)
           
protected  Object chooseMethod(String methodName, List methods, Class[] arguments, boolean coerce)
          Chooses the correct method to use from a list of methods which match by name.
protected  Object chooseMostGeneralMethodWith1NullParam(List methods)
           
protected  Object chooseMostSpecificParams(String name, List matchingMethods, Class[] arguments)
           
protected static boolean coerceGStrings(Object[] arguments)
          Coerces any GString instances into Strings
protected  boolean containsMatchingMethod(List list, MetaMethod method)
           
protected  Object createListenerProxy(Class listenerType, String listenerMethodName, Closure closure)
           
protected  MetaMethod createMetaMethod(Method method)
           
protected  Object doConstructorInvoke(Constructor constructor, Object[] argumentArray)
           
protected  Object doMethodInvoke(Object object, MetaMethod method, Object[] argumentArray)
           
protected  MetaMethod findGetter(Object object, String name)
           
protected  MetaMethod findMethod(Method aMethod)
           
protected  MetaMethod findStaticGetter(Class type, String name)
           
protected  void generateReflector()
           
 Object getAttribute(Object object, String attribute)
          Looks up the given attribute (field) on the given object
 ClassNode getClassNode()
           
protected  List getInterfaceMethods()
           
 List getMetaMethods()
           
 Closure getMethodPointer(Object object, String methodName)
          Returns a callable object for the given method name on the object.
 List getMethods()
           
 List getMethods(String name)
           
protected  Class[] getParameterTypes(Object methodOrConstructor)
           
 List getProperties()
          Get all the properties defined for this type
 Object getProperty(Object object, String property)
           
 List getStaticMethods(String name)
           
protected  Object getStaticProperty(Class aClass, String property)
           
 Object invokeConstructor(Object[] arguments)
           
 Object invokeMethod(Object object, String methodName, Object arguments)
           
 Object invokeMethod(Object object, String methodName, Object[] arguments)
          Invokes the given method on the object.
 Object invokeStaticMethod(Object object, String methodName, Object[] arguments)
           
protected  boolean isAssignableFrom(Class mostSpecificType, Class type)
           
protected static boolean isCompatibleClass(Class type, Class value, boolean includeCoerce)
           
protected static boolean isCompatibleInstance(Class type, Object value, boolean includeCoerce)
           
protected  boolean isGenericGetMethod(MetaMethod method)
           
protected  boolean isGenericSetMethod(MetaMethod method)
           
static boolean isUseReflection()
           
static boolean isValidMethod(Class[] paramTypes, Class[] arguments, boolean includeCoerce)
           
protected  boolean isValidMethod(Object method, Class[] arguments, boolean includeCoerce)
           
protected  boolean isValidReflectorMethod(MetaMethod method)
           
protected  Reflector loadReflector(List methods)
           
protected  Class loadReflectorClass(String name)
           
protected  Class loadReflectorClass(String name, byte[] bytecode)
           
protected  void onMethodChange()
          Call this method when any mutation method is called, such as adding a new method to this MetaClass so that any caching or bytecode generation can be regenerated.
protected  MetaMethod pickMethod(Object object, String methodName, Object[] arguments)
          Picks which method to invoke for the given object, method name and arguments
protected  MetaMethod pickMethod(String methodName, Class[] arguments)
          pick a method in a strict manner, i.e., without reinterpreting the first List argument.
protected  MetaMethod pickStaticMethod(Object object, String methodName, Object[] arguments)
           
protected  MetaMethod pickStaticMethod(String methodName, Class[] arguments)
           
protected  void registerInstanceMethods()
           
protected  void registerStaticMethods()
           
protected  void removeMatchingMethod(List list, MetaMethod method)
          remove a method of the same matching prototype was found in the list
 Constructor retrieveConstructor(Class[] arguments)
           
protected  MetaMethod retrieveMethod(Object owner, String methodName, Object[] arguments)
           
 MetaMethod retrieveMethod(String methodName, Class[] arguments)
           
 MetaMethod retrieveStaticMethod(String methodName, Class[] arguments)
           
 void setAttribute(Object object, String attribute, Object newValue)
          Sets the given attribute (field) on the given object
 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(Object object, String property, Object newValue)
          Sets the property value on an object
protected  void setupProperties(PropertyDescriptor[] propertyDescriptors)
          This will build up the property map (Map of MetaProperty objects, keyed on property name).
static void setUseReflection(boolean useReflection)
          Allows reflection to be enabled in situations where bytecode generation of method invocations causes issues.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_ARRAY

public static final Object[] EMPTY_ARRAY

EMPTY_TYPE_ARRAY

public static Class[] EMPTY_TYPE_ARRAY

ARRAY_WITH_NULL

protected static final Object[] ARRAY_WITH_NULL

registry

protected MetaClassRegistry registry

theClass

protected Class theClass
Constructor Detail

MetaClass

public MetaClass(MetaClassRegistry registry,
                 Class theClass)
          throws IntrospectionException
Method Detail

isUseReflection

public static boolean isUseReflection()

setUseReflection

public static void setUseReflection(boolean useReflection)
Allows reflection to be enabled in situations where bytecode generation of method invocations causes issues.

Parameters:
useReflection -

getMethods

public List getMethods(String name)
Returns:
all the normal instance methods avaiable on this class for the given name

getStaticMethods

public List getStaticMethods(String name)
Returns:
all the normal static methods avaiable on this class for the given name

addNewInstanceMethod

protected void addNewInstanceMethod(Method method)
Allows static method definitions to be added to a meta class as if it was an instance method

Parameters:
method -

addNewStaticMethod

protected void addNewStaticMethod(Method method)

invokeMethod

public Object invokeMethod(Object object,
                           String methodName,
                           Object arguments)

invokeMethod

public Object invokeMethod(Object object,
                           String methodName,
                           Object[] arguments)
Invokes the given method on the object.


retrieveMethod

protected MetaMethod retrieveMethod(Object owner,
                                    String methodName,
                                    Object[] arguments)

retrieveMethod

public MetaMethod retrieveMethod(String methodName,
                                 Class[] arguments)

retrieveConstructor

public Constructor retrieveConstructor(Class[] arguments)

retrieveStaticMethod

public MetaMethod retrieveStaticMethod(String methodName,
                                       Class[] arguments)

pickMethod

protected MetaMethod pickMethod(Object object,
                                String methodName,
                                Object[] arguments)
Picks which method to invoke for the given object, method name and arguments


pickMethod

protected MetaMethod pickMethod(String methodName,
                                Class[] arguments)
pick a method in a strict manner, i.e., without reinterpreting the first List argument. this method is used only by ClassGenerator for static binding

Parameters:
methodName -
arguments -
Returns:

invokeStaticMethod

public Object invokeStaticMethod(Object object,
                                 String methodName,
                                 Object[] arguments)

pickStaticMethod

protected MetaMethod pickStaticMethod(Object object,
                                      String methodName,
                                      Object[] arguments)

pickStaticMethod

protected MetaMethod pickStaticMethod(String methodName,
                                      Class[] arguments)

invokeConstructor

public Object invokeConstructor(Object[] arguments)

setProperties

public 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


getProperty

public Object getProperty(Object object,
                          String property)
Returns:
the given property's value on the object

getProperties

public List getProperties()
Get all the properties defined for this type

Returns:
a list of MetaProperty objects

setupProperties

protected void setupProperties(PropertyDescriptor[] propertyDescriptors)
This will build up the property map (Map of MetaProperty objects, keyed on property name).


setProperty

public void setProperty(Object object,
                        String property,
                        Object newValue)
Sets the property value on an object


getAttribute

public Object getAttribute(Object object,
                           String attribute)
Looks up the given attribute (field) on the given object


setAttribute

public void setAttribute(Object object,
                         String attribute,
                         Object newValue)
Sets the given attribute (field) on the given object


getMethodPointer

public Closure getMethodPointer(Object object,
                                String methodName)
Returns a callable object for the given method name on the object. The object acts like a Closure in that it can be called, like a closure and passed around - though really its a method pointer, not a closure per se.


getClassNode

public ClassNode getClassNode()

toString

public String toString()

asArray

protected Object[] asArray(Object arguments)
Converts the given object into an array; if its an array then just cast otherwise wrap it in an array


createListenerProxy

protected Object createListenerProxy(Class listenerType,
                                     String listenerMethodName,
                                     Closure closure)
Parameters:
listenerType - the interface of the listener to proxy
listenerMethodName - the name of the method in the listener API to call the closure on
closure - the closure to invoke on the listenerMethodName method invocation
Returns:
a dynamic proxy which calls the given closure on the given method name

addMethods

protected void addMethods(Class theClass,
                          boolean forceOverwrite)
Adds all the methods declared in the given class to the metaclass ignoring any matching methods already defined by a derived class

Parameters:
theClass -

addMethod

protected void addMethod(MetaMethod method,
                         boolean forceOverwrite)

containsMatchingMethod

protected boolean containsMatchingMethod(List list,
                                         MetaMethod method)
Returns:
true if a method of the same matching prototype was found in the list

removeMatchingMethod

protected void removeMatchingMethod(List list,
                                    MetaMethod method)
remove a method of the same matching prototype was found in the list


addNewStaticMethodsFrom

protected void addNewStaticMethodsFrom(Class theClass)
Adds all of the newly defined methods from the given class to this metaclass

Parameters:
theClass -

getStaticProperty

protected Object getStaticProperty(Class aClass,
                                   String property)
Returns:
the value of the static property of the given class

findMethod

protected MetaMethod findMethod(Method aMethod)
Returns:
the matching method which should be found

findGetter

protected MetaMethod findGetter(Object object,
                                String name)
Returns:
the getter method for the given object

findStaticGetter

protected MetaMethod findStaticGetter(Class type,
                                      String name)
Returns:
the Method of the given name with no parameters or null

doMethodInvoke

protected Object doMethodInvoke(Object object,
                                MetaMethod method,
                                Object[] argumentArray)

doConstructorInvoke

protected Object doConstructorInvoke(Constructor constructor,
                                     Object[] argumentArray)

chooseMethod

protected Object chooseMethod(String methodName,
                              List methods,
                              Class[] arguments,
                              boolean coerce)
Chooses the correct method to use from a list of methods which match by name.

Parameters:
methods - the possible methods to choose from
arguments - the original argument to the method
Returns:

isValidMethod

protected boolean isValidMethod(Object method,
                                Class[] arguments,
                                boolean includeCoerce)

isValidMethod

public static boolean isValidMethod(Class[] paramTypes,
                                    Class[] arguments,
                                    boolean includeCoerce)

chooseMostSpecificParams

protected Object chooseMostSpecificParams(String name,
                                          List matchingMethods,
                                          Class[] arguments)

checkForInvalidOverloading

protected void checkForInvalidOverloading(String name,
                                          Class[] baseTypes,
                                          Class[] derivedTypes)
Checks that one of the parameter types is a superset of the other and that the two lists of types don't conflict. e.g. foo(String, Object) and foo(Object, String) would conflict if called with foo("a", "b"). Note that this method is only called with 2 possible signatures. i.e. possible invalid combinations will already have been filtered out. So if there were methods foo(String, Object) and foo(Object, String) then one of these would be already filtered out if foo was called as foo(12, "a")


getParameterTypes

protected Class[] getParameterTypes(Object methodOrConstructor)

chooseMostGeneralMethodWith1NullParam

protected Object chooseMostGeneralMethodWith1NullParam(List methods)
Returns:
the method with 1 parameter which takes the most general type of object (e.g. Object) ignoring primitve types

chooseEmptyMethodParams

protected Object chooseEmptyMethodParams(List methods)
Returns:
the method with 1 parameter which takes the most general type of object (e.g. Object)

isCompatibleInstance

protected static boolean isCompatibleInstance(Class type,
                                              Object value,
                                              boolean includeCoerce)

isCompatibleClass

protected static boolean isCompatibleClass(Class type,
                                           Class value,
                                           boolean includeCoerce)

isAssignableFrom

protected boolean isAssignableFrom(Class mostSpecificType,
                                   Class type)

coerceGStrings

protected static boolean coerceGStrings(Object[] arguments)
Coerces any GString instances into Strings

Returns:
true if some coercion was done.

isGenericSetMethod

protected boolean isGenericSetMethod(MetaMethod method)

isGenericGetMethod

protected boolean isGenericGetMethod(MetaMethod method)

registerStaticMethods

protected void registerStaticMethods()

registerInstanceMethods

protected void registerInstanceMethods()

capitalize

protected String capitalize(String property)

onMethodChange

protected void onMethodChange()
Call this method when any mutation method is called, such as adding a new method to this MetaClass so that any caching or bytecode generation can be regenerated.


checkInitialised

protected void checkInitialised()

createMetaMethod

protected MetaMethod createMetaMethod(Method method)

isValidReflectorMethod

protected boolean isValidReflectorMethod(MetaMethod method)

generateReflector

protected void generateReflector()

loadReflector

protected Reflector loadReflector(List methods)

loadReflectorClass

protected Class loadReflectorClass(String name,
                                   byte[] bytecode)
                            throws ClassNotFoundException
Throws:
ClassNotFoundException

loadReflectorClass

protected Class loadReflectorClass(String name)
                            throws ClassNotFoundException
Throws:
ClassNotFoundException

getMethods

public List getMethods()

getMetaMethods

public List getMetaMethods()

getInterfaceMethods

protected List getInterfaceMethods()


Copyright © 2003-2005 The Codehaus. All Rights Reserved.