org.onemind.commons.java.lang.reflect
Class ReflectUtils

java.lang.Object
  extended by org.onemind.commons.java.lang.reflect.ReflectUtils

public final class ReflectUtils
extends java.lang.Object

Reflection related utilities

Version:
$Id: ReflectUtils.java,v 1.12 2006/08/01 23:57:03 thlee Exp $ $Name: $
Author:
TiongHiang Lee (thlee@onemindsoft.org)

Nested Class Summary
private static class ReflectUtils.MethodKey
          The method key
 
Field Summary
private static java.util.Map _classCache
          the lookup cache *
private static boolean _classCaching
          class caching setting
private static java.util.logging.Logger _logger
          the logger *
private static java.util.Map _methodCache
          the method cache
private static boolean _methodCaching
          method caching setting
private static java.util.Map WIDENABLES
          keep a primitive class and their compatible types
 
Constructor Summary
private ReflectUtils()
          
 
Method Summary
static int computeCompatibalityScore(java.lang.Class[] methodTypes, java.lang.Class[] argTypes)
          Return whether the types of arguments is compatible with the argument type spec of a method
private static int computeWideningScore(java.lang.Class primitiveClass, java.lang.Class c)
          Check if class c can be widen to targetClass and return the score.
static java.lang.Class getClass(java.lang.String name)
          Get the class
static java.lang.reflect.Constructor getConstructor(java.lang.Class type, java.lang.Object[] args)
          Get the constructor of the type given the arguments to the constructor
static java.lang.reflect.Method getInterfaceMethod(java.lang.Class[] c, java.lang.String methodName, java.lang.Class[] argTypes)
          Resolve the method from the interfaces
static java.lang.reflect.Method getMethod(java.lang.Class type, java.lang.String methodName, java.lang.Class[] argTypes)
          Get a named method of class type with the argument type compatible with the argument passed in.
static java.lang.reflect.Method getMethod(java.lang.Class type, java.lang.String methodName, java.lang.Object[] args)
          Get a named method of class type with the argument type compatible with the argument passed in.
static java.lang.Object invoke(java.lang.Object o, java.lang.String methodName, java.lang.Object[] args)
          Invoke a named method on the object using the arguments
static boolean isCompatible(java.lang.Class[] types, java.lang.Class[] argTypes)
           
static boolean isCompatible(java.lang.Class[] types, java.lang.Object[] args)
          Return whether the argument objects is compatible with the argument types specification
static boolean isPrimitiveCompatible(java.lang.Class primitiveClass, java.lang.Class clazz)
          Return true if primitiveClass and clazz is both primitive and clazz is primitive compatible with primitiveClass using java rules (unwrapping or widening)
static boolean isPrimitiveInstance(java.lang.Class primitiveClass, java.lang.Object obj)
          Return whether a given object is a primitive or compatible (through unwrapping and widening) instance of primitiveClass
static java.lang.Object newInstance(java.lang.Class type, java.lang.Object[] args)
          Create a new instance of the class type with the arguments to constructor
static java.lang.reflect.Constructor searchConstructor(java.lang.Class type, java.lang.Class[] argTypes)
          Search for a particular constructor based on arg types classes
private static java.lang.reflect.Method searchMethod(java.lang.Class type, java.lang.String methodName, java.lang.Class[] argTypes)
          Search a named method of class type through the class's hierachy
protected static void setClassCaching(boolean caching)
          Set the classCaching
protected static void setMethodCaching(boolean caching)
          Set the _methodCaching
static java.lang.Class[] toArgTypes(java.lang.Object[] args)
          Construct the argument type class array from a list of arg objects
static java.lang.String toMethodString(java.lang.String methodName, java.lang.Object[] args)
          To the method representation string e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_logger

private static final java.util.logging.Logger _logger
the logger *


_classCache

private static final java.util.Map _classCache
the lookup cache *


_methodCache

private static final java.util.Map _methodCache
the method cache


_classCaching

private static boolean _classCaching
class caching setting


_methodCaching

private static boolean _methodCaching
method caching setting


WIDENABLES

private static final java.util.Map WIDENABLES
keep a primitive class and their compatible types

Constructor Detail

ReflectUtils

private ReflectUtils()

Method Detail

toArgTypes

public static final java.lang.Class[] toArgTypes(java.lang.Object[] args)
Construct the argument type class array from a list of arg objects

Parameters:
args - the arguments
Returns:
the class array

getClass

public static final java.lang.Class getClass(java.lang.String name)
                                      throws java.lang.ClassNotFoundException
Get the class

Parameters:
name - the name of the class
Returns:
the class
Throws:
java.lang.ClassNotFoundException - if the class cannot be found

getConstructor

public static final java.lang.reflect.Constructor getConstructor(java.lang.Class type,
                                                                 java.lang.Object[] args)
                                                          throws java.lang.NoSuchMethodException
Get the constructor of the type given the arguments to the constructor

Parameters:
type - the type
args - the arguments
Returns:
the constructor
Throws:
java.lang.NoSuchMethodException - if the constructor cannot be found

toMethodString

public static final java.lang.String toMethodString(java.lang.String methodName,
                                                    java.lang.Object[] args)
