public final class GrailsNameUtils extends Object
Utility methods for converting between different name types, for example from class names -> property names and vice-versa. The key aspect of this class is that it has no dependencies outside the JDK!
Type Params | Return Type | Name and description |
---|---|---|
|
public static String |
getClassName(String logicalName, String trailingName) Returns the class name for the given logical name and trailing name. |
|
public static String |
getClassName(String logicalName) Return the class name for the given logical name. |
|
public static String |
getClassNameRepresentation(String name) Returns the class name representation of the given name |
|
public static String |
getFullClassName(Class<?> cls) Returns the class name, including package, for the given class. |
|
public static String |
getFullClassName(String className) Returns the class name, including package, for the given class. |
|
public static String |
getGetterName(String propertyName) Calculate the name for a getter method to retrieve the specified property |
|
public static String |
getLogicalName(Class<?> clazz, String trailingName) Retrieves the logical class name of a Grails artifact given the Grails class and a specified trailing name. |
|
public static String |
getLogicalName(String name, String trailingName) Retrieves the logical name of the class without the trailing name |
|
public static String |
getLogicalPropertyName(Class<?> clazz, String trailingName) |
|
public static String |
getLogicalPropertyName(String className, String trailingName) |
|
public static String |
getNameFromScript(String scriptName) Calculates the class name from a script name in the form my-funk-grails-script. |
|
public static String |
getNaturalName(String name) Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name') |
|
public static String |
getPackageName(String className) Returns the package prefix without the class name eg ('a.b.ClassName' becomes 'a.b'). |
|
public static String |
getPluginName(String descriptorName) Returns the name of a plugin given the name of the *GrailsPlugin.groovy descriptor file. |
|
public static String |
getPropertyForGetter(String getterName) Returns a property name equivalent for the given getter name or null if it is not a valid getter. |
|
public static String |
getPropertyForGetter(String getterName, Class<?> returnType) Returns a property name equivalent for the given getter name and return type or null if it is not a valid getter. |
|
public static String |
getPropertyForGetter(String getterName, String returnType) Returns a property name equivalent for the given getter name and return type or null if it is not a valid getter. |
|
public static String |
getPropertyForSetter(String setterName) Returns a property name equivalent for the given setter name or null if it is not a valid setter. |
|
public static String |
getPropertyName(String name) Shorter version of getPropertyNameRepresentation. |
|
public static String |
getPropertyName(Class<?> clazz) Shorter version of getPropertyNameRepresentation. |
|
public static String |
getPropertyNameConvention(Object object) Returns an appropriate property name for the given object. |
|
public static String |
getPropertyNameConvention(Object object, String suffix) Returns an appropriate property name for the given object. |
|
public static String |
getPropertyNameForLowerCaseHyphenSeparatedName(String name) Converts foo-bar into fooBar. |
|
public static String |
getPropertyNameRepresentation(Class<?> targetClass) Returns the property name equivalent for the specified class. |
|
public static String |
getPropertyNameRepresentation(String name) Returns the property name representation of the given name. |
|
public static String |
getScriptName(Class<?> clazz) Retrieves the script name representation of the supplied class. |
|
public static String |
getScriptName(String name) Retrieves the script name representation of the given class name. |
|
public static String |
getSetterName(String propertyName) Retrieves the name of a setter for the specified property name |
|
public static String |
getShortName(Class<?> targetClass) Returns the class name without the package prefix. |
|
public static String |
getShortName(String className) Returns the class name without the package prefix. |
|
public static String |
getSnakeCaseName(Class<?> clazz) Retrieves the snake case name of the supplied class. |
|
public static String |
getSnakeCaseName(String name) Retrieves the snake case name of the given class name. |
|
public static boolean |
isBlank(String str) |
|
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 getter. |
|
public static boolean |
isGetter(String name, Class<?> returnType, Class<?>[] args) Returns true if the name of the method specified and the number of arguments make it a javabean property getter. |
|
public static boolean |
isValidJavaIdentifier(String name) Test whether the given name is a valid Java identifier |
|
public static boolean |
isValidJavaPackage(String packageName) Test whether the give package name is a valid Java package |
Returns the class name for the given logical name and trailing name. For example "person" and "Controller" would evaluate to "PersonController"
logicalName
- The logical nametrailingName
- The trailing nameReturn the class name for the given logical name. For example "person" would evaluate to "Person"
logicalName
- The logical nameReturns the class name representation of the given name
name
- The name to convertReturns the class name, including package, for the given class. This method will deals with proxies and closures.
cls
- The class nameReturns the class name, including package, for the given class. This method will deals with proxies and closures.
className
- The class nameCalculate the name for a getter method to retrieve the specified property
Retrieves the logical class name of a Grails artifact given the Grails class and a specified trailing name.
clazz
- The classtrailingName
- The trailing name such as "Controller" or "TagLib"Retrieves the logical name of the class without the trailing name
name
- The name of the classtrailingName
- The trailing nameCalculates the class name from a script name in the form my-funk-grails-script.
scriptName
- The script nameConverts a property name into its natural language equivalent eg ('firstName' becomes 'First Name')
name
- The property name to convertReturns the package prefix without the class name eg ('a.b.ClassName' becomes 'a.b').
className
- The class name to get the package prefix forReturns the name of a plugin given the name of the *GrailsPlugin.groovy descriptor file. For example, "DbUtilsGrailsPlugin.groovy" gives "db-utils".
descriptorName
- The simple name of the plugin descriptor.null
if descriptorName is null
, or an empty string
if descriptorName is an empty string.Returns a property name equivalent for the given getter name or null if it is not a valid getter. If not null or empty the getter name is assumed to be a valid identifier.
getterName
- The getter nameReturns a property name equivalent for the given getter name and return type or null if it is not a valid getter. If not null or empty the getter name is assumed to be a valid identifier.
getterName
- The getter namereturnType
- The type the method returnsReturns a property name equivalent for the given getter name and return type or null if it is not a valid getter. If not null or empty the getter name is assumed to be a valid identifier.
getterName
- The getter namereturnType
- The type the method returnsReturns a property name equivalent for the given setter name or null if it is not a valid setter. If not null or empty the setter name is assumed to be a valid identifier.
setterName
- The setter name, must be null or empty or a valid identifier nameShorter version of getPropertyNameRepresentation.
name
- The name to convertShorter version of getPropertyNameRepresentation.
clazz
- The clazz to convertReturns an appropriate property name for the given object. If the object is a collection will append List, Set, Collection or Map to the property name
object
- The objectReturns an appropriate property name for the given object. If the object is a collection will append List, Set, Collection or Map to the property name
object
- The objectsuffix
- The suffix to append to the name.Converts foo-bar into fooBar.
name
- The lower case hyphen separated nameReturns the property name equivalent for the specified class.
targetClass
- The class to get the property name forReturns the property name representation of the given name.
name
- The name to convertRetrieves the script name representation of the supplied class. For example MyFunkyGrailsScript would be my-funky-grails-script.
clazz
- The class to convertRetrieves the script name representation of the given class name. For example MyFunkyGrailsScript would be my-funky-grails-script.
name
- The class name to convert.Retrieves the name of a setter for the specified property name
propertyName
- The property nameReturns the class name without the package prefix.
targetClass
- The class to get a short name forReturns the class name without the package prefix.
className
- The class name to get a short name forRetrieves the snake case name of the supplied class. For example MyFunkyGrailsScript would be my_funky_grails_script.
clazz
- The class to convertRetrieves the snake case name of the given class name. For example MyFunkyGrailsScript would be my_funky_grails_script.
name
- The class name to convert.
Determines whether a given string is null
, empty,
or only contains whitespace. If it contains anything other than
whitespace then the string is not considered to be blank and the
method returns false
.
We could use Commons Lang for this, but we don't want GrailsNameUtils to have a dependency on any external library to minimise the number of dependencies required to bootstrap Grails.
str
- The string to test.true
if the string is null
, or
blank.Returns true if the name of the method specified and the number of arguments make it a javabean property getter. The name is assumed to be a valid Java method name, that is not verified.
name
- The name of the methodargs
- The argumentsReturns true if the name of the method specified and the number of arguments make it a javabean property getter. The name is assumed to be a valid Java method name, that is not verified.
name
- The name of the methodreturnType
- The return type of the methodargs
- The argumentsTest whether the given name is a valid Java identifier
name
- The nameTest whether the give package name is a valid Java package
packageName
- The name of the package