Groovy Documentation

org.codehaus.groovy.grails.commons
[Java] Class GrailsClassUtils

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

public class GrailsClassUtils
extends java.lang.Object

Utility methods for dealing with Grails class artifacts.

Authors:
Graeme Rocher


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)

Returns whether the specified class is either within one of the specified packages or within a subpackage of one of the packages

static java.lang.Class[] getAllInterfaces(java.lang.Object instance)

Return all interfaces that the given instance implements as array, including ones implemented by superclasses.

static java.util.Set getAllInterfacesAsSet(java.lang.Object instance)

Return all interfaces that the given instance implements as Set, including ones implemented by superclasses.

static java.lang.Class[] getAllInterfacesForClass(java.lang.Class clazz)

Return all interfaces that the given class implements as array, including ones implemented by superclasses.

static java.lang.Class[] getAllInterfacesForClass(java.lang.Class clazz, java.lang.ClassLoader classLoader)

Return all interfaces that the given class implements as array, including ones implemented by superclasses.

static java.util.Set getAllInterfacesForClassAsSet(java.lang.Class clazz)

Return all interfaces that the given class implements as Set, including ones implemented by superclasses.

static java.util.Set getAllInterfacesForClassAsSet(java.lang.Class clazz, java.lang.ClassLoader classLoader)

Return all interfaces that the given class implements as Set, including ones implemented by superclasses.

static boolean getBooleanFromMap(java.lang.String key, java.util.Map map)

Returns the class name for the given logical name and trailing name.

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)

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)

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)

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)

@deprecated Use GrailsNameUtils.getScriptName instead.

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)

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)

Creates a concrete collection for the suppied interface

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)

Checks whether the specified property is inherited from a super class

static boolean isPublicStatic(java.lang.reflect.Method m)

Determine whether the method is declared public static

static boolean isPublicStatic(java.lang.reflect.Field f)

static boolean isSetter(java.lang.String name, java.lang.Class[] args)

static boolean isStaticProperty(java.lang.Class clazz, java.lang.String propertyName)

static boolean isVisible(java.lang.Class clazz, java.lang.ClassLoader classLoader)

Check whether the given class is visible in the given ClassLoader.

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), 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

PRIMITIVE_TYPE_COMPATIBLE_CLASSES

public static final java.util.Map PRIMITIVE_TYPE_COMPATIBLE_CLASSES


 
Method Detail

collectionToObjectArray

@java.lang.SuppressWarnings* either arguments.
public static java.lang.Object[] collectionToObjectArray(java.util.Collection c)
Convenience method for converting a collection to an Object[]
Parameters:
c - The collection
Returns:
An object array


createConcreteCollection

@java.lang.SuppressWarnings* @param clazz The class
public static java.util.Collection createConcreteCollection(java.lang.Class interfaceType)


findPropertyNameForValue

public static java.lang.String findPropertyNameForValue(java.lang.Object target, java.lang.Object obj)
Returns whether the specified class is either within one of the specified packages or within a subpackage of one of the packages
Parameters:
theClass - The class
packageList - The list of packages
Returns:
true if it is within the list of specified packages


getAllInterfaces

public static java.lang.Class[] getAllInterfaces(java.lang.Object instance)
Return all interfaces that the given instance implements as array, including ones implemented by superclasses.
Parameters:
instance - the instance to analyze for interfaces
Returns:
all interfaces that the given instance implements as array


getAllInterfacesAsSet

public static java.util.Set getAllInterfacesAsSet(java.lang.Object instance)
Return all interfaces that the given instance implements as Set, including ones implemented by superclasses.
Parameters:
instance - the instance to analyze for interfaces
Returns:
all interfaces that the given instance implements as Set


getAllInterfacesForClass

public static java.lang.Class[] getAllInterfacesForClass(java.lang.Class clazz)
Return all interfaces that the given class implements as array, including ones implemented by superclasses.

If the class itself is an interface, it gets returned as sole interface.

Parameters:
clazz - the class to analyze for interfaces
Returns:
all interfaces that the given object implements as array


getAllInterfacesForClass

public static java.lang.Class[] getAllInterfacesForClass(java.lang.Class clazz, java.lang.ClassLoader classLoader)
Return all interfaces that the given class implements as array, including ones implemented by superclasses.

If the class itself is an interface, it gets returned as sole interface.

Parameters:
clazz - the class to analyze for interfaces
classLoader - the ClassLoader that the interfaces need to be visible in (may be null when accepting all declared interfaces)
Returns:
all interfaces that the given object implements as array


getAllInterfacesForClassAsSet

public static java.util.Set getAllInterfacesForClassAsSet(java.lang.Class clazz)
Return all interfaces that the given class implements as Set, including ones implemented by superclasses.

If the class itself is an interface, it gets returned as sole interface.

Parameters:
clazz - the class to analyze for interfaces
Returns:
all interfaces that the given object implements as Set


getAllInterfacesForClassAsSet

public static java.util.Set getAllInterfacesForClassAsSet(java.lang.Class clazz, java.lang.ClassLoader classLoader)
Return all interfaces that the given class implements as Set, including ones implemented by superclasses.

If the class itself is an interface, it gets returned as sole interface.

Parameters:
clazz - the class to analyze for interfaces
classLoader - the ClassLoader that the interfaces need to be visible in (may be null when accepting all declared interfaces)
Returns:
all interfaces that the given object implements as Set


getBooleanFromMap

public static boolean getBooleanFromMap(java.lang.String key, java.util.Map map)
Returns the class name for the given logical name and trailing name. For example "person" and "Controller" would evaluate to "PersonController"
deprecated:
Use GrailsNameUtils.getClassName instead.
Parameters:
logicalName - The logical name
trailingName - The trailing name
Returns:
The class name


getClassName

@java.lang.Deprecated* Note that this method uses the reference so the incorrect result could be returned for two properties
public static java.lang.String getClassName(java.lang.String logicalName, java.lang.String trailingName)
Checks whether the given class is a JDK 1.5 enum.
deprecated:
Parameters:
type - The class to check
Returns:
true if it is an enum


getClassNameRepresentation

@java.lang.Deprecated}
public static java.lang.String getClassNameRepresentation(java.lang.String name)
Returns the class name representation of the given name
deprecated:
Use GrailsNameUtils.getClassNameRepresentation instead.
Parameters:
name - The name to convert
Returns:
The property name representation


getExpandoMetaClass

@java.lang.SuppressWarnings}
public static groovy.lang.MetaClass getExpandoMetaClass(java.lang.Class clazz)


getFieldValue

public static java.lang.Object getFieldValue(java.lang.Object obj, java.lang.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


getGetterName

public static java.lang.String getGetterName(java.lang.String propertyName)


getLogicalName

@java.lang.SuppressWarningsreturn shortName.substring(0, shortName.length() - trailingName.length());
@java.lang.Deprecated}
public static java.lang.String getLogicalName(java.lang.Class clazz, java.lang.String trailingName)
Retrieves the logical name of the class without the trailing name
deprecated:
Use GrailsNameUtils.getLogicalName instead.
Parameters:
name - The name of the class
trailingName - The trailing name
Returns:
The logical name


getLogicalName

