com.objectwave.tools
Class MethodBuilder

java.lang.Object
  |
  +--com.objectwave.tools.MethodBuilder

public class MethodBuilder
extends java.lang.Object

This class is used to provide some rudimentary scripting capabilities. Supports... Arrays, local variables, direct field access, constructors,

Version:
$Id: MethodBuilder.java,v 2.1 2002/03/23 14:03:00 dave_hoag Exp $
Author:
Dave Hoag

Nested Class Summary
static class MethodBuilder.Test
          Test case.
 
Field Summary
static java.lang.Integer EOF
           
protected  java.util.Hashtable variables
           
 
Constructor Summary
MethodBuilder()
          Create a new instance and initialize two 'global' variables of "System" and "Class".
 
Method Summary
protected  boolean addMethod(java.lang.String className, java.lang.String methodName, java.lang.String key, java.lang.Class[] types)
          Define a method in our list of methods.
 void configure(java.util.Properties p)
          Configure the MethodBuilder with property information.
 void defineMethods(java.io.LineNumberReader rdr)
          By default, the key to the method list is the method name.
 void defineMethods(java.lang.String fileName)
          Define methods of the format ClassName.methodName(parameterType, parameterType)
protected  java.lang.Object determineVarValue(java.lang.String varDecl, java.io.LineNumberReader rdr, java.lang.Object obj, int[] idxHldr)
           
protected  java.lang.Object doCalculation(java.lang.String varDecl)
          The parameter may be math that needs to be processed.
protected  java.lang.String firstNonBlankLine(java.lang.String varDecl, java.io.LineNumberReader rdr)
          Starting with the varDecl parameter, find the fist non blank line in the rdr.
protected  java.lang.Class[] getArgumentTypes(java.lang.String line)
          Extract all of the fully qualified class names separated by a ',' and turn them into classes.
protected  java.lang.reflect.Field getFieldFor(java.lang.String fieldName, java.lang.Object obj)
          Find the field specified in the object.
protected  java.lang.Object getFieldReference(java.lang.String varDecl, int idx, java.lang.Object targetObject)
          Get the value represented by this string.
 java.lang.Object[] getLastMethod()
          The last method invoked and arguments that were supplied.
protected  java.lang.reflect.Method getMethod(java.lang.String methodKey, java.lang.String methodName, java.lang.Object obj)
          Look for a declared method at that method key.
protected  java.lang.Object[] getMethodArgs(java.io.LineNumberReader rdr, java.lang.String firstLine, java.lang.Class[] types, java.lang.Object obj)
          Get the object values that are to be the parameters to the method.
protected  java.lang.reflect.Method getMethodByName(java.lang.Class c, java.lang.String methodName)
          First, look for a method that takes no arguments with that method name.
protected  void initializePrintMethods()
          Common methods that should not require definition by the user.
protected  java.lang.Object invokeConstructor(java.io.LineNumberReader rdr, java.lang.String className, java.lang.String parameterString, java.lang.Object obj)
          Invoke a constrcutor to create a new instance.
 java.lang.Object invokeMethod(java.lang.Object obj, java.io.LineNumberReader rdr)
          Invoke a method upon the target object.
protected  java.lang.Object invokeMethod(java.lang.Object obj, java.io.LineNumberReader rdr, java.lang.String firstLine, int idx)
          Actually invoke the method.
protected  java.lang.Class primitiveClass(java.lang.String className)
          Get the primitive version of the className.
protected  void setFieldReference(java.lang.String key, int idx, java.lang.Object value)
          Set the field reference to the parameter value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

variables

protected java.util.Hashtable variables

EOF

public static final java.lang.Integer EOF
Constructor Detail

MethodBuilder

public MethodBuilder()
Create a new instance and initialize two 'global' variables of "System" and "Class".

Method Detail

addMethod

protected boolean addMethod(java.lang.String className,
                            java.lang.String methodName,
                            java.lang.String key,
                            java.lang.Class[] types)
                     throws java.lang.ClassNotFoundException
Define a method in our list of methods.

Parameters:
className - The class upon which the method is declared.
methodName - The name of the method.
key - The key for the method cache.
types - The method parameter classes.
Returns:
True if the a method matching the parameters is added.
Throws:
java.lang.ClassNotFoundException

configure

public void configure(java.util.Properties p)
               throws java.io.IOException,
                      java.lang.ClassNotFoundException,
                      java.io.FileNotFoundException
Configure the MethodBuilder with property information. The only property currently used is "MethodDefinitons"

Parameters:
p - java.util.Properties The source of the property information.
Throws:
java.io.IOException
java.lang.ClassNotFoundException
java.io.FileNotFoundException

defineMethods

