bsh
Class Reflect

java.lang.Object
  extended bybsh.Reflect

class Reflect
extends java.lang.Object

All of the reflection API code lies here. It is in the form of static utilities. Maybe this belongs in LHS.java or a generic object wrapper class.


Constructor Summary
(package private) Reflect()
           
 
Method Summary
(package private) static java.lang.Object constructObject(java.lang.Class clas, java.lang.Object[] args)
          Primary object constructor This method is simpler than those that must resolve general method invocation because constructors are not inherited.
(package private) static java.lang.reflect.Method findAccessibleMethod(java.lang.Class clas, java.lang.String name, java.lang.Class[] types)
          Locate a version of the method with the exact signature specified that is accessible via a public interface or through a public superclass or - if accessibility is on - through any interface or superclass.
(package private) static java.lang.reflect.Constructor findMostSpecificConstructor(java.lang.Class[] idealMatch, java.lang.reflect.Constructor[] constructors)
           
(package private) static int findMostSpecificConstructorIndex(java.lang.Class[] idealMatch, java.lang.reflect.Constructor[] constructors)
           
(package private) static java.lang.reflect.Method findMostSpecificMethod(java.lang.String name, java.lang.Class[] idealMatch, java.lang.reflect.Method[] methods, boolean publicOnly)
          Implement JLS 15.11.2 for method resolution
(package private) static int findMostSpecificSignature(java.lang.Class[] idealMatch, java.lang.Class[][] candidates)
          Implement JLS 15.11.2 Return the index of the most specific arguments match or -1 if no match is found.
static java.lang.Class getArrayBaseType(java.lang.Class arrayClass)
          Returns the base type of an array Class.
static int getArrayDimensions(java.lang.Class arrayClass)
          returns the dimensionality of the Class returns 0 if the Class is not an array class
static java.lang.Object getIndex(java.lang.Object array, int index)
           
(package private) static LHS getLHSObjectField(java.lang.Object object, java.lang.String fieldName)
          Get an LHS reference to an object field.
(package private) static LHS getLHSStaticField(java.lang.Class clas, java.lang.String fieldName)
           
static java.lang.Object getObjectField(java.lang.Object object, java.lang.String fieldName)
           
static java.lang.Object getObjectProperty(java.lang.Object obj, java.lang.String propName)
           
static java.lang.Object getStaticField(java.lang.Class clas, java.lang.String fieldName)
           
static boolean hasObjectPropertyGetter(java.lang.Class clas, java.lang.String propName)
           
static boolean hasObjectPropertySetter(java.lang.Class clas, java.lang.String propName)
           
static java.lang.Object invokeCompiledCommand(java.lang.Class commandClass, java.lang.Object[] args, Interpreter interpreter, CallStack callstack)
          A command may be implemented as a compiled Java class containing one or more static invoke() methods of the correct signature.
static java.lang.Object invokeObjectMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, Interpreter interpreter, CallStack callstack, SimpleNode callerInfo)
          Invoke method on arbitrary object.
(package private) static java.lang.Object invokeOnMethod(java.lang.reflect.Method method, java.lang.Object object, java.lang.Object[] args)
          Invoke the Java method on the specified object.
static java.lang.Object invokeStaticMethod(BshClassManager bcm, java.lang.Class clas, java.lang.String methodName, java.lang.Object[] args)
          Invoke a method known to be static.
static java.lang.String normalizeClassName(java.lang.Class type)
          Return a more human readable version of the type name.
protected static java.lang.reflect.Field resolveExpectedJavaField(java.lang.Class clas, java.lang.String fieldName, boolean onlyStatic)
           
protected static java.lang.reflect.Method resolveExpectedJavaMethod(BshClassManager bcm, java.lang.Class clas, java.lang.Object object, java.lang.String name, java.lang.Object[] args, boolean onlyStatic)
          This method expects a non-null method from resolveJavaMethod().
protected static java.lang.reflect.Field resolveJavaField(java.lang.Class clas, java.lang.String fieldName, boolean onlyStatic)
           
protected static java.lang.reflect.Method resolveJavaMethod(BshClassManager bcm, java.lang.Class clas, java.lang.Object object, java.lang.String name, java.lang.Object[] args, boolean onlyStatic)
          Deprecated.  
protected static java.lang.reflect.Method resolveJavaMethod(BshClassManager bcm, java.lang.Class clas, java.lang.String name, java.lang.Class[] types, boolean onlyStatic)
           
static void setIndex(java.lang.Object array, int index, java.lang.Object val)
           