@java.lang.Deprecated/**
public static java.lang.String getLogicalName(java.lang.String name, java.lang.String trailingName)
deprecated:
Use GrailsNameUtils.getLogicalPropertyName instead.


getLogicalPropertyName

@java.lang.Deprecated* @return true if it is a javabean property method
public static java.lang.String getLogicalPropertyName(java.lang.String className, java.lang.String trailingName)
Retrieves the name of a setter for the specified property name
Parameters:
propertyName - The property name
Returns:
The setter equivalent


getNameFromScript

@java.lang.Deprecated*
public static java.lang.String getNameFromScript(java.lang.String scriptName)


getNaturalName

@java.lang.Deprecatedwords.add(i, w);
public static java.lang.String getNaturalName(java.lang.String name)
Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name')
deprecated:
Use GrailsNameUtils.getNaturalName instead.
Parameters:
name - The property name to convert
Returns:
The converted property name


getPropertiesAssignableToType

public static java.beans.PropertyDescriptor[] getPropertiesAssignableToType(java.lang.Class clazz, java.lang.Class propertySuperType)


getPropertiesOfType

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


getProperty

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


getProperty

public 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
Parameters:
clazz - The class to retrieve the property from
propertyName - The name of the property
Returns:
A PropertyDescriptor instance or null if none exists


getPropertyDescriptorForValue

public static java.beans.PropertyDescriptor getPropertyDescriptorForValue(java.lang.Object instance, java.lang.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


getPropertyForGetter

public static java.lang.String getPropertyForGetter(java.lang.String getterName)


getPropertyForSetter

public static java.lang.String getPropertyForSetter(java.lang.String setterName)


getPropertyName

@java.lang.Deprecated* @deprecated Use {@link grails.util.GrailsNameUtils#getPropertyName(Class)} instead.
public static java.lang.String getPropertyName(java.lang.String name)
Shorter version of getPropertyNameRepresentation
deprecated:
Use GrailsNameUtils.getPropertyName instead.
Parameters:
name - The name to convert
Returns:
The property name version


getPropertyName

@java.lang.SuppressWarnings* @param clazz The class to convert
@java.lang.Deprecated* @return The script name representation
public static java.lang.String getPropertyName(java.lang.Class clazz)


getPropertyNameForLowerCaseHyphenSeparatedName

@java.lang.Deprecated*
public static java.lang.String getPropertyNameForLowerCaseHyphenSeparatedName(java.lang.String name)


getPropertyNameRepresentation

@java.lang.SuppressWarnings*
@java.lang.Deprecated* @param name The name to convert
public static java.lang.String getPropertyNameRepresentation(java.lang.Class targetClass)
Returns the property name equivalent for the specified class
deprecated:
Use GrailsNameUtils.getPropertyNameRepresentation instead.
Parameters:
targetClass - The class to get the property name for
Returns:
A property name reperesentation of the class name (eg. MyClass becomes myClass)


getPropertyNameRepresentation

@java.lang.Deprecated// Check whether the name begins with two upper case letters.
public static java.lang.String getPropertyNameRepresentation(java.lang.String name)


getPropertyOrStaticPropertyOrFieldValue

public static java.lang.Object getPropertyOrStaticPropertyOrFieldValue(java.lang.Object obj, java.lang.String name)


getPropertyType

public 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
Parameters:
clazz - The class which contains the property
propertyName - The name of the property
Returns:
The property type or null if none exists


getPropertyValueOfNewInstance

@java.lang.SuppressWarnings// validate
public 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
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 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
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


getScriptName

@java.lang.SuppressWarningspublic static String getScriptName(String name) {
@java.lang.Deprecatedif (name.endsWith(".groovy")) {
public static java.lang.String getScriptName(java.lang.Class clazz)
deprecated:
Use GrailsNameUtils.getScriptName instead.


getScriptName

@java.lang.Deprecated* Calculates the class name from a script name in the form
public static java.lang.String getScriptName(java.lang.String name)


getSetterName

public static java.lang.String getSetterName(java.lang.String propertyName)


getShortName

@java.lang.SuppressWarnings*
@java.lang.Deprecated* @param className The class name to get a short name for
public static java.lang.String getShortName(java.lang.Class targetClass)
Returns the class name without the package prefix
deprecated:
Use GrailsNameUtils.getShortName instead.
Parameters:
targetClass - The class to get a short name for
Returns:
The short name of the class


getShortName

@java.lang.Deprecated
public static java.lang.String getShortName(java.lang.String className)


getStaticFieldValue

public static java.lang.Object getStaticFieldValue(java.lang.Class clazz, java.lang.String name)


getStaticPropertyValue

public static java.lang.Object getStaticPropertyValue(java.lang.Class clazz, java.lang.String name)


instantiateFromConfig

@java.lang.SuppressWarnings
public static java.lang.Object instantiateFromConfig(groovy.util.ConfigObject config, java.lang.String configKey, java.lang.String defaultClassName)


instantiateFromFlatConfig

public static java.lang.Object instantiateFromFlatConfig(java.util.Map flatConfig, java.lang.String configKey, java.lang.String defaultClassName)


isAssignableOrConvertibleFrom

public 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. Converts primitive types to compatible class automatically.
Parameters:
clazz
type
Returns:
true if the class is a taglib
See Also:
java.lang.Class#isAssignableFrom(Class)


isClassBelowPackage

public static boolean isClassBelowPackage(java.lang.Class theClass, java.util.List packageList)


isGetter

public static boolean isGetter(java.lang.String name, java.lang.Class[] args)


isGroovyAssignableFrom

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

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.


isJdk5Enum

public static boolean isJdk5Enum(java.lang.Class type)


isMatchBetweenPrimativeAndWrapperTypes

@java.lang.SuppressWarnings}
public static boolean isMatchBetweenPrimativeAndWrapperTypes(java.lang.Class leftType, java.lang.Class rightType)


isPropertyInherited

@java.lang.SuppressWarnings("rawtypes")
public static boolean isPropertyInherited(java.lang.Class clz, java.lang.String propertyName)
Creates a concrete collection for the suppied interface
Parameters:
interfaceType - The interface
Returns:
ArrayList for List, TreeSet for SortedSet, HashSet for Set etc.


isPropertyOfType

public 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
Parameters:
clazz - The class which contains the property
propertyName - The property name
type - The type to check
Returns:
A boolean value


isPublicField

public static boolean isPublicField(java.lang.Object obj, java.lang.String name)
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


isPublicStatic

public static boolean isPublicStatic(java.lang.reflect.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(java.lang.reflect.Field f)


isSetter

@java.lang.SuppressWarningsMetaClass mc = registry.getMetaClass(clazz);
public static boolean isSetter(java.lang.String name, java.lang.Class[] args)


isStaticProperty

@java.lang.SuppressWarnings}
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

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


isVisible

public static boolean isVisible(java.lang.Class clazz, java.lang.ClassLoader classLoader)
Check whether the given class is visible in the given ClassLoader.
Parameters:
clazz - the class to check (typically an interface)
classLoader - the ClassLoader to check against (may be null, in which case this method will always return true)


 

Groovy Documentation