|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.modeshape.common.util.Reflection
@Immutable public class Reflection
Utility class for working reflectively with objects.
Nested Class Summary | |
---|---|
static class |
Reflection.Property
A representation of a property on a Java object. |
Field Summary | |
---|---|
protected static Inflector |
INFLECTOR
|
Constructor Summary | |
---|---|
Reflection(Class<?> targetClass)
Construct a Reflection instance that cache's some information about the target class. |
Method Summary | ||
---|---|---|
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. |
|
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. |
|
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
|
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. |
|
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. |
|
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. |
|
protected void |
writeObjectAsString(Object obj,
StringBuilder sb,
boolean wrapWithBrackets)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final Inflector INFLECTOR
Constructor Detail |
---|
public Reflection(Class<?> targetClass)
targetClass
- the target class
IllegalArgumentException
- if the target class is nullMethod Detail |
---|
public 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 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 matter
public 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 matter
public 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 matter
public 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 property
NoSuchMethodException
- 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 invoked
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
InvocationTargetException
IllegalAccessException
IllegalArgumentException
protected String[] findMethodNames(String methodName)
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 constrained
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
IllegalAccessException
- if the setter method could not be accessed
InvocationTargetException
- if there was an error invoking the setter method on the target
IllegalArgumentException
- 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)
protected static String localizedString(Class<?> i18nClass, String id)
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 known
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
IllegalAccessException
- if the setter method could not be accessed
InvocationTargetException
- if there was an error invoking the setter method on the target
IllegalArgumentException
- 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 null
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
IllegalAccessException
- if the setter method could not be accessed
InvocationTargetException
- if there was an error invoking the setter method on the target
IllegalArgumentException
- 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 null
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
IllegalAccessException
- if the setter method could not be accessed
InvocationTargetException
- if there was an error invoking the setter method on the target
IllegalArgumentException
- 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 null
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
IllegalAccessException
- if the setter method could not be accessed
InvocationTargetException
- if there was an error invoking the setter method on the target
IllegalArgumentException
- 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 property
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
IllegalAccessException
- if the setter method could not be accessed
InvocationTargetException
- if there was an error invoking the setter method on the target
IllegalArgumentException
- 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 target
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
IllegalAccessException
- if the setter method could not be accessed
InvocationTargetException
- if there was an error invoking the setter method on the target
IllegalArgumentException
- 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 target
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
IllegalAccessException
- if the setter method could not be accessed
InvocationTargetException
- if there was an error invoking the setter method on the target
IllegalArgumentException
- if 'target' is null, 'property' is null, or 'property.getName()' is nullprotected void writeObjectAsString(Object obj, StringBuilder sb, boolean wrapWithBrackets)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |