@Status(stage=PRODUCTION, unitTests=COMPLETE) @Review(by="Kees Schotanus",at="2009-09-28") @Review(by="Kees Schotanus",at="2010-08-26",reason="Reviewed new functionality to getAllGetters/Setters") public final class CReflection extends Object
Modifier and Type | Field and Description |
---|---|
static Map<Class<?>,Object> |
primitiveClassToDefaultValue
Mapping of primitive class types to a default value.
|
Modifier and Type | Method and Description |
---|---|
static void |
clearAllMembers(Object object)
Clears all member fields on the supplied object.
|
static Object |
evaluatePropertyExpression(Object bean,
String propertyExpression)
Gets the value of the supplied propertyExpression from the supplied
bean.
|
static List<Field> |
getAllAnnotatedFields(Class<?> classWithAnnotatedFields,
Class<? extends Annotation> annotation)
Gets all the Fields with an annotation of the supplied annotation type,
that are directly or indirectly (at a super class) defined on the
supplied classWithAnnotatedFields.
|
static List<Method> |
getAllAnnotatedMethods(Class<?> classWithAnnotatedMethods,
Class<? extends Annotation> annotation)
Gets all the Methods with an annotation of the supplied annotation type,
that are directly or indirectly (at a super class) defined on the
supplied classWithAnnotatedMethods.
|
static List<Field> |
getAllFields(Class<?> theClass)
Gets all the fields of the supplied class.
|
static List<Method> |
getAllGetters(Class<?> theClass)
Gets all the getters declared in the supplied class or one of its super
classes, up to, but not including class Object.
|
static List<Method> |
getAllSetters(Class<?> theClass)
Gets all the setters declared in the supplied class or one of its super
classes, up to, but not including class Object.
|
static String |
getCallingMethod(String methodName)
Determines what method called the method with the supplied methodName.
jxpfw uses this functionality, allowing you to just code: addPropertyException(Exception) without the need to supply the actual name of the property. |
static List<Field> |
getDeclaredAnnotatedFields(Class<?> classWithAnnotatedFields,
Class<? extends Annotation> annotation)
Gets all the Fields with an annotation of the supplied annotation type,
that are directly defined on the supplied classWithAnnotatedFields.
|
static List<Method> |
getDeclaredAnnotatedMethods(Class<?> classWithAnnotatedMethods,
Class<? extends Annotation> annotation)
Gets all the Methods with an annotation of the supplied annotation type,
that are directly defined on the supplied classWithAnnotatedMethods.
|
static List<Method> |
getDeclaredGetters(Class<?> theClass)
Gets all the getters declared in the supplied class.
|
static List<Method> |
getDeclaredPublicStaticMethods(Class<?> theClass)
Gets all the public static methods declared in the supplied class.
|
static List<Method> |
getDeclaredSetters(Class<?> theClass)
Gets all the setters declared in the supplied class.
|
static String |
getScalarPropertyName(String propertyName)
Gets the scalar (without an index) property name of the supplied
propertyName.
|
static Object |
invokeGetMethod(Object object,
String propertyName)
Invokes the get...() method corresponding to the supplied propertyName.
|
static Object |
invokeGetMethod(Object object,
String propertyName,
int index)
Invokes the get...(index) method corresponding to the supplied
propertyName.
|
static void |
invokeSetMethod(Object object,
String propertyName,
Object propertyValue,
Class<?> propertyClass)
Invokes the set...(propertyValue) method that corresponds to the supplied
property.
|
static String |
methodToProperty(String method)
Converts the supplied method name to the corresponding property name.
|
static Method |
propertyToGetMethod(Object object,
String property)
Gets the method that would get the supplied property.
|
static String |
propertyToGetMethod(String property)
Gets the name of the method that would get the supplied property.
|
static String |
propertyToSetMethod(String property)
Creates the name of the set...() method from the supplied property name.
|
public static final Map<Class<?>,Object> primitiveClassToDefaultValue
Method.invoke(Object, Object...)
and one of the arguments is of
primitive type.public static List<Method> getDeclaredPublicStaticMethods(Class<?> theClass)
theClass
- The class to get all the public static methods from.public static List<Method> getAllGetters(Class<?> theClass)
theClass
- The class to get all the getters from.getDeclaredGetters(Class)
public static List<Method> getDeclaredGetters(Class<?> theClass)
theClass
- The class to get all the getters from.public static List<Method> getAllSetters(Class<?> theClass)
theClass
- The class to get all the setters from.getDeclaredSetters(Class)
public static List<Method> getDeclaredSetters(Class<?> theClass)
theClass
- The class to get all the setters from.public static List<Field> getAllFields(Class<?> theClass)
theClass
- The class to get the fields from.public static Object evaluatePropertyExpression(Object bean, String propertyExpression)
bean
- The bean to get the property value from.propertyExpression
- The property expression to evaluate.
IllegalArgumentException
- When the supplied propertyExpression is
empty or when it can not be evaluated.NullPointerException
- When either the supplied bean or
propertyExpression is null.public static String getScalarPropertyName(String propertyName)
propertyName
- The property name to get the scalar property name
from.IllegalArgumentException
- When the supplied propertyName is
empty or, when there is no matching set of opening and closing
brackets.NullPointerException
- When the supplied propertyName is null.public static Object invokeGetMethod(Object object, String propertyName) throws ReflectiveOperationException
object
- Object on which the get...() method will be invoked.propertyName
- Name of the property.
ReflectiveOperationException
- When an error occurs while invoking the method.NullPointerException
- When the supplied object is null or when the
supplied propertyName is null.public static Object invokeGetMethod(Object object, String propertyName, int index) throws ReflectiveOperationException
object
- Object on which the get...() method will be invoked.propertyName
- Name of the property.
index
- Index of the array element that should be fetched.ReflectiveOperationException
- When an error occurs while invoking the method.NullPointerException
- When the supplied object is null or when the
supplied propertyName is null.public static void invokeSetMethod(Object object, String propertyName, Object propertyValue, Class<?> propertyClass) throws ReflectiveOperationException
object
- Object on which the set...() method will be invoked.propertyName
- Name of the property.propertyValue
- Value to which the property should be set.propertyClass
- Class of the property.
ReflectiveOperationException
- When an error occurs while invoking the method.NullPointerException
- When the supplied object is null or when the supplied propertyName is null.public static String methodToProperty(String method)
method
- Method name.
IllegalArgumentException
- When the supplied method could not be
converted to a property name or when the method is empty.NullPointerException
- When the supplied method is null.public static String propertyToGetMethod(String property)
property
- The name of the property.IllegalArgumentException
- When the supplied property is empty.NullPointerException
- When the supplied property is null.public static Method propertyToGetMethod(Object object, String property) throws NoSuchMethodException
object
- The object that contains the supplied property.property
- Name of the property.NoSuchMethodException
- When no getters method exists that
corresponds to the supplied property.NullPointerException
- When either the supplied object or property
is null.IllegalArgumentException
- When the supplied property is empty.public static String propertyToSetMethod(String property)
property
- Name of the property.IllegalArgumentException
- When the supplied property is empty.NullPointerException
- When the supplied property is null.public static void clearAllMembers(Object object)
object
- The object for which all fields must be cleared.NullPointerException
- When the supplied object is null.public static String getCallingMethod(String methodName)
CReflection.getCallingMethod("org.josef.util.CReflectionTest.<init>
results in: "newInstance", for example.methodName
- Name of the method for which the caller must be returned.public static List<Field> getDeclaredAnnotatedFields(Class<?> classWithAnnotatedFields, Class<? extends Annotation> annotation)
classWithAnnotatedFields
- The class to check for Fields with
annotations of type annotation.annotation
- The type of annotation the Field should have in order
to be returned.NullPointerException
- When either the supplied
classWithAnnotatedFields or annotation is null.public static List<Field> getAllAnnotatedFields(Class<?> classWithAnnotatedFields, Class<? extends Annotation> annotation)
classWithAnnotatedFields
- The class to check for Fields with
annotations of type annotation.annotation
- The type of annotation the Field should have in order
to be returned.NullPointerException
- When either the supplied
classWithAnnotatedFields or annotation is null.public static List<Method> getDeclaredAnnotatedMethods(Class<?> classWithAnnotatedMethods, Class<? extends Annotation> annotation)
classWithAnnotatedMethods
- The class to check for Methods with
annotations of type annotation.annotation
- The type of annotation the Method should have in order
to be returned.NullPointerException
- When either the supplied
classWithAnnotatedMethods or annotation is null.public static List<Method> getAllAnnotatedMethods(Class<?> classWithAnnotatedMethods, Class<? extends Annotation> annotation)
classWithAnnotatedMethods
- The class to check for Methods with
annotations of type annotation.annotation
- The type of annotation the Method should have in order
to be returned.NullPointerException
- When either the supplied
classWithAnnotatedMethods or annotation is null.Copyright © 2008–2018. All rights reserved.