com.sibvisions.util
Class Reflective

java.lang.Object
  extended by com.sibvisions.util.Reflective

public final class Reflective
extends java.lang.Object

This is a utility class to call different methods of classes via java.lang.reflect package.


Nested Class Summary
static class Reflective.Parameter
          The Parameter class is a placeholder for parameters.
 
Method Summary
static java.lang.Object call(java.lang.Object pObject, boolean pOnlyDeclared, java.lang.String pMethod, java.lang.Object... pParams)
          Invokes the method of an object without changing the access to the method.
static java.lang.Object call(java.lang.Object pObject, java.lang.Class<?> pBaseClass, boolean pOnlyDeclared, java.lang.String pMethod, java.lang.Object... pParams)
          Invokes the method of an object without changing the access to the method.
static java.lang.Object call(java.lang.Object pObject, java.lang.reflect.Method pMethod, java.lang.Object... pParams)
          Invokes the given method.
static java.lang.Object call(java.lang.Object pObject, java.lang.String pMethod, java.lang.Object... pParams)
          Invokes the method of an object without changing the access to the method.
static java.lang.Object construct(java.lang.Class<?> pClass, boolean pOnlyDeclared, java.lang.Object... pParams)
          Invokes the constructor of a pre-loaded class.
static java.lang.Object construct(java.lang.Class<?> pClass, java.lang.Object... pParams)
          Invokes the constructor of a pre-loaded class.
static java.lang.Object construct(java.lang.ClassLoader pClassLoader, java.lang.String pClassName, java.lang.Object... pParams)
          Invokes the constructor of a desired class.
static java.lang.Object construct(java.lang.String pClassName, java.lang.Object... pParams)
          Invokes the constructor of a desired class.
static java.lang.Object get(java.lang.Object pObject, java.lang.String pProperty)
          Invokes the get method of the given property.
static java.lang.Class getAutoboxClass(java.lang.Class pClass)
          Gets the outerboxed class when primitive class or the object class itself.
static java.lang.reflect.Constructor getConstructor(java.lang.Class<?> pClass, boolean pOnlyDeclared, java.lang.Object... pParams)
          Gets the best matching constructor from the given class.
static java.lang.reflect.Constructor getConstructor(java.lang.ClassLoader pClassLoader, java.lang.String pClassName, java.lang.Object... pParams)
          Gets the best matching constructor from the given class.
static java.lang.reflect.Method getMethod(java.lang.Class<?> pBaseClass, boolean pOnlyDeclared, java.lang.String pMethod, java.lang.Class... pParamTypes)
          Gets the method with the given types.
static java.lang.reflect.Method getMethod(java.lang.Class<?> pBaseClass, java.lang.String pMethod, java.lang.Class... pParamTypes)
          Gets the method with the given types.
static java.lang.String getMethodDeclaration(java.lang.Class pClass, java.lang.String pMethod, java.lang.Object... pParams)
          Gets the human readable method declaration of a method with its parameter list.
static java.lang.reflect.Method getMethodForCall(java.lang.Class<?> pBaseClass, boolean pOnlyDeclared, java.lang.String pMethod, java.lang.Object... pParams)
          Gets the method for a possible call.
static java.lang.reflect.Method getMethodForCall(java.lang.Class<?> pBaseClass, java.lang.String pMethod, java.lang.Object... pParams)
          Gets the method for a possible call.
static java.lang.reflect.Method[] getMethodsByReturnValue(java.lang.Class<?> pClass, java.lang.Class<?> pReturn, boolean pOnlyDeclared)
          Gets all methods (without parameters) which has a return value that is assignable from the given return value.
static java.lang.Class getOuterBoxClass(java.lang.Class pClass)
          Deprecated. since 2.7, use getAutoboxClass(Class) instead.
static java.lang.Object getValue(java.lang.Object pObject, java.lang.reflect.Field pField)
          Gets the value from a vield.
static java.lang.Object getValue(java.lang.Object pObject, java.lang.String pFieldName)
          Gets the value from a field.
static java.lang.Object invoke(java.lang.Object pObject, java.lang.reflect.Method pMethod, java.lang.Object... pParams)
          Invokes a method with parameters.
static boolean isParamTypeListValid(java.lang.Class<?>[] pTypes, java.lang.Class[] pParamTypes, boolean pVarArg)
          Checks if the class definitions of a parameter list are equal to a desired class list.
static void printFields(java.lang.Object pObject, boolean pOnlyDeclared)
          Prints out the fields and field values from a specific object.
static void printMethods(java.lang.Object pObject, boolean pOnlyDeclared)
          Prints out the methods and return values from a specific object.
static void set(java.lang.Object pObject, java.lang.String pProperty, java.lang.Object pValue)
          Invokes the set method of the given property.
static void setValue(java.lang.Object pObject, java.lang.reflect.Field pField, java.lang.Object pValue)
          Sets a field on a specified object value.
static void setValue(java.lang.Object pObject, java.lang.String pFieldName, java.lang.Object pValue)
          Sets a field on a specified object value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAutoboxClass

public static java.lang.Class getAutoboxClass(java.lang.Class pClass)
Gets the outerboxed class when primitive class or the object class itself.

Parameters:
pClass - the primitive or object class
Returns:
the outerboxed class when primitive class or the object class itself.

getOuterBoxClass

@Deprecated
public static java.lang.Class getOuterBoxClass(java.lang.Class pClass)
Deprecated. since 2.7, use getAutoboxClass(Class) instead.

Gets the outerboxed class when primitive class or the object class itself.

Parameters:
pClass - the primitive or object class
Returns:
the outerboxed class when primitive class or the object class itself.

construct

public static final java.lang.Object construct(java.lang.String pClassName,
                                               java.lang.Object... pParams)
                                        throws java.lang.Throwable
Invokes the constructor of a desired class. The constructor will be found through its parameter list.

Parameters:
pClassName - full qualified class name
pParams - parameters for the constructor
Returns:
object newly created object
Throws:
java.lang.Throwable - if it is not possible to invoke the constructor

construct

public static final java.lang.Object construct(java.lang.ClassLoader pClassLoader,
                                               java.lang.String pClassName,
                                               java.lang.Object... pParams)
                                        throws java.lang.Throwable
Invokes the constructor of a desired class. The constructor will be found through its parameter list.

Parameters:
pClassLoader - an optional ClassLoader for dynamic class loading
pClassName - full qualified class name
pParams - parameters for the constructor
Returns:
object newly created object
Throws:
java.lang.Throwable - if it is not possible to invoke the constructor

construct

public static final java.lang.Object construct(java.lang.Class<?> pClass,
                                               java.lang.Object... pParams)
                                        throws java.lang.Throwable
Invokes the constructor of a pre-loaded class. The constructor will be found through its parameter list.

Parameters:
pClass - the pre-loaded class
pParams - parameters for the constructor
Returns:
object newly created object
Throws:
java.lang.Throwable - if it is not possible to invoke the constructor

construct

public static final java.lang.Object construct(java.lang.Class<?> pClass,
                                               boolean pOnlyDeclared,
                                               java.lang.Object... pParams)
                                        throws java.lang.Throwable
Invokes the constructor of a pre-loaded class. The constructor will be found through its parameter list.

Parameters:
pClass - the pre-loaded class
pOnlyDeclared - true to use only declared constructors
pParams - parameters for the constructor
Returns:
object newly created object
Throws:
java.lang.Throwable - if it is not possible to invoke the constructor

call

public static final java.lang.Object call(java.lang.Object pObject,
                                          java.lang.String pMethod,
                                          java.lang.Object... pParams)
                                   throws java.lang.Throwable
Invokes the method of an object without changing the access to the method. The method needs the public modifier. If there are more than one methods with the same name, the parameterlist will be used to find the fitting method.

Parameters:
pObject - object with the desired method
pMethod - invocable method
pParams - parameters for the method
Returns:
return value from the method invocation
Throws:
java.lang.Throwable - if it is not possible to invoke the method

call

public static final java.lang.Object call(java.lang.Object pObject,
                                          boolean pOnlyDeclared,
                                          java.lang.String pMethod,
                                          java.lang.Object... pParams)
                                   throws java.lang.Throwable
Invokes the method of an object without changing the access to the method. The method needs the public modifier. If there are more than one methods with the same name, the parameterlist will be used to find the fitting method.

Parameters:
pObject - object with the desired method
pOnlyDeclared - true to use only declared methods
pMethod - invocable method
pParams - parameters for the method
Returns:
return value from the method invocation
Throws:
java.lang.Throwable - if it is not possible to invoke the method

call

public static final java.lang.Object call(java.lang.Object pObject,
                                          java.lang.Class<?> pBaseClass,
                                          boolean pOnlyDeclared,
                                          java.lang.String pMethod,
                                          java.lang.Object... pParams)
                                   throws java.lang.Throwable
Invokes the method of an object without changing the access to the method. The method needs the public modifier. If there are more than one methods with the same name, the parameterlist will be used to find the fitting method.

