groovy.lang
Class MetaClass

java.lang.Object
  |
  +--groovy.lang.MetaClass

public class MetaClass
extends Object

Allows methods to be dynamically added to existing classes at runtime

Version:
$Revision: 1.19 $
Author:
James Strachan

Field Summary
protected static Object[] ARRAY_WITH_NULL
           
protected static Object[] EMPTY_ARRAY
           
 
Constructor Summary
MetaClass(groovy.lang.MetaClassRegistry registry, Class theClass)
           
 
Method Summary
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 addNewStaticInstanceMethod(Method method)
          Allows static method definitions to be added to a meta class as if it was an instance 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  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  Object chooseEmptyMethodParams(List methods)
           
protected  Object chooseMethod(String methodName, List methods, Object[] arguments)
          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 containsMatchingMethod(List list, Method method)
           
protected  Object createListenerProxy(Class listenerType, String listenerMethodName, groovy.lang.Closure closure)
           
protected  Object doConstructorInvoke(Constructor constructor, Object[] argumentArray)
           
protected  Object doMethodInvoke(Object object, Method method, Object[] argumentArray)
           
protected  Method findNewStaticInstanceMethod(String methodName, Object[] staticArguments)
          Lets walk the base class & interfaces list to see if we can find the method
 org.codehaus.groovy.ast.ClassNode getClassNode()
           
 List getMethods(String name)
           
 List getNewStaticInstanceMethods(String methodName)
           
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 isCompatibleInstance(Class type, Object value)
           
protected  boolean isGenericGetMethod(Method method)
           
protected  boolean isGenericSetMethod(Method method)
           
protected  void registerStaticMethods()
           
 void setProperty(Object object, String property, Object newValue)
          Sets the property value on an object
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_ARRAY

protected static final Object[] EMPTY_ARRAY

ARRAY_WITH_NULL

protected static final Object[] ARRAY_WITH_NULL
Constructor Detail

MetaClass

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

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

addNewStaticInstanceMethod

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

Parameters:
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.


invokeStaticMethod

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

invokeConstructor

public Object invokeConstructor(Object[] arguments)

getNewStaticInstanceMethods

public List getNewStaticInstanceMethods(String methodName)
Returns:
the currently registered static methods against this class

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 org.codehaus.groovy.ast.ClassNode getClassNode()

toString

public String toString()
Overrides:
toString in class Object

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,
                                     groovy.lang.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 -

containsMatchingMethod

protected boolean containsMatchingMethod(List list,
                                         Method 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

findNewStaticInstanceMethod

protected Method findNewStaticInstanceMethod(String methodName,
                                             Object[] staticArguments)
Lets walk the base class & interfaces list to see if we can find the method


doMethodInvoke

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

doConstructorInvoke

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

chooseMethod

protected Object chooseMethod(String methodName,
                              List methods,
                              Object[] arguments)
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:

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 signnatures. 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 boolean isCompatibleInstance(Class type,
                                       Object value)

isGenericSetMethod

protected boolean isGenericSetMethod(Method method)

isGenericGetMethod

protected boolean isGenericGetMethod(Method method)

registerStaticMethods

protected void registerStaticMethods()


Copyright © 2003 The Codehaus. All Rights Reserved.