From 159bbbd285a5958bc19605c22b788cdc23df58d5 Mon Sep 17 00:00:00 2001 From: Andriy Dmytruk Date: Thu, 5 Dec 2024 14:10:05 -0500 Subject: [PATCH] Add nullability annotations --- .../io/micronaut/core/naming/NameUtils.java | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/io/micronaut/core/naming/NameUtils.java b/core/src/main/java/io/micronaut/core/naming/NameUtils.java index 60264538e5..4c679d7cb8 100644 --- a/core/src/main/java/io/micronaut/core/naming/NameUtils.java +++ b/core/src/main/java/io/micronaut/core/naming/NameUtils.java @@ -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; @@ -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; } @@ -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); } @@ -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)) { @@ -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. @@ -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); } @@ -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; @@ -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))) { @@ -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(); @@ -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); } @@ -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, '_'); } @@ -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); } @@ -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(); @@ -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('.'); @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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; } @@ -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; } @@ -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; @@ -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('\\'); @@ -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); } @@ -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); @@ -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('\\'); @@ -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(); } @@ -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(); }