org.codehaus.groovy.grails.commons
Class GrailsClassUtils

java.lang.Object
  extended by org.codehaus.groovy.grails.commons.GrailsClassUtils

public class GrailsClassUtils
extends Object

Since:
08-Jul-2005 Class containing utility methods for dealing with Grails class artifacts
Author:
Graeme Rocher

Field Summary
static Map PRIMITIVE_TYPE_COMPATIBLE_CLASSES
           
 
Constructor Summary
GrailsClassUtils()
           
 
Method Summary
static Object[] collectionToObjectArray(Collection c)
          Convenience method for converting a collection to an Object[]
static Collection createConcreteCollection(Class interfaceType)
          Creates a concrete collection for the suppied interface
static String findPropertyNameForValue(Object target, Object obj)
          Locates the name of a property for the given value on the target object using Groovy's meta APIs.
static boolean getBooleanFromMap(String key, Map map)
          Retrieves a boolean value from a Map for the given key
static String getClassName(String logicalName, String trailingName)
          Deprecated. Use GrailsNameUtils.getClassName(String, String) instead.
static String getClassNameRepresentation(String name)
          Deprecated. Use GrailsNameUtils.getClassNameRepresentation(String) instead.
static groovy.lang.MetaClass getExpandoMetaClass(Class clazz)
           
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 getLogicalName(Class clazz, String trailingName)
          Deprecated. Use GrailsNameUtils.getLogicalName(Class, String) instead.
static String getLogicalName(String name, String trailingName)
          Deprecated. Use GrailsNameUtils.getLogicalName(String, String) instead.
static String getLogicalPropertyName(String className, String trailingName)
          Deprecated. Use GrailsNameUtils.getLogicalPropertyName(String, String) instead.
static String getNameFromScript(String scriptName)
          Deprecated. Use GrailsNameUtils.getNameFromScript(String) instead.
static String getNaturalName(String name)
          Deprecated. Use GrailsNameUtils.getNaturalName(String) instead.
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 PropertyDescriptor getPropertyDescriptorForValue(Object instance, Object propertyValue)
          Retrieves a PropertyDescriptor for the specified instance and property value
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 String getPropertyName(Class clazz)
          Deprecated. Use GrailsNameUtils.getPropertyName(Class) instead.
static String getPropertyName(String name)
          Deprecated. Use GrailsNameUtils.getPropertyName(String) instead.
static String getPropertyNameForLowerCaseHyphenSeparatedName(String name)
          Deprecated. Use GrailsNameUtils.getPropertyNameForLowerCaseHyphenSeparatedName(String) instead.
static String getPropertyNameRepresentation(Class targetClass)
          Deprecated. Use GrailsNameUtils.getPropertyNameRepresentation(Class) instead.
static String getPropertyNameRepresentation(String name)
          Deprecated. Use GrailsNameUtils.getPropertyNameRepresentation(String) instead.
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 getPropertyValueOfNewInstance(Class clazz, String propertyName)
          Returns the value of the specified property and type from an instance of the specified Grails 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 Grails class
static String getScriptName(Class clazz)
          Deprecated. Use GrailsNameUtils.getScriptName(Class) instead.
static String getScriptName(String name)
          Deprecated. Use GrailsNameUtils.getScriptName(String) instead.
static String getSetterName(String propertyName)
          Retrieves the name of a setter for the specified property name
static String getShortName(Class targetClass)
          Deprecated. Use GrailsNameUtils.getShortName(Class) instead.
static String getShortName(String className)
          Deprecated. Use GrailsNameUtils.getShortName(String) instead.
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 boolean isAssignableOrConvertibleFrom(Class clazz, 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 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 isGroovyAssignableFrom(Class leftType, 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.
static boolean isJdk5Enum(Class type)
          Checks whether the given class is a JDK 1.5 enum or not
static boolean isMatchBetweenPrimativeAndWrapperTypes(Class leftType, Class rightType)
          Detect if left and right types are matching types.
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 isSetter(String name, Class[] args)
           
static boolean isStaticProperty(Class clazz, String propertyName)
          Work out if the specified property is readable and static.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRIMITIVE_TYPE_COMPATIBLE_CLASSES

public static final Map PRIMITIVE_TYPE_COMPATIBLE_CLASSES
Constructor Detail

GrailsClassUtils

public GrailsClassUtils()
Method Detail

isPropertyOfType

public static boolean isPropertyOfType(Class clazz,
                                       String propertyName,
                                       Class type)
Returns true if the specified property in the specified class is of the specified type

Parameters:
clazz - The class which contains the property
propertyName - The property name
type - The type to check
Returns:
A boolean value

getPropertyValueOfNewInstance

public static Object getPropertyValueOfNewInstance(Class clazz,
                                                   String propertyName,
                                                   Class propertyType)
Returns the value of the specified property and type from an instance of the specified Grails class

Parameters:
clazz - The name of the class which contains the property
propertyName - The property name
propertyType - The property type
Returns:
The value of the property or null if none exists

getPropertyValueOfNewInstance

public static Object getPropertyValueOfNewInstance(Class clazz,
                                                   String propertyName)
Returns the value of the specified property and type from an instance of the specified Grails class

Parameters:
clazz - The name of the class which contains the property
propertyName - The property name
Returns:
The value of the property or null if none exists

getPropertyDescriptorForValue

public static PropertyDescriptor getPropertyDescriptorForValue(Object instance,
                                                               Object propertyValue)
Retrieves a PropertyDescriptor for the specified instance and property value

Parameters:
instance - The instance
propertyValue - The value of the property
Returns:
The PropertyDescriptor

getPropertyType

public static Class getPropertyType(Class clazz,
                                    String propertyName)
Returns the type of the given property contained within the specified class

Parameters:
clazz - The class which contains the property
propertyName - The name of the property
Returns:
The property type or null if none exists

getPropertiesOfType

public static PropertyDescriptor[] getPropertiesOfType(Class clazz,
                                                       Class propertyType)
Retrieves all the properties of the given class for the given type

Parameters:
clazz - The class to retrieve the properties from
propertyType - The type of the properties you wish to retrieve
Returns:
An array of PropertyDescriptor instances

getPropertiesAssignableToType

public static PropertyDescriptor[] getPropertiesAssignableToType(Class clazz,
                                                                 Class propertySuperType)
Retrieves all the properties of the given class which are assignable to the given type

Parameters:
clazz - The class to retrieve the properties from
propertySuperType - The type of the properties you wish to retrieve
Returns:
An array of PropertyDescriptor instances

getProperty

public static PropertyDescriptor getProperty(Class clazz,
                                             String propertyName,
                                             Class propertyType)
Retrieves a property of the given class of the specified name and type

Parameters:
clazz - The class to retrieve the property from
propertyName - The name of the property
propertyType - The type of the property
Returns:
A PropertyDescriptor instance or null if none exists

getShortName

public static String getShortName(Class targetClass)
Deprecated. Use GrailsNameUtils.getShortName(Class) instead.

Returns the class name without the package prefix

Parameters:
targetClass - The class to get a short name for
Returns:
The short name of the class

getShortName

public static String getShortName(String className)
Deprecated. Use GrailsNameUtils.getShortName(String) instead.

Returns the class name without the package prefix

Parameters:
className - The class name to get a short name for
Returns:
The short name of the class

getPropertyNameRepresentation

public static String getPropertyNameRepresentation(Class targetClass)
Deprecated. Use GrailsNameUtils.getPropertyNameRepresentation(Class) instead.

Returns the property name equivalent for the specified class

Parameters:
targetClass - The class to get the property name for
Returns:
A property name reperesentation of the class name (eg. MyClass becomes myClass)

getPropertyNameRepresentation

public static String getPropertyNameRepresentation(String name)
Deprecated. Use GrailsNameUtils.getPropertyNameRepresentation(String) instead.

Returns the property name representation of the given name

Parameters:
name - The name to convert
Returns:
The property name representation

getClassNameRepresentation

public static String getClassNameRepresentation(String name)
Deprecated. Use GrailsNameUtils.getClassNameRepresentation(String) instead.

Returns the class name representation of the given name

Parameters:
name - The name to convert
Returns:
The property name representation

getPropertyName

public static String getPropertyName(String name)
Deprecated. Use GrailsNameUtils.getPropertyName(String) instead.

Shorter version of getPropertyNameRepresentation

Parameters:
name - The name to convert
Returns:
The property name version

getPropertyName

public static String getPropertyName(Class clazz)
Deprecated. Use GrailsNameUtils.getPropertyName(Class) instead.

Shorter version of getPropertyNameRepresentation

Parameters:
clazz - The clazz to convert
Returns:
The property name version

getScriptName

public static String getScriptName(Class clazz)
Deprecated. Use GrailsNameUtils.getScriptName(Class) instead.

Retrieves the script name representation of the supplied class. For example MyFunkyGrailsScript would be my-funky-grails-script

Parameters:
clazz - The class to convert
Returns:
The script name representation

getScriptName

public static String getScriptName(String name)
Deprecated. Use GrailsNameUtils.getScriptName(String) instead.


getNameFromScript

public static String getNameFromScript(String scriptName)
Deprecated. Use GrailsNameUtils.getNameFromScript(String) instead.

Calculates the class name from a script name in the form my-funk-grails-script

Parameters:
scriptName - The script name
Returns:
A class name

getPropertyNameForLowerCaseHyphenSeparatedName

public static String getPropertyNameForLowerCaseHyphenSeparatedName(String name)
Deprecated. Use GrailsNameUtils.getPropertyNameForLowerCaseHyphenSeparatedName(String) instead.

Converts foo-bar into fooBar

Parameters:
name - The lower case hyphen separated name
Returns:
The property name equivalent

getNaturalName

public static String getNaturalName(String name)
Deprecated. Use GrailsNameUtils.getNaturalName(String) instead.

Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name')

Parameters:
name - The property name to convert
Returns:
The converted property name

collectionToObjectArray

public static Object[] collectionToObjectArray(Collection c)
Convenience method for converting a collection to an Object[]

Parameters:
c - The collection
Returns:
An object array

isMatchBetweenPrimativeAndWrapperTypes

public static boolean isMatchBetweenPrimativeAndWrapperTypes(Class leftType,
                                                             Class rightType)
Detect if left and right types are matching types. In particular, test if one is a primitive type and the other is the corresponding Java wrapper type. Primitive and wrapper classes may be passed to either arguments.

Parameters:
leftType -
rightType -
Returns:
true if one of the classes is a native type and the other the object representation of the same native type

isGroovyAssignableFrom

public static boolean isGroovyAssignableFrom(Class leftType,
                                             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

Parameters:
leftType - The type of the left hand part of a notional assignment
rightType - The type of the right hand part of a notional assignment
Returns:
True if values of the right hand type can be assigned in Groovy to variables of the left hand type.

isStaticProperty

public static boolean isStaticProperty(Class clazz,
                                       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

Parameters:
clazz - The class to check for static property
propertyName - The property name
Returns:
true if the property with name propertyName has a static getter method

isPublicStatic

public static boolean isPublicStatic(Method m)
Determine whether the method is declared public static

Parameters:
m -
Returns:
True if the method is declared public static

isPublicStatic

public static boolean isPublicStatic(Field f)
Determine whether the field is declared public static

Parameters:
f -
Returns:
True if the field is declared public static

getGetterName

public static String getGetterName(String propertyName)
Calculate the name for a getter method to retrieve the specified property

Parameters:
propertyName -
Returns:
The name for the getter method for this property, if it were to exist, i.e. getConstraints

getStaticPropertyValue

public 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.

Parameters:
clazz - The class to check for static property
name - The property name
Returns:
The value if there is one, or null if unset OR there is no such property

getPropertyOrStaticPropertyOrFieldValue

public static Object getPropertyOrStaticPropertyOrFieldValue(Object obj,
                                                             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:

  1. Standard public bean property (with getter or just public field, using normal introspection)
  2. Public static property with getter method
  3. Public static field

Returns:
property value or null if no property found
Throws:
BeansException

getFieldValue

public static Object getFieldValue(Object obj,
                                   String name)
Get the value of a declared field on an object

Parameters:
obj -
name -
Returns:
The object value or null if there is no such field or access problems

isPublicField

public static boolean isPublicField(Object obj,
                                    String name)
Work out if the specified object has a public field with the name supplied.

Parameters:
obj -
name -
Returns:
True if a public field with the name exists

isPropertyInherited

public static boolean isPropertyInherited(Class clz,
                                          String propertyName)
Checks whether the specified property is inherited from a super class

Parameters:
clz - The class to check
propertyName - The property name
Returns:
True if the property is inherited

createConcreteCollection

public static Collection createConcreteCollection(Class interfaceType)
Creates a concrete collection for the suppied interface

Parameters:
interfaceType - The interface
Returns:
ArrayList for List, TreeSet for SortedSet, HashSet for Set etc.

getLogicalName

public static String getLogicalName(Class clazz,
                                    String trailingName)
Deprecated. Use GrailsNameUtils.getLogicalName(Class, String) instead.

Retrieves the logical class name of a Grails artifact given the Grails class and a specified trailing name

Parameters:
clazz - The class
trailingName - The trailing name such as "Controller" or "TagLib"
Returns:
The logical class name

getLogicalName

public static String getLogicalName(String name,
                                    String trailingName)
Deprecated. Use GrailsNameUtils.getLogicalName(String, String) instead.

Retrieves the logical name of the classs without the trailing name

Parameters:
name - The name of the class
trailingName - The trailing name
Returns:
The logical name

getLogicalPropertyName

public static String getLogicalPropertyName(String className,
                                            String trailingName)
Deprecated. Use GrailsNameUtils.getLogicalPropertyName(String, String) instead.


getSetterName

public static String getSetterName(String propertyName)
Retrieves the name of a setter for the specified property name

Parameters:
propertyName - The property name
Returns:
The setter equivalent

isGetter

public 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

Parameters:
name - True if its a Javabean property
args - The arguments
Returns:
True if it is a javabean property method

getPropertyForGetter

public static String getPropertyForGetter(String getterName)
Returns a property name equivalent for the given getter name or null if it is not a getter

Parameters:
getterName - The getter name
Returns:
The property name equivalent

getPropertyForSetter

public static String getPropertyForSetter(String setterName)
Returns a property name equivalent for the given setter name or null if it is not a getter

Parameters:
setterName - The setter name
Returns:
The property name equivalent

isSetter

public static boolean isSetter(String name,
                               Class[] args)

getExpandoMetaClass

public static groovy.lang.MetaClass getExpandoMetaClass(Class clazz)

isAssignableOrConvertibleFrom

public static boolean isAssignableOrConvertibleFrom(Class clazz,
                                                    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. Converts primitive types to compatible class automatically.

Parameters:
clazz -
type -
Returns:
True if the class is a taglib
See Also:
Class.isAssignableFrom(Class)

getBooleanFromMap

public static boolean getBooleanFromMap(String key,
                                        Map map)
Retrieves a boolean value from a Map for the given key

Parameters:
key - The key that references the boolean value
map - The map to look in
Returns:
A boolean value which will be false if the map is null, the map doesn't contain the key or the value is false

getClassName

public static String getClassName(String logicalName,
                                  String trailingName)
Deprecated. Use GrailsNameUtils.getClassName(String, String) instead.

Returns the class name for the given logical name and trailing name. For example "person" and "Controller" would evaluate to "PersonController"

Parameters:
logicalName - The logical name
trailingName - The trailing name
Returns:
The class name

isJdk5Enum

public static boolean isJdk5Enum(Class type)
Checks whether the given class is a JDK 1.5 enum or not

Parameters:
type - The class to check
Returns:
True if it is an enum

findPropertyNameForValue

public static String findPropertyNameForValue(Object target,
                                              Object obj)
Locates the name of a property for the given value on the target object using Groovy's meta APIs. Note that this method uses the reference so the incorrect result could be returned for two properties that refer to the same reference. Use with caution.

Parameters:
target - The target
obj - The property value
Returns:
The property name or null


Copyright (c) 2005-2009 The Grails project