Skip to content

Commit

Permalink
Fix identification getters / setters for properties with second upper…
Browse files Browse the repository at this point in the history
… case symbol. For example eTemperature
  • Loading branch information
altro3 committed Jan 27, 2024
1 parent 443de14 commit 94c45d4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 25 deletions.
25 changes: 18 additions & 7 deletions core/src/main/java/io/micronaut/core/naming/NameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,9 @@ public static boolean isWriterName(@NonNull String methodName, @NonNull String[]
int len = methodName.length();
int prefixLength = writePrefix.length();
if (len > prefixLength && methodName.startsWith(writePrefix)) {
char nextChar = methodName.charAt(prefixLength);
isValid = isValidCharacterAfterReaderWriterPrefix(nextChar);
char firstVarNameChar = methodName.charAt(prefixLength);
Character secondVarNameChar = len > prefixLength + 1 ? methodName.charAt(prefixLength + 1) : null;
isValid = isValidCharacterAfterReaderWriterPrefix(firstVarNameChar, secondVarNameChar);
}

if (isValid) {
Expand Down Expand Up @@ -371,7 +372,8 @@ public static boolean isReaderName(@NonNull String methodName, @NonNull String[]
int len = methodName.length();
if (len > prefixLength) {
char firstVarNameChar = methodName.charAt(prefixLength);
isValid = isValidCharacterAfterReaderWriterPrefix(firstVarNameChar);
Character secondVarNameChar = len > prefixLength + 1 ? methodName.charAt(prefixLength + 1) : null;
isValid = isValidCharacterAfterReaderWriterPrefix(firstVarNameChar, secondVarNameChar);
}

if (isValid) {
Expand All @@ -382,8 +384,11 @@ public static boolean isReaderName(@NonNull String methodName, @NonNull String[]
return isValid;
}

private static boolean isValidCharacterAfterReaderWriterPrefix(char c) {
return c == '_' || c == '$' || Character.isUpperCase(c);
private static boolean isValidCharacterAfterReaderWriterPrefix(char c, Character nextChar) {
return c == '_' || c == '$' || Character.isUpperCase(c)
// case with properties with second uupercase letter. For example `eTemperature`
// valid getter name will be `geteTemperature`
|| (nextChar != null && Character.isLowerCase(c) && Character.isUpperCase(nextChar));
}

/**
Expand Down Expand Up @@ -507,8 +512,14 @@ private static String nameFor(String prefix, @NonNull String propertyName) {
return propertyName;
case 1:
return prefix + propertyName.toUpperCase(Locale.ENGLISH);
default:
return prefix + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
default: {
var firstChar = propertyName.charAt(0);
var secondChar = propertyName.charAt(1);
if (Character.isLowerCase(firstChar) && Character.isUpperCase(secondChar)) {
return prefix + propertyName;
}
return prefix + Character.toUpperCase(firstChar) + propertyName.substring(1);
}
}
}

Expand Down
77 changes: 59 additions & 18 deletions core/src/test/groovy/io/micronaut/core/naming/NameUtilsSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ class NameUtilsSpec extends Specification {
"getFoo" | ["get"] | true
'get$foo' | ["get"] | true
'get_foo' | ["get"] | true
'getfRo' | ["get"] | true
'get$__$A'| ["get"] | true
'getf_A' | ["get"] | false
'getF_A' | ["get"] | true
'getF$A' | ["get"] | true
'getA' | ["get"] | true
"getfoo" | ["get"] | false
"a" | ["get"] | false
"foo" | ["with"] | false
Expand All @@ -322,24 +328,31 @@ class NameUtilsSpec extends Specification {
where:
name | prefixes | isValid
"foo" | ["set"] | false
"setFoo" | ["set"] | true
'set$foo' | ["set"] | true
'set_foo' | ["set"] | true
"setfoo" | ["set"] | false
"a" | ["set"] | false
"foo" | ["with"] | false
"withFoo" | ["with"] | true
"withfoo" | ["with"] | false
"foo" | [""] | true
"fooBar" | [""] | true
"isfoo" | [""] | true
"isFoo" | [""] | true
"is" | [""] | true
"setFoo" | ["set", "with"] | true
"setfoo" | ["set", "with"] | false
"withFoo" | ["set", "with"] | true
"withfoo" | ["set", "with"] | false
// "foo" | ["set"] | false
// "setFoo" | ["set"] | true
// 'set$foo' | ["set"] | true
// 'set_foo' | ["set"] | true
// 'setfRo' | ["set"] | true
// 'set$__$A'| ["set"] | true
// 'setf_A' | ["set"] | false
// 'setF_A' | ["set"] | true
// 'setF$A' | ["set"] | true
// 'setA' | ["set"] | true
// "setfoo" | ["set"] | false
// "a" | ["set"] | false
// "foo" | ["with"] | false
// "withFoo" | ["with"] | true
// "withfoo" | ["with"] | false
// "foo" | [""] | true
// "fooBar" | [""] | true
// "isfoo" | [""] | true
// "isFoo" | [""] | true
// "is" | [""] | true
// "setFoo" | ["set", "with"] | true
// "setfoo" | ["set", "with"] | false
// "withFoo" | ["set", "with"] | true
// "withfoo" | ["set", "with"] | false
"setorXyz"| ["set"] | false
}
void "test getPropertyNameForGetter (#getter, #prefixes)"() {
Expand All @@ -357,6 +370,13 @@ class NameUtilsSpec extends Specification {
"is" | [""] | "is"
"getFoo" | ["get", "with"] | "foo"
"withFoo" | ["get", "with"] | "foo"
'get$foo' | ["get"] | '$foo'
'get_foo' | ["get"] | '_foo'
'getfRo' | ["get"] | 'fRo'
'get$__$A'| ["get"] | '$__$A'
'getF_A' | ["get"] | 'f_A'
'getF$A' | ["get"] | 'f$A'
'getA' | ["get"] | 'a'
}
void "test getPropertyNameForSetter (#setter, #prefixes)"() {
Expand All @@ -374,6 +394,13 @@ class NameUtilsSpec extends Specification {
"is" | [""] | "is"
"setFoo" | ["set", "with"] | "foo"
"withFoo" | ["set", "with"] | "foo"
'set$foo' | ["set"] | '$foo'
'set_foo' | ["set"] | '_foo'
'setfRo' | ["set"] | 'fRo'
'set$__$A'| ["set"] | '$__$A'
'setF_A' | ["set"] | 'f_A'
'setF$A' | ["set"] | 'f$A'
'setA' | ["set"] | 'a'
}
void "test getterNameFor (#name, #prefixes)"() {
Expand All @@ -389,6 +416,13 @@ class NameUtilsSpec extends Specification {
"fooBar" | ["with"] | "withFooBar"
"fooBar" | ["set", "with"] | "setFooBar"
"fooBar" | ["with", "set"] | "withFooBar"
'$foo' | ["get"] | 'get$foo'
'_foo' | ["get"] | 'get_foo'
'fRo' | ["get"] | 'getfRo'
'$__$A' | ["get"] | 'get$__$A'
'f_A' | ["get"] | 'getF_A'
'f$A' | ["get"] | 'getF$A'
'a' | ["get"] | 'getA'
}
void "test setterNameFor (#name, #prefixes)"() {
Expand All @@ -404,6 +438,13 @@ class NameUtilsSpec extends Specification {
"fooBar" | ["with"] | "withFooBar"
"fooBar" | ["set", "with"] | "setFooBar"
"fooBar" | ["with", "set"] | "withFooBar"
'$foo' | ["set"] | 'set$foo'
'_foo' | ["set"] | 'set_foo'
'fRo' | ["set"] | 'setfRo'
'$__$A' | ["set"] | 'set$__$A'
'f_A' | ["set"] | 'setF_A'
'f$A' | ["set"] | 'setF$A'
'a' | ["set"] | 'setA'
}
}

0 comments on commit 94c45d4

Please sign in to comment.