com.sibvisions.util
Class Reflective

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

public final class Reflective
extends 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.
 
Constructor Summary
Reflective()
          Creates a new instance of Reflective to ensure thread safety.
 
Method Summary
static Object call(Object pObject, boolean pOnlyDeclared, String pMethod, Object... pParams)
          Invokes the method of an object without changing the access to the method.
static Object call(Object pObject, Class<?> pBaseClass, boolean pOnlyDeclared, String pMethod, Object... pParams)
          Invokes the method of an object without changing the access to the method.
static Object call(Object pObject, String pMethod, Object... pParams)
          Invokes the method of an object without changing the access to the method.
static Object construct(Class<?> pClass, boolean pOnlyDeclared, Object... pParams)
          Invokes the constructor of a pre-loaded class.
static Object construct(Class<?> pClass, Object... pParams)
          Invokes the constructor of a pre-loaded class.
static Object construct(ClassLoader pClassLoader, String pClassName, Object... pParams)
          Invokes the constructor of a desired class.
static Object construct(String pClassName, Object... pParams)
          Invokes the constructor of a desired class.
static Object get(Object pObject, String pProperty)
          Invokes the get method of the given property.
static Method getMethod(Class<?> pBaseClass, boolean pOnlyDeclared, String pMethod, Class... pParamTypes)
          Gets the method with the given types.
static Method getMethod(Class<?> pBaseClass, String pMethod, Class... pParamTypes)
          Gets the method with the given types.
static String getMethodDeclaration(Class pClass, String pMethod, Object... pParams)
          Gets the human readable method declaration of a method with its parameter list.
static Object getValue(Object pObject, Field pField)
          Gets the value from a vield.
static Object getValue(Object pObject, String pFieldName)
          Gets the value from a field.
static Object invoke(Object pObject, Method pMethod, Object... pParams)
          Invokes a method with parameters.
 void invokeLater(Runnable pRunnable)
          Causes pRunnable.run() to be executed asynchronously on the current event dispatching thread.
static void printFields(Object pObject, boolean pOnlyDeclared)
          Prints out the fields and field values from a specific object.
static void printMethods(Object pObject, boolean pOnlyDeclared)
          Prints out the methods and return values from a specific object.
static void set(Object pObject, String pProperty, Object pValue)
          Invokes the set method of the given property.
static void setValue(Object pObject, Field pField, Object pValue)
          Sets a field on a specified object value.
static void setValue(Object pObject, String pFieldName, 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
 

Constructor Detail

Reflective

public Reflective()
Creates a new instance of Reflective to ensure thread safety.

Method Detail

construct

public static final Object construct(String pClassName,
                                     Object... pParams)
                              throws 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:
Throwable - if it is not possible to invoke the constructor

construct

public static final Object construct(ClassLoader pClassLoader,
                                     String pClassName,
                                     Object... pParams)
                              throws 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:
Throwable - if it is not possible to invoke the constructor

construct

public static final Object construct(Class<?> pClass,
                                     Object... pParams)
                              throws 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:
Throwable - if it is not possible to invoke the constructor

construct

public static final Object construct(Class<?> pClass,
                                     boolean pOnlyDeclared,
                                     Object... pParams)
                              throws 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:
Throwable - if it is not possible to invoke the constructor

call

public static final Object call(Object pObject,
                                String pMethod,
                                Object... pParams)
                         throws 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:
Throwable - if it is not possible to invoke the method

call

public static final Object call(Object pObject,
                                boolean pOnlyDeclared,
                                String pMethod,
                                Object... pParams)
                         throws 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:
Throwable - if it is not possible to invoke the method

call

public static final Object call(Object pObject,
                                Class<?> pBaseClass,
                                boolean pOnlyDeclared,
                                String pMethod,
                                Object... pParams)
                         throws 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:
Throwable - if it is not possible to invoke the method

getMethodDeclaration

public static final String getMethodDeclaration(Class pClass,
                                                String pMethod,
                                                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(Object pObject,
                            String pFieldName,
                            Object pValue)
                     throws 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:
Throwable - if it is not possible to set the desired field
See Also:
setValue(Object, Field, Object)

setValue

public static void setValue(Object pObject,
                            Field pField,
                            Object pValue)
                     throws 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:
Throwable - if it is not possible to set the desired field

getValue

public static Object getValue(Object pObject,
                              String pFieldName)
                       throws 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:
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 Object getValue(Object pObject,
                              Field pField)
                       throws 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:
Throwable - if it is not possible to get the value of desired field or the field is not presend

invoke

public static Object invoke(Object pObject,
                            Method pMethod,
                            Object... pParams)
                     throws 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:
Throwable - if an error occurs during invocation

get

public static Object get(Object pObject,
                         String pProperty)
                  throws 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:
Throwable - if an error occurs during invocation

set

public static void set(Object pObject,
                       String pProperty,
                       Object pValue)
                throws 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:
Throwable - if an error occurs during invocation

invokeLater

public void invokeLater(Runnable pRunnable)
Causes pRunnable.run() to be executed asynchronously on the current event dispatching thread. There are different technologies with different invokeLater implementations. To find the UI dependent method the current factory manager will be used. If there is no factory manager, then the pRunnable.run() will be started within the current thread.

Parameters:
pRunnable - specific functionality to run asynchronous

printFields

public static final void printFields(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(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

getMethod

public static final Method getMethod(Class<?> pBaseClass,
                                     String pMethod,
                                     Class... pParamTypes)
                              throws 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:
NoSuchMethodException - if Method does not exist.

getMethod

public static final Method getMethod(Class<?> pBaseClass,
                                     boolean pOnlyDeclared,
                                     String pMethod,
                                     Class... pParamTypes)
                              throws 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
pOnlyDeclared - true to use only declared methods
pMethod - invocable method
pParamTypes - parameters for the method
Returns:
the method
Throws:
NoSuchMethodException - if Method does not exist.


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.