To the method representation string e.g. toString()

Parameters:
methodName - the method
args - the arguments
Returns:
the method representation string

searchConstructor

public static final java.lang.reflect.Constructor searchConstructor(java.lang.Class type,
                                                                    java.lang.Class[] argTypes)
Search for a particular constructor based on arg types classes

Parameters:
type - the type
argTypes - the argument types
Returns:
the constructor

isCompatible

public static final boolean isCompatible(java.lang.Class[] types,
                                         java.lang.Object[] args)
Return whether the argument objects is compatible with the argument types specification

Parameters:
types - the argument types
args - the arguments
Returns:
true if compatible

isCompatible

public static final boolean isCompatible(java.lang.Class[] types,
                                         java.lang.Class[] argTypes)

computeCompatibalityScore

public static final int computeCompatibalityScore(java.lang.Class[] methodTypes,
                                                  java.lang.Class[] argTypes)
Return whether the types of arguments is compatible with the argument type spec of a method

Parameters:
methodTypes - the argument type spec of a method
argTypes - the argument type
Returns:
true if compatible

newInstance

public static final java.lang.Object newInstance(java.lang.Class type,
                                                 java.lang.Object[] args)
                                          throws java.lang.IllegalAccessException,
                                                 java.lang.InstantiationException,
                                                 java.lang.reflect.InvocationTargetException,
                                                 java.lang.NoSuchMethodException
Create a new instance of the class type with the arguments to constructor

Parameters:
type - the type
args - the argument
Returns:
the new instance
Throws:
java.lang.IllegalAccessException - if there's access problem
java.lang.InstantiationException - if there's instantiation problem
java.lang.reflect.InvocationTargetException - if there's target exception
java.lang.NoSuchMethodException - if there's no such constructor

invoke

public static final java.lang.Object invoke(java.lang.Object o,
                                            java.lang.String methodName,
                                            java.lang.Object[] args)
                                     throws java.lang.NoSuchMethodException,
                                            java.lang.IllegalAccessException,
                                            java.lang.reflect.InvocationTargetException
Invoke a named method on the object using the arguments

Parameters:
o - the object
methodName - the name of the method
args - the arguments
Returns:
the object return by the invocation
Throws:
java.lang.NoSuchMethodException - if there's no such method
java.lang.IllegalAccessException - if there's access problem
java.lang.reflect.InvocationTargetException - if there's target problem

getInterfaceMethod

public static final java.lang.reflect.Method getInterfaceMethod(java.lang.Class[] c,
                                                                java.lang.String methodName,
                                                                java.lang.Class[] argTypes)
Resolve the method from the interfaces

Parameters:
c - the class
methodName - the method
argTypes - the arg types
Returns:
the method or null

getMethod

public static final java.lang.reflect.Method getMethod(java.lang.Class type,
                                                       java.lang.String methodName,
                                                       java.lang.Object[] args)
                                                throws java.lang.NoSuchMethodException
Get a named method of class type with the argument type compatible with the argument passed in.

Parameters:
type - the class
methodName - the method name
args - the arguments
Returns:
the method
Throws:
java.lang.NoSuchMethodException - if the method cannot be found

getMethod

public static final java.lang.reflect.Method getMethod(java.lang.Class type,
                                                       java.lang.String methodName,
                                                       java.lang.Class[] argTypes)
                                                throws java.lang.NoSuchMethodException
Get a named method of class type with the argument type compatible with the argument passed in.

Parameters:
type - the class
methodName - the method name
args - the arguments
Returns:
the method
Throws:
java.lang.NoSuchMethodException - if the method cannot be found

searchMethod

private static final java.lang.reflect.Method searchMethod(java.lang.Class type,
                                                           java.lang.String methodName,
                                                           java.lang.Class[] argTypes)
Search a named method of class type through the class's hierachy

Parameters:
type - the class
methodName - the method name
argTypes - the argument types
Returns:
the method

setClassCaching

protected static final void setClassCaching(boolean caching)
Set the classCaching

Parameters:
caching - true to turn on class caching

setMethodCaching

protected static final void setMethodCaching(boolean caching)
Set the _methodCaching

Parameters:
caching - true to turn on method caching

isPrimitiveInstance

public static final boolean isPrimitiveInstance(java.lang.Class primitiveClass,
                                                java.lang.Object obj)
Return whether a given object is a primitive or compatible (through unwrapping and widening) instance of primitiveClass

Parameters:
primitiveClass - the primitive class
obj - the object
Returns:
true if is instance

computeWideningScore

private static final int computeWideningScore(java.lang.Class primitiveClass,
                                              java.lang.Class c)
Check if class c can be widen to targetClass and return the score. Return 2 if c==primitiveClass, 1 if c can be widened, or 0 if c cannot be widened.

Parameters:
primitiveClass -
c -
Returns:

isPrimitiveCompatible

public static final boolean isPrimitiveCompatible(java.lang.Class primitiveClass,
                                                  java.lang.Class clazz)
Return true if primitiveClass and clazz is both primitive and clazz is primitive compatible with primitiveClass using java rules (unwrapping or widening)

Parameters:
primitiveClass -
clazz -
Returns: