@Immutable public class Reflection extends Object
Modifier and Type | Class and Description |
---|---|
static class |
Reflection.Property
A representation of a property on a Java object.
|
Constructor and Description |
---|
Reflection(Class<?> targetClass)
Construct a Reflection instance that cache's some information about the target class.
|
Modifier and Type | Method and Description |
---|---|
static Class<?>[] |
buildArgumentClasses(Object... arguments)
Build the list of classes that correspond to the list of argument objects.
|
static List<Class<?>> |
buildArgumentClassList(Object... arguments)
Build the list of classes that correspond to the list of argument objects.
|
static List<Class<?>> |
convertArgumentClassesToPrimitives(Class<?>... arguments)
Convert any argument classes to primitives.
|
Iterable<Method> |
findAllMethods(Pattern methodNamePattern)
Finds the methods on the target class that match the supplied method name.
|
Iterable<Method> |
findAllMethods(String methodName,
boolean caseSensitive)
Finds the methods on the target class that match the supplied method name.
|
Method |
findBestMethodOnTarget(String methodName,
Object... arguments)
Find the best method on the target class that matches the signature specified with the specified name and the list of
arguments.
|
Method |
findBestMethodWithSignature(String methodName,
boolean caseSensitive,
Class<?>... argumentsClasses)
Find the best method on the target class that matches the signature specified with the specified name and the list of
argument classes.
|
Method |
findBestMethodWithSignature(String methodName,
Class<?>... argumentsClasses)
Find the best method on the target class that matches the signature specified with the specified name and the list of
argument classes.
|
static Field |
findFieldRecursively(Class<?> c,
String fieldName)
Searches for a given field recursively under a particular class
|
Method |
findFirstMethod(Pattern methodNamePattern)
Find the method on the target class that matches the supplied method name.
|
Method |
findFirstMethod(String methodName,
boolean caseSensitive)
Find the method on the target class that matches the supplied method name.
|
Method[] |
findGetterMethods()
Find the getter methods on the target class that begin with "get" or "is", that have no parameters, and that return
something other than void.
|
String[] |
findGetterPropertyNames()
Find the property names with getter methods on the target class.
|
static Method |
findMethod(Class<?> type,
String methodName)
Searches for a method with a given name in a class.
|
protected String[] |
findMethodNames(String methodName) |
Method[] |
findMethods(Pattern methodNamePattern)
Find the methods on the target class that matches the supplied method name.
|
Method[] |
findMethods(String methodName,
boolean caseSensitive)
Find the method on the target class that matches the supplied method name.
|
Map<String,Reflection.Property> |
getAllPropertiesByNameOn(Object target)
Get representations for all of the Java properties on the supplied object.
|
List<Reflection.Property> |
getAllPropertiesOn(Object target)
Get representations for all of the Java properties on the supplied object.
|
protected static <AnnotationType extends Annotation> |
getAnnotation(Class<AnnotationType> annotationType,
Field field,
Method[] getters,
Method[] setters) |
static String |
getClassName(Class<?> clazz)
Returns the name of the class.
|
protected Field |
getField(Class<?> targetClass,
String propertyName)
Get a Field intance for a given class and property.
|
static Field |
getField(String fieldName,
Class<?> objectClass)
Retrieves the field with the given name from a class
|
static <T> T |
getInstance(String classname,
ClassLoader cl)
Instantiates a class based on the class name provided.
|
Object |
getProperty(Object target,
Reflection.Property property)
Get current value for the property on the supplied target object.
|
Reflection.Property |
getProperty(Object target,
String propertyName)
Get the representation of the named property on the target object.
|
Reflection.Property |
getProperty(Object target,
String propertyName,
String description)
Get the representation of the named property (with the supplied description) on the target object.
|
Reflection.Property |
getProperty(Object target,
String propertyName,
String label,
String category,
String description,
Object... allowedValues)
Get the representation of the named property (with the supplied labe, category, description, and allowed values) on the
target object.
|
String |
getPropertyAsString(Object target,
Reflection.Property property)
Get current value represented as a string for the property on the supplied target object.
|
Class<?> |
getTargetClass()
Return the class that is the target for the reflection methods.
|
static Object |
invokeAccessibly(Object instance,
Method method,
Object[] parameters)
Invokes a method using reflection, in an accessible manner (by using
AccessibleObject.setAccessible(boolean) |
Object |
invokeBestMethodOnTarget(String[] methodNames,
Object target,
Object... arguments)
Find and execute the best method on the target class that matches the signature specified with one of the specified names
and the list of arguments.
|
Object |
invokeGetterMethodOnTarget(String javaPropertyName,
Object target)
Find and execute the getter method on the target class for the supplied property name.
|
Object |
invokeSetterMethodOnTarget(String javaPropertyName,
Object target,
Object argument)
Find and execute the best setter method on the target class for the supplied property name and the supplied list of
arguments.
|
protected static String |
localizedString(Class<?> i18nClass,
String id) |
void |
setProperty(Object target,
Reflection.Property property,
Object value)
Set the property on the supplied target object to the specified value.
|
static void |
setValue(Object instance,
String fieldName,
Object value)
Sets the value of a field of an object instance via reflection
|
protected void |
writeObjectAsString(Object obj,
StringBuilder sb,
boolean wrapWithBrackets) |
protected static final Inflector INFLECTOR
public Reflection(Class<?> targetClass)
targetClass
- the target classIllegalArgumentException
- if the target class is nullpublic static Class<?>[] buildArgumentClasses(Object... arguments)
arguments
- the list of argument objects.public static List<Class<?>> buildArgumentClassList(Object... arguments)
arguments
- the list of argument objects.public static List<Class<?>> convertArgumentClassesToPrimitives(Class<?>... arguments)
arguments
- the list of argument classes.public static String getClassName(Class<?> clazz)
clazz
- the class for which the class name is to be returned.public static void setValue(Object instance, String fieldName, Object value)
instance
- to inspectfieldName
- name of field to setvalue
- the value to setpublic static Field getField(String fieldName, Class<?> objectClass)
fieldName
- the field to retrieveobjectClass
- the class from which to retrieve the fieldField
instance or null
if no such field exists.public static Method findMethod(Class<?> type, String methodName)
public static Field findFieldRecursively(Class<?> c, String fieldName)
public static <T> T getInstance(String classname, ClassLoader cl)
classname
- class to instantiatepublic static Object invokeAccessibly(Object instance, Method method, Object[] parameters)
AccessibleObject.setAccessible(boolean)
instance
- instance on which to execute the methodmethod
- method to executeparameters
- parameterspublic Class<?> getTargetClass()
public Method[] findMethods(String methodName, boolean caseSensitive)
methodName
- the name of the method that is to be found.caseSensitive
- true if the method name supplied should match case-sensitively, or false if case does not matterpublic Method[] findMethods(Pattern methodNamePattern)
methodNamePattern
- the regular expression pattern for the name of the method that is to be found.public Method[] findGetterMethods()
Object.getClass()
method.public String[] findGetterPropertyNames()
findGetterMethods()
.public Method findFirstMethod(String methodName, boolean caseSensitive)
methodName
- the name of the method that is to be found.caseSensitive
- true if the method name supplied should match case-sensitively, or false if case does not matterpublic Method findFirstMethod(Pattern methodNamePattern)
methodNamePattern
- the regular expression pattern for the name of the method that is to be found.public Iterable<Method> findAllMethods(String methodName, boolean caseSensitive)
methodName
- the name of the method that is to be found.caseSensitive
- true if the method name supplied should match case-sensitively, or false if case does not matterpublic Iterable<Method> findAllMethods(Pattern methodNamePattern)
methodNamePattern
- the regular expression pattern for the name of the method that is to be found.public Object invokeBestMethodOnTarget(String[] methodNames, Object target, Object... arguments) throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
This method is unable to find methods with signatures that include both primitive arguments and arguments that are
instances of Number
or its subclasses.
methodNames
- the names of the methods that are to be invoked, in the order they are to be triedtarget
- the object on which the method is to be invokedarguments
- the array of Object instances that correspond to the arguments passed to the method.NoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.InvocationTargetException
IllegalAccessException
IllegalArgumentException
public Object invokeSetterMethodOnTarget(String javaPropertyName, Object target, Object argument) throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
This method is unable to find methods with signatures that include both primitive arguments and arguments that are
instances of Number
or its subclasses.
javaPropertyName
- the name of the property whose setter is to be invoked, in the order they are to be triedtarget
- the object on which the method is to be invokedargument
- the new value for the propertyNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.InvocationTargetException
IllegalAccessException
IllegalArgumentException
public Object invokeGetterMethodOnTarget(String javaPropertyName, Object target) throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
javaPropertyName
- the name of the property whose getter is to be invoked, in the order they are to be triedtarget
- the object on which the method is to be invokedNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.InvocationTargetException
IllegalAccessException
IllegalArgumentException
public Method findBestMethodOnTarget(String methodName, Object... arguments) throws NoSuchMethodException, SecurityException
This method is unable to find methods with signatures that include both primitive arguments and arguments that are
instances of Number
or its subclasses.
methodName
- the name of the method that is to be invoked.arguments
- the array of Object instances that correspond to the arguments passed to the method.NoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.public Method findBestMethodWithSignature(String methodName, Class<?>... argumentsClasses) throws NoSuchMethodException, SecurityException
methodName
- the name of the method that is to be invoked.argumentsClasses
- the list of Class instances that correspond to the classes for each argument passed to the method.NoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.public Method findBestMethodWithSignature(String methodName, boolean caseSensitive, Class<?>... argumentsClasses) throws NoSuchMethodException, SecurityException
methodName
- the name of the method that is to be invoked.caseSensitive
- true if the method name supplied should match case-sensitively, or false if case does not matterargumentsClasses
- the list of Class instances that correspond to the classes for each argument passed to the method.NoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.public Reflection.Property getProperty(Object target, String propertyName, String label, String category, String description, Object... allowedValues) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
If the label is not provided, this method looks for the Label
annotation on the property's field and sets the label
to the annotation's literal value, or if the Label.i18n()
class is referenced, the localized value of the
referenced I18n
.
Description
annotation on the property's field and
sets the label to the annotation's literal value, or if the Description.i18n()
class is referenced, the localized
value of the referenced I18n
.
And if the category is not provided, this method looks for the Category
annotation on the property's field and sets
the label to the annotation's literal value, or if the Category.i18n()
class is referenced, the localized value of
the referenced I18n
.
target
- the target on which the setter is to be called; may not be nullpropertyName
- the name of the Java object property; may not be nulllabel
- the new label for the property; may be nullcategory
- the category for this property; may be nulldescription
- the description for the property; may be null if this is not knownallowedValues
- the of allowed values, or null or empty if the values are not constrainedNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.IllegalAccessException
- if the setter method could not be accessedInvocationTargetException
- if there was an error invoking the setter method on the targetIllegalArgumentException
- if 'target' is null, or if 'propertyName' is null or emptyprotected Field getField(Class<?> targetClass, String propertyName) throws NoSuchFieldException
targetClass
- propertyName
- NoSuchFieldException
protected static <AnnotationType extends Annotation> AnnotationType getAnnotation(Class<AnnotationType> annotationType, Field field, Method[] getters, Method[] setters)
public Reflection.Property getProperty(Object target, String propertyName, String description) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
target
- the target on which the setter is to be called; may not be nullpropertyName
- the name of the Java object property; may not be nulldescription
- the description for the property; may be null if this is not knownNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.IllegalAccessException
- if the setter method could not be accessedInvocationTargetException
- if there was an error invoking the setter method on the targetIllegalArgumentException
- if 'target' is null, or if 'propertyName' is null or emptypublic Reflection.Property getProperty(Object target, String propertyName) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
target
- the target on which the setter is to be called; may not be nullpropertyName
- the name of the Java object property; may not be nullNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.IllegalAccessException
- if the setter method could not be accessedInvocationTargetException
- if there was an error invoking the setter method on the targetIllegalArgumentException
- if 'target' is null, or if 'propertyName' is null or emptypublic List<Reflection.Property> getAllPropertiesOn(Object target) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
target
- the target on which the setter is to be called; may not be nullNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.IllegalAccessException
- if the setter method could not be accessedInvocationTargetException
- if there was an error invoking the setter method on the targetIllegalArgumentException
- if 'target' is null, or if 'propertyName' is null or emptypublic Map<String,Reflection.Property> getAllPropertiesByNameOn(Object target) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
target
- the target on which the setter is to be called; may not be nullNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.IllegalAccessException
- if the setter method could not be accessedInvocationTargetException
- if there was an error invoking the setter method on the targetIllegalArgumentException
- if 'target' is null, or if 'propertyName' is null or emptypublic void setProperty(Object target, Reflection.Property property, Object value) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
target
- the target on which the setter is to be called; may not be nullproperty
- the property that is to be set on the targetvalue
- the new value for the propertyNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.IllegalAccessException
- if the setter method could not be accessedInvocationTargetException
- if there was an error invoking the setter method on the targetIllegalArgumentException
- if 'target' is null, 'property' is null, or 'property.getName()' is nullpublic Object getProperty(Object target, Reflection.Property property) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
target
- the target on which the setter is to be called; may not be nullproperty
- the property that is to be set on the targetNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.IllegalAccessException
- if the setter method could not be accessedInvocationTargetException
- if there was an error invoking the setter method on the targetIllegalArgumentException
- if 'target' is null, 'property' is null, or 'property.getName()' is nullpublic String getPropertyAsString(Object target, Reflection.Property property) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
target
- the target on which the setter is to be called; may not be nullproperty
- the property that is to be set on the targetNoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.IllegalAccessException
- if the setter method could not be accessedInvocationTargetException
- if there was an error invoking the setter method on the targetIllegalArgumentException
- if 'target' is null, 'property' is null, or 'property.getName()' is nullprotected void writeObjectAsString(Object obj, StringBuilder sb, boolean wrapWithBrackets)
Copyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.