org.codehaus.groovy.runtime
Class Invoker

java.lang.Object
  extended by org.codehaus.groovy.runtime.Invoker

public class Invoker
extends Object

A helper class to invoke methods or extract properties on arbitrary Java objects dynamically

Version:
$Revision: 1.87 $
Author:
James Strachan

Field Summary
protected static Object[] EMPTY_ARGUMENTS
           
protected static Class[] EMPTY_TYPES
           
 
Constructor Summary
Invoker()
           
 
Method Summary
 Object[] asArray(Object arguments)
          Converts the given object into an array; if its an array then just cast otherwise wrap it in an array
 Object asArray(Object object, Class type)
           
 boolean asBool(Object object)
           
 byte asByte(Object element)
           
 char asChar(Object element)
           
protected  Character asCharacter(Number value)
           
protected  Character asCharacter(String text)
           
 Collection asCollection(Object value)
          Converts the value parameter into a Collection.
 double asDouble(Object element)
           
 float asFloat(Object element)
           
 int asInt(Object element)
           
 Iterator asIterator(Object value)
           
 List asList(Object value)
           
 long asLong(Object element)
           
 Number asNumber(Object value)
           
 short asShort(Object element)
           
 Object asType(Object object, Class type)
           
 int compareTo(Object left, Object right)
          Compares the two objects handling nulls gracefully and performing numeric type coercion if required
protected  String format(Object arguments, boolean verbose)
           
 Object getAttribute(Object object, String attribute)
          Looks up the given attribute (field) on the given object
 MetaClass getMetaClass(Object object)
           
 MetaClassRegistry getMetaRegistry()
           
 Closure getMethodPointer(Object object, String methodName)
          Returns the method pointer for the given object name
 Object getProperty(Object object, String property)
          Looks up the given property of the given object
 String inspect(Object self)
           
 Object invokeConstructorAt(Class at, Class type, Object arguments)
           
 Object invokeConstructorAt(Class at, String type, Object arguments)
           
 Object invokeConstructorOf(Class type, Object arguments)
           
 Object invokeConstructorOf(String type, Object arguments)
           
 Object invokeMethod(Object object, String methodName, Object arguments)
          Invokes the given method on the object.
 Object invokeStaticMethod(String type, String method, Object arguments)
           
 Object invokeSuperMethod(Object object, String methodName, Object arguments)
           
protected  boolean isValidCharacterString(Object value)
           
protected  Class loadClass(String type)
          Attempts to load the given class via name using the current class loader for this code or the thread context class loader
 Matcher objectFindRegex(Object left, Object right)
          Find the right hand regex within the left hand string and return a matcher.
 boolean objectMatchRegex(Object left, Object right)
          Find the right hand regex within the left hand string and return a matcher.
 boolean objectsEqual(Object left, Object right)
           
 Pattern regexPattern(Object regex)
          Compile a regular expression from a string.
 void removeMetaClass(Class clazz)
           
 void setAttribute(Object object, String attribute, Object newValue)
          Sets the given attribute (field) on the given object
 void setProperty(Object object, String property, Object newValue)
          Sets the property on the given object
 String toArrayString(Object[] arguments)
          A helper method to return the string representation of an arrray of objects with brace boundaries "{" and "}".
 String toListString(Collection arg)
          A helper method to return the string representation of a list with bracket boundaries "[" and "]".
 String toMapString(Map arg)
          A helper method to return the string representation of a map with bracket boundaries "[" and "]".
 String toString(Object arguments)
          A helper method to provide some better toString() behaviour such as turning arrays into tuples
 String toTypeString(Object[] arguments)
          A helper method to format the arguments types as a comma-separated list
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ARGUMENTS

protected static final Object[] EMPTY_ARGUMENTS

EMPTY_TYPES

protected static final Class[] EMPTY_TYPES
Constructor Detail

Invoker

public Invoker()
Method Detail

getMetaRegistry

public MetaClassRegistry getMetaRegistry()

getMetaClass

public MetaClass getMetaClass(Object object)

invokeMethod

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

Parameters:
object -
methodName -
arguments -
Returns:

invokeSuperMethod

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

invokeStaticMethod

public Object invokeStaticMethod(String type,
                                 String method,
                                 Object arguments)

invokeConstructorAt

public Object invokeConstructorAt(Class at,
                                  Class type,
                                  Object arguments)

invokeConstructorAt

public Object invokeConstructorAt(Class at,
                                  String type,
                                  Object arguments)

invokeConstructorOf

public Object invokeConstructorOf(Class type,
                                  Object arguments)

invokeConstructorOf

public Object invokeConstructorOf(String type,
                                  Object arguments)

asArray

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


asList

public List asList(Object value)

asCollection

public Collection asCollection(Object value)
Converts the value parameter into a Collection.

Parameters:
value - value to convert
Returns:
a Collection

asIterator

public Iterator asIterator(Object value)

objectsEqual

public boolean objectsEqual(Object left,
                            Object right)
Returns:
true if the two objects are null or the objects are equal

inspect

public String inspect(Object self)

compareTo

public int compareTo(Object left,
                     Object right)
Compares the two objects handling nulls gracefully and performing numeric type coercion if required


toString

public String toString(Object arguments)
A helper method to provide some better toString() behaviour such as turning arrays into tuples


toTypeString

public String toTypeString(Object[] arguments)
A helper method to format the arguments types as a comma-separated list


toMapString

public String toMapString(Map arg)
A helper method to return the string representation of a map with bracket boundaries "[" and "]".


toListString

public String toListString(Collection arg)
A helper method to return the string representation of a list with bracket boundaries "[" and "]".


toArrayString

public String toArrayString(Object[] arguments)
A helper method to return the string representation of an arrray of objects with brace boundaries "{" and "}".


format

protected String format(Object arguments,
                        boolean verbose)

getProperty

public Object getProperty(Object object,
                          String property)
Looks up the given property of the given object


setProperty

public void setProperty(Object object,
                        String property,
                        Object newValue)
Sets the property on the given 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 the method pointer for the given object name


loadClass

protected Class loadClass(String type)
Attempts to load the given class via name using the current class loader for this code or the thread context class loader


objectFindRegex

public Matcher objectFindRegex(Object left,
                               Object right)
Find the right hand regex within the left hand string and return a matcher.

Parameters:
left - string to compare
right - regular expression to compare the string to
Returns:

objectMatchRegex

public boolean objectMatchRegex(Object left,
                                Object right)
Find the right hand regex within the left hand string and return a matcher.

Parameters:
left - string to compare
right - regular expression to compare the string to
Returns:

regexPattern

public Pattern regexPattern(Object regex)
Compile a regular expression from a string.

Parameters:
regex -
Returns:

asType

public Object asType(Object object,
                     Class type)

asArray

public Object asArray(Object object,
                      Class type)

asNumber

public Number asNumber(Object value)

asByte

public byte asByte(Object element)

asChar

public char asChar(Object element)

asFloat

public float asFloat(Object element)

asDouble

public double asDouble(Object element)

asShort

public short asShort(Object element)

asInt

public int asInt(Object element)

asLong

public long asLong(Object element)

asBool

public boolean asBool(Object object)

asCharacter

protected Character asCharacter(Number value)

asCharacter

protected Character asCharacter(String text)

isValidCharacterString

protected boolean isValidCharacterString(Object value)
Returns:
true if the given value is a valid character string (i.e. has length of 1)

removeMetaClass

public void removeMetaClass(Class clazz)


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