public void defineMethods(java.lang.String fileName)
                   throws java.io.IOException,
                          java.lang.ClassNotFoundException,
                          java.io.FileNotFoundException
Define methods of the format ClassName.methodName(parameterType, parameterType)

Parameters:
fileName - String Name of a file that contains methodDefinitions.
Throws:
java.io.IOException
java.lang.ClassNotFoundException
java.io.FileNotFoundException
See Also:
defineMethods(java.io.LineNumberReader)

defineMethods

public void defineMethods(java.io.LineNumberReader rdr)
                   throws java.lang.ClassNotFoundException,
                          java.io.IOException
By default, the key to the method list is the method name. This doesn't work will with regards to operator This will read and define methods until the end of the reader is reached. Method are defined like: ...Define a method that is called getInt. com.objectwave.tools.MethodBuilder.getInt(); ...Define a constructor that takes a String as an argument com.objectwave.tools.MethodBuilder.new(java.lang.String); ...Define a constructor that takes a primitive int as an argument. com.objectwave.tools.MethodBuilder.new(int);

Parameters:
rdr -
Throws:
java.lang.ClassNotFoundException
java.io.IOException

determineVarValue

protected java.lang.Object determineVarValue(java.lang.String varDecl,
                                             java.io.LineNumberReader rdr,
                                             java.lang.Object obj,
                                             int[] idxHldr)
                                      throws java.io.IOException,
                                             java.lang.reflect.InvocationTargetException,
                                             java.lang.NoSuchMethodException,
                                             java.lang.IllegalAccessException,
                                             java.lang.ClassNotFoundException,
                                             java.lang.InstantiationException,
                                             java.lang.NoSuchFieldException
Parameters:
varDecl -
rdr -
obj -
idxHldr -
Returns:
Description of the Return Value
Throws:
java.io.IOException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.NoSuchFieldException

doCalculation

protected java.lang.Object doCalculation(java.lang.String varDecl)
                                  throws java.text.ParseException
The parameter may be math that needs to be processed. This will attempt to treat that String as math. If an exception occurs, the string did not represent a valid mathematical formula.

Parameters:
varDecl - String Perhaps math. Ex. "x + 10".
Returns:
The result of the math.
Throws:
java.text.ParseException - The provided string is not a mathematical formula.

firstNonBlankLine

protected java.lang.String firstNonBlankLine(java.lang.String varDecl,
                                             java.io.LineNumberReader rdr)
                                      throws java.io.IOException
Starting with the varDecl parameter, find the fist non blank line in the rdr.

Parameters:
varDecl - A line already read from the rdr.
rdr - The source of additional lines of data.
Returns:
The fist non blank line.
Throws:
java.io.IOException

getArgumentTypes

protected java.lang.Class[] getArgumentTypes(java.lang.String line)
                                      throws java.lang.ClassNotFoundException
Extract all of the fully qualified class names separated by a ',' and turn them into classes.

Parameters:
line - String A line of argument types. Ex. "java.lang.String, int, float" Three arguments with two primitive classes.
Returns:
Class [] The java Class objects represented by the strings.
Throws:
java.lang.ClassNotFoundException

getFieldFor

protected java.lang.reflect.Field getFieldFor(java.lang.String fieldName,
                                              java.lang.Object obj)
                                       throws java.lang.NoSuchFieldException,
                                              java.lang.IllegalAccessException
Find the field specified in the object.

Parameters:
fieldName - String name of an attribute.
obj - The object containing the field.
Returns:
The reflected Field object.
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

getFieldReference

protected java.lang.Object getFieldReference(java.lang.String varDecl,
                                             int idx,
                                             java.lang.Object targetObject)
Get the value represented by this string.

Parameters:
varDecl - A string that may be a field reference. Ex. aValue.aField
idx - The index of the period in the varDecl parameter.
targetObject -
Returns:
The value of the field in the target object.

getLastMethod

public java.lang.Object[] getLastMethod()
The last method invoked and arguments that were supplied. The invoking object may have no idea what method was invoked. This gives the invoker a chance to find out. The first parameter is always the java.lang.reflect.Method (or java.lang.reflect.Constructor) The next values in the Object array are the parameters.

Returns:
Object [] Containing the method object and the parameters sent.

getMethod

protected java.lang.reflect.Method getMethod(java.lang.String methodKey,
                                             java.lang.String methodName,
                                             java.lang.Object obj)
                                      throws java.lang.NoSuchMethodException
Look for a declared method at that method key. If no declared method is found, look for a no argument method with that method name. Look for any method with that name.

Parameters:
methodKey - java.lang.String The key used in the dictionary.
methodName - Name of the method for which we are searching.
obj - The object we expect to have the target method.
Returns:
java.lang.reflect.Method Method found in the cache of methods, null, or a looked up method
Throws:
java.lang.NoSuchMethodException

