RESOLVED - issue BATCH-924: BeanWrapperFieldSetMapper could create child objects if they are null when needed

This commit is contained in:
dsyer
2009-12-09 13:09:02 +00:00
parent f28b03ac89
commit cb17024c01
2 changed files with 45 additions and 8 deletions

View File

@@ -264,6 +264,10 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
private String findPropertyName(Object bean, String key) {
if (bean == null) {
return null;
}
Class<?> cls = bean.getClass();
int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(key);
@@ -280,7 +284,7 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
return null;
}
Object nestedValue = new BeanWrapperImpl(bean).getPropertyValue(nestedName);
Object nestedValue = getPropertyValue(bean, nestedName);
String nestedPropertyName = findPropertyName(nestedValue, suffix);
return nestedPropertyName == null ? null : nestedName + "." + nestedPropertyName;
}
@@ -316,6 +320,24 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
return name;
}
private Object getPropertyValue(Object bean, String nestedName) {
BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
Object nestedValue = wrapper.getPropertyValue(nestedName);
if (nestedValue == null) {
try {
nestedValue = wrapper.getPropertyType(nestedName).newInstance();
wrapper.setPropertyValue(nestedName, nestedValue);
}
catch (InstantiationException e) {
ReflectionUtils.handleReflectionException(e);
}
catch (IllegalAccessException e) {
ReflectionUtils.handleReflectionException(e);
}
}
return nestedValue;
}
private void switchPropertyNames(Properties properties, String oldName, String newName) {
String value = properties.getProperty(oldName);
properties.remove(oldName);

View File

@@ -86,6 +86,18 @@ public class BeanWrapperFieldSetMapperTests {
assertEquals('C', result.getVarChar());
}
@Test
public void testNullPropertyAutoCreated() throws Exception {
BeanWrapperFieldSetMapper<TestNestedA> mapper = new BeanWrapperFieldSetMapper<TestNestedA>();
mapper.setTargetType(TestNestedA.class);
mapper.afterPropertiesSet();
FieldSet fieldSet = new DefaultFieldSet(new String[] { "Foo", "Bar" }, new String[] { "valueA",
"testObjectB.valueA" });
TestNestedA result = mapper.mapFieldSet(fieldSet);
assertEquals("Bar", result.getTestObjectB().getValueA());
}
@Test
public void testMapperWithSingleton() throws Exception {
BeanWrapperFieldSetMapper<TestObject> mapper = new BeanWrapperFieldSetMapper<TestObject>();
@@ -377,11 +389,13 @@ public class BeanWrapperFieldSetMapperTests {
BeanWrapperFieldSetMapper<TestObject> mapper = new BeanWrapperFieldSetMapper<TestObject>();
mapper.setTargetType(TestObject.class);
FieldSet fieldSet = new DefaultFieldSet(new String[] { "foo", "7890.1" }, new String[] { "varDouble", "varFloat" });
FieldSet fieldSet = new DefaultFieldSet(new String[] { "foo", "7890.1" }, new String[] { "varDouble",
"varFloat" });
try {
mapper.mapFieldSet(fieldSet);
fail("Expected BindException");
} catch (BindException e) {
}
catch (BindException e) {
assertEquals(1, e.getErrorCount());
assertEquals("typeMismatch", e.getFieldError("varDouble").getCode());
}
@@ -474,7 +488,7 @@ public class BeanWrapperFieldSetMapperTests {
}
private static class TestNestedA {
public static class TestNestedA {
private String valueA;
private int valueB;
@@ -507,7 +521,7 @@ public class BeanWrapperFieldSetMapperTests {
}
private static class TestNestedB {
public static class TestNestedB {
private String valueA;
private TestNestedC testObjectC;
@@ -530,6 +544,7 @@ public class BeanWrapperFieldSetMapperTests {
}
@SuppressWarnings("unused")
private static class TestNestedC {
private int value;
@@ -564,7 +579,7 @@ public class BeanWrapperFieldSetMapperTests {
}
}
public static class TestObject {
String varString;
@@ -589,11 +604,11 @@ public class BeanWrapperFieldSetMapperTests {
Date varDate;
public Date getVarDate() {
return (Date)varDate.clone();
return (Date) varDate.clone();
}
public void setVarDate(Date varDate) {
this.varDate = varDate == null ? null : (Date)varDate.clone();
this.varDate = varDate == null ? null : (Date) varDate.clone();
}
public TestObject() {