Parameters:
pObject - object with the desired method
pBaseClass - the object class or a super class from which the method will be called
pOnlyDeclared - true to use only declared methods
pMethod - invocable method
pParams - parameters for the method
Returns:
return value from the method invocation
Throws:
java.lang.Throwable - if it is not possible to invoke the method

call

public static final java.lang.Object call(java.lang.Object pObject,
                                          java.lang.reflect.Method pMethod,
                                          java.lang.Object... pParams)
                                   throws java.lang.Throwable
Invokes the given method. This method will be used in call(Object, Class, boolean, String, Object...).

Parameters:
pMethod - the method to call
pObject - the caller object
pParams - the method parameters
Returns:
return value from the method invocation
Throws:
java.lang.Throwable - if it is not possible to invoke the method
java.lang.NullPointerException - if pMethod is null

getMethodDeclaration

public static final java.lang.String getMethodDeclaration(java.lang.Class pClass,
                                                          java.lang.String pMethod,
                                                          java.lang.Object... pParams)
Gets the human readable method declaration of a method with its parameter list.

Parameters:
pClass - the class
pMethod - method or full qualified class name
pParams - parameter list
Returns:
human readable method declaration e.g. java.util.File(java.lang.String);

setValue

public static void setValue(java.lang.Object pObject,
                            java.lang.String pFieldName,
                            java.lang.Object pValue)
                     throws java.lang.Throwable
Sets a field on a specified object value.

Parameters:
pObject - the object with the field
pFieldName - the field name
pValue - the new value
Throws:
java.lang.Throwable - if it is not possible to set the desired field
See Also:
setValue(Object, Field, Object)

setValue

public static void setValue(java.lang.Object pObject,
                            java.lang.reflect.Field pField,
                            java.lang.Object pValue)
                     throws java.lang.Throwable
Sets a field on a specified object value. If the field is not accessible, the access will be granted before, and revoked after the execution.

Parameters:
pObject - the object with the field
pField - the field
pValue - the new value
Throws:
java.lang.Throwable - if it is not possible to set the desired field

getValue

public static java.lang.Object getValue(java.lang.Object pObject,
                                        java.lang.String pFieldName)
                                 throws java.lang.Throwable
Gets the value from a field.

Parameters:
pObject - the object with the field
pFieldName - the field name
Returns:
the value of the field
Throws:
java.lang.Throwable - if it is not possible to get the value of desired field or the field is not presend
See Also:
getValue(Object, Field)

getValue

public static java.lang.Object getValue(java.lang.Object pObject,
                                        java.lang.reflect.Field pField)
                                 throws java.lang.Throwable
Gets the value from a vield. If the field is not accessible, the access will be granted before, and revoked after the execution.

Parameters:
pObject - the object with the field
pField - the field
Returns:
the value of the field
Throws:
java.lang.Throwable - if it is not possible to get the value of desired field or the field is not presend

invoke

public static java.lang.Object invoke(java.lang.Object pObject,
                                      java.lang.reflect.Method pMethod,
                                      java.lang.Object... pParams)
                               throws java.lang.Throwable
Invokes a method with parameters. If the method is not accessible, the access will be granted before, and revoked after the execution.

Parameters:
pObject - the object with the method
pMethod - the method
pParams - the method parameters
Returns:
the result of the method invokation
Throws:
java.lang.Throwable - if an error occurs during invocation

get

public static java.lang.Object get(java.lang.Object pObject,
                                   java.lang.String pProperty)
                            throws java.lang.Throwable
Invokes the get method of the given property.

Parameters:
pObject - the object with the method
pProperty - the property
Returns:
the value of the property.
Throws:
java.lang.Throwable - if an error occurs during invocation

set

public static void set(java.lang.Object pObject,
                       java.lang.String pProperty,
                       java.lang.Object pValue)
                throws java.lang.Throwable
Invokes the set method of the given property.

Parameters:
pObject - the object with the method
pProperty - the property
pValue - the value of the property.
Throws:
java.lang.Throwable - if an error occurs during invocation

printFields

public static final void printFields(java.lang.Object pObject,
                                     boolean pOnlyDeclared)
Prints out the fields and field values from a specific object. If the field is not accessible, the access will be enabled.

Parameters:
pObject - an object
pOnlyDeclared - use only declared fields

printMethods

public static final void printMethods(java.lang.Object pObject,
                                      boolean pOnlyDeclared)
Prints out the methods and return values from a specific object. If the method is not accessible, the access will be enabled. Only methods which starts with 'get', 'is' or 'has' and without parameters will be used.

