|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.codehaus.groovy.grails.commons.GrailsClassUtils
public class GrailsClassUtils extends java.lang.Object
Utility methods for dealing with Grails class artifacts.
Field Summary | |
---|---|
static java.util.Map |
PRIMITIVE_TYPE_COMPATIBLE_CLASSES
|
Method Summary | |
---|---|
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)
|
static java.lang.String
|
findPropertyNameForValue(java.lang.Object target, java.lang.Object obj)
|
static boolean
|
getBooleanFromMap(java.lang.String key, java.util.Map map)
|
static java.lang.String
|
getClassName(java.lang.String logicalName, java.lang.String trailingName)
Checks whether the given class is a JDK 1.5 enum. |
static java.lang.String
|
getClassNameRepresentation(java.lang.String name)
Returns the class name representation of the given name |
static groovy.lang.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)
|
static java.lang.String
|
getLogicalName(java.lang.Class clazz, java.lang.String trailingName)
Retrieves the logical name of the class without the trailing name |
static java.lang.String
|
getLogicalName(java.lang.String name, java.lang.String trailingName)
@deprecated Use GrailsNameUtils.getLogicalPropertyName instead. |
static java.lang.String
|
getLogicalPropertyName(java.lang.String className, java.lang.String trailingName)
Retrieves the name of a setter for the specified property name |
static java.lang.String
|
getNameFromScript(java.lang.String scriptName)
Calculates the class name from a script name in the form my-funk-grails-script |
static java.lang.String
|
getNaturalName(java.lang.String name)
Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name') |
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
|
getProperty(java.lang.Class clazz, java.lang.String propertyName)
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)
|
static java.lang.String
|
getPropertyForSetter(java.lang.String setterName)
|
static java.lang.String
|
getPropertyName(java.lang.String name)
Shorter version of getPropertyNameRepresentation |
static java.lang.String
|
getPropertyName(java.lang.Class clazz)
|
static java.lang.String
|
getPropertyNameForLowerCaseHyphenSeparatedName(java.lang.String name)
|
static java.lang.String
|
getPropertyNameRepresentation(java.lang.Class targetClass)
Returns the property name equivalent for the specified class |
static java.lang.String
|
getPropertyNameRepresentation(java.lang.String name)
Returns the property name representation of the given name |
static java.lang.Object
|
getPropertyOrStaticPropertyOrFieldValue(java.lang.Object obj, java.lang.String 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, java.lang.Class propertyType)
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)
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)
|
static java.lang.String
|
getScriptName(java.lang.String name)
|
static java.lang.String
|
getSetterName(java.lang.String propertyName)
|
static java.lang.String
|
getShortName(java.lang.Class targetClass)
Returns the class name without the package prefix |
static java.lang.String
|
getShortName(java.lang.String className)
Returns the class name without the package prefix |
static java.lang.Object
|
getStaticFieldValue(java.lang.Class clazz, java.lang.String name)
|
static java.lang.Object
|
getStaticPropertyValue(java.lang.Class clazz, java.lang.String name)
|
static java.lang.Object
|
instantiateFromConfig(groovy.util.ConfigObject config, java.lang.String configKey, java.lang.String defaultClassName)
|
static java.lang.Object
|
instantiateFromFlatConfig(java.util.Map 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)
|
static boolean
|
isGetter(java.lang.String name, java.lang.Class[] args)
|
static boolean
|
isGroovyAssignableFrom(java.lang.Class leftType, java.lang.Class rightType)
|
static boolean
|
isJdk5Enum(java.lang.Class type)
|
static boolean
|
isMatchBetweenPrimativeAndWrapperTypes(java.lang.Class leftType, java.lang.Class rightType)
|
static boolean
|
isPropertyInherited(java.lang.Class clz, java.lang.String propertyName)
|
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)
|
static boolean
|
isPublicStatic(java.lang.reflect.Method m)
Determine whether the method is declared public static |
static boolean
|
isPublicStatic(java.lang.reflect.Field f)
Calculate the name for a getter method to retrieve the specified property |
static boolean
|
isSetter(java.lang.String name, java.lang.Class[] args)
|
static boolean
|
isStaticProperty(java.lang.Class clazz, java.lang.String propertyName)
|
Methods inherited from class java.lang.Object | |
---|---|
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Field Detail |
---|
public static final java.util.Map PRIMITIVE_TYPE_COMPATIBLE_CLASSES
Method Detail |
---|
* test if one is a primitive type and the other is the corresponding public static java.lang.Object[] collectionToObjectArray(java.util.Collection c)
c
- The collection
* and a specified trailing name public static java.util.Collection createConcreteCollection(java.lang.Class interfaceType)
public static java.lang.String findPropertyNameForValue(java.lang.Object target, java.lang.Object obj)
public static boolean getBooleanFromMap(java.lang.String key, java.util.Map map)
Method m = BeanUtils.findMethod(type.getClass(),"isEnum"); public static java.lang.String getClassName(java.lang.String logicalName, java.lang.String trailingName)
type
- The class to check
buf.append(token.substring(0, 1).toUpperCase(Locale.ENGLISH)) public static java.lang.String getClassNameRepresentation(java.lang.String name)
name
- The name to convert
if (adapter != null) { public static groovy.lang.MetaClass getExpandoMetaClass(java.lang.Class clazz)
public static java.lang.Object getFieldValue(java.lang.Object obj, java.lang.String name)
public static java.lang.String getGetterName(java.lang.String propertyName)
Get a static field value.
clazz
- The class to check for static propertyname
- The field name
String shortName = getShortName(name); if (shortName.indexOf(trailingName) > - 1) { public static java.lang.String getLogicalName(java.lang.Class clazz, java.lang.String trailingName)
name
- The name of the classtrailingName
- The trailing name
} public static java.lang.String getLogicalName(java.lang.String name, java.lang.String trailingName)
* @param name True if its a Javabean property public static java.lang.String getLogicalPropertyName(java.lang.String className, java.lang.String trailingName)
propertyName
- The property name
* @param name The lower case hyphen separated name public static java.lang.String getNameFromScript(java.lang.String scriptName)
scriptName
- The script name
if (i >= words.size()) { public static java.lang.String getNaturalName(java.lang.String name)
name
- The property name to convert
public 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 retrieve
public 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 retrieve
public 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
public static java.beans.PropertyDescriptor getProperty(java.lang.Class clazz, java.lang.String propertyName)
clazz
- The class to retrieve the property frompropertyName
- The name of the property
public static java.beans.PropertyDescriptor getPropertyDescriptorForValue(java.lang.Object instance, java.lang.Object propertyValue)
instance
- The instancepropertyValue
- The value of the property
public static java.lang.String getPropertyForGetter(java.lang.String getterName)
public static java.lang.String getPropertyForSetter(java.lang.String setterName)
* @return The property name version public static java.lang.String getPropertyName(java.lang.String name)
name
- The name to convert
* MyFunkyGrailsScript would be my-funky-grails-script * public static java.lang.String getPropertyName(java.lang.Class clazz)
* @param name The lower case hyphen separated name public static java.lang.String getPropertyNameForLowerCaseHyphenSeparatedName(java.lang.String name)
/** * Returns the property name representation of the given name public static java.lang.String getPropertyNameRepresentation(java.lang.Class targetClass)
targetClass
- The class to get the property name for
} public static java.lang.String getPropertyNameRepresentation(java.lang.String name)
name
- The name to convert
public 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.
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:
public static java.lang.Class getPropertyType(java.lang.Class clazz, java.lang.String propertyName)
clazz
- The class which contains the propertypropertyName
- The name of the property
@SuppressWarnings({ "unchecked", "rawtypes" }) public static java.lang.Object getPropertyValueOfNewInstance(java.lang.Class clazz, java.lang.String propertyName, @SuppressWarnings("unused") java.lang.Class propertyType)
clazz
- The name of the class which contains the propertypropertyName
- The property namepropertyType
- The property type
public 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 name
*/ @Deprecated public static java.lang.String getScriptName(java.lang.Class clazz)
public static java.lang.String getScriptName(java.lang.String name)
public static java.lang.String getSetterName(java.lang.String propertyName)
/** * Returns the class name without the package prefix public static java.lang.String getShortName(java.lang.Class targetClass)
targetClass
- The class to get a short name for
return className; public static java.lang.String getShortName(java.lang.String className)
className
- The class name to get a short name for
public static java.lang.Object getStaticFieldValue(java.lang.Class clazz, java.lang.String name)
public static java.lang.Object getStaticPropertyValue(java.lang.Class clazz, java.lang.String name)
public static java.lang.Object instantiateFromConfig(groovy.util.ConfigObject config, java.lang.String configKey, java.lang.String defaultClassName)
public static java.lang.Object instantiateFromFlatConfig(java.util.Map flatConfig, java.lang.String configKey, java.lang.String defaultClassName)
public static boolean isAssignableOrConvertibleFrom(java.lang.Class clazz, java.lang.Class type)
public static boolean isClassBelowPackage(java.lang.Class theClass, java.util.List packageList)
public static boolean isGetter(java.lang.String name, java.lang.Class[] args)
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 assignment
public static boolean isJdk5Enum(java.lang.Class type)
Class> r = PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(leftType); public static boolean isMatchBetweenPrimativeAndWrapperTypes(java.lang.Class leftType, java.lang.Class rightType)
* @return ArrayList for List, TreeSet for SortedSet, HashSet for Set etc. public static boolean isPropertyInherited(java.lang.Class clz, java.lang.String propertyName)
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 check
public static boolean isPublicField(java.lang.Object obj, java.lang.String name)
public static boolean isPublicStatic(java.lang.reflect.Method m)
public static boolean isPublicStatic(java.lang.reflect.Field f)
"Grails requires an instance of [ExpandoMetaClassCreationHandle] to be set in Groovy's MetaClassRegistry! (current is : "+registry.getMetaClassCreationHandler()+")"); public static boolean isSetter(java.lang.String name, java.lang.Class[] args)
catch (NoSuchFieldException ignored) { public 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 name
Groovy Documentation