Skip to content

Commit

Permalink
Add nullability annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
andriy-dmytruk committed Dec 5, 2024
1 parent c5b8ae4 commit 159bbbd
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions core/src/main/java/io/micronaut/core/naming/NameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.micronaut.core.annotation.AccessorsStyle;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.StringUtils;

Expand Down Expand Up @@ -48,7 +49,7 @@ public class NameUtils {
* @param name The name
* @return True if it is
*/
public static boolean isHyphenatedLowerCase(String name) {
public static boolean isHyphenatedLowerCase(@Nullable String name) {
if (name == null || name.isEmpty() || !Character.isLetter(name.charAt(0))) {
return false;
}
Expand All @@ -68,7 +69,7 @@ public static boolean isHyphenatedLowerCase(String name) {
* @param suffixes The suffix to remove
* @return The decapitalized name
*/
public static String decapitalizeWithoutSuffix(String name, String... suffixes) {
public static @NonNull String decapitalizeWithoutSuffix(@NonNull String name, String... suffixes) {
String decapitalized = decapitalize(name);
return trimSuffix(decapitalized, suffixes);
}
Expand All @@ -80,7 +81,7 @@ public static String decapitalizeWithoutSuffix(String name, String... suffixes)
* @param suffixes The suffixes
* @return The trimmed string
*/
public static String trimSuffix(String string, String... suffixes) {
public static @NonNull String trimSuffix(@NonNull String string, String... suffixes) {
if (suffixes != null) {
for (String suffix : suffixes) {
if (string.endsWith(suffix)) {
Expand All @@ -97,7 +98,7 @@ public static String trimSuffix(String string, String... suffixes) {
* @param name The property name
* @return The class name
*/
public static String capitalize(String name) {
public static @NonNull String capitalize(@NonNull String name) {
final String rest = name.substring(1);

// Funky rule so that names like 'pNAME' will still work.
Expand All @@ -114,7 +115,7 @@ public static String capitalize(String name) {
* @param name The name
* @return The hyphenated string
*/
public static String hyphenate(String name) {
public static @NonNull String hyphenate(@NonNull String name) {
return hyphenate(name, true);
}

Expand All @@ -125,7 +126,7 @@ public static String hyphenate(String name) {
* @param lowerCase Whether the result should be converted to lower case
* @return The hyphenated string
*/
public static String hyphenate(String name, boolean lowerCase) {
public static @NonNull String hyphenate(@NonNull String name, boolean lowerCase) {
String kebabReplaced = name.replace('_', '-').replace(' ', '-');
if (isHyphenatedLowerCase(name)) {
return kebabReplaced;
Expand All @@ -140,7 +141,7 @@ public static String hyphenate(String name, boolean lowerCase) {
* @param name The hyphenated string
* @return The camel case form
*/
public static String dehyphenate(String name) {
public static @NonNull String dehyphenate(@NonNull String name) {
StringBuilder sb = new StringBuilder(name.length());
for (String token : StringUtils.splitOmitEmptyStrings(name, '-')) {
if (!token.isEmpty() && Character.isLetter(token.charAt(0))) {
Expand All @@ -159,7 +160,7 @@ public static String dehyphenate(String name) {
* @param className The class name
* @return The package name
*/
public static String getPackageName(String className) {
public static @NonNull String getPackageName(@NonNull String className) {
Matcher matcher = DOT_UPPER.matcher(className);
if (matcher.find()) {
int position = matcher.start();
Expand All @@ -174,7 +175,7 @@ public static String getPackageName(String className) {
* @param camelCase The camel case name
* @return The underscore separated version
*/
public static String underscoreSeparate(String camelCase) {
public static @NonNull String underscoreSeparate(@NonNull String camelCase) {
return underscoreSeparate(camelCase, false);
}

Expand All @@ -185,7 +186,7 @@ public static String underscoreSeparate(String camelCase) {
* @param lowercase true to lowercase the result
* @return The underscore separated version
*/
public static String underscoreSeparate(String camelCase, boolean lowercase) {
public static @NonNull String underscoreSeparate(@NonNull String camelCase, boolean lowercase) {
return separateCamelCase(camelCase.replace('-', '_'), lowercase, '_');
}

Expand All @@ -195,7 +196,7 @@ public static String underscoreSeparate(String camelCase, boolean lowercase) {
* @param camelCase The camel case name
* @return The underscore separated version
*/
public static String environmentName(String camelCase) {
public static @NonNull String environmentName(@NonNull String camelCase) {
return separateCamelCase(camelCase.replace('-', '_').replace('.', '_'), false, '_')
.toUpperCase(Locale.ENGLISH);
}
Expand All @@ -206,7 +207,7 @@ public static String environmentName(String camelCase) {
* @param className The class name
* @return The simple name of the class
*/
public static String getSimpleName(String className) {
public static @NonNull String getSimpleName(@NonNull String className) {
Matcher matcher = DOT_UPPER.matcher(className);
if (matcher.find()) {
int position = matcher.start();
Expand All @@ -227,7 +228,7 @@ public static String getSimpleName(String className) {
* @return The shortened type name
*/
@Experimental
public static String getShortenedName(String typeName) {
public static @NonNull String getShortenedName(@NonNull String typeName) {
int nameStart = typeName.lastIndexOf('$');
if (nameStart < 0) {
nameStart = typeName.lastIndexOf('.');
Expand Down Expand Up @@ -256,7 +257,7 @@ public static String getShortenedName(String typeName) {
* @param methodName The method name
* @return True if it is a valid setter name
*/
public static boolean isSetterName(String methodName) {
public static boolean isSetterName(@NonNull String methodName) {
return isWriterName(methodName, AccessorsStyle.DEFAULT_WRITE_PREFIX);
}

Expand Down Expand Up @@ -307,7 +308,7 @@ public static boolean isWriterName(@NonNull String methodName, @NonNull String[]
* @param setterName The setter
* @return The property name
*/
public static String getPropertyNameForSetter(String setterName) {
public static @NonNull String getPropertyNameForSetter(@NonNull String setterName) {
return getPropertyNameForSetter(setterName, AccessorsStyle.DEFAULT_WRITE_PREFIX);
}

Expand Down Expand Up @@ -387,7 +388,7 @@ public static String getPropertyNameForSetter(String setterName) {
* @param methodName The method name
* @return True if it is a valid getter name
*/
public static boolean isGetterName(String methodName) {
public static boolean isGetterName(@NonNull String methodName) {
return isReaderName(methodName, AccessorsStyle.DEFAULT_READ_PREFIX);
}

Expand Down Expand Up @@ -446,7 +447,7 @@ private static boolean isValidCharacterAfterReaderWriterPrefix(char c) {
* @param getterName The getter
* @return The property name
*/
public static String getPropertyNameForGetter(String getterName) {
public static @NonNull String getPropertyNameForGetter(@NonNull String getterName) {
return getPropertyNameForGetter(getterName, AccessorsStyle.DEFAULT_READ_PREFIX);
}

Expand Down Expand Up @@ -550,8 +551,8 @@ public static String getterNameFor(@NonNull String propertyName, boolean isBoole
return nameFor(isBoolean ? PREFIX_IS : PREFIX_GET, propertyName);
}

private static String nameFor(String prefix, @NonNull String propertyName) {
if (prefix.isEmpty()) {
private static @NonNull String nameFor(@Nullable String prefix, @NonNull String propertyName) {
if (StringUtils.isEmpty(prefix)) {
return propertyName;
}

Expand All @@ -577,7 +578,7 @@ private static String nameFor(String prefix, @NonNull String propertyName) {
* @param name The String to decapitalize
* @return The decapitalized version of the String
*/
public static String decapitalize(String name) {
public static @Nullable String decapitalize(@Nullable String name) {
if (name == null) {
return null;
}
Expand Down Expand Up @@ -608,7 +609,7 @@ public static String decapitalize(String name) {
return name;
}

static String separateCamelCase(String name, boolean lowerCase, char separatorChar) {
static @NonNull String separateCamelCase(@NonNull String name, boolean lowerCase, char separatorChar) {
StringBuilder newName = new StringBuilder(name.length() + 4);
if (!lowerCase) {
boolean first = true;
Expand Down Expand Up @@ -686,7 +687,7 @@ static String separateCamelCase(String name, boolean lowerCase, char separatorCh
* @param filename The name of the file
* @return The file extension
*/
public static String extension(String filename) {
public static @NonNull String extension(@NonNull String filename) {
int extensionPos = filename.lastIndexOf('.');
int lastUnixPos = filename.lastIndexOf('/');
int lastWindowsPos = filename.lastIndexOf('\\');
Expand All @@ -705,7 +706,7 @@ public static String extension(String filename) {
* @param str The string
* @return The new string in camel case
*/
public static String camelCase(String str) {
public static @NonNull String camelCase(@NonNull String str) {
return camelCase(str, true);
}

Expand All @@ -716,7 +717,7 @@ public static String camelCase(String str) {
* @param lowerCaseFirstLetter Whether the first letter is in upper case or lower case
* @return The new string in camel case
*/
public static String camelCase(String str, boolean lowerCaseFirstLetter) {
public static @NonNull String camelCase(@NonNull String str, boolean lowerCaseFirstLetter) {
StringBuilder sb = new StringBuilder(str.length());
for (String s : str.split("[\\s_-]")) {
String capitalize = capitalize(s);
Expand All @@ -736,7 +737,7 @@ public static String camelCase(String str, boolean lowerCaseFirstLetter) {
* @param path The path of the file
* @return The file name without extension
*/
public static String filename(String path) {
public static @NonNull String filename(@NonNull String path) {
int extensionPos = path.lastIndexOf('.');
int lastUnixPos = path.lastIndexOf('/');
int lastWindowsPos = path.lastIndexOf('\\');
Expand All @@ -755,7 +756,7 @@ public static String filename(String path) {
* @param str The string to check
* @return Whether is valid kebab-case or not
*/
public static boolean isValidHyphenatedPropertyName(String str) {
public static boolean isValidHyphenatedPropertyName(@NonNull String str) {
return KEBAB_CASE_SEQUENCE.matcher(str).matches();
}

Expand All @@ -765,7 +766,7 @@ public static boolean isValidHyphenatedPropertyName(String str) {
* @param str The string to check
* @return Whether is valid environment-style property name or not
*/
public static boolean isEnvironmentName(String str) {
public static boolean isEnvironmentName(@NonNull String str) {
return ENVIRONMENT_VAR_SEQUENCE.matcher(str).matches();
}

Expand Down

0 comments on commit 159bbbd

Please sign in to comment.