public class ReflectionHelper extends Object
Constructor and Description |
---|
ReflectionHelper() |
Modifier and Type | Method and Description |
---|---|
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.
|
public static void setPackageResolver(PackageResolver res)
res
- the package resolvergetFilesFromPackage(String)
public static Method findSetter(Class<?> c, String property, Class<?> propertyType)
c
- the class which would contain the setter methodproperty
- the propertypropertyType
- the property's typepublic static Method findDeepSetter(Class<?> c, String property, Class<?> propertyType)
c
- the class which would contain the setter methodproperty
- the propertypropertyType
- the property's typepublic static Method findGetter(Class<?> c, String property, Class<?> propertyType)
c
- the class which would contain the getter methodproperty
- the propertypropertyType
- the property's typepublic static Method findDeepGetter(Class<?> c, String property, Class<?> propertyType)
c
- the class which would contain the getter methodproperty
- the propertypropertyType
- the property's type (can be null if the type does not
matter)public static Field findField(Class<?> c, String property, Class<?> propertyType)
c
- the class which would contain the fieldproperty
- the propertypropertyType
- the property's typepublic static Field findDeepField(Class<?> c, String property, Class<?> propertyType)
c
- the class which would contain the fieldproperty
- the propertypropertyType
- the property's typepublic static void setProperty(Object bean, String propertyName, Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
bean
- the object to invoke the public setter onpropertyName
- the name of the property that shall be updatedvalue
- the value passed to the setterIllegalArgumentException
- if the argument's type is invalidIllegalAccessException
- if the setter is not accessibleInvocationTargetException
- if the setter throws an exceptionNoSuchMethodException
- if there is no setter for this propertypublic static void setDeepProperty(Object bean, String propertyName, Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
bean
- the object to invoke the public setter onpropertyName
- the name of the property that shall be updatedvalue
- the value passed to the setterIllegalArgumentException
- if the argument's type is invalidIllegalAccessException
- if the setter is not accessibleInvocationTargetException
- if the setter throws an exceptionNoSuchMethodException
- if there is no setter for this propertypublic static <T> T getProperty(Object bean, String propertyName) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
T
- the result typebean
- the object to invoke the public getter onpropertyName
- the name of the property to retrieveIllegalArgumentException
- if the argument's type is invalidIllegalAccessException
- if the getter is not accessibleInvocationTargetException
- if the getter throws an exceptionNoSuchMethodException
- if there is no getter for this propertypublic static <T> T getDeepProperty(Object bean, String propertyName) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
T
- the result typebean
- the object to invoke the getter onpropertyName
- the name of the property to retrieveIllegalArgumentException
- if the argument's type is invalidIllegalAccessException
- if the setter is not accessibleInvocationTargetException
- if the setter throws an exceptionNoSuchMethodException
- if there is no setter for this propertypublic static <T> T getDeepPropertyOrField(Object bean, String propertyName, Class<T> valueClass) throws IllegalArgumentException, InvocationTargetException
bean
- the object to invoke the getter onpropertyName
- the name of the property to retrievevalueClass
- the class of the property or fieldIllegalArgumentException
- if the argument's type is invalidInvocationTargetException
- if the getter throws an exceptionIllegalStateException
- is the field could be found or accessedpublic static URL getCurrentJarURL() throws MalformedURLException
MalformedURLException
- if the URL to the jar file could not be
createdpublic static File[] getFilesFromPackage(String pkg) throws IOException
pkg
- the package (e.g. "de.igd.fhg.CityServer3D")IOException
- if the package could not be foundpublic static List<Class<?>> getClassesFromPackage(String pkg, ClassLoader classLoader) throws IOException
pkg
- the packageclassLoader
- the class loader to useIOException
- if a subpackage or a class could not be loadedpublic static List<Class<?>> getClassesFromPackage(String pkg, ClassLoader classLoader, boolean recursive) throws IOException
pkg
- the packageclassLoader
- the class loader to userecursive
- true if all subpackages shall be traversed tooIOException
- if a subpackage or a class could not be loadedpublic static List<String> getSubPackagesFromPackage(String pkg) throws IOException
pkg
- the packageIOException
- if a subpackage or a class could not be loadedpublic static List<String> getSubPackagesFromPackage(String pkg, boolean recursive) throws IOException
pkg
- the packagerecursive
- true if all subpackages shall be traversed tooIOException
- if a subpackage or a class could not be loadedpublic 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.
clazz
- a classgroup
- a collection of classes to match againstcheckAssignability
- whether to use assignability when no direct
match is foundpublic static <T> void shallowEnforceDeepProperties(T src, T dst) throws IllegalArgumentException, IllegalAccessException
T
- the type of the source and destination objectsrc
- the source objectdst
- the destination objectIllegalArgumentException
- if a field is unaccessibleIllegalAccessException
- if a field is not accessibleBuild 2013-10-07_11-27-38
Revision 99b27713a799a59a21b0b166eaaea8aaefbf125a