From 6fb31903536c0a41dd4fbe153c81494ccfd4d405 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 21 May 2015 17:48:11 +0200 Subject: [PATCH] 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 --- .../beans/AbstractPropertyAccessor.java | 14 +++++++++++++- .../springframework/beans/BeanWrapperTests.java | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java b/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java index c30f89eb068..45f8ed5ce86 100644 --- a/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java @@ -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 } 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()); diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java index cd29de7aefd..24311b0e433 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java @@ -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();