Top Description Constructors Methods
bsh

pack-priv Class Reflect

extends Object
Class Inheritance
Imports
java.lang.reflect.*, java.util.Vector

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

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private static String
accessorName(String getorset, String propName)

private static Vector<E>
addCandidates(Method[] methods, String methodName, int numArgs, boolean publicOnly, Vector<E> candidates)

private static ReflectError
cantFindConstructor(Class<T> clas, Class<T>[] types)

private static void
checkFoundStaticMethod(Method method, boolean staticOnly, Class<T> clas)

pack-priv static Object
constructObject(Class<T> clas, Object[] args)

Primary object constructor This method is simpler than those that must resolve general method invocation because constructors are not inherited.

private static Field

Returns:

the Field or throws NoSuchFieldException
findAccessibleField
(Class<T> clas, String fieldName)

Used when accessibility capability is available to locate an occurrance of the field in the most derived class or superclass and set its accessibility flag.

pack-priv static Constructor<T>
findMostSpecificConstructor(Class<T>[] idealMatch, Constructor<T>[] constructors)

pack-priv static int
findMostSpecificConstructorIndex(Class<T>[] idealMatch, Constructor<T>[] constructors)

pack-priv static Method
findMostSpecificMethod(Class<T>[] idealMatch, Method[]
the set of candidate method which differ only in the types of their arguments.
methods
)

Find the best match for signature idealMatch.

pack-priv static int

Returns:

the index of the most specific candidate
findMostSpecificSignature
(Class<T>[] idealMatch, Class<T>[][] candidates)

Implement JLS 15.11.2 Return the index of the most specific arguments match or -1 if no match is found.

private static Method

Returns:

the method or null for not found
findOverloadedMethod
(Class<T> baseClass, String methodName, Class<T>[] types, boolean publicOnly)

Get the candidate methods by searching the class and interface graph of baseClass and resolve the most specific.

private static Vector<E>

Returns:

the candidate methods vector
gatherMethodsRecursive
(Class<T> baseClass, String methodName, int numArgs, boolean publicOnly, Vector<E> candidates)

Accumulate all methods, optionally including non-public methods, class and interface, in the inheritence tree of baseClass.

public static Class<T>
getArrayBaseType(Class<T> arrayClass)

Returns the base type of an array Class.

public static int
getArrayDimensions(Class<T> arrayClass)

returns the dimensionality of the Class returns 0 if the Class is not an array class

pack-priv static Method[]
getCandidateMethods(Class<T> baseClass, String methodName, int numArgs, boolean publicOnly)

Climb the class and interface inheritence graph of the type and collect all methods matching the specified name and criterion.

private static Object
getFieldValue(Class<T> clas, Object object, String fieldName, boolean staticOnly)

public static Object
getIndex(Object array, int index)

pack-priv static LHS
getLHSObjectField(Object object, String fieldName)

Get an LHS reference to an object field.

pack-priv static LHS
getLHSStaticField(Class<T> clas, String fieldName)

public static Object
getObjectFieldValue(Object object, String fieldName)

public static Object
public static Object
getStaticFieldValue(Class<T> clas, String fieldName)

