groovy.lang
Class MetaClass

java.lang.Object
  extended bygroovy.lang.MetaClass

public class MetaClass
extends java.lang.Object

Allows methods to be dynamically added to existing classes at runtime

Version:
$Revision: 1.61 $
Author:
James Strachan

Field Summary
protected static java.lang.Object[] ARRAY_WITH_NULL
           
static java.lang.Object[] EMPTY_ARRAY
           
static java.lang.Class[] EMPTY_TYPE_ARRAY
           
 
Constructor Summary
MetaClass(MetaClassRegistry registry, java.lang.Class theClass)
           
 
Method Summary
protected  void addMethod(MetaMethod method)
           
protected  void addMethods(java.lang.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 addNewStaticInstanceMethod(MetaMethod method)
           
protected  void addNewStaticInstanceMethod(java.lang.reflect.Method method)
          Allows static method definitions to be added to a meta class as if it was an instance method
protected  void addNewStaticMethodsFrom(java.lang.Class theClass)
          Adds all of the newly defined methods from the given class to this metaclass
protected  java.lang.Object[] asArray(java.lang.Object arguments)
          Converts the given object into an array; if its an array then just cast otherwise wrap it in an array
protected  java.lang.String capitalize(java.lang.String property)
           
protected  void checkForInvalidOverloading(java.lang.String name, java.lang.Class[] baseTypes, java.lang.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  java.lang.Object chooseEmptyMethodParams(java.util.List methods)
           
protected  java.lang.Object chooseMethod(java.lang.String methodName, java.util.List methods, java.lang.Object[] arguments, boolean coerce)
          Chooses the correct method to use from a list of methods which match by name.
protected  java.lang.Object chooseMostGeneralMethodWith1Param(java.util.List methods)
           
protected  java.lang.Object chooseMostSpecificParams(java.lang.String name, java.util.List matchingMethods, java.lang.Object[] arguments)
           
protected  boolean coerceGStrings(java.lang.Object[] arguments)
          Coerces any GString instances into Strings
protected  boolean containsMatchingMethod(java.util.List list, MetaMethod method)
           
protected  java.lang.Object createListenerProxy(java.lang.Class listenerType, java.lang.String listenerMethodName, Closure closure)
           
protected  MetaMethod createMetaMethod(java.lang.reflect.Method method)
           
protected  java.lang.Object doConstructorInvoke(java.lang.reflect.Constructor constructor, java.lang.Object[] argumentArray)
           
protected  java.lang.Object doMethodInvoke(java.lang.Object object, MetaMethod method, java.lang.Object[] argumentArray)
           
protected  MetaMethod findGetter(java.lang.Object object, java.lang.String name)
           
protected  MetaMethod findMethod(java.lang.reflect.Method aMethod)
           
protected  MetaMethod findStaticGetter(java.lang.Class type, java.lang.String name)
           
protected  void generateReflector()
           
 ClassNode getClassNode()
           
protected  java.util.List getInterfaceMethods()
           
 java.util.List getMethods()
           
 java.util.List getMethods(java.lang.String name)
           
protected  java.lang.Class[] getParameterTypes(java.lang.Object methodOrConstructor)
           
 java.lang.Object getProperty(java.lang.Object object, java.lang.String property)
           
 java.util.List getStaticMethods(java.lang.String name)
           
protected  java.lang.Object getStaticProperty(java.lang.Class aClass, java.lang.String property)
           
 java.lang.Object invokeConstructor(java.lang.Object[] arguments)
           
 java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object arguments)
           
 java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] arguments)
          Invokes the given method on the object.
 java.lang.Object invokeStaticMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] arguments)
           
protected  boolean isAssignableFrom(java.lang.Class mostSpecificType, java.lang.Class type)
           
protected static boolean isCompatibleInstance(java.lang.Class type, java.lang.Object value, boolean includeCoerce)
           
protected  boolean isGenericGetMethod(MetaMethod method)
           
protected  boolean isGenericSetMethod(MetaMethod method)
           
static boolean isUseReflection()
           
protected static boolean isValidMethod(java.lang.Class[] paramTypes, java.lang.Object[] arguments, boolean includeCoerce)
           
protected  boolean isValidMethod(java.lang.Object method, java.lang.Object[] arguments, boolean includeCoerce)
           
protected  boolean isValidReflectorMethod(MetaMethod method)
           
protected  Reflector loadReflector(java.util.List methods)
           
protected  java.lang.Class loadReflectorClass(java.lang.String name)
           
protected  java.lang.Class loadReflectorClass(java.lang.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(java.lang.Object object, java.lang.String methodName, java.lang.Object[] arguments)
          Picks which method to invoke for the given object, method name and arguments
protected  MetaMethod pickStaticMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] arguments)
           
protected  void registerStaticMethods()
           
 void setProperties(java.lang.Object bean, java.util.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(java.lang.Object object, java.lang.String property, java.lang.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.
 java.lang.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 java.lang.Object[] EMPTY_ARRAY

EMPTY_TYPE_ARRAY

public static java.lang.Class[] EMPTY_TYPE_ARRAY

ARRAY_WITH_NULL

protected static final java.lang.Object[] ARRAY_WITH_NULL
Constructor Detail

MetaClass

public MetaClass(MetaClassRegistry registry,
                 java.lang.Class theClass)
          throws java.beans.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 java.util.List getMethods(java.lang.String name)
Returns:
all the normal instance methods avaiable on this class for the given name

getStaticMethods

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

addNewStaticInstanceMethod

protected void addNewStaticInstanceMethod(java.lang.reflect.Method method)
Allows static method definitions to be added to a meta class as if it was an instance method

Parameters:
method -

addNewStaticInstanceMethod

protected void addNewStaticInstanceMethod(MetaMethod method)

invokeMethod

public java.lang.Object invokeMethod(java.lang.Object object,
                                     java.lang.String methodName,
                                     java.lang.Object arguments)

invokeMethod

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


pickMethod

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


invokeStaticMethod

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

pickStaticMethod

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

invokeConstructor

public java.lang.Object invokeConstructor(java.lang.Object[] arguments)

setProperties

public void setProperties(java.lang.Object bean,
                          java.util.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 java.lang.Object getProperty(java.lang.Object object,
                                    java.lang.String property)
Returns:
the given property's value on the object

setProperty

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


getClassNode

public ClassNode getClassNode()

toString

public java.lang.String toString()

asArray

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


createListenerProxy

protected java.lang.Object createListenerProxy(java.lang.Class listenerType,
                                               java.lang.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(java.lang.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(java.util.List list,
                                         MetaMethod method)
Returns:
true if a method of the same matching prototype was found in the list

addNewStaticMethodsFrom

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

Parameters:
theClass -

getStaticProperty

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

findMethod

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

findGetter

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

findStaticGetter

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

doMethodInvoke

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

doConstructorInvoke

protected java.lang.Object doConstructorInvoke(java.lang.reflect.Constructor constructor,
                                               java.lang.Object[] argumentArray)

chooseMethod

protected java.lang.Object chooseMethod(java.lang.String methodName,
                                        java.util.List methods,
                                        java.lang.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(java.lang.Object method,
                                java.lang.Object[] arguments,
                                boolean includeCoerce)

isValidMethod

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

chooseMostSpecificParams

protected java.lang.Object chooseMostSpecificParams(java.lang.String name,
                                                    java.util.List matchingMethods,
                                                    java.lang.Object[] arguments)

checkForInvalidOverloading

protected void checkForInvalidOverloading(java.lang.String name,
                                          java.lang.Class[] baseTypes,
                                          java.lang.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 java.lang.Class[] getParameterTypes(java.lang.Object methodOrConstructor)

chooseMostGeneralMethodWith1Param

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

chooseEmptyMethodParams

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

isCompatibleInstance

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

isAssignableFrom

protected boolean isAssignableFrom(java.lang.Class mostSpecificType,
                                   java.lang.Class type)

coerceGStrings

protected boolean coerceGStrings(java.lang.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()

capitalize

protected java.lang.String capitalize(java.lang.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(java.lang.reflect.Method method)

isValidReflectorMethod

protected boolean isValidReflectorMethod(MetaMethod method)

generateReflector

protected void generateReflector()

loadReflector

protected Reflector loadReflector(java.util.List methods)

loadReflectorClass

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

loadReflectorClass

protected java.lang.Class loadReflectorClass(java.lang.String name)
                                      throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

getMethods

public java.util.List getMethods()

getInterfaceMethods

protected java.util.List getInterfaceMethods()


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