org.openbp.common
Class ReflectUtil

java.lang.Object
  extended by org.openbp.common.ReflectUtil

public final class ReflectUtil
extends java.lang.Object

This class contains various static utility methods concerning reflections and class processing.

Author:
Heiko Erhardt

Field Summary
static int CM_PRINT
          Print error message to standard error
static int CM_THROW
          Re-throw any exceptions
 
Method Summary
static java.lang.Object callMethod(java.lang.Object o, java.lang.reflect.Method method, java.lang.Object[] argValues, int errorHandlingMode)
          Calls a method using Java reflection.
static java.lang.Object callMethod(java.lang.Object o, java.lang.String methodName, java.lang.Class[] argClasses, java.lang.Object[] argValues, int errorHandlingMode)
          Calls a method using Java reflection.
static java.lang.Class determineClass(java.lang.String stringRepresentation)
          Determines the class object by type string.
static java.lang.reflect.Method findByQualifiedMethodSignature(java.lang.String signature)
          Finds a method according to its signature.
static java.lang.reflect.Method findByUnqualifiedMethodSignature(java.lang.Class objectClass, java.lang.String signature)
          Finds a method according to its signature.
static java.lang.Class findClassInPackageList(java.lang.String className, java.util.List packageList)
          Searches for a particular class in a list of packages.
static java.lang.Class findClassInPackageList(java.lang.String className, java.util.List packageList, java.lang.ClassLoader cl)
          Searches for a particular class in a list of packages.
static java.lang.reflect.Field getFieldOrNull(java.lang.Class cls, java.lang.String name)
          Get a field using reflections.
static java.lang.reflect.Method getMethodOrNull(java.lang.Class cls, java.lang.String name, java.lang.Class[] args)
          Get a method using reflections.
static java.lang.String getPrintableClassName(java.lang.Class cls)
          Gets a printable form of a class name.
static java.lang.String getQualifiedMethodSignature(java.lang.reflect.Method method)
          Creates a string that specifies the method signature containing a fullly qualified method name.
static java.lang.String getUnqualifiedMethodSignature(java.lang.reflect.Method method)
          Creates a string that specifies the method signature containing an unqualified method name.
static java.lang.Object instantiate(java.lang.Class cls, java.lang.ClassLoader cl, java.lang.Class type, java.lang.String displayName)
          Creates an object instance given its class using the default constructor.
static java.lang.Object instantiate(java.lang.Class cls, java.lang.Class type, java.lang.String displayName)
          Creates an object instance given its class using the default constructor.
static java.lang.Object instantiate(java.lang.String className, java.lang.ClassLoader cl, java.lang.Class type, java.lang.String displayName)
          Creates an object instance by its fully qualified class name using the default constructor.
static java.lang.Object instantiate(java.lang.String className, java.lang.Class type, java.lang.String displayName)
          Creates a class by its fully qualified class name using the default constructor (convenience method).
static java.lang.Object instantiateByConstructor(java.lang.Class cls, java.lang.Class[] argTypes, java.lang.Object[] args, java.lang.Class type, java.lang.String displayName, boolean allowDefaultConstructor)
          Creates an object instance given its class using the constructor with the given argument types.
static java.lang.Class loadClass(java.lang.String className)
          Loads a class by its fully qualified class name.
static java.lang.Class loadClass(java.lang.String className, java.lang.ClassLoader cl)
          Loads a class by its fully qualified class name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CM_PRINT

public static final int CM_PRINT
Print error message to standard error

See Also:
Constant Field Values

CM_THROW

public static final int CM_THROW
Re-throw any exceptions

See Also:
Constant Field Values
Method Detail

findClassInPackageList

public static java.lang.Class findClassInPackageList(java.lang.String className,
                                                     java.util.List packageList)
Searches for a particular class in a list of packages.

Parameters:
className - Unqualified name of the class
packageList - List of package to search the class in (contains strings)
Returns:
The class or null if no such class could be found

findClassInPackageList

public static java.lang.Class findClassInPackageList(java.lang.String className,
                                                     java.util.List packageList,
                                                     java.lang.ClassLoader cl)
Searches for a particular class in a list of packages.