public static boolean
public static boolean
public static Object
invokeCompiledCommand(Class<T> commandClass, 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.

pack-priv static Object
invokeMethod(Method method, Object object, Object[]
may be null
args
)

Invoke the Java method on the specified object, performing needed type mappings on arguments and return values.

public static Object

Returns:

the result of the method call
invokeObjectMethod
(Object object, String methodName, Object[] args, Interpreter interpreter, CallStack callstack, SimpleNode callerInfo)

Invoke method on arbitrary object instance.

public static Object
invokeStaticMethod(BshClassManager bcm, Class<T> clas, String methodName, Object[] args)

Invoke a method known to be static.

private static boolean
private static boolean
private static boolean
private static boolean
private static void
logInvokeMethod(String msg, Method method, Object[] args)

public static String
normalizeClassName(Class<T> type)

Return a more human readable version of the type name.

protected static Field
resolveExpectedJavaField(Class<T> clas, String fieldName, boolean staticOnly)

protected static Method
resolveExpectedJavaMethod(BshClassManager bcm, Class<T> clas, Object object, String name, Object[] args, boolean staticOnly)

This method wraps resolveJavaMethod() and expects a non-null method result.

protected static Field
resolveJavaField(Class<T> clas, String fieldName, boolean staticOnly)

protected static Method

Returns:

the method or null if no matching method was found.
resolveJavaMethod
(BshClassManager bcm, Class<T> clas, String name, Class<T>[] types, boolean
The method located must be static, the object param may be null.
staticOnly
)

The full blown resolver method.

public static void
setIndex(Object array, int index, Object val)

public static void
setObjectProperty(Object obj, String propName, Object value)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Reflectback to summary
pack-priv Reflect()

Method Detail

accessorNameback to summary
private static String accessorName(String getorset, String propName)
addCandidatesback to summary
private static Vector<E> addCandidates(Method[] methods, String methodName, int numArgs, boolean publicOnly, Vector<E> candidates)
cantFindConstructorback to summary
private static ReflectError cantFindConstructor(Class<T> clas, Class<T>[] types)
checkFoundStaticMethodback to summary
private static void checkFoundStaticMethod(Method method, boolean staticOnly, Class<T> clas) throws UtilEvalError
constructObjectback to summary
pack-priv static Object constructObject(Class<T> clas, Object[] args) throws ReflectError, InvocationTargetException

Primary object constructor This method is simpler than those that must resolve general method invocation because constructors are not inherited.

This method determines whether to attempt to use non-public constructors based on Capabilities.haveAccessibility() and will set the accessibilty flag on the method as necessary.

findAccessibleFieldback to summary
private static Field findAccessibleField(Class<T> clas, String fieldName) throws UtilEvalError, NoSuchFieldException

Used when accessibility capability is available to locate an occurrance of the field in the most derived class or superclass and set its accessibility flag. Note that this method is not needed in the simple non accessible case because we don't have to hunt for fields. Note that classes may declare overlapping private fields, so the distinction about the most derived is important. Java doesn't normally allow this kind of access (super won't show private variables) so there is no real syntax for specifying which class scope to use...

Returns:Field

the Field or throws NoSuchFieldException

Exceptions
NoSuchFieldException:
if the field is not found
findMostSpecificConstructorback to summary
pack-priv static Constructor<T> findMostSpecificConstructor(Class<T>[] idealMatch, Constructor<T>[] constructors)
findMostSpecificConstructorIndexback to summary
pack-priv static int findMostSpecificConstructorIndex(Class<T>[] idealMatch, Constructor<T>[] constructors)
findMostSpecificMethodback to summary
pack-priv static Method findMostSpecificMethod(Class<T>[] idealMatch, Method[] methods)

Find the best match for signature idealMatch. It is assumed that the methods array holds only valid candidates (e.g. method name and number of args already matched). This method currently does not take into account Java 5 covariant return types... which I think will require that we find the most derived return type of otherwise identical best matches.

Parameters
methods:Method[]

the set of candidate method which differ only in the types of their arguments.

See Also
findMostSpecificSignature(Class[], Class[][])
findMostSpecificSignatureback to summary
pack-priv static int findMostSpecificSignature(Class<T>[] idealMatch, Class<T>[][] candidates)

Implement JLS 15.11.2 Return the index of the most specific arguments match or -1 if no match is found. This method is used by both methods and constructors (which unfortunately don't share a common interface for signature info).

Returns:int

the index of the most specific candidate

findOverloadedMethodback to summary
private static Method findOverloadedMethod(Class<T> baseClass, String methodName, Class<T>[] types, boolean publicOnly)

Get the candidate methods by searching the class and interface graph of baseClass and resolve the most specific.

Returns:Method

the method or null for not found

gatherMethodsRecursiveback to summary
private static Vector<E> gatherMethodsRecursive(Class<T> baseClass, String methodName, int numArgs, boolean publicOnly, Vector<E> candidates)

Accumulate all methods, optionally including non-public methods, class and interface, in the inheritence tree of baseClass. This method is analogous to Class getMethods() which returns all public methods in the inheritence tree. In the normal (non-accessible) case this also addresses the problem that arises when a package private class or private inner class implements a public interface or derives from a public type. In other words, sometimes we'll find public methods that we can't use directly and we have to find the same public method in a parent class or interface.

Returns:Vector<E>

the candidate methods vector

getArrayBaseTypeback to summary
public static Class<T> getArrayBaseType(Class<T> arrayClass) throws ReflectError

Returns the base type of an array Class. throws ReflectError if the Class is not an array class.

getArrayDimensionsback to summary
public static int getArrayDimensions(Class<T> arrayClass)

returns the dimensionality of the Class returns 0 if the Class is not an array class

getCandidateMethodsback to summary
pack-priv static Method[] getCandidateMethods(Class<T> baseClass, String methodName, int numArgs, boolean publicOnly)

Climb the class and interface inheritence graph of the type and collect all methods matching the specified name and criterion. If publicOnly is true then only public methods in *public* classes or interfaces will be returned. In the normal (non-accessible) case this addresses the problem that arises when a package private class or private inner class implements a public interface or derives from a public type.

This method primarily just delegates to gatherMethodsRecursive()

See Also
gatherMethodsRecursive(Class, String, int, boolean, java.util.Vector)
getFieldValueback to summary
private static Object getFieldValue(Class<T> clas, Object object, String fieldName, boolean staticOnly) throws UtilEvalError, ReflectError
getIndexback to summary
public static Object getIndex(Object array, int index) throws ReflectError, UtilTargetError
getLHSObjectFieldback to summary
pack-priv static LHS getLHSObjectField(Object object, 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.

getLHSStaticFieldback to summary
pack-priv static LHS getLHSStaticField(Class<T> clas, String fieldName) throws UtilEvalError, ReflectError
getObjectFieldValueback to summary
public static Object getObjectFieldValue(Object object, String fieldName) throws UtilEvalError, ReflectError
getObjectPropertyback to summary
public static Object getObjectProperty(Object obj, String propName) throws UtilEvalError, ReflectError
getStaticFieldValueback to summary
public static Object getStaticFieldValue(Class<T> clas, String fieldName) throws UtilEvalError, ReflectError
hasObjectPropertyGetterback to summary
public static boolean hasObjectPropertyGetter(Class<T> clas, String propName)
hasObjectPropertySetterback to summary
public static boolean hasObjectPropertySetter(Class<T> clas, String propName)
invokeCompiledCommandback to summary
public static Object invokeCompiledCommand(Class<T> commandClass, 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.

invokeMethodback to summary
pack-priv static Object invokeMethod(Method method, Object object, Object[] args) throws ReflectError, InvocationTargetException

Invoke the Java method on the specified object, performing needed type mappings on arguments and return values.

Parameters
args:Object[]

may be null

invokeObjectMethodback to summary
public static Object invokeObjectMethod(Object object, String methodName, Object[] args, Interpreter interpreter, CallStack callstack, SimpleNode callerInfo) throws ReflectError, EvalError, InvocationTargetException

Invoke method on arbitrary object instance. invocation may be static (through the object instance) or dynamic. Object may be a bsh scripted object (bsh.This type).

Returns:Object

the result of the method call

invokeStaticMethodback to summary
public static Object invokeStaticMethod(BshClassManager bcm, Class<T> clas, String methodName, Object[] args) throws ReflectError, UtilEvalError, 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.

isPublicback to summary
private static boolean isPublic(Class<T> c)
isPublicback to summary
private static boolean isPublic(Method m)
isPublicback to summary
private static boolean isPublic(Constructor<T> c)
isStaticback to summary
private static boolean isStatic(Method m)
logInvokeMethodback to summary
private static void logInvokeMethod(String msg, Method method, Object[] args)
normalizeClassNameback to summary
public static String normalizeClassName(Class<T> 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.

resolveExpectedJavaFieldback to summary
protected static Field resolveExpectedJavaField(Class<T> clas, String fieldName, boolean staticOnly) throws UtilEvalError, ReflectError
Exceptions
ReflectError:
if the field is not found.
resolveExpectedJavaMethodback to summary
protected static Method resolveExpectedJavaMethod(BshClassManager bcm, Class<T> clas, Object object, String name, Object[] args, boolean staticOnly) throws ReflectError, UtilEvalError

This method wraps resolveJavaMethod() and expects a non-null method result. If the method is not found it throws a descriptive ReflectError.

resolveJavaFieldback to summary
protected static Field resolveJavaField(Class<T> clas, String fieldName, boolean staticOnly) throws UtilEvalError
resolveJavaMethodback to summary
protected static Method resolveJavaMethod(BshClassManager bcm, Class<T> clas, String name, Class<T>[] types, boolean staticOnly) throws UtilEvalError

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

This method performs caching (caches discovered methods through the class manager and utilizes cached methods.)

This method determines whether to attempt to use non-public methods based on Capabilities.haveAccessibility() and will set the accessibilty flag on the method as necessary.

If, when directed to find a static method, this method locates a more specific matching instance method it will throw a descriptive exception analogous to the error that the Java compiler would produce.

Note

as of 2.0.x this is a problem because there is no way to work around this with a cast.

Parameters
staticOnly:boolean

The method located must be static, the object param may be null.

Returns:Method

the method or null if no matching method was found.

setIndexback to summary
public static void setIndex(Object array, int index, Object val) throws ReflectError, UtilTargetError
setObjectPropertyback to summary
public static void setObjectProperty(Object obj, String propName, Object value) throws ReflectError, UtilEvalError