Skip to content

Commit 8212151

Browse files
committed
Suppress NotWritablePropertyException in case of ignoreUnknown=true
Closes gh-25986
1 parent 6557767 commit 8212151

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -431,9 +431,12 @@ private void processLocalProperty(PropertyTokenHolder tokens, PropertyValue pv)
431431
}
432432
return;
433433
}
434-
else {
435-
throw createNotWritablePropertyException(tokens.canonicalName);
434+
if (this.suppressNotWritablePropertyException) {
435+
// Optimization for common ignoreUnknown=true scenario since the
436+
// exception would be caught and swallowed higher up anyway...
437+
return;
436438
}
439+
throw createNotWritablePropertyException(tokens.canonicalName);
437440
}
438441

439442
Object oldValue = null;
@@ -815,7 +818,6 @@ protected String getFinalPath(AbstractNestablePropertyAccessor pa, String nested
815818
* @param propertyPath property path, which may be nested
816819
* @return a property accessor for the target bean
817820
*/
818-
@SuppressWarnings("unchecked") // avoid nested generic
819821
protected AbstractNestablePropertyAccessor getPropertyAccessorForPropertyPath(String propertyPath) {
820822
int pos = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(propertyPath);
821823
// Handle nested properties recursively.

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,8 @@ public abstract class AbstractPropertyAccessor extends TypeConverterSupport impl
3838

3939
private boolean autoGrowNestedPaths = false;
4040

41+
boolean suppressNotWritablePropertyException = false;
42+
4143

4244
@Override
4345
public void setExtractOldValueForEditor(boolean extractOldValueForEditor) {
@@ -87,30 +89,41 @@ public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean
8789
List<PropertyAccessException> propertyAccessExceptions = null;
8890
List<PropertyValue> propertyValues = (pvs instanceof MutablePropertyValues ?
8991
((MutablePropertyValues) pvs).getPropertyValueList() : Arrays.asList(pvs.getPropertyValues()));
90-
for (PropertyValue pv : propertyValues) {
91-
try {
92-
// This method may throw any BeansException, which won't be caught
92+
93+
if (ignoreUnknown) {
94+
this.suppressNotWritablePropertyException = true;
95+
}
96+
try {
97+
for (PropertyValue pv : propertyValues) {
98+
// setPropertyValue may throw any BeansException, which won't be caught
9399
// here, if there is a critical failure such as no matching field.
94100
// We can attempt to deal only with less serious exceptions.
95-
setPropertyValue(pv);
96-
}
97-
catch (NotWritablePropertyException ex) {
98-
if (!ignoreUnknown) {
99-
throw ex;
101+
try {
102+
setPropertyValue(pv);
100103
}
101-
// Otherwise, just ignore it and continue...
102-
}
103-
catch (NullValueInNestedPathException ex) {
104-
if (!ignoreInvalid) {
105-
throw ex;
104+
catch (NotWritablePropertyException ex) {
105+
if (!ignoreUnknown) {
106+
throw ex;
107+
}
108+
// Otherwise, just ignore it and continue...
106109
}
107-
// Otherwise, just ignore it and continue...
108-
}
109-
catch (PropertyAccessException ex) {
110-
if (propertyAccessExceptions == null) {
111-
propertyAccessExceptions = new LinkedList<PropertyAccessException>();
110+
catch (NullValueInNestedPathException ex) {
111+
if (!ignoreInvalid) {
112+
throw ex;
113+
}
114+
// Otherwise, just ignore it and continue...
115+
}
116+
catch (PropertyAccessException ex) {
117+
if (propertyAccessExceptions == null) {
118+
propertyAccessExceptions = new LinkedList<PropertyAccessException>();
119+
}
120+
propertyAccessExceptions.add(ex);
112121
}
113-
propertyAccessExceptions.add(ex);
122+
}
123+
}
124+
finally {
125+
if (ignoreUnknown) {
126+
this.suppressNotWritablePropertyException = false;
114127
}
115128
}
116129

0 commit comments

Comments
 (0)