static void setObjectProperty(java.lang.Object obj, java.lang.String propName, java.lang.Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Reflect

Reflect()
Method Detail

invokeObjectMethod

public static java.lang.Object invokeObjectMethod(java.lang.Object object,
                                                  java.lang.String methodName,
                                                  java.lang.Object[] args,
                                                  Interpreter interpreter,
                                                  CallStack callstack,
                                                  SimpleNode callerInfo)
                                           throws ReflectError,
                                                  EvalError,
                                                  java.lang.reflect.InvocationTargetException
Invoke method on arbitrary object. invocation may be static (through the object instance) or dynamic. Object may be a bsh scripted object (This type).

Throws:
ReflectError
EvalError
java.lang.reflect.InvocationTargetException

invokeStaticMethod

public static java.lang.Object invokeStaticMethod(BshClassManager bcm,
                                                  java.lang.Class clas,
                                                  java.lang.String methodName,
                                                  java.lang.Object[] args)
                                           throws ReflectError,
                                                  UtilEvalError,
                                                  java.lang.reflect.InvocationTargetException
Invoke a method known to be static. No object instance is needed and there is no possibility of the method being a bsh scripted method.

Throws:
ReflectError
UtilEvalError
java.lang.reflect.InvocationTargetException

invokeOnMethod

static java.lang.Object invokeOnMethod(java.lang.reflect.Method method,
                                       java.lang.Object object,
                                       java.lang.Object[] args)
                                throws ReflectError,
                                       java.lang.reflect.InvocationTargetException
Invoke the Java method on the specified object.

Parameters:
args - may be null
Throws:
ReflectError
java.lang.reflect.InvocationTargetException

getIndex

public static java.lang.Object getIndex(java.lang.Object array,
                                        int index)
                                 throws ReflectError,
                                        UtilTargetError
Throws:
ReflectError
UtilTargetError

setIndex

public static void setIndex(java.lang.Object array,
                            int index,
                            java.lang.Object val)
                     throws ReflectError,
                            UtilTargetError
Throws:
ReflectError
UtilTargetError

getStaticField

public static java.lang.Object getStaticField(java.lang.Class clas,
                                              java.lang.String fieldName)
                                       throws UtilEvalError,
                                              ReflectError
Throws:
UtilEvalError
ReflectError

getObjectField

public static java.lang.Object getObjectField(java.lang.Object object,
                                              java.lang.String fieldName)
                                       throws UtilEvalError,
                                              ReflectError
Throws:
UtilEvalError
ReflectError

getLHSStaticField

static LHS getLHSStaticField(java.lang.Class clas,
                             java.lang.String fieldName)
                      throws UtilEvalError,
                             ReflectError
Throws:
UtilEvalError
ReflectError

getLHSObjectField

static LHS getLHSObjectField(java.lang.Object object,
                             java.lang.String fieldName)
                      throws UtilEvalError,
                             ReflectError
Get an LHS reference to an object field. This method also deals with the field style property access. In the field does not exist we check for a property setter.

Throws:
UtilEvalError
ReflectError

resolveJavaField

protected static java.lang.reflect.Field resolveJavaField(java.lang.Class clas,
                                                          java.lang.String fieldName,
                                                          boolean onlyStatic)
                                                   throws UtilEvalError
Throws:
UtilEvalError

resolveExpectedJavaField

protected static java.lang.reflect.Field resolveExpectedJavaField(java.lang.Class clas,
                                                                  java.lang.String fieldName,
                                                                  boolean onlyStatic)
                                                           throws UtilEvalError,
                                                                  ReflectError
Throws:
ReflectError - if the field is not found.
UtilEvalError

resolveExpectedJavaMethod

protected static java.lang.reflect.Method resolveExpectedJavaMethod(BshClassManager bcm,
                                                                    java.lang.Class clas,
                                                                    java.lang.Object object,
                                                                    java.lang.String name,
                                                                    java.lang.Object[] args,
                                                                    boolean onlyStatic)
                                                             throws ReflectError,
                                                                    UtilEvalError
This method expects a non-null method from resolveJavaMethod(). If the method is not found it throws a descriptive ReflectError.

Throws:
ReflectError
UtilEvalError

resolveJavaMethod

protected static java.lang.reflect.Method resolveJavaMethod(BshClassManager bcm,
                                                            java.lang.Class clas,
                                                            java.lang.Object object,
                                                            java.lang.String name,
                                                            java.lang.Object[] args,
                                                            boolean onlyStatic)
                                                     throws UtilEvalError
Deprecated.  

The full blown resolver method. All other method invocation methods delegate to this. The method may be static or dynamic unless onlyStatic is set (in which case object may be null). If onlyStatic is set the only static methods will be located.

This method performs Java method caching internally.

Parameters:
onlyStatic - The method located must be static, the object param may be null.
Returns:
the method or null if no matching method was found.
Throws:
UtilEvalError

resolveJavaMethod

protected static java.lang.reflect.Method resolveJavaMethod(BshClassManager bcm,
                                                            java.lang.Class clas,
                                                            java.lang.String name,
                                                            java.lang.Class[] types,
                                                            boolean onlyStatic)
                                                     throws UtilEvalError
Throws:
UtilEvalError

findAccessibleMethod

static java.lang.reflect.Method findAccessibleMethod(java.lang.Class clas,
                                                     java.lang.String name,
                                                     java.lang.Class[] types)
                                              throws UtilEvalError
Locate a version of the method with the exact signature specified that is accessible via a public interface or through a public superclass or - if accessibility is on - through any interface or superclass. In the normal (non-accessible) case this still solves the problem that arises when a package private class or private inner class implements a public interface or derives from a public type.

Returns:
null on not found
Throws:
UtilEvalError

constructObject

static java.lang.Object constructObject(java.lang.Class clas,
                                        java.lang.Object[] args)
                                 throws ReflectError,
                                        java.lang.reflect.InvocationTargetException
Primary object constructor This method is simpler than those that must resolve general method invocation because constructors are not inherited.

Throws:
ReflectError
java.lang.reflect.InvocationTargetException

findMostSpecificMethod

static java.lang.reflect.Method findMostSpecificMethod(java.lang.String name,
                                                       java.lang.Class[] idealMatch,
                                                       java.lang.reflect.Method[] methods,
                                                       boolean publicOnly)
Implement JLS 15.11.2 for method resolution

Returns:
null on no match

findMostSpecificConstructor

static java.lang.reflect.Constructor findMostSpecificConstructor(java.lang.Class[] idealMatch,
                                                                 java.lang.reflect.Constructor[] constructors)

findMostSpecificConstructorIndex

static int findMostSpecificConstructorIndex(java.lang.Class[] idealMatch,
                                            java.lang.reflect.Constructor[] constructors)

findMostSpecificSignature

static int findMostSpecificSignature(java.lang.Class[] idealMatch,
                                     java.lang.Class[][] candidates)
Implement JLS 15.11.2 Return the index of the most specific arguments match or -1 if no match is found.


hasObjectPropertyGetter

public static boolean hasObjectPropertyGetter(java.lang.Class clas,
                                              java.lang.String propName)

hasObjectPropertySetter

public static boolean hasObjectPropertySetter(java.lang.Class clas,
                                              java.lang.String propName)

getObjectProperty

public static java.lang.Object getObjectProperty(java.lang.Object obj,
                                                 java.lang.String propName)
                                          throws UtilEvalError,
                                                 ReflectError
Throws:
UtilEvalError
ReflectError

setObjectProperty

public static void setObjectProperty(java.lang.Object obj,
                                     java.lang.String propName,
                                     java.lang.Object value)
                              throws ReflectError,
                                     UtilEvalError
Throws:
ReflectError
UtilEvalError

normalizeClassName

public static java.lang.String normalizeClassName(java.lang.Class type)
Return a more human readable version of the type name. Specifically, array types are returned with postfix "[]" dimensions. e.g. return "int []" for integer array instead of "class [I" as would be returned by Class getName() in that case.


getArrayDimensions

public static int getArrayDimensions(java.lang.Class arrayClass)
returns the dimensionality of the Class returns 0 if the Class is not an array class


getArrayBaseType

public static java.lang.Class getArrayBaseType(java.lang.Class arrayClass)
                                        throws ReflectError
Returns the base type of an array Class. throws ReflectError if the Class is not an array class.

Throws:
ReflectError

invokeCompiledCommand

public static java.lang.Object invokeCompiledCommand(java.lang.Class commandClass,
                                                     java.lang.Object[] args,
                                                     Interpreter interpreter,
                                                     CallStack callstack)
                                              throws UtilEvalError
A command may be implemented as a compiled Java class containing one or more static invoke() methods of the correct signature. The invoke() methods must accept two additional leading arguments of the interpreter and callstack, respectively. e.g. invoke(interpreter, callstack, ... ) This method adds the arguments and invokes the static method, returning the result.

Throws:
UtilEvalError