Skip to content

Commit 58aa065

Browse files
committed
Suppress NotWritablePropertyException in case of ignoreUnknown=true
Closes gh-25986
1 parent dbbedc6 commit 58aa065

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-2019 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.
@@ -422,9 +422,12 @@ private void processLocalProperty(PropertyTokenHolder tokens, PropertyValue pv)
422422
}
423423
return;
424424
}
425-
else {
426-
throw createNotWritablePropertyException(tokens.canonicalName);
425+
if (this.suppressNotWritablePropertyException) {
426+
// Optimization for common ignoreUnknown=true scenario since the
427+
// exception would be caught and swallowed higher up anyway...
428+
return;
427429
}
430+
throw createNotWritablePropertyException(tokens.canonicalName);
428431
}
429432

430433
Object oldValue = null;
@@ -806,7 +809,6 @@ protected String getFinalPath(AbstractNestablePropertyAccessor pa, String nested
806809
* @param propertyPath property path, which may be nested
807810
* @return a property accessor for the target bean
808811
*/
809-
@SuppressWarnings("unchecked") // avoid nested generic
810812
protected AbstractNestablePropertyAccessor getPropertyAccessorForPropertyPath(String propertyPath) {
811813
int pos = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(propertyPath);
812814
// 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-2018 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.
@@ -40,6 +40,8 @@ public abstract class AbstractPropertyAccessor extends TypeConverterSupport impl
4040

4141
private boolean autoGrowNestedPaths = false;
4242

43+
boolean suppressNotWritablePropertyException = false;
44+
4345

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

0 commit comments

Comments
 (0)