Parameters:
className - Unqualified name of the class
packageList - List of package to search the class in (contains strings)
cl - The class loader which shall load the class or null for the default loader
Returns:
The class or null if no such class could be found

loadClass

public static java.lang.Class loadClass(java.lang.String className)
Loads a class by its fully qualified class name.

Parameters:
className - Qualified class name
Returns:
The class or null if no such class exists

loadClass

public static java.lang.Class loadClass(java.lang.String className,
                                        java.lang.ClassLoader cl)
Loads a class by its fully qualified class name.

Parameters:
className - Qualified class name
cl - The class loader which shall load the class or null for the default loader
Returns:
The class or null if no such class exists

instantiate

public static java.lang.Object instantiate(java.lang.String className,
                                           java.lang.ClassLoader cl,
                                           java.lang.Class type,
                                           java.lang.String displayName)
Creates an object instance by its fully qualified class name using the default constructor.

Parameters:
className - Qualified class name
cl - The class loader which shall load the class or null for the default loader
type - If not null, the method will check if the instantiated class matches the given type
displayName - Display name of the type of object to instantiate for error messages
Returns:
The new object
Throws:
ReflectException - On any error. The exception message will describe the cause of the error. The exception will encapsulate the exception that led to the error.

instantiate

public static java.lang.Object instantiate(java.lang.Class cls,
                                           java.lang.Class type,
                                           java.lang.String displayName)
Creates an object instance given its class using the default constructor.

Parameters:
cls - Object class to use for the instantiation
type - If not null, the method will check if the instantiated class matches the given type
displayName - Display name of the type of object to instantiate for error messages
Returns:
The new object
Throws:
ReflectException - On any error. The exception message will describe the cause of the error. The exception will encapsulate the exception that led to the error.

instantiate

public static java.lang.Object instantiate(java.lang.Class cls,
                                           java.lang.ClassLoader cl,
                                           java.lang.Class type,
                                           java.lang.String displayName)
Creates an object instance given its class using the default constructor.

Parameters:
cls - Object class to use for the instantiation
cl - The class loader which shall load the class or null for the default loader
type - If not null, the method will check if the instantiated class matches the given type
displayName - Display name of the type of object to instantiate for error messages
Returns:
The new object
Throws:
ReflectException - On any error. The exception message will describe the cause of the error. The exception will encapsulate the exception that led to the error.

instantiate

public static java.lang.Object instantiate(java.lang.String className,
                                           java.lang.Class type,
                                           java.lang.String displayName)
Creates a class by its fully qualified class name using the default constructor (convenience method).

Parameters:
className - Qualified class name
type - If not null, the method will check if the instantiated class matches the given type
displayName - Display name of the type of object to instantiate for error messages
Returns:
The new object
Throws:
ReflectException - On any error. The exception message will describe the cause of the error. The exception will encapsulate the exception that led to the error.

instantiateByConstructor

public static java.lang.Object instantiateByConstructor(java.lang.Class cls,
                                                        java.lang.Class[] argTypes,
                                                        java.lang.Object[] args,
                                                        java.lang.Class type,
                                                        java.lang.String displayName,
                                                        boolean allowDefaultConstructor)
Creates an object instance given its class using the constructor with the given argument types.

Parameters:
cls - Object class to use for the instantiation
argTypes - Object classes of the constructor arguments
args - constructor arguments
type - If not null, the method will check if the instantiated class matches the given type
displayName - Display name of the type of object to instantiate for error messages
allowDefaultConstructor - If true, the default constructor will be used, if no constructor with the given argument types could be found. This might result in an error, if the Class has no accessible default constructor.
Returns:
The new object
Throws:
ReflectException - On any error. The exception message will describe the cause of the error. The exception will encapsulate the exception that led to the error.

callMethod

public static java.lang.Object callMethod(java.lang.Object o,
                                          java.lang.String methodName,
                                          java.lang.Class[] argClasses,
                                          java.lang.Object[] argValues,
                                          int errorHandlingMode)
                                   throws java.lang.Exception
Calls a method using Java reflection.

Parameters:
o - Object to invoke the method upon
methodName - Name of the method
argClasses - Argument classes or null
argValues - Argument values or null
errorHandlingMode - Mode that describes how errors are to be handled.
(any combination of CM_PRINT | CM_THROW)
Returns:
The return value of the method call
Throws:
java.lang.Exception - If an error occurred and errorHandlingMode was set to CM_THROW

callMethod

public static java.lang.Object callMethod(java.lang.Object o,
                                          java.lang.reflect.Method method,
                                          java.lang.Object[] argValues,
                                          int errorHandlingMode)
                                   throws java.lang.Exception
Calls a method using Java reflection.

Parameters:
o - Object to invoke the method upon
method - Method to invoke
argValues - Argument values or null
errorHandlingMode - Mode that describes how errors are to be handled.
(any combination of CM_PRINT | CM_THROW)
Returns:
The return value of the method call
Throws:
java.lang.Exception - If an error occurred and errorHandlingMode was set to CM_THROW

getQualifiedMethodSignature

public static java.lang.String getQualifiedMethodSignature(java.lang.reflect.Method method)
Creates a string that specifies the method signature containing a fullly qualified method name.

Parameters:
method - Method
Returns:
String in the form class.method(argclass1,argclass2,...), e. g.
"org.openbp.common.ReflectUtil.callMethod(java.lang.Object,java.lang.reflect.Method,java.lang.Object[],int)"

getUnqualifiedMethodSignature

public static java.lang.String getUnqualifiedMethodSignature(java.lang.reflect.Method method)
Creates a string that specifies the method signature containing an unqualified method name.

Parameters:
method - Method
Returns:
String in the form class.method(argclass1,argclass2,...), e. g.
"org.openbp.common.ReflectUtil.callMethod(java.lang.Object,java.lang.reflect.Method,java.lang.Object[],int)"

findByQualifiedMethodSignature

public static java.lang.reflect.Method findByQualifiedMethodSignature(java.lang.String signature)
                                                               throws java.lang.NoSuchMethodException,
                                                                      java.lang.ClassNotFoundException
Finds a method according to its signature.

Parameters:
signature - Signature as generated by getQualifiedMethodSignature(java.lang.reflect.Method)
Returns:
The method
Throws:
java.lang.NoSuchMethodException - If no matching method was found
java.lang.ClassNotFoundException - If an argument class could not be loaded by the thread or standard class loader

findByUnqualifiedMethodSignature

public static java.lang.reflect.Method findByUnqualifiedMethodSignature(java.lang.Class objectClass,
                                                                        java.lang.String signature)
                                                                 throws java.lang.NoSuchMethodException,
                                                                        java.lang.ClassNotFoundException
Finds a method according to its signature.

Parameters:
objectClass - Class to search for the method
signature - Signature as generated by getQualifiedMethodSignature(java.lang.reflect.Method)
Returns:
The method
Throws:
java.lang.NoSuchMethodException - If no matching method was found
java.lang.ClassNotFoundException - If an argument class could not be loaded by the thread or standard class loader

getPrintableClassName

public static java.lang.String getPrintableClassName(java.lang.Class cls)
Gets a printable form of a class name. By default, array class names are printed like "Lclass;". This looks awful. Therefore, we format them a little prettier, like "class[]".

Parameters:
cls - The class to get the name for
Returns:
Formatted class name

determineClass

public static java.lang.Class determineClass(java.lang.String stringRepresentation)
Determines the class object by type string.

Parameters:
stringRepresentation - Example: java.lang.String or java.lang.String[] or int
Returns:
The class

getMethodOrNull

public static java.lang.reflect.Method getMethodOrNull(java.lang.Class cls,
                                                       java.lang.String name,
                                                       java.lang.Class[] args)
Get a method using reflections. Does not throw an exception if it's not there.

Parameters:
cls - Class
name - Name of the method to locate
args - Arguments of the method signature looking for
Returns:
The method or null if the specified method does not exist

getFieldOrNull

public static java.lang.reflect.Field getFieldOrNull(java.lang.Class cls,
                                                     java.lang.String name)
Get a field using reflections. Does not throw an exception if it's not there.

Parameters:
cls - Class
name - Name of the field to locate
Returns:
The field or null if the specified field does not exist


Copyright © 2011. All Rights Reserved.