groovy.lang
Class MetaClass

java.lang.Object
  extended bygroovy.lang.MetaClass

public class MetaClass
extends Object

Allows methods to be dynamically added to existing classes at runtime

Version:
$Revision: 1.73 $
Author:
James Strachan

Field Summary
protected static Object[] ARRAY_WITH_NULL
           
static Object[] EMPTY_ARRAY
           
static Class[] EMPTY_TYPE_ARRAY
           
 
Constructor Summary
MetaClass(MetaClassRegistry registry, Class theClass)
           
 
Method Summary
protected  void addMethod(MetaMethod method)
           
protected  void addMethods(Class theClass)
          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(MetaMethod method)
           
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(MetaMethod 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, Object[] arguments, boolean coerce)
          Chooses the correct method to use from a list of methods which match by name.
protected  Object chooseMostGeneralMethodWith1Param(List methods)
           
protected  Object chooseMostSpecificParams(String name, List matchingMethods, Object[] arguments)
           
protected  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()
           
 ClassNode getClassNode()
           
protected  List getInterfaceMethods()
           
 List getMethods()
           
 List getMethods(String name)
           
protected  Class[] getParameterTypes(Object methodOrConstructor)
           
 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 isCompatibleInstance(Class type, Object value, boolean includeCoerce)
           
protected  boolean isGenericGetMethod(MetaMethod method)
           
protected  boolean isGenericSetMethod(MetaMethod method)
           
static boolean isUseReflection()
           
protected static boolean isValidMethod(Class[] paramTypes, Object[] arguments, boolean includeCoerce)
           
protected  boolean isValidMethod(Object method, Object[] 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 pickStaticMethod(Object object, String methodName, Object[] arguments)
           
protected  void registerInstanceMethods()
           
protected  void registerStaticMethods()
           
 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
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
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 -

addNewInstanceMethod

protected void addNewInstanceMethod(MetaMethod method)

addNewStaticMethod

protected void addNewStaticMethod(Method method)

addNewStaticMethod

protected void addNewStaticMethod(MetaMethod 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.


pickMethod

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


invokeStaticMethod

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

pickStaticMethod

protected MetaMethod pickStaticMethod(Object object,
                                      String methodName,
                                      Object[] 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

setProperty

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


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)
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)

containsMatchingMethod

protected boolean containsMatchingMethod(List list,
                                         MetaMethod method)
Returns:
true if 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,
                              Object[] 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,
                                Object[] arguments,
                                boolean includeCoerce)

isValidMethod

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

chooseMostSpecificParams

protected Object chooseMostSpecificParams(String name,
                                          List matchingMethods,
                                          Object[] 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)

chooseMostGeneralMethodWith1Param

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

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)

isAssignableFrom

protected boolean isAssignableFrom(Class mostSpecificType,
                                   Class type)

coerceGStrings

protected 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()

getInterfaceMethods

protected List getInterfaceMethods()


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