Merge pull request #2479 from linead/master
* pull2479: Supported relaxed binding on inner classes
This commit is contained in:
commit
0484e5cb4d
|
|
@ -286,12 +286,38 @@ public class RelaxedDataBinder extends DataBinder {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getActualPropertyName(BeanWrapper target, String prefix, String name) {
|
private String getActualPropertyName(BeanWrapper target, String prefix, String name) {
|
||||||
prefix = StringUtils.hasText(prefix) ? prefix + "." : "";
|
String propertyName = resolvePropertyName(target, prefix, name);
|
||||||
|
if (propertyName == null) {
|
||||||
|
propertyName = resolveNestedPropertyName(target, prefix, name);
|
||||||
|
}
|
||||||
|
return (propertyName == null ? name : propertyName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveNestedPropertyName(BeanWrapper target, String prefix,
|
||||||
|
String name) {
|
||||||
|
StringBuilder candidate = new StringBuilder();
|
||||||
|
for (String field : name.split("[_\\-\\.]")) {
|
||||||
|
candidate.append(candidate.length() > 0 ? "." : "");
|
||||||
|
candidate.append(field);
|
||||||
|
String nested = resolvePropertyName(target, prefix, candidate.toString());
|
||||||
|
if (nested != null) {
|
||||||
|
String propertyName = resolvePropertyName(target,
|
||||||
|
joinString(prefix, nested),
|
||||||
|
name.substring(candidate.length() + 1));
|
||||||
|
if (propertyName != null) {
|
||||||
|
return joinString(nested, propertyName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolvePropertyName(BeanWrapper target, String prefix, String name) {
|
||||||
Iterable<String> names = getNameAndAliases(name);
|
Iterable<String> names = getNameAndAliases(name);
|
||||||
for (String nameOrAlias : names) {
|
for (String nameOrAlias : names) {
|
||||||
for (String candidate : new RelaxedNames(nameOrAlias)) {
|
for (String candidate : new RelaxedNames(nameOrAlias)) {
|
||||||
try {
|
try {
|
||||||
if (target.getPropertyType(prefix + candidate) != null) {
|
if (target.getPropertyType(joinString(prefix, candidate)) != null) {
|
||||||
return candidate;
|
return candidate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -300,7 +326,11 @@ public class RelaxedDataBinder extends DataBinder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return name;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String joinString(String prefix, String name) {
|
||||||
|
return (StringUtils.hasLength(prefix) ? prefix + "." + name : name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Iterable<String> getNameAndAliases(String name) {
|
private Iterable<String> getNameAndAliases(String name) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2014 the original author or authors.
|
* Copyright 2012-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -201,6 +201,22 @@ public class RelaxedDataBinderTests {
|
||||||
assertEquals(123, target.getNested().getValue());
|
assertEquals(123, target.getNested().getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBindRelaxedNestedValue() throws Exception {
|
||||||
|
TargetWithNestedObject target = new TargetWithNestedObject();
|
||||||
|
bind(target, "nested_foo_Baz: bar\n" + "nested_value: 123");
|
||||||
|
assertEquals("bar", target.getNested().getFooBaz());
|
||||||
|
assertEquals(123, target.getNested().getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBindRelaxedNestedCamelValue() throws Exception {
|
||||||
|
TargetWithNestedObject target = new TargetWithNestedObject();
|
||||||
|
bind(target, "another_nested_foo_Baz: bar\n" + "another-nested_value: 123");
|
||||||
|
assertEquals("bar", target.getAnotherNested().getFooBaz());
|
||||||
|
assertEquals(123, target.getAnotherNested().getValue());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindNestedWithEnviromentStyle() throws Exception {
|
public void testBindNestedWithEnviromentStyle() throws Exception {
|
||||||
TargetWithNestedObject target = new TargetWithNestedObject();
|
TargetWithNestedObject target = new TargetWithNestedObject();
|
||||||
|
|
@ -736,8 +752,11 @@ public class RelaxedDataBinderTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TargetWithNestedObject {
|
public static class TargetWithNestedObject {
|
||||||
|
|
||||||
private VanillaTarget nested;
|
private VanillaTarget nested;
|
||||||
|
|
||||||
|
private VanillaTarget anotherNested;
|
||||||
|
|
||||||
public VanillaTarget getNested() {
|
public VanillaTarget getNested() {
|
||||||
return this.nested;
|
return this.nested;
|
||||||
}
|
}
|
||||||
|
|
@ -745,6 +764,15 @@ public class RelaxedDataBinderTests {
|
||||||
public void setNested(VanillaTarget nested) {
|
public void setNested(VanillaTarget nested) {
|
||||||
this.nested = nested;
|
this.nested = nested;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public VanillaTarget getAnotherNested() {
|
||||||
|
return this.anotherNested;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAnotherNested(VanillaTarget anotherNested) {
|
||||||
|
this.anotherNested = anotherNested;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class VanillaTarget {
|
public static class VanillaTarget {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue