Class NameUtils
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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
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
getNaturalNameOfEnum
(String name) Converts an enum style name into its natural language equivalent eg ('FIRST_NAME' 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, String suffix) Returns an appropriate property name for the given object.static String
Converts foo-bar into fooBar.static String
getPropertyNameRepresentation
(Class<?> targetClass) Returns the property name equivalent for the specified class.static String
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
Determines whether a given string isnull
, empty, or only contains whitespace.static boolean
Deprecated.static boolean
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
isValidAppName
(String appName) verifies the app is not blank, null or matches micronautstatic 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.static Project
static ProjectIdentifier
parseProjectIdentifier
(String name)
-
Field Details
-
MICRONAUT
- See Also:
-
-
Method Details
-
parseProjectIdentifier
- Throws:
IllegalArgumentException
-
isValidAppName
verifies the app is not blank, null or matches micronaut- Parameters:
appName
- Application's name- Returns:
- True if it is valid
-
parse
- Throws:
IllegalArgumentException
-
isValidServiceId
Checks whether the given name is a valid service identifier.- Parameters:
name
- The name- Returns:
- True if it is
-
getSetterName
Retrieves the name of a setter for the specified property name.- Parameters:
propertyName
- The property name- Returns:
- The setter equivalent
-
getGetterName
Calculate the name for a getter method to retrieve the specified property.- Parameters:
propertyName
- The property name- Returns:
- The name for the getter method for this property, if it were to exist, i.e. getConstraints
-
getClassName
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 nametrailingName
- The trailing name- Returns:
- The class name
-
getFullClassName
Returns the class name, including package, for the given class. This method will deals with proxies and closures.- Parameters:
cls
- The class name- Returns:
- The full class name
-
getFullClassName
Returns the class name, including package, for the given class. This method will deals with proxies and closures.- Parameters:
className
- The class name- Returns:
- the full class name
-
getClassName
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
-
getClassNameRepresentation
Returns the class name representation of the given name.- Parameters:
name
- The name to convert- Returns:
- The property name representation
-
getLogicalName
Retrieves the logical class name of a Micronaut artifact given the Micronaut class and a specified trailing name.- Parameters:
clazz
- The classtrailingName
- The trailing name such as "Controller" or "Service"- Returns:
- The logical class name
-
getLogicalName
Retrieves the logical name of the class without the trailing name.- Parameters:
name
- The name of the classtrailingName
- The trailing name- Returns:
- The logical name
-
getLogicalPropertyName
- Parameters:
className
- The class nametrailingName
- The trailing name- Returns:
- The logical property name
-
getPropertyName
Shorter version of getPropertyNameRepresentation.- Parameters:
name
- The name to convert- Returns:
- The property name version
-
getPropertyName
Shorter version of getPropertyNameRepresentation.- Parameters:
clazz
- The clazz to convert- Returns:
- The property name version
-
getPropertyNameRepresentation
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
Returns the property name representation of the given name.- Parameters:
name
- The name to convert- Returns:
- The property name representation
-
getPropertyNameForLowerCaseHyphenSeparatedName
Converts foo-bar into fooBar.- Parameters:
name
- The lower case hyphen separated name- Returns:
- The property name equivalent
-
getShortName
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
Returns the class name without the package prefix.- Parameters:
className
- The class name from which to get a short name- Returns:
- The short name of the class
-
getPackageName
Returns the package name without the class.- Parameters:
className
- The class name from which to get a package name- Returns:
- The short name of the class
-
getScriptName
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
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.
-
getNameFromScript
Calculates the class name from a script name in the form my-funk-grails-script.- Parameters:
scriptName
- The script name- Returns:
- A class name
-
getPluginName
Returns the name of a plugin given the name of the *GrailsPlugin.groovy descriptor file. For example, "DbUtilsGrailsPlugin.groovy" gives "db-utils".- Parameters:
descriptorName
- The simple name of the plugin descriptor.- Returns:
- The plugin name for the descriptor, or
null
if descriptorName isnull
, or an empty string if descriptorName is an empty string. - Throws:
IllegalArgumentException
- if the given descriptor name is not valid, i.e. if it doesn't end with "GrailsPlugin.groovy".
-
getNaturalName
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
-
getNaturalNameOfEnum
Converts an enum style name into its natural language equivalent eg ('FIRST_NAME' becomes 'First Name').- Parameters:
name
- The string to convert- Returns:
- The converted property name
-
isBlank
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 returnsfalse
.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.
- Parameters:
str
- The string to test.- Returns:
true
if the string isnull
, or blank.
-
isValidJavaPackage
Test whether the give package name is a valid Java package.- Parameters:
packageName
- The name of the package- Returns:
- True if it is valid
-
isValidJavaIdentifier
Test whether the given name is a valid Java identifier.- Parameters:
name
- The name- Returns:
- True if it is
-
getPropertyNameConvention
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 objectsuffix
- The suffix to append to the name.- Returns:
- The property name convention
-
getPropertyForGetter
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
-
getPropertyForGetter
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 namereturnType
- The type the method returns- Returns:
- The property name equivalent
-
isGetter
Deprecated.useisGetter(String, Class, Class[])
instead because this method has a defect for "is.." method with Boolean return types.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 methodargs
- The arguments- Returns:
- true if it is a javabean property getter
-
isGetter
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 methodreturnType
- The return type of the methodargs
- The arguments- Returns:
- true if it is a javabean property getter
-
isPropertyMethodSuffix
This method is used when interrogating a method name to determine if the method represents a property getter. For example, if a method is namedgetSomeProperty
, 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 methodgetprop()
is not recognized as a valid suffix. However Groovy will recognize such a method as a property getter but only if a methodgetProp()
or a propertyprop
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
-
getPropertyForSetter
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
-
isGetter(String, Class, Class[])
instead because this method has a defect for "is.." method with Boolean return types.