diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java index 9e21d9ca1e50..ace258a4987d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,7 +91,7 @@ public String apply(String value) { @Override public String apply(String value) { - return value.toLowerCase(); + return value.isEmpty() ? value : value.toLowerCase(); } }, @@ -100,7 +100,7 @@ public String apply(String value) { @Override public String apply(String value) { - return value.toUpperCase(); + return value.isEmpty() ? value : value.toUpperCase(); } }; @@ -127,7 +127,7 @@ public String apply(String value) { @Override public String apply(String value) { - return value.replace("-", "_"); + return value.indexOf('-') != -1 ? value.replace("-", "_") : value; } }, @@ -136,7 +136,7 @@ public String apply(String value) { @Override public String apply(String value) { - return value.replace("_", "."); + return value.indexOf('_') != -1 ? value.replace("_", ".") : value; } }, @@ -145,7 +145,7 @@ public String apply(String value) { @Override public String apply(String value) { - return value.replace(".", "_"); + return value.indexOf('.') != -1 ? value.replace(".", "_") : value; } }, @@ -154,7 +154,14 @@ public String apply(String value) { @Override public String apply(String value) { + if (value.isEmpty()) { + return value; + } Matcher matcher = CAMEL_CASE_PATTERN.matcher(value); + if (!matcher.find()) { + return value; + } + matcher = matcher.reset(); StringBuffer result = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(result, matcher.group(1) + '_' @@ -170,7 +177,14 @@ public String apply(String value) { @Override public String apply(String value) { + if (value.isEmpty()) { + return value; + } Matcher matcher = CAMEL_CASE_PATTERN.matcher(value); + if (!matcher.find()) { + return value; + } + matcher = matcher.reset(); StringBuffer result = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(result, matcher.group(1) + '-' @@ -206,7 +220,7 @@ public String apply(String value) { private static String separatedToCamelCase(String value, boolean caseInsensitive) { - if (value.length() == 0) { + if (value.isEmpty()) { return value; } StringBuilder builder = new StringBuilder(); @@ -233,7 +247,7 @@ private static String separatedToCamelCase(String value, * @return the relaxed names */ public static RelaxedNames forCamelCase(String name) { - StringBuffer result = new StringBuffer(); + StringBuilder result = new StringBuilder(); for (char c : name.toCharArray()) { result.append(Character.isUpperCase(c) && result.length() > 0 && result.charAt(result.length() - 1) != '-'