getMethodArgs

protected java.lang.Object[] getMethodArgs(java.io.LineNumberReader rdr,
                                           java.lang.String firstLine,
                                           java.lang.Class[] types,
                                           java.lang.Object obj)
                                    throws java.io.IOException,
                                           java.lang.reflect.InvocationTargetException,
                                           java.lang.NoSuchMethodException,
                                           java.lang.ClassNotFoundException,
                                           java.lang.IllegalAccessException,
                                           java.lang.InstantiationException,
                                           java.lang.NoSuchFieldException
Get the object values that are to be the parameters to the method.

Parameters:
rdr - The source to read more lines if necessary.
types - Expected types of the parameters.
firstLine -
obj -
Returns:
Object [] of the arguments for the method we are to invoke.
Throws:
java.io.IOException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.lang.NoSuchFieldException

getMethodByName

protected java.lang.reflect.Method getMethodByName(java.lang.Class c,
                                                   java.lang.String methodName)
First, look for a method that takes no arguments with that method name. Next find the first method that matches that name. BEWARE: Results are inconsistent if there are two methods with the same name

Parameters:
c - java.lang.Class The class upon which we are searching for a method.
methodName - java.lang.String Name of method for which we are to look
Returns:
java.lang.reflect.Method The found method or null

initializePrintMethods

protected void initializePrintMethods()
Common methods that should not require definition by the user.


invokeConstructor

protected java.lang.Object invokeConstructor(java.io.LineNumberReader rdr,
                                             java.lang.String className,
                                             java.lang.String parameterString,
                                             java.lang.Object obj)
                                      throws java.lang.ClassNotFoundException,
                                             java.lang.IllegalAccessException,
                                             java.lang.InstantiationException,
                                             java.io.IOException,
                                             java.lang.reflect.InvocationTargetException,
                                             java.lang.NoSuchMethodException,
                                             java.lang.NoSuchFieldException
Invoke a constrcutor to create a new instance.

Parameters:
rdr - The source of the script data.
className - The fully qualified name of the class we are constructing.
parameterString - Potential parameters for the constructor.
obj - The object that is the current target of the method builder.
Returns:
Object The new instance created by the constructor
Throws:
java.lang.reflect.InvocationTargetException
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.io.IOException
java.lang.NoSuchMethodException
java.lang.NoSuchFieldException

invokeMethod

protected java.lang.Object invokeMethod(java.lang.Object obj,
                                        java.io.LineNumberReader rdr,
                                        java.lang.String firstLine,
                                        int idx)
                                 throws java.io.IOException,
                                        java.lang.reflect.InvocationTargetException,
                                        java.lang.NoSuchMethodException,
                                        java.lang.IllegalAccessException,
                                        java.lang.ClassNotFoundException,
                                        java.lang.InstantiationException,
                                        java.lang.NoSuchFieldException
Actually invoke the method.

Parameters:
firstLine - The first line has likely been read already, so it is passed into this method.
idx - The index of the '(' character.
rdr - LineNumberReader Source from which this can get more information.
obj - The target object.
Returns:
The result of the method invocation.
Throws:
java.lang.reflect.InvocationTargetException - An exception during the method invocation.
java.io.IOException
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.NoSuchFieldException

invokeMethod

public java.lang.Object invokeMethod(java.lang.Object obj,
                                     java.io.LineNumberReader rdr)
                              throws java.io.IOException,
                                     java.lang.reflect.InvocationTargetException,
                                     java.lang.NoSuchMethodException,
                                     java.lang.ClassNotFoundException,
                                     java.lang.IllegalAccessException,
                                     java.lang.InstantiationException,
                                     java.lang.NoSuchFieldException
Invoke a method upon the target object. This will recursively call itself until a method is invoked with out a corresponding assignment.

Parameters:
rdr - java.io.LineNumberReader The source from which this class will read data to determine what methods to invoke.
obj - Object the target object.
Returns:
The result of the invoked method.
Throws:
java.lang.reflect.InvocationTargetException - An exception during the reflected invocation.
java.io.IOException
java.lang.NoSuchMethodException
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.lang.NoSuchFieldException

primitiveClass

protected java.lang.Class primitiveClass(java.lang.String className)
Get the primitive version of the className. If the className is not a primitive, return null.

Parameters:
className - String potentionally representing a primivitive value. Such as "int".
Returns:
A primitive Class or null.

setFieldReference

protected void setFieldReference(java.lang.String key,
                                 int idx,
                                 java.lang.Object value)
                          throws java.lang.IllegalAccessException,
                                 java.lang.NoSuchFieldException
Set the field reference to the parameter value.

Parameters:
value - Object to be set.
idx - The index of the period in the varDecl parameter.
key - The new fieldReference value
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchFieldException