GrailsNameUtils (Grails 6.0.0)

Package: grails.util

## [Java] Class GrailsNameUtils

- grails.util.GrailsNameUtils

-     public class GrailsNameUtils
      extends java.lang.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!

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `private static java.lang.String` | `PROPERTY_GET_PREFIX` |
    `private static java.lang.String` | `PROPERTY_SET_PREFIX` |

  - ### Constructor Summary

    Constructors
    Constructor and description
    `private GrailsNameUtils ()`

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `static java.lang.String` | `convertValidPropertyMethodSuffix(java.lang.String suffix)` This method functions the same as isPropertyMethodSuffix(String), but in addition returns the property name, or null if not a valid property.
    `` | `public static java.lang.String` | `getClassName(java.lang.String logicalName, java.lang.String trailingName)` Returns the class name for the given logical name and trailing name.
    `` | `public static java.lang.String` | `getClassName(java.lang.String logicalName)` Return the class name for the given logical name.
    `` | `private static java.lang.String` | `getClassNameForLowerCaseHyphenSeparatedName(java.lang.String name)` Converts foo-bar into FooBar.
    `` | `public static java.lang.String` | `getClassNameRepresentation(java.lang.String name)` Returns the class name representation of the given name
    `` | `public static java.lang.String` | `getFullClassName(java.lang.Class cls)` Returns the class name, including package, for the given class.
    `` | `public static java.lang.String` | `getFullClassName(java.lang.String className)` Returns the class name, including package, for the given class.
    `` | `public static java.lang.String` | `getGetterName(java.lang.String propertyName)` Calculate the name for a getter method to retrieve the specified property
    `` | `public static java.lang.String` | `getLogicalName(java.lang.Class<?> clazz, java.lang.String trailingName)` Retrieves the logical class name of a Grails artifact given the Grails class and a specified trailing name.
    `` | `public static java.lang.String` | `getLogicalName(java.lang.String name, java.lang.String trailingName)` Retrieves the logical name of the class without the trailing name
    `` | `public static java.lang.String` | `getLogicalPropertyName(java.lang.String className, java.lang.String trailingName)`
    `` | `public static java.lang.String` | `getNameFromScript(java.lang.String scriptName)` Calculates the class name from a script name in the form my-funk-grails-script.
    `` | `public static java.lang.String` | `getNaturalName(java.lang.String name)` Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name')
    `` | `public static java.lang.String` | `getPackageName(java.lang.String className)` Returns the package prefix without the class name eg ('a.b.ClassName' becomes 'a.b').
    `` | `public static java.lang.String` | `getPluginName(java.lang.String descriptorName)` Returns the name of a plugin given the name of the *GrailsPlugin.groovy descriptor file.
    `` | `public 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 valid getter.
    `` | `public static java.lang.String` | `getPropertyForGetter(java.lang.String getterName, java.lang.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 java.lang.String` | `getPropertyForGetter(java.lang.String getterName, java.lang.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 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 valid setter.
    `` | `public static java.lang.String` | `getPropertyName(java.lang.String name)` Shorter version of getPropertyNameRepresentation.
    `` | `public static java.lang.String` | `getPropertyName(java.lang.Class<?> clazz)` Shorter version of getPropertyNameRepresentation.
    `` | `public static java.lang.String` | `getPropertyNameConvention(java.lang.Object object)` Returns an appropriate property name for the given object.
    `` | `public static java.lang.String` | `getPropertyNameConvention(java.lang.Object object, java.lang.String suffix)` Returns an appropriate property name for the given object.
    `` | `public static java.lang.String` | `getPropertyNameForLowerCaseHyphenSeparatedName(java.lang.String name)` Converts foo-bar into fooBar.
    `` | `public static java.lang.String` | `getPropertyNameRepresentation(java.lang.Class<?> targetClass)` Returns the property name equivalent for the specified class.
    `` | `public static java.lang.String` | `getPropertyNameRepresentation(java.lang.String name)` Returns the property name representation of the given name.
    `` | `public static java.lang.String` | `getScriptName(java.lang.Class<?> clazz)` Retrieves the script name representation of the supplied class.
    `` | `public static java.lang.String` | `getScriptName(java.lang.String name)` Retrieves the script name representation of the given class name.
    `` | `public static java.lang.String` | `getSetterName(java.lang.String propertyName)` Retrieves the name of a setter for the specified property name
    `` | `public static java.lang.String` | `getShortName(java.lang.Class<?> targetClass)` Returns the class name without the package prefix.
    `` | `public static java.lang.String` | `getShortName(java.lang.String className)` Returns the class name without the package prefix.
    `` | `private static java.lang.String` | `getSuffixForGetterOrSetter(java.lang.String propertyName)`
    `` | `public static boolean` | `isBlank(java.lang.String str)`
    `` | `public 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 getter.
    `` | `public static boolean` | `isGetter(java.lang.String name, java.lang.Class returnType, java.lang.Class<?>[] args)` Returns true if the name of the method specified and the number of arguments make it a javabean property getter.
    `` | `protected static boolean` | `isPropertyMethodSuffix(java.lang.String suffix)` This method is used when interrogating a method name to determine if the method represents a property getter.
    `` | `public static boolean` | `isValidJavaIdentifier(java.lang.String name)` Test whether the given name is a valid Java identifier
    `` | `public static boolean` | `isValidJavaPackage(java.lang.String packageName)` Test whether the give package name is a valid Java package

  - ### Inherited Methods Summary

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

    - #### private static final java.lang.String PROPERTY_GET_PREFIX

    - #### private static final java.lang.String PROPERTY_SET_PREFIX

  - ### Constructor Detail

    - #### private GrailsNameUtils()

  - ### Method Detail

    - #### static java.lang.String convertValidPropertyMethodSuffix(java.lang.String suffix)

      This method functions the same as isPropertyMethodSuffix(String), but in addition returns the property name, or null if not a valid property.

      Parameters:
        `suffix` - The suffix to inspect
      Returns:
        The property name or null

    - #### public static java.lang.String getClassName(java.lang.String logicalName, java.lang.String trailingName)

      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

    - #### public static java.lang.String getClassName(java.lang.String logicalName)

      Return the class name for the given logical name. For example "person" would evaluate to "Person"

      Parameters:
        `logicalName` - The logical name
      Returns:
        The class name

    - #### private static java.lang.String getClassNameForLowerCaseHyphenSeparatedName(java.lang.String name)

      Converts foo-bar into FooBar. Empty and null strings are returned as-is.

      Parameters:
        `name` - The lower case hyphen separated name
      Returns:
        The class name equivalent.

    - #### public static java.lang.String getClassNameRepresentation(java.lang.String name)

      Returns the class name representation of the given name

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

    - #### public static java.lang.String getFullClassName(java.lang.Class cls)

      Returns the class name, including package, for the given class. This method will deals with proxies and closures.

      Parameters:
        `cls` - The class name

    - #### public static java.lang.String getFullClassName(java.lang.String className)

      Returns the class name, including package, for the given class. This method will deals with proxies and closures.

      Parameters:
        `className` - The class name

    - #### public static java.lang.String getGetterName(java.lang.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

    - #### public static java.lang.String getLogicalName(java.lang.Class<?> clazz, java.lang.String trailingName)

      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

    - #### public static java.lang.String getLogicalName(java.lang.String name, java.lang.String trailingName)

      Retrieves the logical name of the class without the trailing name

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

    - #### public static java.lang.String getLogicalPropertyName(java.lang.String className, java.lang.String trailingName)

    - #### public static java.lang.String getNameFromScript(java.lang.String scriptName)

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

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

    - #### public static java.lang.String getNaturalName(java.lang.String name)

      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

    - #### public static java.lang.String getPackageName(java.lang.String className)

      Returns the package prefix without the class name eg ('a.b.ClassName' becomes 'a.b').

      Parameters:
        `className` - The class name to get the package prefix for
      Returns:
        The package prefix of the class

    - #### public static java.lang.String getPluginName(java.lang.String descriptorName)

      Returns the name of a plugin given the name of the *GrailsPlugin.groovy descriptor file. For example, "DbUtilsGrailsPlugin.groovy" gives "db-utils".

      throws:
        IllegalArgumentException if the given descriptor name is not valid, i.e. if it doesn't end with "GrailsPlugin.groovy".
      Parameters:
        `descriptorName` - The simple name of the plugin descriptor.
      Returns:
        The plugin name for the descriptor, or `null` if descriptorName is `null`, or an empty string if descriptorName is an empty string.

    - #### public 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 valid getter. If not null or empty the getter name is assumed to be a valid identifier.

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

    - #### public static java.lang.String getPropertyForGetter(java.lang.String getterName, java.lang.Class returnType)

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

      Parameters:
        `getterName` - The getter name
        `returnType` - The type the method returns
      Returns:
        The property name equivalent

    - #### public static java.lang.String getPropertyForGetter(java.lang.String getterName, java.lang.String returnType)

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

      Parameters:
        `getterName` - The getter name
        `returnType` - The type the method returns
      Returns:
        The property name equivalent

    - #### public 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 valid setter. If not null or empty the setter name is assumed to be a valid identifier.

      Parameters:
        `setterName` - The setter name, must be null or empty or a valid identifier name
      Returns:
        The property name equivalent

    - #### public static java.lang.String getPropertyName(java.lang.String name)

      Shorter version of getPropertyNameRepresentation.

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

    - #### public static java.lang.String getPropertyName(java.lang.Class<?> clazz)

      Shorter version of getPropertyNameRepresentation.

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

    - #### public static java.lang.String getPropertyNameConvention(java.lang.Object object)

      Returns 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

      Parameters:
        `object` - The object
      Returns:
        The property name convention

    - #### public static java.lang.String getPropertyNameConvention(java.lang.Object object, java.lang.String suffix)

      Returns 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

      Parameters:
        `object` - The object
        `suffix` - The suffix to append to the name.
      Returns:
        The property name convention

    - #### public static java.lang.String getPropertyNameForLowerCaseHyphenSeparatedName(java.lang.String name)

      Converts foo-bar into fooBar.

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

    - #### public static java.lang.String getPropertyNameRepresentation(java.lang.Class<?> targetClass)

      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)

    - #### public static java.lang.String getPropertyNameRepresentation(java.lang.String name)

      Returns the property name representation of the given name.

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

    - #### public static java.lang.String getScriptName(java.lang.Class<?> clazz)

      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

    - #### public static java.lang.String getScriptName(java.lang.String name)

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

      Parameters:
        `name` - The class name to convert.
      Returns:
        The script name representation.

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

      Retrieves the name of a setter for the specified property name

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

    - #### public static java.lang.String getShortName(java.lang.Class<?> targetClass)

      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

    - #### public static java.lang.String getShortName(java.lang.String className)

      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

    - #### private static java.lang.String getSuffixForGetterOrSetter(java.lang.String propertyName)

    - #### public static boolean isBlank(java.lang.String str)

      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.

      Parameters:
        `str` - The string to test.
      Returns:
        `true` if the string is `null`, or blank.

    - #### @java.lang.Deprecated public 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 getter. The name is assumed to be a valid Java method name, that is not verified.

      deprecated:
        use isGetter(String, Class, Class[]) instead because this method has a defect for "is.." method with Boolean return types.
      Parameters:
        `name` - The name of the method
        `args` - The arguments
      Returns:
        true if it is a javabean property getter

    - #### public static boolean isGetter(java.lang.String name, java.lang.Class returnType, java.lang.Class<?>[] args)

      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.

      Parameters:
        `name` - The name of the method
        `returnType` - The return type of the method
        `args` - The arguments
      Returns:
        true if it is a javabean property getter

    - #### protected static boolean isPropertyMethodSuffix(java.lang.String suffix)

      This method is used when interrogating a method name to determine if the method represents a property getter. For example, if a method is named `getSomeProperty`, the value `"SomeProperty"` could be passed to this method to determine that the method should be considered a property getter. Examples of suffixes that would be considered property getters:

      - SomeProperty
      - Word
      - aProperty
      - S
      - X567
      Examples of suffixes that would not be considered property getters:
      - someProperty
      - word
      - s
      - x567
      - 2other
      - 5
      A suffix like `prop` from a method `getprop()` is not recognized as a valid suffix. However Groovy will recognize such a method as a property getter but only if a method `getProp()` or a property `prop` does not also exist. The Java Beans specification is unclear on how to treat such method names, it only says that "by default" the suffix will start with a capital letter because of the camel case style usually used. (See the JavaBeans API specification sections 8.3 and 8.8.) This method assumes that all characters in the name are valid Java identifier letters.
      Parameters:
        `suffix` - The suffix to inspect
      Returns:
        true if suffix indicates a property name

    - #### public static boolean isValidJavaIdentifier(java.lang.String name)

      Test whether the given name is a valid Java identifier

      Parameters:
        `name` - The name
      Returns:
        True if it is

    - #### public static boolean isValidJavaPackage(java.lang.String packageName)

      Test whether the give package name is a valid Java package

      Parameters:
        `packageName` - The name of the package
      Returns:
        True if it is valid