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

java.lang.Object
  extended byorg.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)

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
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
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. toString()
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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
Returns:
the method
Throws:
java.lang.NoSuchMethodException - if the method cannot be found

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

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:


Copyright © 2004-2006 . All Rights Reserved.