Class ReflectUtils
- java.lang.Object
-
- 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
Nested Classes Modifier and Type Class Description private static class
ReflectUtils.MethodKey
The method key
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map
_classCache
the lookup cache *private static boolean
_classCaching
class caching settingprivate static java.util.logging.Logger
_logger
the logger *private static java.util.Map
_methodCache
the method cacheprivate static boolean
_methodCaching
method caching settingprivate static java.util.Map
WIDENABLES
keep a primitive class and their compatible types
-
Constructor Summary
Constructors Modifier Constructor Description private
ReflectUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 methodprivate 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 classstatic java.lang.reflect.Constructor
getConstructor(java.lang.Class type, java.lang.Object[] args)
Get the constructor of the type given the arguments to the constructorstatic java.lang.reflect.Method
getInterfaceMethod(java.lang.Class[] c, java.lang.String methodName, java.lang.Class[] argTypes)
Resolve the method from the interfacesstatic 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 argumentsstatic 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 specificationstatic 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 primitiveClassstatic java.lang.Object
newInstance(java.lang.Class type, java.lang.Object[] args)
Create a new instance of the class type with the arguments to constructorstatic java.lang.reflect.Constructor
searchConstructor(java.lang.Class type, java.lang.Class[] argTypes)
Search for a particular constructor based on arg types classesprivate 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 hierachyprotected static void
setClassCaching(boolean caching)
Set the classCachingprotected static void
setMethodCaching(boolean caching)
Set the _methodCachingstatic java.lang.Class[]
toArgTypes(java.lang.Object[] args)
Construct the argument type class array from a list of arg objectsstatic java.lang.String
toMethodString(java.lang.String methodName, java.lang.Object[] args)
To the method representation string e.g.
-
-
-
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
-
-
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 typeargs
- 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 methodargs
- 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 typeargTypes
- 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 typesargs
- 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 methodargTypes
- 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 typeargs
- the argument- Returns:
- the new instance
- Throws:
java.lang.IllegalAccessException
- if there's access problemjava.lang.InstantiationException
- if there's instantiation problemjava.lang.reflect.InvocationTargetException
- if there's target exceptionjava.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 objectmethodName
- the name of the methodargs
- the arguments- Returns:
- the object return by the invocation
- Throws:
java.lang.NoSuchMethodException
- if there's no such methodjava.lang.IllegalAccessException
- if there's access problemjava.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 classmethodName
- the methodargTypes
- 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 classmethodName
- the method nameargs
- 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 classmethodName
- the method nameargs
- 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 classmethodName
- the method nameargTypes
- 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 classobj
- 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:
-
-