Humboldt Alignment Editor 2.5.0

eu.esdihumboldt.util.reflection
Class ReflectionHelper

java.lang.Object
  extended by eu.esdihumboldt.util.reflection.ReflectionHelper

public class ReflectionHelper
extends Object

Provides several utility methods which use Java Reflections to access hidden types, fields and methods.

Author:
Michel Kraemer

Constructor Summary
ReflectionHelper()
           
 
Method Summary
static Field findDeepField(Class<?> c, String property, Class<?> propertyType)
          Returns a field for a given property, no matter if the field is visible or hidden or if it is declared in the given class or in a superclass.
static Method findDeepGetter(Class<?> c, String property, Class<?> propertyType)
          Returns a getter method for a given property, no matter if the method is visible or hidden or if it is declared in the given class or in a superclass or implemented interface.
static Method findDeepSetter(Class<?> c, String property, Class<?> propertyType)
          Returns a setter method for a given property, no matter if the method is visible or hidden or if it is declared in the given class or in a superclass or implemented interface.
static Field findField(Class<?> c, String property, Class<?> propertyType)
          Returns a public field for a given property
static Method findGetter(Class<?> c, String property, Class<?> propertyType)
          Returns a public getter method for a given property
static Class<?> findMostSpecificMatch(Class<?> clazz, Collection<Class<?>> group, boolean checkAssignability)
           Find the most specialised class from group compatible with clazz.
static Method findSetter(Class<?> c, String property, Class<?> propertyType)
          Returns a public setter method for a given property
static List<Class<?>> getClassesFromPackage(String pkg, ClassLoader classLoader)
          Gets a list of all classes in the given package and all subpackages recursively.
static List<Class<?>> getClassesFromPackage(String pkg, ClassLoader classLoader, boolean recursive)
          Gets a list of all classes in the given package
static URL getCurrentJarURL()
           
static
<T> T
getDeepProperty(Object bean, String propertyName)
          Invokes a getter for a property, no matter if the getter is visible or hidden or if it is declared in the given class or in a superclass or implemented interface.
static
<T> T
getDeepPropertyOrField(Object bean, String propertyName, Class<T> valueClass)
          Invokes a getter for a property, or gets the matching field, no matter what.
static File[] getFilesFromPackage(String pkg)
          Returns an array of all files contained by a given package
static
<T> T
getProperty(Object bean, String propertyName)
          Invokes a public getter for a property
static List<String> getSubPackagesFromPackage(String pkg)
          Gets a list of all subpackages in the given package
static List<String> getSubPackagesFromPackage(String pkg, boolean recursive)
          Gets a list of all subpackages in the given package
static void setDeepProperty(Object bean, String propertyName, Object value)
          Invokes a setter for a property, no matter if the setter is visible or hidden or if it is declared in the given class or in a superclass or implemented interface.
static void setPackageResolver(PackageResolver res)
          Sets the package resolver used to retrieve URLs to packages
static void setProperty(Object bean, String propertyName, Object value)
          Invokes a public setter for a property
static
<T> void
shallowEnforceDeepProperties(T src, T dst)
          Performs a shallow copy of all fields defined by the class of src and all superclasses.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionHelper

public ReflectionHelper()
Method Detail

setPackageResolver

public static void setPackageResolver(PackageResolver res)
Sets the package resolver used to retrieve URLs to packages

Parameters:
res - the package resolver
See Also:
getFilesFromPackage(String)

findSetter

public static Method findSetter(Class<?> c,
                                String property,
                                Class<?> propertyType)
Returns a public setter method for a given property

Parameters:
c - the class which would contain the setter method
property - the property
propertyType - the property's type
Returns:
the public setter or null if there is no setter for this property

findDeepSetter

public static Method findDeepSetter(Class<?> c,
                                    String property,
                                    Class<?> propertyType)