Parameters:
pObject - an object
pOnlyDeclared - use only declared methods

isParamTypeListValid

public static final boolean isParamTypeListValid(java.lang.Class<?>[] pTypes,
                                                 java.lang.Class[] pParamTypes,
                                                 boolean pVarArg)
Checks if the class definitions of a parameter list are equal to a desired class list. for easier usage all arrays at the end

Parameters:
pTypes - desired class list
pParamTypes - parameter list
pVarArg - check varArg parameter.
Returns:
true if the class definitions are equal to the desired class list

getMethod

public static final java.lang.reflect.Method getMethod(java.lang.Class<?> pBaseClass,
                                                       java.lang.String pMethod,
                                                       java.lang.Class... pParamTypes)
                                                throws java.lang.NoSuchMethodException
Gets the method with the given types. The method needs the public modifier. If there are more than one methods with the same name, the parameterlist will be used to find the fitting method.

Parameters:
pBaseClass - the object class or a super class from which the method will be called
pMethod - invocable method
pParamTypes - parameters for the method
Returns:
the method
Throws:
java.lang.NoSuchMethodException - if Method does not exist.

getMethod

public static final java.lang.reflect.Method getMethod(java.lang.Class<?> pBaseClass,
                                                       boolean pOnlyDeclared,
                                                       java.lang.String pMethod,
                                                       java.lang.Class... pParamTypes)
                                                throws java.lang.NoSuchMethodException
Gets the method with the given types. The method needs the public modifier. If there is more than one method with the same name, the parameterlist will be used to find the fitting method.

Parameters:
pBaseClass - the object class or a super class from which the method will be called
pOnlyDeclared - true to use only declared methods
pMethod - invocable method
pParamTypes - parameters for the method
Returns:
the method
Throws:
java.lang.NoSuchMethodException - if Method does not exist.

getMethodForCall

public static final java.lang.reflect.Method getMethodForCall(java.lang.Class<?> pBaseClass,
                                                              java.lang.String pMethod,
                                                              java.lang.Object... pParams)
                                                       throws java.lang.Throwable
Gets the method for a possible call.

Parameters:
pBaseClass - the object class or a super class from which the method will be called
pMethod - invocable method
pParams - parameters for the method
Returns:
return value from the method invocation
Throws:
java.lang.Throwable - if it is not possible to invoke the method
See Also:
getMethodForCall(Class, boolean, String, Object...)

getMethodForCall

public static final java.lang.reflect.Method getMethodForCall(java.lang.Class<?> pBaseClass,
                                                              boolean pOnlyDeclared,
                                                              java.lang.String pMethod,
                                                              java.lang.Object... pParams)
                                                       throws java.lang.Throwable
Gets the method for a possible call. This method will be used in call(Object, Class, boolean, String, Object...).

Parameters:
pBaseClass - the object class or a super class from which the method will be called
pOnlyDeclared - true to use only declared methods
pMethod - invocable method
pParams - parameters for the method
Returns:
return value from the method invocation
Throws:
java.lang.Throwable - if it is not possible to invoke the method

getConstructor

public static java.lang.reflect.Constructor getConstructor(java.lang.ClassLoader pClassLoader,
                                                           java.lang.String pClassName,
                                                           java.lang.Object... pParams)
                                                    throws java.lang.Exception
Gets the best matching constructor from the given class.

Parameters:
pClassLoader - the class loader that should load the class
pClassName - the full qualified class name "com.package.classname"
pParams - the parameters for the constructor
Returns:
the constructor
Throws:
java.lang.Exception - if no constructor was found

getConstructor

public static java.lang.reflect.Constructor getConstructor(java.lang.Class<?> pClass,
                                                           boolean pOnlyDeclared,
                                                           java.lang.Object... pParams)
                                                    throws java.lang.Exception
Gets the best matching constructor from the given class.

Parameters:
pClass - the class that should checked
pOnlyDeclared - true if only declared constructors should be used, false to search all constructors
pParams - the parameters for the constructor
Returns:
the constructor
Throws:
java.lang.Exception - if no constructor was found

getMethodsByReturnValue

public static java.lang.reflect.Method[] getMethodsByReturnValue(java.lang.Class<?> pClass,
                                                                 java.lang.Class<?> pReturn,
                                                                 boolean pOnlyDeclared)
Gets all methods (without parameters) which has a return value that is assignable from the given return value.

Parameters:
pClass - the class to check for the methods
pReturn - the expected return value
pOnlyDeclared - true to get only declared methods, false to get all methods
Returns:
the found methods or an empty array


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.