public final class NameUtils extends Object
Modifier and Type | Method and Description |
---|---|
static String |
getClassName(String logicalName)
Return the class name for the given logical name.
|
static String |
getClassName(String logicalName,
String trailingName)
Returns the class name for the given logical name and trailing name.
|
static String |
getClassNameRepresentation(String name)
Returns the class name representation of the given name.
|
static String |
getFullClassName(Class cls)
Returns the class name, including package, for the given class.
|
static String |
getFullClassName(String className)
Returns the class name, including package, for the given class.
|
static String |
getGetterName(String propertyName)
Calculate the name for a getter method to retrieve the specified property.
|
static String |
getLogicalName(Class<?> clazz,
String trailingName)
Retrieves the logical class name of a Micronaut artifact given the Micronaut class
and a specified trailing name.
|
static String |
getLogicalName(String name,
String trailingName)
Retrieves the logical name of the class without the trailing name.
|
static String |
getLogicalPropertyName(String className,
String trailingName) |
static String |
getNameFromScript(String scriptName)
Calculates the class name from a script name in the form my-funk-grails-script.
|
static String |
getNaturalName(String name)
Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name').
|
static String |
getPackageName(String className)
Returns the package name without the class.
|
static String |
getPluginName(String descriptorName)
Returns the name of a plugin given the name of the *GrailsPlugin.groovy
descriptor file.
|
static String |
getPropertyForGetter(String getterName)
Returns a property name equivalent for the given getter name or null if it is not a valid getter.
|
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.
|
static String |
getPropertyForSetter(String setterName)
Returns a property name equivalent for the given setter name or null if it is not a valid setter.
|
static String |
getPropertyName(Class<?> clazz)
Shorter version of getPropertyNameRepresentation.
|
static String |
getPropertyName(String name)
Shorter version of getPropertyNameRepresentation.
|
static String |
getPropertyNameConvention(Object object)
Returns an appropriate property name for the given object.
|
static String |
getPropertyNameConvention(Object object,
String suffix)
Returns an appropriate property name for the given object.
|
static String |
getPropertyNameForLowerCaseHyphenSeparatedName(String name)
Converts foo-bar into fooBar.
|
static String |
getPropertyNameRepresentation(Class<?> targetClass)
Returns the property name equivalent for the specified class.
|
static String |
getPropertyNameRepresentation(String name)
Returns the property name representation of the given name.
|
static String |
getScriptName(Class<?> clazz)
Retrieves the script name representation of the supplied class.
|
static String |
getScriptName(String name)
Retrieves the script name representation of the given class name.
|
static String |
getSetterName(String propertyName)
Retrieves the name of a setter for the specified property name.
|
static String |
getShortName(Class<?> targetClass)
Returns the class name without the package prefix.
|
static String |
getShortName(String className)
Returns the class name without the package prefix.
|
static boolean |
isBlank(String str)
Determines whether a given string is
null , empty,
or only contains whitespace. |
static boolean |
isGetter(String name,
Class<?>[] args)
Deprecated.
use
isGetter(String, Class, Class[]) instead because this method has a defect for "is.." method with Boolean return types. |
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.
|
protected static boolean |
isPropertyMethodSuffix(String suffix)
This method is used when interrogating a method name to determine if the
method represents a property getter.
|
static boolean |
isValidJavaIdentifier(String name)
Test whether the given name is a valid Java identifier.
|
static boolean |
isValidJavaPackage(String packageName)
Test whether the give package name is a valid Java package.
|
static boolean |
isValidServiceId(String name)
Checks whether the given name is a valid service identifier.
|
public static boolean isValidServiceId(String name)
name
- The namepublic static String getSetterName(String propertyName)
propertyName
- The property namepublic static String getGetterName(String propertyName)
propertyName
- The property namepublic static String getClassName(String logicalName, String trailingName)
logicalName
- The logical nametrailingName
- The trailing namepublic static String getFullClassName(Class cls)
cls
- The class namepublic static String getFullClassName(String className)
className
- The class namepublic static String getClassName(String logicalName)
logicalName
- The logical namepublic static String getClassNameRepresentation(String name)
name
- The name to convertpublic static String getLogicalName(Class<?> clazz, String trailingName)
clazz
- The classtrailingName
- The trailing name such as "Controller" or "Service"public static String getLogicalName(String name, String trailingName)
name
- The name of the classtrailingName
- The trailing namepublic static String getLogicalPropertyName(String className, String trailingName)
className
- The class nametrailingName
- The trailing namepublic static String getPropertyName(String name)
name
- The name to convertpublic static String getPropertyName(Class<?> clazz)
clazz
- The clazz to convertpublic static String getPropertyNameRepresentation(Class<?> targetClass)
targetClass
- The class to get the property name forpublic static String getPropertyNameRepresentation(String name)
name
- The name to convertpublic static String getPropertyNameForLowerCaseHyphenSeparatedName(String name)
name
- The lower case hyphen separated namepublic static String getShortName(Class<?> targetClass)
targetClass
- The class to get a short name forpublic static String getShortName(String className)
className
- The class name from which to get a short namepublic static String getPackageName(String className)
className
- The class name from which to get a package namepublic static String getScriptName(Class<?> clazz)
clazz
- The class to convertpublic static String getScriptName(String name)
name
- The class name to convert.public static String getNameFromScript(String scriptName)
scriptName
- The script namepublic static String getPluginName(String descriptorName)
descriptorName
- The simple name of the plugin descriptor.null
if descriptorName is null
, or an empty string
if descriptorName is an empty string.IllegalArgumentException
- if the given descriptor name is
not valid, i.e. if it doesn't end with "GrailsPlugin.groovy".public static String getNaturalName(String name)
name
- The property name to convertpublic static boolean isBlank(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 NameUtils to have a dependency on any external library to minimise the number of dependencies required to bootstrap Micronaut.
str
- The string to test.true
if the string is null
, or
blank.public static String getPropertyNameConvention(Object object)
object
- The objectpublic static boolean isValidJavaPackage(String packageName)
packageName
- The name of the packagepublic static boolean isValidJavaIdentifier(String name)
name
- The namepublic static String getPropertyNameConvention(Object object, String suffix)
object
- The objectsuffix
- The suffix to append to the name.public static String getPropertyForGetter(String getterName)
getterName
- The getter namepublic static String getPropertyForGetter(String getterName, Class returnType)
getterName
- The getter namereturnType
- The type the method returnspublic static boolean isGetter(String name, Class<?>[] args)
isGetter(String, Class, Class[])
instead because this method has a defect for "is.." method with Boolean return types.name
- The name of the methodargs
- The argumentspublic static boolean isGetter(String name, Class returnType, Class<?>[] args)
name
- The name of the methodreturnType
- The return type of the methodargs
- The argumentsprotected static boolean isPropertyMethodSuffix(String suffix)
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:
Examples of suffixes that would not be considered property getters:
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.
suffix
- The suffix to inspectpublic static String getPropertyForSetter(String setterName)
setterName
- The setter name, must be null or empty or a valid identifier name