Returns a setter method for a given property, no matter if the method is visible or hidden or if it is declared in the given class or in a superclass or implemented interface.

Parameters:
c - the class which would contain the setter method
property - the property
propertyType - the property's type
Returns:
the setter or null if there is no setter for this property

findGetter

public static Method findGetter(Class<?> c,
                                String property,
                                Class<?> propertyType)
Returns a public getter method for a given property

Parameters:
c - the class which would contain the getter method
property - the property
propertyType - the property's type
Returns:
the public getter or null if there is no getter for this property

findDeepGetter

public static Method findDeepGetter(Class<?> c,
                                    String property,
                                    Class<?> propertyType)
Returns a getter method for a given property, no matter if the method is visible or hidden or if it is declared in the given class or in a superclass or implemented interface.

Parameters:
c - the class which would contain the getter method
property - the property
propertyType - the property's type (can be null if the type does not matter)
Returns:
the getter or null if there is no getter for this property

findField

public static Field findField(Class<?> c,
                              String property,
                              Class<?> propertyType)
Returns a public field for a given property

Parameters:
c - the class which would contain the field
property - the property
propertyType - the property's type
Returns:
the field or null if there is no field for this property

findDeepField

public static Field findDeepField(Class<?> c,
                                  String property,
                                  Class<?> propertyType)
Returns a field for a given property, no matter if the field is visible or hidden or if it is declared in the given class or in a superclass.

Parameters:
c - the class which would contain the field
property - the property
propertyType - the property's type
Returns:
the field or null if there is no field for this property

setProperty

public static void setProperty(Object bean,
                               String propertyName,
                               Object value)
                        throws IllegalArgumentException,
                               IllegalAccessException,
                               InvocationTargetException,
                               NoSuchMethodException
Invokes a public setter for a property

Parameters:
bean - the object to invoke the public setter on
propertyName - the name of the property that shall be updated
value - the value passed to the setter
Throws:
IllegalArgumentException - if the argument's type is invalid
IllegalAccessException - if the setter is not accessible
InvocationTargetException - if the setter throws an exception
NoSuchMethodException - if there is no setter for this property

setDeepProperty

public static void setDeepProperty(Object bean,
                                   String propertyName,
                                   Object value)
                            throws IllegalArgumentException,
                                   IllegalAccessException,
                                   InvocationTargetException,
                                   NoSuchMethodException
Invokes a setter for a property, no matter if the setter is visible or hidden or if it is declared in the given class or in a superclass or implemented interface.

Parameters:
bean - the object to invoke the public setter on
propertyName - the name of the property that shall be updated
value - the value passed to the setter
Throws:
IllegalArgumentException - if the argument's type is invalid
IllegalAccessException - if the setter is not accessible
InvocationTargetException - if the setter throws an exception
NoSuchMethodException - if there is no setter for this property

getProperty

public static <T> T getProperty(Object bean,
                                String propertyName)
                     throws IllegalArgumentException,
                            IllegalAccessException,
                            InvocationTargetException,
                            NoSuchMethodException
Invokes a public getter for a property

Type Parameters:
T - the result type
Parameters:
bean - the object to invoke the public getter on
propertyName - the name of the property to retrieve
Returns:
the property's value
Throws:
IllegalArgumentException - if the argument's type is invalid
IllegalAccessException - if the getter is not accessible
InvocationTargetException - if the getter throws an exception
NoSuchMethodException - if there is no getter for this property

getDeepProperty

public static <T> T getDeepProperty(Object bean,
                                    String propertyName)
                         throws IllegalArgumentException,
                                IllegalAccessException,
                                InvocationTargetException,
                                NoSuchMethodException
Invokes a getter for a property, no matter if the getter is visible or hidden or if it is declared in the given class or in a superclass or implemented interface.

