Browse Source

Fix regression with binding and validation

Previously, the binding may have to call the getter first to retrieve the
old value of a property before actually setting it. This was guarded by
a catch block that was accidentally removed in 3d86f15

Restore that catch block and add a test to cover it.

Issue: SPR-12805
pull/808/head
Stephane Nicoll 10 years ago
parent
commit
6fb3190353
  1. 14
      spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java
  2. 9
      spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java

14
spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java

@ -20,6 +20,7 @@ import java.beans.PropertyChangeEvent; @@ -20,6 +20,7 @@ import java.beans.PropertyChangeEvent;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -523,7 +524,18 @@ public abstract class AbstractPropertyAccessor extends TypeConverterSupport impl @@ -523,7 +524,18 @@ public abstract class AbstractPropertyAccessor extends TypeConverterSupport impl
}
else {
if (isExtractOldValueForEditor() && ph.isReadable()) {
oldValue = ph.getValue();
try {
oldValue = ph.getValue();
}
catch (Exception ex) {
if (ex instanceof PrivilegedActionException) {
ex = ((PrivilegedActionException) ex).getException();
}
if (logger.isDebugEnabled()) {
logger.debug("Could not read previous value of property '" +
this.nestedPath + propertyName + "'", ex);
}
}
}
valueToApply = convertForProperty(
propertyName, oldValue, originalValue, ph.toTypeDescriptor());

9
spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java

@ -51,6 +51,15 @@ public final class BeanWrapperTests extends AbstractConfigurablePropertyAccessor @@ -51,6 +51,15 @@ public final class BeanWrapperTests extends AbstractConfigurablePropertyAccessor
assertTrue("Set name to tom", target.getName().equals("tom"));
}
@Test
public void getterSilentlyFailWithOldValueExtraction() {
GetterBean target = new GetterBean();
BeanWrapper accessor = createAccessor(target);
accessor.setExtractOldValueForEditor(true); // This will call the getter
accessor.setPropertyValue("name", "tom");
assertTrue("Set name to tom", target.getName().equals("tom"));
}
@Test
public void setValidAndInvalidPropertyValuesShouldContainExceptionDetails() {
TestBean target = new TestBean();

Loading…
Cancel
Save