public class ReflectionHelper extends Object
| Constructor and Description |
|---|
ReflectionHelper() |
| Modifier and Type | Method and Description |
|---|---|
<T extends Annotation> |
getAnnotationWithMeta(AnnotatedElement element,
Class<T> annotation)
Get the annotations of given type which are available on the annotated element.
|
List<Field> |
getFields(Class<?> clazz)
Find all declared fields of a class.
|
Method |
getMethod(Class<?> type,
Class<?> returnType,
String name,
Class<?>... parameters)
Get method with given name and parameters and given return type.
|
List<SyntheticField> |
getSyntheticFields(Class<?> clazz)
Find all declared (synthetic) fields of a class.
|
<T> Class<T> |
loadClass(String name)
Load class with given name from the correct class loader.
|
void |
makeAccessible(Field field)
Make the given field accessible, explicitly setting it accessible if necessary.
|
List<SyntheticField> |
makeSynthetic(Class<?> clazz,
List<Field> fields)
Convert list of (real) fields to synthetic fields (which use accessor methods).
|
<T> T |
newInstance(Class<T> clazz)
Create a new instance of a class.
|
<T> T |
newInstance(String className)
Create a new instance of a class given the class name.
|
public <T> T newInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
Provided to allow mocking, making it possible to test the exception handling.
T - type of object to createclazz - class to create new instance ofIllegalAccessException - see Class.newInstance()InstantiationException - see Class.newInstance()public <T> T newInstance(String className) throws IllegalAccessException, InstantiationException, ClassNotFoundException
Provided to allow mocking, making it possible to test the exception handling.
T - type of object to createclassName - name of class to create new instance ofIllegalAccessException - see Class.newInstance()InstantiationException - see Class.newInstance()ClassNotFoundException - see ClassLoader.loadClass(String)public <T> Class<T> loadClass(String name) throws ClassNotFoundException
T - type of object to createname - name of class to loadClassNotFoundException - see ClassLoader.loadClass(String)public List<Field> getFields(Class<?> clazz)
clazz - class to find fields forpublic List<SyntheticField> makeSynthetic(Class<?> clazz, List<Field> fields)
clazz - class which contains the fieldsfields - fields to convert to synthetic fieldspublic List<SyntheticField> getSyntheticFields(Class<?> clazz)
clazz - class to find fields forpublic void makeAccessible(Field field)
setAccessible(true) method is only called when actually necessary, to avoid unnecessary
conflicts with a JVM SecurityManager (if active).
This method is borrowed from Spring's ReflectionUtil class.
field - the field to make accessibleAccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)public Method getMethod(Class<?> type, Class<?> returnType, String name, Class<?>... parameters)
type - class on which method should be foundreturnType - required return type (or null for void or no check)name - method nameparameters - method parameter typespublic <T extends Annotation> List<T> getAnnotationWithMeta(AnnotatedElement element, Class<T> annotation)
The result is given in no specific order
T - annotation typeelement - annotated elementannotation - annotation to findCopyright © 2017. All rights reserved.