public class GriffonClassUtils extends Object
Contains utility methods copied from commons-lang and commons-beanutils in order to reduce dependencies on external libraries.
Contains code copied from commons-beanutils and commons-langs
Modifier and Type | Field and Description |
---|---|
static Object[] |
EMPTY_ARGS |
static Class<?>[] |
EMPTY_CLASS_ARRAY |
static Object[] |
EMPTY_OBJECT_ARRAY |
static String |
INNER_CLASS_SEPARATOR
The inner class separator String:
"$" . |
static char |
INNER_CLASS_SEPARATOR_CHAR
The inner class separator character:
'$' == 36 . |
static String |
PACKAGE_SEPARATOR
The package separator String:
"." . |
static char |
PACKAGE_SEPARATOR_CHAR
The package separator character:
'.' == 46 . |
static Map<Class<?>,Class<?>> |
PRIMITIVE_TYPE_COMPATIBLE_CLASSES |
static Map<String,String> |
PRIMITIVE_TYPE_COMPATIBLE_TYPES |
Constructor and Description |
---|
GriffonClassUtils() |
Modifier and Type | Method and Description |
---|---|
static Object[] |
collectionToObjectArray(Collection<?> c)
Convenience method for converting a collection to an Object[]
|
static Class<?>[] |
convertToTypeArray(Object[] args)
param instance array to the type array
|
static Collection<?> |
createConcreteCollection(Class<?> interfaceType)
Creates a concrete collection for the supplied interface
|
static boolean |
getBooleanFromMap(String key,
Map<String,Object> map)
Retrieves a boolean value from a Map for the given key
|
static Field |
getField(Class<?> clazz,
String name)
Get the a declared field on a class
|
static Field |
getField(Object obj,
String name)
Get the a declared field on an object
|
static Object |
getFieldValue(Object obj,
String name)
Get the value of a declared field on an object
|
static String |
getGetterName(String propertyName)
Calculate the name for a getter method to retrieve the specified property
|
static String |
getPackageName(Class<?> cls)
Gets the package name of a
Class . |
static String |
getPackageName(Object object,
String valueIfNull)
Gets the package name of an
Object . |
static String |
getPackageName(String className)
Gets the package name from a
String . |
static PropertyDescriptor[] |
getPropertiesAssignableToType(Class<?> clazz,
Class<?> propertySuperType)
Retrieves all the properties of the given class which are assignable to the given type
|
static PropertyDescriptor[] |
getPropertiesOfType(Class<?> clazz,
Class<?> propertyType)
Retrieves all the properties of the given class for the given type
|
static PropertyDescriptor |
getProperty(Class<?> clazz,
String propertyName,
Class<?> propertyType)
Retrieves a property of the given class of the specified name and type
|
static Object |
getProperty(Object bean,
String name)
Return the value of the specified property of the specified bean,
no matter which property reference format is used, with no
type conversions.
|
static PropertyDescriptor |
getPropertyDescriptor(Class<?> clazz,
String name)
Retrieve the property descriptor for the specified property of the
specified class, or return
null if there is no such
descriptor. |
static PropertyDescriptor |
getPropertyDescriptor(Object bean,
String name)
Retrieve the property descriptor for the specified property of the
specified bean, or return
null if there is no such
descriptor. |
static PropertyDescriptor |
getPropertyDescriptorForValue(Object instance,
Object propertyValue)
Retrieves a PropertyDescriptor for the specified instance and property value
|
static PropertyDescriptor[] |
getPropertyDescriptors(Class<?> beanClass)
Retrieve the property descriptors for the specified class,
introspecting and caching them the first time a particular bean class
is encountered.
|
static String |
getPropertyForGetter(String getterName)
Returns a property name equivalent for the given getter name or null if it is not a getter
|
static String |
getPropertyForSetter(String setterName)
Returns a property name equivalent for the given setter name or null if it is not a getter
|
static Object |
getPropertyOrStaticPropertyOrFieldValue(Object obj,
String name)
Looks for a property of the reference instance with a given name.
|
static Class<?> |
getPropertyType(Class<?> clazz,
String propertyName)
Returns the type of the given property contained within the specified class
|
static Object |
getPropertyValue(Object bean,
String name)
Returns the value of a property.
|
static Object |
getPropertyValueOfNewInstance(Class<?> clazz,
String propertyName)
Returns the value of the specified property and type from an instance of the specified Griffon class
|
static Object |
getPropertyValueOfNewInstance(Class<?> clazz,
String propertyName,
Class<?> propertyType)
Returns the value of the specified property and type from an instance of the specified Griffon class
|
static Method |
getReadMethod(Class<?> clazz,
PropertyDescriptor descriptor)
Return an accessible property getter method for this property,
if there is one; otherwise return
null . |
static Method |
getReadMethod(PropertyDescriptor descriptor)
Return an accessible property getter method for this property,
if there is one; otherwise return
null . |
static String |
getSetterName(String propertyName)
Retrieves the name of a setter for the specified property name
|
static String |
getShortClassName(Class<?> cls)
Gets the class name minus the package name from a
Class . |
static String |
getShortClassName(Object object,
String valueIfNull)
Gets the class name minus the package name for an
Object . |
static String |
getShortClassName(String className)
Gets the class name minus the package name from a String.
|
static Object |
getStaticPropertyValue(Class<?> clazz,
String name)
Get a static property value, which has a public static getter or is just a public static field.
|
static Method |
getWriteMethod(Class<?> clazz,
PropertyDescriptor descriptor)
Return an accessible property setter method for this property,
if there is one; otherwise return
null . |
static Method |
getWriteMethod(PropertyDescriptor descriptor)
Return an accessible property setter method for this property,
if there is one; otherwise return
null . |
static Object |
instantiate(Class<?> clazz,
Object[] args) |
static Object |
instantiateClass(Class<?> clazz)
Instantiates a Class, wrapping any exceptions in a RuntimeException.
|
static Object |
invokeExactInstanceMethod(Object object,
String methodName) |
static Object |
invokeExactInstanceMethod(Object object,
String methodName,
Object... args) |
static Object |
invokeExactInstanceMethod(Object object,
String methodName,
Object arg) |
static Object |
invokeExactStaticMethod(Class<?> type,
String methodName) |
static Object |
invokeExactStaticMethod(Class<?> type,
String methodName,
Object... args) |
static Object |
invokeExactStaticMethod(Class<?> type,
String methodName,
Object arg) |
static Object |
invokeInstanceMethod(Object object,
String methodName) |
static Object |
invokeInstanceMethod(Object object,
String methodName,
Object... args) |
static Object |
invokeInstanceMethod(Object object,
String methodName,
Object arg) |
static Object |
invokeStaticMethod(Class<?> type,
String methodName) |
static Object |
invokeStaticMethod(Class<?> type,
String methodName,
Object... args) |
static Object |
invokeStaticMethod(Class<?> type,
String methodName,
Object arg) |
static boolean |
isArtifactMethod(Method method)
Finds out if the given
Method belongs to the set of
predefined Artifact methods by convention. |
static boolean |
isArtifactMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs to the set of
predefined Artifact methods by convention. |
static boolean |
isAssignableOrConvertibleFrom(Class<?> clazz,
Class<?> type)
Returns true if the specified clazz parameter is either the same as, or is a superclass or super interface
of, the specified type parameter.
|
static boolean |
isBasicMethod(Method method)
Finds out if the given
Method belongs either to the
Object class or the GroovyObject class. |
static boolean |
isBasicMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs either to the
Object class or the GroovyObject class. |
static boolean |
isClassBelowPackage(Class<?> clazz,
List<?> packageList)
Returns whether the specified class is either within one of the specified packages or
within a subpackage of one of the packages
|
static boolean |
isContributionMethod(Method method)
Finds out if the given Method represents a contribution method
by matching its name against the following pattern:
"^with[A-Z][a-z0-9_]*[\w]*$"
|
static boolean |
isContributionMethod(MethodDescriptor method)
Finds out if the given Method represents a contribution method
by matching its name against the following pattern:
"^with[A-Z][a-z0-9_]*[\w]*$"
|
static boolean |
isContributionMethod(String name)
Finds out if the given string represents the name of a
contribution method by matching against the following pattern:
"^with[A-Z][a-z0-9_]*[\w]*$"
|
static boolean |
isEventHandler(Method method)
Finds out if the given Method represents an event handler
by matching its name against the following pattern:
"^on[A-Z][\\w]*$"
|
static boolean |
isEventHandler(MethodDescriptor method)
Finds out if the given Method represents an event handler
by matching its name against the following pattern:
"^on[A-Z][\\w]*$"
|
static boolean |
isEventHandler(String name)
Finds out if the given string represents the name of an
event handler by matching against the following pattern:
"^on[A-Z][\\w]*$"
|
static boolean |
isEventPublisherMethod(Method method)
Finds out if the given
Method belongs to the set of
predefined event publisher methods by convention. |
static boolean |
isEventPublisherMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs to the set of
predefined event publisher methods by convention. |
static boolean |
isGetter(String name,
Class[] args)
Returns true if the name of the method specified and the number of arguments make it a javabean property
|
static boolean |
isGetterMethod(Method method)
Finds out if the given
Method is a getter method. |
static boolean |
isGetterMethod(MethodDescriptor method)
Finds out if the given
MetaMethod is a getter method. |
static boolean |
isGroovyInjectedMethod(Method method)
Finds out if the given
Method was injected by the Groovy
compiler. |
static boolean |
isGroovyInjectedMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor was injected by the Groovy
compiler. |
static boolean |
isInstanceMethod(Method method)
Finds out if the given
Method is an instance method, i.e,
it is public and non-static. |
static boolean |
isInstanceMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor is an instance method, i.e,
it is public and non-static. |
static boolean |
isMatchBetweenPrimitiveAndWrapperTypes(Class<?> leftType,
Class<?> rightType)
Detect if left and right types are matching types.
|
static boolean |
isMatchBetweenPrimitiveAndWrapperTypes(String leftType,
String rightType)
Detect if left and right types are matching types.
|
static boolean |
isMessageSourceMethod(Method method)
Finds out if the given
Method belongs to the set of
predefined message source methods by convention. |
static boolean |
isMessageSourceMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs to the set of
predefined message source methods by convention. |
static boolean |
isMvcMethod(Method method)
Finds out if the given
Method belongs to the set of
predefined MVC methods by convention. |
static boolean |
isMvcMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs to the set of
predefined MVC methods by convention. |
static boolean |
isObservableMethod(Method method)
Finds out if the given
Method belongs to the set of
predefined observable methods by convention. |
static boolean |
isObservableMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs to the set of
predefined observable methods by convention. |
static boolean |
isPlainMethod(Method method)
Finds out if the given
Method matches the following criteria:
isInstanceMethod(method)
! isBasicMethod(method)
! isGroovyInjectedMethod(method)
! isThreadingMethod(method)
! isArtifactMethod(method)
! isMvcMethod(method)
! isServiceMethod(method)
! isEventPublisherMethod(method)
! isObservableMethod(method)
! isResourceHandlerMethod(method)
! isGetterMethod(method)
! isSetterMethod(method)
! isContributionMethod(method)
|
static boolean |
isPlainMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor matches the following criteria:
isInstanceMethod(method)
! isBasicMethod(method)
! isGroovyInjectedMethod(method)
! isThreadingMethod(method)
! isArtifactMethod(method)
! isMvcMethod(method)
! isServiceMethod(method)
! isEventPublisherMethod(method)
! isObservableMethod(method)
! isResourceHandlerMethod(method)
! isGetterMethod(method)
! isSetterMethod(method)
! isContributionMethod(method)
|
static boolean |
isPropertyInherited(Class<?> clz,
String propertyName)
Checks whether the specified property is inherited from a super class
|
static boolean |
isPropertyOfType(Class<?> clazz,
String propertyName,
Class<?> type)
Returns true if the specified property in the specified class is of the specified type
|
static boolean |
isPublicField(Object obj,
String name)
Work out if the specified object has a public field with the name supplied.
|
static boolean |
isPublicStatic(Field f)
Determine whether the field is declared public static
|
static boolean |
isPublicStatic(Method m)
Determine whether the method is declared public static
|
static boolean |
isReadable(Object bean,
String name)
Return
true if the specified property name identifies
a readable property on the specified bean; otherwise, return
false . |
static boolean |
isResourceHandlerMethod(Method method)
Finds out if the given
Method belongs to the set of
predefined resources methods by convention. |
static boolean |
isResourceHandlerMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs to the set of
predefined resources methods by convention. |
static boolean |
isResourceResolverMethod(Method method)
Finds out if the given
Method belongs to the set of
predefined resource resolver methods by convention. |
static boolean |
isResourceResolverMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs to the set of
predefined resource resolver methods by convention. |
static boolean |
isSetter(String name,
Class[] args) |
static boolean |
isSetterMethod(Method method)
Finds out if the given
Method is a setter method. |
static boolean |
isSetterMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor is a setter method. |
static boolean |
isStaticProperty(Class<?> clazz,
String propertyName)
Work out if the specified property is readable and static.
|
static boolean |
isThreadingMethod(Method method)
Finds out if the given
Method belongs to the set of
predefined threading methods by convention. |
static boolean |
isThreadingMethod(MethodDescriptor method)
Finds out if the given
MethodDescriptor belongs to the set of
predefined threading methods by convention. |
static void |
isTrue(boolean expression,
String message)
Validate that the argument condition is
true ; otherwise
throwing an exception with the specified message. |
static boolean |
isWritable(Object bean,
String name)
Return
true if the specified property name identifies
a writable property on the specified bean; otherwise, return
false . |
static void |
requireState(boolean condition)
Checks that the specified condition is met.
|
static void |
requireState(boolean condition,
String message)
Checks that the specified condition is met and throws a customized
IllegalStateException if it is. |
static void |
setField(Object bean,
String name,
Object value)
Sets the value of the specified field of the specified bean.
|
static void |
setFieldOrPropertyValue(Object bean,
String name,
Object value)
Sets or updates an object's property.
|
static void |
setFields(Object bean,
Map<String,Object> fields)
Sets or updates field values on an object.
|
static void |
setFieldsNoException(Object bean,
Map<String,Object> fields)
Sets or updates field values on an object.
|
static void |
setFieldsOrProperties(Object bean,
Map<String,Object> properties)
Sets or updates an object's properties.
|
static void |
setFieldsOrPropertiesNoException(Object bean,
Map<String,Object> properties)
Sets or updates an object's properties.
|
static void |
setFieldValue(Object bean,
String name,
Object value)
Sets or updates an object's field.
|
static void |
setProperties(Object bean,
Map<String,Object> properties)
Sets or updates properties on an object.
|
static void |
setPropertiesNoException(Object bean,
Map<String,Object> properties)
Sets or updates properties on an object.
|
static void |
setPropertiesOrFields(Object bean,
Map<String,Object> properties)
Sets or updates an object's properties.
|
static void |
setPropertiesOrFieldsNoException(Object bean,
Map<String,Object> properties)
Sets or updates an object's properties.
|
static void |
setProperty(Object bean,
String name,
Object value)
Sets the value of the specified property of the specified bean.
|
static void |
setPropertyOrFieldValue(Object bean,
String name,
Object value)
Sets or updates an object's property.
|
static void |
setPropertyValue(Object bean,
String name,
Object value)
/**
Sets or updates a property on an object.
|
public static final Class<?>[] EMPTY_CLASS_ARRAY
public static final Object[] EMPTY_OBJECT_ARRAY
public static final Object[] EMPTY_ARGS
public static final Map<Class<?>,Class<?>> PRIMITIVE_TYPE_COMPATIBLE_CLASSES
public static final Map<String,String> PRIMITIVE_TYPE_COMPATIBLE_TYPES
public static final char PACKAGE_SEPARATOR_CHAR
The package separator character: '.' == 46
.
public static final String PACKAGE_SEPARATOR
The package separator String: "."
.
public static final char INNER_CLASS_SEPARATOR_CHAR
The inner class separator character: '$' == 36
.
public static final String INNER_CLASS_SEPARATOR
The inner class separator String: "$"
.
public static void requireState(boolean condition)
public Foo(int[] array) { GriffonClassUtils.requireState(array.length > 0); }
condition
- the condition to checkIllegalStateException
- if condition
evaluates to falsepublic static void requireState(boolean condition, String message)
IllegalStateException
if it is. This method is designed primarily
for doing parameter validation in methods and constructors with multiple
parameters, as demonstrated below:
public Foo(int[] array) { GriffonClassUtils.requireState(array.length > 0, "array must not be empty"); }
condition
- the condition to checkmessage
- detail message to be used in the event that a IllegalStateException
is thrownIllegalStateException
- if condition
evaluates to falsepublic static boolean isEventHandler(@Nonnull String name)
isEventHandler("onBootstrapEnd") = true isEventHandler("mvcGroupInit") = false isEventHandler("online") = false
name
- the name of a possible event handlerpublic static boolean isEventHandler(@Nonnull Method method)
// assuming getMethod() returns an appropriate Method reference isEventHandler(getMethod("onBootstrapEnd")) = true isEventHandler(getMethod("mvcGroupInit")) = false isEventHandler(getMethod("online")) = false
method
- a Method referencepublic static boolean isEventHandler(@Nonnull MethodDescriptor method)
// assuming getMethod() returns an appropriate MethodDescriptor reference isEventHandler(getMethod("onBootstrapEnd")) = true isEventHandler(getMethod("mvcGroupInit")) = false isEventHandler(getMethod("online")) = false
method
- a MethodDescriptor referencepublic static boolean isBasicMethod(@Nonnull Method method)
Method
belongs either to the
Object
class or the GroovyObject
class.method
- a Method referenceObject
or
GroovyObject
, false otherwise.public static boolean isContributionMethod(@Nonnull String name)
isContributionMethod("withRest") = true isContributionMethod("withMVCGroup") = false isContributionMethod("without") = false
name
- the name of a possible contribution methodpublic static boolean isContributionMethod(@Nonnull Method method)
// assuming getMethod() returns an appropriate Method reference isContributionMethod(getMethod("withRest")) = true isContributionMethod(getMethod("withMVCGroup")) = false isContributionMethod(getMethod("without")) = false
method
- a Method referencepublic static boolean isContributionMethod(@Nonnull MethodDescriptor method)
// assuming getMethod() returns an appropriate MethodDescriptor reference isContributionMethod(getMethod("withRest")) = true isContributionMethod(getMethod("withMVCGroup")) = false isContributionMethod(getMethod("without")) = false
method
- a MethodDescriptor referencepublic static boolean isBasicMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs either to the
Object
class or the GroovyObject
class.method
- a MethodDescriptor referenceObject
or
GroovyObject
, false otherwise.public static boolean isGroovyInjectedMethod(@Nonnull Method method)
Method
was injected by the Groovy
compiler.Performs a basic checks against the method's name, returning true if the name starts with either "super$" or "this$".
method
- a Method referencepublic static boolean isGroovyInjectedMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
was injected by the Groovy
compiler.Performs a basic checks against the method's name, returning true if the name starts with either "super$" or "this$".
method
- a MethodDescriptor referencepublic static boolean isGetterMethod(@Nonnull Method method)
Method
is a getter method.
// assuming getMethod() returns an appropriate Method reference isGetterMethod(getMethod("getFoo")) = true isGetterMethod(getMethod("getfoo") ) = false isGetterMethod(getMethod("mvcGroupInit")) = false isGetterMethod(getMethod("isFoo")) = true isGetterMethod(getMethod("island")) = false
method
- a Method referencepublic static boolean isGetterMethod(@Nonnull MethodDescriptor method)
MetaMethod
is a getter method.
// assuming getMethod() returns an appropriate MethodDescriptor reference isGetterMethod(getMethod("getFoo")) = true isGetterMethod(getMethod("getfoo") ) = false isGetterMethod(getMethod("mvcGroupInit")) = false isGetterMethod(getMethod("isFoo")) = true isGetterMethod(getMethod("island")) = false
method
- a MethodDescriptor referencepublic static boolean isSetterMethod(@Nonnull Method method)
Method
is a setter method.
// assuming getMethod() returns an appropriate Method reference isGetterMethod(getMethod("setFoo")) = true isGetterMethod(getMethod("setfoo")) = false isGetterMethod(getMethod("mvcGroupInit")) = false
method
- a Method referencepublic static boolean isSetterMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
is a setter method.
// assuming getMethod() returns an appropriate MethodDescriptor reference isGetterMethod(getMethod("setFoo")) = true isGetterMethod(getMethod("setfoo")) = false isGetterMethod(getMethod("mvcGroupInit")) = false
method
- a MethodDescriptor referencepublic static boolean isArtifactMethod(@Nonnull Method method)
Method
belongs to the set of
predefined Artifact methods by convention.
// assuming getMethod() returns an appropriate Method reference isArtifactMethod(getMethod("newInstance")) = true isArtifactMethod(getMethod("griffonDestroy")) = false isArtifactMethod(getMethod("foo")) = false
method
- a Method referencepublic static boolean isArtifactMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs to the set of
predefined Artifact methods by convention.
// assuming getMethod() returns an appropriate MethodDescriptor reference isArtifactMethod(getMethod("newInstance")) = true isArtifactMethod(getMethod("griffonDestroy")) = false isArtifactMethod(getMethod("foo")) = false
method
- a MethodDescriptor referencepublic static boolean isMvcMethod(@Nonnull Method method)
Method
belongs to the set of
predefined MVC methods by convention.
// assuming getMethod() returns an appropriate Method reference isMvcMethod(getMethod("mvcGroupInit")) = true isMvcMethod(getMethod("mvcGroupDestroy")) = true isMvcMethod(getMethod("foo")) = false
method
- a Method referencepublic static boolean isMvcMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs to the set of
predefined MVC methods by convention.
// assuming getMethod() returns an appropriate MethodDescriptor reference isMvcMethod(getMethod("mvcGroupInit")) = true isMvcMethod(getMethod("mvcGroupDestroy")) = true isMvcMethod(getMethod("foo")) = false
method
- a MethodDescriptor referencepublic static boolean isThreadingMethod(@Nonnull Method method)
Method
belongs to the set of
predefined threading methods by convention.
// assuming getMethod() returns an appropriate Method reference isThreadingMethod(getMethod("execOutsideUI")) = true isThreadingMethod(getMethod("doLater")) = true isThreadingMethod(getMethod("foo")) = false
method
- a Method referencepublic static boolean isThreadingMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs to the set of
predefined threading methods by convention.
// assuming getMethod() returns an appropriate MethodDescriptor reference isThreadingMethod(getMethod("execOutsideUI")) = true isThreadingMethod(getMethod("doLater")) = true isThreadingMethod(getMethod("foo")) = false
method
- a MethodDescriptor referencepublic static boolean isEventPublisherMethod(@Nonnull Method method)
Method
belongs to the set of
predefined event publisher methods by convention.
// assuming getMethod() returns an appropriate Method reference isEventPublisherMethod(getMethod("addEventPublisher")) = true isEventPublisherMethod(getMethod("publishEvent")) = true isEventPublisherMethod(getMethod("foo")) = false
method
- a Method referencepublic static boolean isEventPublisherMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs to the set of
predefined event publisher methods by convention.
// assuming getMethod() returns an appropriate MethodDescriptor reference isEventPublisherMethod(getMethod("addEventPublisher")) = true isEventPublisherMethod(getMethod("publishEvent")) = true isEventPublisherMethod(getMethod("foo")) = false
method
- a MethodDescriptor referencepublic static boolean isObservableMethod(@Nonnull Method method)
Method
belongs to the set of
predefined observable methods by convention.
// assuming getMethod() returns an appropriate Method reference isObservableMethod(getMethod("addPropertyChangeListener")) = true isObservableMethod(getMethod("getPropertyChangeListeners")) = true isObservableMethod(getMethod("foo")) = false
method
- a Method referencepublic static boolean isObservableMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs to the set of
predefined observable methods by convention.
// assuming getMethod() returns an appropriate MethodDescriptor reference isObservableMethod(getMethod("addPropertyChangeListener")) = true isObservableMethod(getMethod("getPropertyChangeListeners")) = true isObservableMethod(getMethod("foo")) = false
method
- a MethodDescriptor referencepublic static boolean isResourceHandlerMethod(@Nonnull Method method)
Method
belongs to the set of
predefined resources methods by convention.
// assuming getMethod() returns an appropriate Method reference isResourceHandlerMethod(getMethod("getResourceAsURL")) = true isResourceHandlerMethod(getMethod("getResourceAsStream")) = true isResourceHandlerMethod(getMethod("foo")) = false
method
- a Method referencepublic static boolean isResourceHandlerMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs to the set of
predefined resources methods by convention.
// assuming getMethod() returns an appropriate MethodDescriptor reference isResourceHandlerMethod(getMethod("getResourceAsURL")) = true isResourceHandlerMethod(getMethod("getResourceAsStream")) = true isResourceHandlerMethod(getMethod("foo")) = false
method
- a MethodDescriptor referencepublic static boolean isMessageSourceMethod(@Nonnull Method method)
Method
belongs to the set of
predefined message source methods by convention.
// assuming getMethod() returns an appropriate Method reference isMessageSourceMethod(getMethod("getMessage")) = true isMessageSourceMethod(getMethod("foo")) = false
method
- a Method referencepublic static boolean isMessageSourceMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs to the set of
predefined message source methods by convention.
// assuming getMethod() returns an appropriate MethodDescriptor reference isMessageSourceMethod(getMethod("getMessage")) = true isMessageSourceMethod(getMethod("foo")) = false
method
- a MethodDescriptor referencepublic static boolean isResourceResolverMethod(@Nonnull Method method)
Method
belongs to the set of
predefined resource resolver methods by convention.
// assuming getMethod() returns an appropriate Method reference isResourceResolverMethod(getMethod("resolveResource")) = true isResourceResolverMethod(getMethod("foo")) = false
method
- a Method referencepublic static boolean isResourceResolverMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
belongs to the set of
predefined resource resolver methods by convention.
// assuming getMethod() returns an appropriate MethodDescriptor reference isResourceResolverMethod(getMethod("resolveResource")) = true isResourceResolverMethod(getMethod("foo")) = false
method
- a MethodDescriptor referencepublic static boolean isInstanceMethod(@Nonnull Method method)
Method
is an instance method, i.e,
it is public and non-static.method
- a Method referencepublic static boolean isInstanceMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
is an instance method, i.e,
it is public and non-static.method
- a MethodDescriptor referencepublic static boolean isPlainMethod(@Nonnull Method method)
Method
matches the following criteria:method
- a Method referencepublic static boolean isPlainMethod(@Nonnull MethodDescriptor method)
MethodDescriptor
matches the following criteria:method
- a MethodDescriptor referencepublic static boolean isPropertyOfType(Class<?> clazz, String propertyName, Class<?> type)
clazz
- The class which contains the propertypropertyName
- The property nametype
- The type to check@Nonnull public static Object instantiateClass(@Nonnull Class<?> clazz)
clazz
- target Class for which an object will be instantiatedBeanInstantiationException
- if an error occurs when creating the object@Nonnull public static Object instantiate(@Nonnull Class<?> clazz, @Nullable Object[] args)
@Nullable public static Object getPropertyValueOfNewInstance(@Nullable Class<?> clazz, @Nullable String propertyName, Class<?> propertyType)
clazz
- The name of the class which contains the propertypropertyName
- The property namepropertyType
- The property typepublic static Object getPropertyValueOfNewInstance(Class<?> clazz, String propertyName)
clazz
- The name of the class which contains the propertypropertyName
- The property namepublic static PropertyDescriptor getPropertyDescriptorForValue(Object instance, Object propertyValue)
instance
- The instancepropertyValue
- The value of the property@Nullable public static Class<?> getPropertyType(@Nullable Class<?> clazz, @Nullable String propertyName)
clazz
- The class which contains the propertypropertyName
- The name of the property@Nonnull public static PropertyDescriptor[] getPropertiesOfType(@Nullable Class<?> clazz, @Nullable Class<?> propertyType)
clazz
- The class to retrieve the properties frompropertyType
- The type of the properties you wish to retrievepublic static PropertyDescriptor[] getPropertiesAssignableToType(Class<?> clazz, Class<?> propertySuperType)
clazz
- The class to retrieve the properties frompropertySuperType
- The type of the properties you wish to retrievepublic static PropertyDescriptor getProperty(Class<?> clazz, String propertyName, Class<?> propertyType)
clazz
- The class to retrieve the property frompropertyName
- The name of the propertypropertyType
- The type of the propertypublic static Object[] collectionToObjectArray(Collection<?> c)
c
- The collectionpublic static boolean isMatchBetweenPrimitiveAndWrapperTypes(@Nonnull Class<?> leftType, @Nonnull Class<?> rightType)
leftType
- rightType
- public static boolean isMatchBetweenPrimitiveAndWrapperTypes(@Nonnull String leftType, @Nonnull String rightType)
leftType
- rightType
- public static boolean isStaticProperty(@Nonnull Class<?> clazz, @Nonnull String propertyName)
Work out if the specified property is readable and static. Java introspection does not recognize this concept of static properties but Groovy does. We also consider public static fields as static properties with no getters/setters
clazz
- The class to check for static propertypropertyName
- The property namepublic static boolean isPublicStatic(@Nonnull Method m)
m
- the method to be testedpublic static boolean isPublicStatic(@Nonnull Field f)
f
- the field to be tested@Nonnull public static String getGetterName(@Nonnull String propertyName)
propertyName
- the name of the property@Nullable public static Object getStaticPropertyValue(@Nonnull Class<?> clazz, @Nonnull String name)
Get a static property value, which has a public static getter or is just a public static field.
clazz
- The class to check for static propertyname
- The property name@Nullable public static Object getPropertyOrStaticPropertyOrFieldValue(@Nonnull Object obj, @Nonnull String name)
Looks for a property of the reference instance with a given name.
If found its value is returned. We follow the Java bean conventions with augmentation for groovy support and static fields/properties. We will therefore match, in this order:
@Nullable public static Object getFieldValue(@Nonnull Object obj, @Nonnull String name)
obj
- the instance that owns the fieldname
- the name of the file to lookup@Nullable public static Field getField(@Nonnull Object obj, @Nonnull String name)
obj
- the instance that owns the fieldname
- the name of the file to lookup@Nullable public static Field getField(@Nonnull Class<?> clazz, @Nonnull String name)
clazz
- the clazz that owns the fieldname
- the name of the file to lookuppublic static boolean isPublicField(@Nonnull Object obj, @Nonnull String name)
obj
- the instance that owns the fieldname
- the name of the file to lookuppublic static boolean isPropertyInherited(@Nullable Class<?> clz, @Nonnull String propertyName)
clz
- The class to checkpropertyName
- The property name@Nonnull public static Collection<?> createConcreteCollection(@Nonnull Class<?> interfaceType)
interfaceType
- The interface@Nonnull public static String getSetterName(@Nonnull String propertyName)
propertyName
- The property namepublic static boolean isGetter(@Nullable String name, @Nullable Class[] args)
name
- True if its a Javabean propertyargs
- The arguments@Nullable public static String getPropertyForGetter(@Nullable String getterName)
getterName
- The getter name@Nullable public static String getPropertyForSetter(@Nullable String setterName)
setterName
- The setter namepublic static boolean isAssignableOrConvertibleFrom(@Nullable Class<?> clazz, @Nullable Class<?> type)
clazz
- type
- Class.isAssignableFrom(Class)
public static boolean getBooleanFromMap(@Nullable String key, @Nullable Map<String,Object> map)
key
- The key that references the boolean valuemap
- The map to look inpublic static boolean isClassBelowPackage(@Nonnull Class<?> clazz, @Nonnull List<?> packageList)
clazz
- The classpackageList
- The list of packagespublic static void setPropertiesOrFields(@Nonnull Object bean, @Nonnull Map<String,Object> properties) throws PropertyException
PropertyDescriptor
; next it will try direct field
access if the property was not found.bean
- the target object on which properties will be setproperties
- names and values for properties to be setPropertyException
- if a property could be foundpublic static void setPropertiesOrFieldsNoException(@Nonnull Object bean, @Nonnull Map<String,Object> properties)
PropertyDescriptor
; next it will try direct field
access if the property was not found.bean
- the target object on which properties will be setproperties
- names and values for properties to be setpublic static void setPropertyOrFieldValue(@Nonnull Object bean, @Nonnull String name, @Nullable Object value) throws PropertyException
PropertyDescriptor
; next it will try direct field
access if the property was not found.bean
- the target object on which the property will be setname
- the name of the property to setvalue
- the value to be setPropertyException
- if the property could not be foundpublic static void setFieldsOrProperties(@Nonnull Object bean, @Nonnull Map<String,Object> properties) throws FieldException
PropertyDescriptor
if a
matching field name was not found.bean
- the target object on which properties will be setproperties
- names and values for properties to be setFieldException
- if the field could not be foundpublic static void setFieldsOrPropertiesNoException(@Nonnull Object bean, @Nonnull Map<String,Object> properties)
PropertyDescriptor
if a
matching field name was not found.bean
- the target object on which properties will be setproperties
- names and values for properties to be setpublic static void setFieldOrPropertyValue(@Nonnull Object bean, @Nonnull String name, @Nullable Object value) throws FieldException
PropertyDescriptor
if a
matching field name was not found.bean
- the target object on which the property will be setname
- the name of the property to setvalue
- the value to be setFieldException
- if the property could not be foundpublic static void setFields(@Nonnull Object bean, @Nonnull Map<String,Object> fields) throws FieldException
bean
- the target object on which field values will be setfields
- names and values of fields to be setFieldException
- if a field could not be foundpublic static void setFieldsNoException(@Nonnull Object bean, @Nonnull Map<String,Object> fields)
bean
- the target object on which field values will be setfields
- names and values of fields to be setpublic static void setFieldValue(@Nonnull Object bean, @Nonnull String name, @Nullable Object value) throws FieldException
bean
- the target object on which the field will be setname
- the name of the field to setvalue
- the value to be setFieldException
- if the field could not be foundpublic static void setProperties(@Nonnull Object bean, @Nonnull Map<String,Object> properties) throws PropertyException
bean
- the target object on which properties will be setproperties
- names and values of properties to be setPropertyException
- if a property could not be foundpublic static void setPropertiesNoException(@Nonnull Object bean, @Nonnull Map<String,Object> properties)
bean
- the target object on which properties will be setproperties
- names and values of properties to be setpublic static void setPropertyValue(@Nonnull Object bean, @Nonnull String name, @Nullable Object value) throws PropertyException
bean
- the target object on which the property will be setname
- the name of the property to setvalue
- the value to be setPropertyException
- if the property could not be found@Nullable public static Object getPropertyValue(@Nonnull Object bean, @Nonnull String name) throws PropertyException
bean
- the owner of the propertyname
- the name of the property to retrievePropertyException
- if the property could not be found@Nullable public static PropertyDescriptor getPropertyDescriptor(@Nonnull Object bean, @Nonnull String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Retrieve the property descriptor for the specified property of the
specified bean, or return null
if there is no such
descriptor.
bean
- Bean for which a property descriptor is requestedname
- name of the property for which a property descriptor
is requestedIllegalAccessException
- if the caller does not have
access to the property accessor methodIllegalArgumentException
- if bean
or
name
is nullInvocationTargetException
- if the property accessor method
throws an exceptionNoSuchMethodException
- if an accessor method for this
property cannot be found@Nullable public static PropertyDescriptor getPropertyDescriptor(@Nonnull Class<?> clazz, @Nonnull String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Retrieve the property descriptor for the specified property of the
specified class, or return null
if there is no such
descriptor.
clazz
- class for which a property descriptor is requestedname
- name of the property for which a property descriptor
is requestedIllegalAccessException
- if the caller does not have
access to the property accessor methodIllegalArgumentException
- if bean
or
name
is nullInvocationTargetException
- if the property accessor method
throws an exceptionNoSuchMethodException
- if an accessor method for this
property cannot be found@Nonnull public static PropertyDescriptor[] getPropertyDescriptors(@Nonnull Class<?> beanClass)
Retrieve the property descriptors for the specified class, introspecting and caching them the first time a particular bean class is encountered.
beanClass
- Bean class for which property descriptors are requestedIllegalArgumentException
- if beanClass
is null@Nullable public static Method getReadMethod(@Nonnull PropertyDescriptor descriptor)
Return an accessible property getter method for this property,
if there is one; otherwise return null
.
descriptor
- Property descriptor to return a getter forpublic static boolean isReadable(@Nonnull Object bean, @Nonnull String name)
Return true
if the specified property name identifies
a readable property on the specified bean; otherwise, return
false
.
bean
- Bean to be examinedname
- Property name to be evaluatedtrue
if the property is readable,
otherwise false
IllegalArgumentException
- if bean
or name
is null
@Nullable public static Method getWriteMethod(@Nonnull PropertyDescriptor descriptor)
Return an accessible property setter method for this property,
if there is one; otherwise return null
.
descriptor
- Property descriptor to return a setter forpublic static boolean isWritable(@Nonnull Object bean, @Nonnull String name)
Return true
if the specified property name identifies
a writable property on the specified bean; otherwise, return
false
.
bean
- Bean to be examinedname
- Property name to be evaluatedtrue
if the property is writable,
otherwise false
IllegalArgumentException
- if bean
or name
is null
public static void setField(@Nonnull Object bean, @Nonnull String name, @Nullable Object value) throws NoSuchFieldException, IllegalAccessException, FieldException
bean
- Bean whose field is to be mutatedname
- Name of the field to be mutatedvalue
- The value to be set on the propertyIllegalAccessException
- if the caller does not have
access to the fieldIllegalArgumentException
- if bean
or
name
is nullNoSuchFieldException
- if the named field cannot be foundFieldException
- if the field cannot be setpublic static void setProperty(@Nonnull Object bean, @Nonnull String name, @Nullable Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, PropertyException
bean
- Bean whose property is to be mutatedname
- Name of the property to be mutatedvalue
- The value to be set on the propertyIllegalAccessException
- if the caller does not have
access to the property accessor methodIllegalArgumentException
- if bean
or
name
is nullInvocationTargetException
- if the property accessor method
throws an exceptionNoSuchMethodException
- if an accessor method for this
property cannot be foundPropertyException
- if the property cannot be set@Nullable public static Object getProperty(@Nonnull Object bean, @Nonnull String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
bean
- Bean whose property is to be extractedname
- Possibly indexed and/or nested name of the property
to be extractedIllegalAccessException
- if the caller does not have
access to the property accessor methodIllegalArgumentException
- if bean
or
name
is nullInvocationTargetException
- if the property accessor method
throws an exceptionNoSuchMethodException
- if an accessor method for this
property cannot be found@Nullable public static Method getReadMethod(@Nonnull Class<?> clazz, @Nonnull PropertyDescriptor descriptor)
Return an accessible property getter method for this property,
if there is one; otherwise return null
.
clazz
- The class of the read method will be invoked ondescriptor
- Property descriptor to return a getter for@Nullable public static Method getWriteMethod(@Nonnull Class<?> clazz, @Nonnull PropertyDescriptor descriptor)
Return an accessible property setter method for this property,
if there is one; otherwise return null
.
clazz
- The class of the write method will be invoked ondescriptor
- Property descriptor to return a setter forpublic static void isTrue(boolean expression, String message)
Validate that the argument condition is true
; otherwise
throwing an exception with the specified message. This method is useful when
validating according to an arbitrary boolean expression, such as validating a
primitive number or using your own custom validation expression.
isTrue( (i > 0), "The value must be greater than zero"); isTrue( myObject.isOk(), "The object is not OK");
expression
- the boolean expression to checkmessage
- the exception message if invalidIllegalArgumentException
- if expression is false
@Nullable public static Object invokeInstanceMethod(@Nonnull Object object, @Nonnull String methodName)
@Nullable public static Object invokeInstanceMethod(@Nonnull Object object, @Nonnull String methodName, Object arg)
@Nullable public static Object invokeInstanceMethod(@Nonnull Object object, @Nonnull String methodName, Object... args)
@Nullable public static Object invokeExactInstanceMethod(@Nonnull Object object, @Nonnull String methodName)
@Nullable public static Object invokeExactInstanceMethod(@Nonnull Object object, @Nonnull String methodName, Object arg)
@Nullable public static Object invokeExactInstanceMethod(@Nonnull Object object, @Nonnull String methodName, Object... args)
@Nullable public static Object invokeStaticMethod(@Nonnull Class<?> type, @Nonnull String methodName)
@Nullable public static Object invokeStaticMethod(@Nonnull Class<?> type, @Nonnull String methodName, Object arg)
@Nullable public static Object invokeStaticMethod(@Nonnull Class<?> type, @Nonnull String methodName, Object... args)
@Nullable public static Object invokeExactStaticMethod(@Nonnull Class<?> type, @Nonnull String methodName)
@Nullable public static Object invokeExactStaticMethod(@Nonnull Class<?> type, @Nonnull String methodName, Object arg)
@Nullable public static Object invokeExactStaticMethod(@Nonnull Class<?> type, @Nonnull String methodName, Object... args)
@Nonnull public static String getShortClassName(@Nullable Object object, @Nonnull String valueIfNull)
Gets the class name minus the package name for an Object
.
object
- the class to get the short name for, may be nullvalueIfNull
- the value to return if null@Nonnull public static String getShortClassName(@Nullable Class<?> cls)
Gets the class name minus the package name from a Class
.
cls
- the class to get the short name for.@Nonnull public static String getShortClassName(@Nullable String className)
Gets the class name minus the package name from a String.
The string passed in is assumed to be a class name - it is not checked.
className
- the className to get the short name for@Nonnull public static String getPackageName(@Nullable Object object, @Nonnull String valueIfNull)
Gets the package name of an Object
.
object
- the class to get the package name for, may be nullvalueIfNull
- the value to return if null@Nonnull public static String getPackageName(@Nullable Class<?> cls)
Gets the package name of a Class
.
cls
- the class to get the package name for, may be null
.@Nonnull public static String getPackageName(@Nullable String className)
Gets the package name from a String
.
The string passed in is assumed to be a class name - it is not checked.
If the class is unpackaged, return an empty string.
className
- the className to get the package name for, may be null