public class GrailsClassUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.util.Map<java.lang.Class<?>,java.lang.Class<?>> |
PRIMITIVE_TYPE_COMPATIBLE_CLASSES |
Constructor and Description |
---|
GrailsClassUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object[] |
collectionToObjectArray(java.util.Collection c)
Convenience method for converting a collection to an Object[]
|
static java.util.Collection |
createConcreteCollection(java.lang.Class interfaceType)
Creates a concrete collection for the suppied interface
|
static java.lang.String |
findPropertyNameForValue(java.lang.Object target,
java.lang.Object obj)
Locates the name of a property for the given value on the target object using Groovy's meta APIs.
|
static boolean |
getBooleanFromMap(java.lang.String key,
java.util.Map<?,?> map)
Retrieves a boolean value from a Map for the given key
|
static java.lang.String |
getClassName(java.lang.String logicalName,
java.lang.String trailingName)
Deprecated.
Use
grails.util.GrailsNameUtils#getClassName(String, String) instead. |
static java.lang.String |
getClassNameRepresentation(java.lang.String name)
Deprecated.
Use
grails.util.GrailsNameUtils#getClassNameRepresentation(String) instead. |
static MetaClass |
getExpandoMetaClass(java.lang.Class clazz) |
static java.lang.Object |
getFieldValue(java.lang.Object obj,
java.lang.String name)
Get the value of a declared field on an object
|
static java.lang.String |
getGetterName(java.lang.String propertyName)
Calculate the name for a getter method to retrieve the specified property
|
static java.lang.String |
getLogicalName(java.lang.Class clazz,
java.lang.String trailingName)
Deprecated.
Use
grails.util.GrailsNameUtils#getLogicalName(Class, String) instead. |
static java.lang.String |
getLogicalName(java.lang.String name,
java.lang.String trailingName)
Deprecated.
Use
grails.util.GrailsNameUtils#getLogicalName(String, String) instead. |
static java.lang.String |
getLogicalPropertyName(java.lang.String className,
java.lang.String trailingName)
Deprecated.
Use
grails.util.GrailsNameUtils#getLogicalPropertyName(String, String) instead. |
static java.lang.String |
getNameFromScript(java.lang.String scriptName)
Deprecated.
Use
grails.util.GrailsNameUtils#getNameFromScript(String) instead. |
static java.lang.String |
getNaturalName(java.lang.String name)
Deprecated.
Use
grails.util.GrailsNameUtils#getNaturalName(String) instead. |
static java.beans.PropertyDescriptor[] |
getPropertiesAssignableToType(java.lang.Class<?> clazz,
java.lang.Class<?> propertySuperType)
Retrieves all the properties of the given class which are assignable to the given type
|
static java.beans.PropertyDescriptor[] |
getPropertiesOfType(java.lang.Class<?> clazz,
java.lang.Class<?> propertyType)
Retrieves all the properties of the given class for the given type
|
static java.beans.PropertyDescriptor |
getProperty(java.lang.Class<?> clazz,
java.lang.String propertyName,
java.lang.Class<?> propertyType)
Retrieves a property of the given class of the specified name and type
|
static java.beans.PropertyDescriptor |
getPropertyDescriptorForValue(java.lang.Object instance,
java.lang.Object propertyValue)
Retrieves a PropertyDescriptor for the specified instance and property value
|
static java.lang.String |
getPropertyForGetter(java.lang.String getterName)
Returns a property name equivalent for the given getter name or null if it is not a getter
|
static java.lang.String |
getPropertyForSetter(java.lang.String setterName)
Returns a property name equivalent for the given setter name or null if it is not a getter
|
static java.lang.String |
getPropertyName(java.lang.Class clazz)
Deprecated.
Use
grails.util.GrailsNameUtils#getPropertyName(Class) instead. |
static java.lang.String |
getPropertyName(java.lang.String name)
Deprecated.
Use
grails.util.GrailsNameUtils#getPropertyName(String) instead. |
static java.lang.String |
getPropertyNameForLowerCaseHyphenSeparatedName(java.lang.String name)
Deprecated.
Use
grails.util.GrailsNameUtils#getPropertyNameForLowerCaseHyphenSeparatedName(String) instead. |
static java.lang.String |
getPropertyNameRepresentation(java.lang.Class targetClass)
Deprecated.
Use
grails.util.GrailsNameUtils#getPropertyNameRepresentation(Class) instead. |
static java.lang.String |
getPropertyNameRepresentation(java.lang.String name)
Deprecated.
Use
grails.util.GrailsNameUtils#getPropertyNameRepresentation(String) instead. |
static java.lang.Object |
getPropertyOrStaticPropertyOrFieldValue(java.lang.Object obj,
java.lang.String name)
Looks for a property of the reference instance with a given name.
|
static java.lang.Class<?> |
getPropertyType(java.lang.Class<?> clazz,
java.lang.String propertyName)
Returns the type of the given property contained within the specified class
|
static java.lang.Object |
getPropertyValueOfNewInstance(java.lang.Class<?> clazz,
java.lang.String propertyName)
Returns the value of the specified property and type from an instance of the specified Grails class
|
static java.lang.Object |
getPropertyValueOfNewInstance(java.lang.Class clazz,
java.lang.String propertyName,
java.lang.Class<?> propertyType)
Returns the value of the specified property and type from an instance of the specified Grails class
|
static java.lang.String |
getScriptName(java.lang.Class clazz)
Deprecated.
Use
grails.util.GrailsNameUtils#getScriptName(Class) instead. |
static java.lang.String |
getScriptName(java.lang.String name)
Deprecated.
Use
grails.util.GrailsNameUtils#getScriptName(String) instead. |
static java.lang.String |
getSetterName(java.lang.String propertyName)
Retrieves the name of a setter for the specified property name
|
static java.lang.String |
getShortName(java.lang.Class targetClass)
Deprecated.
Use
grails.util.GrailsNameUtils#getShortName(Class) instead. |
static java.lang.String |
getShortName(java.lang.String className)
Deprecated.
Use
grails.util.GrailsNameUtils#getShortName(String) instead. |
static java.lang.Object |
getStaticFieldValue(java.lang.Class<?> clazz,
java.lang.String name)
Get a static field value.
|
static java.lang.Object |
getStaticPropertyValue(java.lang.Class<?> clazz,
java.lang.String name)
Get a static property value, which has a public static getter or is just a public static field.
|
static java.lang.Object |
instantiateFromConfig(ConfigObject config,
java.lang.String configKey,
java.lang.String defaultClassName) |
static java.lang.Object |
instantiateFromFlatConfig(java.util.Map<java.lang.String,java.lang.Object> flatConfig,
java.lang.String configKey,
java.lang.String defaultClassName) |
static boolean |
isAssignableOrConvertibleFrom(java.lang.Class<?> clazz,
java.lang.Class<?> type)
Returns true if the specified clazz parameter is either the same as, or is a superclass or superinterface
of, the specified type parameter.
|
static boolean |
isClassBelowPackage(java.lang.Class<?> theClass,
java.util.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 |
isGetter(java.lang.String name,
java.lang.Class<?>[] args)
Returns true if the name of the method specified and the number of arguments make it a javabean property
|
static boolean |
isGroovyAssignableFrom(java.lang.Class<?> leftType,
java.lang.Class<?> rightType)
Tests whether or not the left hand type is compatible with the right hand type in Groovy
terms, i.e.
|
static boolean |
isJdk5Enum(java.lang.Class<?> type)
Checks whether the given class is a JDK 1.5 enum or not
|
static boolean |
isMatchBetweenPrimativeAndWrapperTypes(java.lang.Class leftType,
java.lang.Class rightType)
Detect if left and right types are matching types.
|
static boolean |
isPropertyInherited(java.lang.Class clz,
java.lang.String propertyName)
Checks whether the specified property is inherited from a super class
|
static boolean |
isPropertyOfType(java.lang.Class<?> clazz,
java.lang.String propertyName,
java.lang.Class<?> type)
Returns true if the specified property in the specified class is of the specified type
|
static boolean |
isPublicField(java.lang.Object obj,
java.lang.String name)
Work out if the specified object has a public field with the name supplied.
|
static boolean |
isPublicStatic(java.lang.reflect.Field f)
Determine whether the field is declared public static
|
static boolean |
isPublicStatic(java.lang.reflect.Method m)
Determine whether the method is declared public static
|
static boolean |
isSetter(java.lang.String name,
java.lang.Class[] args) |
static boolean |
isStaticProperty(java.lang.Class clazz,
java.lang.String propertyName)
Work out if the specified property is readable and static.
|
public static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> PRIMITIVE_TYPE_COMPATIBLE_CLASSES
public static boolean isPropertyOfType(java.lang.Class<?> clazz, java.lang.String propertyName, java.lang.Class<?> type)
clazz
- The class which contains the propertypropertyName
- The property nametype
- The type to checkpublic static java.lang.Object getPropertyValueOfNewInstance(java.lang.Class clazz, java.lang.String propertyName, java.lang.Class<?> propertyType)
clazz
- The name of the class which contains the propertypropertyName
- The property namepropertyType
- The property typepublic static java.lang.Object getPropertyValueOfNewInstance(java.lang.Class<?> clazz, java.lang.String propertyName)
clazz
- The name of the class which contains the propertypropertyName
- The property namepublic static java.beans.PropertyDescriptor getPropertyDescriptorForValue(java.lang.Object instance, java.lang.Object propertyValue)
instance
- The instancepropertyValue
- The value of the propertypublic static java.lang.Class<?> getPropertyType(java.lang.Class<?> clazz, java.lang.String propertyName)
clazz
- The class which contains the propertypropertyName
- The name of the propertypublic static java.beans.PropertyDescriptor[] getPropertiesOfType(java.lang.Class<?> clazz, java.lang.Class<?> propertyType)
clazz
- The class to retrieve the properties frompropertyType
- The type of the properties you wish to retrievepublic static java.beans.PropertyDescriptor[] getPropertiesAssignableToType(java.lang.Class<?> clazz, java.lang.Class<?> propertySuperType)
clazz
- The class to retrieve the properties frompropertySuperType
- The type of the properties you wish to retrievepublic static java.beans.PropertyDescriptor getProperty(java.lang.Class<?> clazz, java.lang.String propertyName, java.lang.Class<?> propertyType)
clazz
- The class to retrieve the property frompropertyName
- The name of the propertypropertyType
- The type of the property@Deprecated public static java.lang.String getShortName(java.lang.Class targetClass)
grails.util.GrailsNameUtils#getShortName(Class)
instead.targetClass
- The class to get a short name for@Deprecated public static java.lang.String getShortName(java.lang.String className)
grails.util.GrailsNameUtils#getShortName(String)
instead.className
- The class name to get a short name for@Deprecated public static java.lang.String getPropertyNameRepresentation(java.lang.Class targetClass)
grails.util.GrailsNameUtils#getPropertyNameRepresentation(Class)
instead.targetClass
- The class to get the property name for@Deprecated public static java.lang.String getPropertyNameRepresentation(java.lang.String name)
grails.util.GrailsNameUtils#getPropertyNameRepresentation(String)
instead.name
- The name to convert@Deprecated public static java.lang.String getClassNameRepresentation(java.lang.String name)
grails.util.GrailsNameUtils#getClassNameRepresentation(String)
instead.name
- The name to convert@Deprecated public static java.lang.String getPropertyName(java.lang.String name)
grails.util.GrailsNameUtils#getPropertyName(String)
instead.name
- The name to convert@Deprecated public static java.lang.String getPropertyName(java.lang.Class clazz)
grails.util.GrailsNameUtils#getPropertyName(Class)
instead.clazz
- The clazz to convert@Deprecated public static java.lang.String getScriptName(java.lang.Class clazz)
grails.util.GrailsNameUtils#getScriptName(Class)
instead.clazz
- The class to convert@Deprecated public static java.lang.String getScriptName(java.lang.String name)
grails.util.GrailsNameUtils#getScriptName(String)
instead.@Deprecated public static java.lang.String getNameFromScript(java.lang.String scriptName)
grails.util.GrailsNameUtils#getNameFromScript(String)
instead.scriptName
- The script name@Deprecated public static java.lang.String getPropertyNameForLowerCaseHyphenSeparatedName(java.lang.String name)
grails.util.GrailsNameUtils#getPropertyNameForLowerCaseHyphenSeparatedName(String)
instead.name
- The lower case hyphen separated name@Deprecated public static java.lang.String getNaturalName(java.lang.String name)
grails.util.GrailsNameUtils#getNaturalName(String)
instead.name
- The property name to convertpublic static java.lang.Object[] collectionToObjectArray(java.util.Collection c)
c
- The collectionpublic static boolean isMatchBetweenPrimativeAndWrapperTypes(java.lang.Class leftType, java.lang.Class rightType)
leftType
- rightType
- public static boolean isGroovyAssignableFrom(java.lang.Class<?> leftType, java.lang.Class<?> rightType)
Tests whether or not the left hand type is compatible with the right hand type in Groovy terms, i.e. can the left type be assigned a value of the right hand type in Groovy.
This handles Java primitive type equivalence and uses isAssignableFrom for all other types, with a bit of magic for native types and polymorphism i.e. Number assigned an int. If either parameter is null an exception is thrown
leftType
- The type of the left hand part of a notional assignmentrightType
- The type of the right hand part of a notional assignmentpublic static boolean isStaticProperty(java.lang.Class clazz, java.lang.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(java.lang.reflect.Method m)
m
- public static boolean isPublicStatic(java.lang.reflect.Field f)
f
- public static java.lang.String getGetterName(java.lang.String propertyName)
propertyName
- public static java.lang.Object getStaticFieldValue(java.lang.Class<?> clazz, java.lang.String name)
Get a static field value.
clazz
- The class to check for static propertyname
- The field namepublic static java.lang.Object getStaticPropertyValue(java.lang.Class<?> clazz, java.lang.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 namepublic static java.lang.Object getPropertyOrStaticPropertyOrFieldValue(java.lang.Object obj, java.lang.String name) throws BeansException
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:
BeansException
public static java.lang.Object getFieldValue(java.lang.Object obj, java.lang.String name)
obj
- name
- public static boolean isPublicField(java.lang.Object obj, java.lang.String name)
obj
- name
- public static boolean isPropertyInherited(java.lang.Class clz, java.lang.String propertyName)
clz
- The class to checkpropertyName
- The property namepublic static java.util.Collection createConcreteCollection(java.lang.Class interfaceType)
interfaceType
- The interface@Deprecated public static java.lang.String getLogicalName(java.lang.Class clazz, java.lang.String trailingName)
grails.util.GrailsNameUtils#getLogicalName(Class, String)
instead.clazz
- The classtrailingName
- The trailing name such as "Controller" or "TagLib"@Deprecated public static java.lang.String getLogicalName(java.lang.String name, java.lang.String trailingName)
grails.util.GrailsNameUtils#getLogicalName(String, String)
instead.name
- The name of the classtrailingName
- The trailing name@Deprecated public static java.lang.String getLogicalPropertyName(java.lang.String className, java.lang.String trailingName)
grails.util.GrailsNameUtils#getLogicalPropertyName(String, String)
instead.public static java.lang.String getSetterName(java.lang.String propertyName)
propertyName
- The property namepublic static boolean isGetter(java.lang.String name, java.lang.Class<?>[] args)
name
- True if its a Javabean propertyargs
- The argumentspublic static java.lang.String getPropertyForGetter(java.lang.String getterName)
getterName
- The getter namepublic static java.lang.String getPropertyForSetter(java.lang.String setterName)
setterName
- The setter namepublic static boolean isSetter(java.lang.String name, java.lang.Class[] args)
public static MetaClass getExpandoMetaClass(java.lang.Class clazz)
public static boolean isAssignableOrConvertibleFrom(java.lang.Class<?> clazz, java.lang.Class<?> type)
clazz
- type
- Class.isAssignableFrom(Class)
public static boolean getBooleanFromMap(java.lang.String key, java.util.Map<?,?> map)
key
- The key that references the boolean valuemap
- The map to look in@Deprecated public static java.lang.String getClassName(java.lang.String logicalName, java.lang.String trailingName)
grails.util.GrailsNameUtils#getClassName(String, String)
instead.logicalName
- The logical nametrailingName
- The trailing namepublic static boolean isJdk5Enum(java.lang.Class<?> type)
type
- The class to checkpublic static java.lang.String findPropertyNameForValue(java.lang.Object target, java.lang.Object obj)
target
- The targetobj
- The property valuepublic static boolean isClassBelowPackage(java.lang.Class<?> theClass, java.util.List<?> packageList)
theClass
- The classpackageList
- The list of packagespublic static java.lang.Object instantiateFromConfig(ConfigObject config, java.lang.String configKey, java.lang.String defaultClassName) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.ClassNotFoundException, java.lang.LinkageError
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.lang.LinkageError
public static java.lang.Object instantiateFromFlatConfig(java.util.Map<java.lang.String,java.lang.Object> flatConfig, java.lang.String configKey, java.lang.String defaultClassName) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.ClassNotFoundException, java.lang.LinkageError
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.lang.LinkageError