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.96 $
Author:
James Strachan, Guillaume Laforge

Field Summary
private  java.util.List allMethods
           
protected static java.lang.Object[] ARRAY_WITH_NULL
           
private  MetaProperty arrayLengthProperty
           
private  ClassNode classNode
           
private  java.util.List constructors
           
static java.lang.Object[] EMPTY_ARRAY
           
static java.lang.Class[] EMPTY_TYPE_ARRAY
           
private  MetaMethod genericGetMethod
           
private  MetaMethod genericSetMethod
           
private  boolean initialised
           
private  java.util.List interfaceMethods
           
private  java.util.Map listeners
           
private static java.util.logging.Logger log
           
private  java.util.Map methodCache
           
private  java.util.Map methodIndex
           
private  java.util.List newGroovyMethodsList
           
private  java.util.Map propertyMap
           
private  Reflector reflector
           
private  MetaClassRegistry registry
           
private  java.util.Map staticMethodCache
           
private  java.util.Map staticMethodIndex
           
private  java.lang.Class theClass
           
private static boolean useReflection
           
 
Constructor Summary
MetaClass(MetaClassRegistry registry, java.lang.Class theClass)
           
 
Method Summary
private  void addInheritedMethods(java.lang.Class theClass)
           
private  void addInterfaceMethods(java.util.List list, java.lang.reflect.Method[] methods)
           
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 addNewInstanceMethod(MetaMethod method)
           
protected  void addNewInstanceMethod(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 addNewStaticMethod(MetaMethod method)
           
protected  void addNewStaticMethod(java.lang.reflect.Method 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
private  java.lang.Object asPrimitiveArray(java.util.List list, java.lang.Class parameterType)
           
private  java.lang.Class autoboxType(java.lang.Class type)
           
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.Class[] arguments, boolean coerce)
          Chooses the correct method to use from a list of methods which match by name.
protected  java.lang.Object chooseMostGeneralMethodWith1NullParam(java.util.List methods)
           
protected  java.lang.Object chooseMostSpecificParams(java.lang.String name, java.util.List matchingMethods, java.lang.Class[] arguments)
           
protected static boolean coerceGStrings(java.lang.Object[] arguments)
          Coerces any GString instances into Strings
private static java.lang.Object[] coerceNumbers(MetaMethod method, java.lang.Object[] arguments)
           
protected  boolean containsMatchingMethod(java.util.List list, MetaMethod method)
           
(package private)  java.lang.Class[] convertToTypeArray(java.lang.Object[] args)
          param instance array to the type array
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()
           
 java.lang.Object getAttribute(java.lang.Object object, java.lang.String attribute)
          Looks up the given attribute (field) on the given object
 ClassNode getClassNode()
           
protected  java.util.List getInterfaceMethods()
           
 java.util.List getMetaMethods()
           
 java.util.List getMethods()
           
 java.util.List getMethods(java.lang.String name)
           
protected  java.lang.Class[] getParameterTypes(java.lang.Object methodOrConstructor)
           
 java.util.List getProperties()
          Get all the properties defined for this type
 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 isCompatibleClass(java.lang.Class type, java.lang.Class value, boolean includeCoerce)
           
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()
           
static boolean isValidMethod(java.lang.Class[] paramTypes, java.lang.Class[] arguments, boolean includeCoerce)
           
protected  boolean isValidMethod(java.lang.Object method, java.lang.Class[] 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 pickMethod(java.lang.String methodName, java.lang.Class[] arguments)
          pick a method in a strict manner, i.e., without reinterpreting the first List argument.
protected  MetaMethod pickStaticMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] arguments)
           
protected  MetaMethod pickStaticMethod(java.lang.String methodName, java.lang.Class[] arguments)
           
protected  void registerInstanceMethods()
           
private  void registerMethods(boolean instanceMethods)
           
protected  void registerStaticMethods()
           
 java.lang.reflect.Constructor retrieveConstructor(java.lang.Class[] arguments)
           
protected  MetaMethod retrieveMethod(java.lang.Object owner, java.lang.String methodName, java.lang.Object[] arguments)
           
 MetaMethod retrieveMethod(java.lang.String methodName, java.lang.Class[] arguments)
           
 MetaMethod retrieveStaticMethod(java.lang.String methodName, java.lang.Class[] arguments)
           
 void setAttribute(java.lang.Object object, java.lang.String attribute, java.lang.Object newValue)
          Sets the given attribute (field) on the given object
 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
protected  void setupProperties(java.beans.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.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

private static final java.util.logging.Logger log

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

useReflection

private static boolean useReflection

registry

private MetaClassRegistry registry

theClass

private java.lang.Class theClass

classNode

private ClassNode classNode

methodIndex

private java.util.Map methodIndex

staticMethodIndex

private java.util.Map staticMethodIndex

newGroovyMethodsList

private java.util.List newGroovyMethodsList

propertyMap

private java.util.Map propertyMap

listeners

private java.util.Map listeners

methodCache

private java.util.Map methodCache

staticMethodCache

private java.util.Map staticMethodCache

genericGetMethod

private MetaMethod genericGetMethod

genericSetMethod

private MetaMethod genericSetMethod

constructors

private java.util.List constructors

allMethods

private java.util.List allMethods

interfaceMethods

private java.util.List interfaceMethods

reflector

private Reflector reflector

initialised

private boolean initialised

arrayLengthProperty

private MetaProperty arrayLengthProperty
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 -

addInheritedMethods

private void addInheritedMethods(java.lang.Class theClass)

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

addNewInstanceMethod

protected void addNewInstanceMethod(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 -

addNewInstanceMethod

protected void addNewInstanceMethod(MetaMethod method)

addNewStaticMethod

protected void addNewStaticMethod(java.lang.reflect.Method method)

addNewStaticMethod

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


retrieveMethod

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

retrieveMethod

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

retrieveConstructor

public java.lang.reflect.Constructor retrieveConstructor(java.lang.Class[] arguments)

retrieveStaticMethod

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

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


pickMethod

protected MetaMethod pickMethod(java.lang.String methodName,
                                java.lang.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 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)

pickStaticMethod

protected MetaMethod pickStaticMethod(java.lang.String methodName,
                                      java.lang.Class[] 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

getProperties

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

Returns:
a list of MetaProperty objects

setupProperties

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


setProperty

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


getAttribute

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


setAttribute

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


asPrimitiveArray

private java.lang.Object asPrimitiveArray(java.util.List list,
                                          java.lang.Class parameterType)
Parameters:
list -
parameterType -
Returns:

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)

coerceNumbers

private static java.lang.Object[] coerceNumbers(MetaMethod method,
                                                java.lang.Object[] arguments)

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.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(java.lang.Object method,
                                java.lang.Class[] arguments,
                                boolean includeCoerce)

isValidMethod

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

chooseMostSpecificParams

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

chooseMostGeneralMethodWith1NullParam

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

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)

isCompatibleClass

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

isAssignableFrom

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

autoboxType

private java.lang.Class autoboxType(java.lang.Class type)

coerceGStrings

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

registerMethods

private void registerMethods(boolean instanceMethods)

registerStaticMethods

protected void registerStaticMethods()

registerInstanceMethods

protected void registerInstanceMethods()

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

getMetaMethods

public java.util.List getMetaMethods()

getInterfaceMethods

protected java.util.List getInterfaceMethods()

addInterfaceMethods

private void addInterfaceMethods(java.util.List list,
                                 java.lang.reflect.Method[] methods)

convertToTypeArray

java.lang.Class[] convertToTypeArray(java.lang.Object[] args)
param instance array to the type array

Parameters:
args -
Returns:


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