Improve property target name

Previously, non camel case properties were wrongly resolved, i.e.
getFOO() leading to a 'f-o-o'. While unusual, underscores can also be
added to a property name. In that case, the hyphen should not be added
as the binder consider this to be a single "word". Typically setFoo_Bar
on the "something" prefix is mapped using "something.foo_bar".

All these cases are now handled properly, generating the target name that
the binder expects.

Fixes gh-2118
This commit is contained in:
Stephane Nicoll
2014-12-11 14:38:33 +01:00
parent e96f75fdc1
commit 8f6f25f88e
2 changed files with 89 additions and 7 deletions

View File

@@ -19,6 +19,8 @@ package org.springframework.boot.configurationprocessor.metadata;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Configuration meta-data.
@@ -30,6 +32,8 @@ import java.util.List;
*/
public class ConfigurationMetadata {
private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([^A-Z-])([A-Z])");
private final List<ItemMetadata> items;
public ConfigurationMetadata() {
@@ -73,15 +77,22 @@ public class ConfigurationMetadata {
}
static String toDashedCase(String name) {
StringBuilder dashed = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (Character.isUpperCase(c) && dashed.length() > 0) {
dashed.append("-");
Matcher matcher = CAMEL_CASE_PATTERN.matcher(name);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
String first = matcher.group(1);
String second = matcher.group(2);
String target;
if (first.equals("_")) { // not a word for the binder
target = first + second;
} else {
target = first + "-" + second;
}
dashed.append(Character.toLowerCase(c));
matcher.appendReplacement(result,target);
}
return dashed.toString();
matcher.appendTail(result);
String value = result.toString();
return value.toLowerCase();
}
}