Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isReaderName for fields started with _ or $ #8435

Merged
merged 1 commit into from Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions core/src/main/java/io/micronaut/core/naming/NameUtils.java
Expand Up @@ -93,7 +93,7 @@ public static String capitalize(String name) {
final String rest = name.substring(1);

// Funky rule so that names like 'pNAME' will still work.
if (Character.isLowerCase(name.charAt(0)) && (rest.length() > 0) && Character.isUpperCase(rest.charAt(0))) {
if (Character.isLowerCase(name.charAt(0)) && (!rest.isEmpty()) && Character.isUpperCase(rest.charAt(0))) {
return name;
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public static String hyphenate(String name, boolean lowerCase) {
public static String dehyphenate(String name) {
StringBuilder sb = new StringBuilder(name.length());
for (String token : StringUtils.splitOmitEmptyStrings(name, '-')) {
if (token.length() > 0 && Character.isLetter(token.charAt(0))) {
if (!token.isEmpty() && Character.isLetter(token.charAt(0))) {
sb.append(Character.toUpperCase(token.charAt(0)));
sb.append(token.substring(1));
} else {
Expand Down Expand Up @@ -229,7 +229,7 @@ public static boolean isWriterName(@NonNull String methodName, @NonNull String w
public static boolean isWriterName(@NonNull String methodName, @NonNull String[] writePrefixes) {
boolean isValid = false;
for (String writePrefix : writePrefixes) {
if (writePrefix.length() == 0) {
if (writePrefix.isEmpty()) {
return true;
}
int len = methodName.length();
Expand Down Expand Up @@ -360,7 +360,7 @@ public static boolean isReaderName(@NonNull String methodName, @NonNull String[]
boolean isValid = false;
for (String readPrefix : readPrefixes) {
int prefixLength = 0;
if (readPrefix.length() == 0) {
if (readPrefix.isEmpty()) {
return true;
} else if (methodName.startsWith(readPrefix)) {
prefixLength = readPrefix.length();
Expand All @@ -369,7 +369,8 @@ public static boolean isReaderName(@NonNull String methodName, @NonNull String[]
}
int len = methodName.length();
if (len > prefixLength) {
isValid = Character.isUpperCase(methodName.charAt(prefixLength));
char firstVarNameChar = methodName.charAt(prefixLength);
isValid = firstVarNameChar == '_' || firstVarNameChar == '$' || Character.isUpperCase(firstVarNameChar);
}

if (isValid) {
Expand Down Expand Up @@ -491,7 +492,7 @@ public static String getterNameFor(@NonNull String propertyName, boolean isBoole
}

private static String nameFor(String prefix, @NonNull String propertyName) {
if (prefix.length() == 0) {
if (prefix.isEmpty()) {
return propertyName;
}

Expand Down
Expand Up @@ -244,6 +244,8 @@ class NameUtilsSpec extends Specification {
"getFoo" | true
"getfoo" | false
"a" | false
"get_foo" | true
'get$foo' | true
}

@Unroll
Expand Down