Type Parameters:
T - the result type
Parameters:
bean - the object to invoke the getter on
propertyName - the name of the property to retrieve
Returns:
the property's value
Throws:
IllegalArgumentException - if the argument's type is invalid
IllegalAccessException - if the setter is not accessible
InvocationTargetException - if the setter throws an exception
NoSuchMethodException - if there is no setter for this property

getDeepPropertyOrField

public static <T> T getDeepPropertyOrField(Object bean,
                                           String propertyName,
                                           Class<T> valueClass)
                                throws IllegalArgumentException,
                                       InvocationTargetException
Invokes a getter for a property, or gets the matching field, no matter what.

Parameters:
bean - the object to invoke the getter on
propertyName - the name of the property to retrieve
valueClass - the class of the property or field
Returns:
the property's value
Throws:
IllegalArgumentException - if the argument's type is invalid
InvocationTargetException - if the getter throws an exception
IllegalStateException - is the field could be found or accessed

getCurrentJarURL

public static URL getCurrentJarURL()
                            throws MalformedURLException
Returns:
the URL to the JAR file this class is in or null
Throws:
MalformedURLException - if the URL to the jar file could not be created

getFilesFromPackage

public static File[] getFilesFromPackage(String pkg)
                                  throws IOException
Returns an array of all files contained by a given package

Parameters:
pkg - the package (e.g. "de.igd.fhg.CityServer3D")
Returns:
an array of files
Throws:
IOException - if the package could not be found

getClassesFromPackage

public static List<Class<?>> getClassesFromPackage(String pkg,
                                                   ClassLoader classLoader)
                                            throws IOException
Gets a list of all classes in the given package and all subpackages recursively.

Parameters:
pkg - the package
classLoader - the class loader to use
Returns:
the list of classes
Throws:
IOException - if a subpackage or a class could not be loaded

getClassesFromPackage

public static List<Class<?>> getClassesFromPackage(String pkg,
                                                   ClassLoader classLoader,
                                                   boolean recursive)
                                            throws IOException
Gets a list of all classes in the given package

Parameters:
pkg - the package
classLoader - the class loader to use
recursive - true if all subpackages shall be traversed too
Returns:
the list of classes
Throws:
IOException - if a subpackage or a class could not be loaded

getSubPackagesFromPackage

public static List<String> getSubPackagesFromPackage(String pkg)
                                              throws IOException
Gets a list of all subpackages in the given package

Parameters:
pkg - the package
Returns:
the list of classes
Throws:
IOException - if a subpackage or a class could not be loaded

getSubPackagesFromPackage

public static List<String> getSubPackagesFromPackage(String pkg,
                                                     boolean recursive)
                                              throws IOException
Gets a list of all subpackages in the given package

Parameters:
pkg - the package
recursive - true if all subpackages shall be traversed too
Returns:
the list of classes
Throws:
IOException - if a subpackage or a class could not be loaded

findMostSpecificMatch

public static Class<?> findMostSpecificMatch(Class<?> clazz,
                                             Collection<Class<?>> group,
                                             boolean checkAssignability)

Find the most specialised class from group compatible with clazz. A direct superclass match is searched and returned if found.

If not and checkAssignability is true, the most derived assignable class is being searched.

See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

Parameters:
clazz - a class
group - a collection of classes to match against
checkAssignability - whether to use assignability when no direct match is found
Returns:
null or the most specialised match from group

shallowEnforceDeepProperties

public static <T> void shallowEnforceDeepProperties(T src,
                                                    T dst)
                                         throws IllegalArgumentException,
                                                IllegalAccessException
Performs a shallow copy of all fields defined by the class of src and all superclasses.

Type Parameters:
T - the type of the source and destination object
Parameters:
src - the source object
dst - the destination object
Throws:
IllegalArgumentException - if a field is unaccessible
IllegalAccessException - if a field is not accessible

Humboldt Alignment Editor 2.5.0

Build 2012-12-03_12-30-59
Revision 9d2dd4b0318cda9f1393b1ddd390f7f792d70a2d