Skip to content

Commit 4a25e2d

Browse files
committed
log a warning in case of ambiguous setter methods (SPR-4931)
1 parent e97c2bd commit 4a25e2d

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import java.beans.IntrospectionException;
2020
import java.beans.PropertyDescriptor;
2121
import java.lang.reflect.Method;
22+
import java.util.HashSet;
23+
import java.util.Set;
24+
25+
import org.apache.commons.logging.LogFactory;
2226

2327
import org.springframework.core.BridgeMethodResolver;
2428
import org.springframework.core.GenericTypeResolver;
@@ -44,6 +48,8 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor {
4448

4549
private final Class propertyEditorClass;
4650

51+
private volatile Set<Method> ambiguousWriteMethods;
52+
4753
private Class propertyType;
4854

4955
private MethodParameter writeMethodParameter;
@@ -55,6 +61,8 @@ public GenericTypeAwarePropertyDescriptor(Class beanClass, String propertyName,
5561

5662
super(propertyName, null, null);
5763
this.beanClass = beanClass;
64+
this.propertyEditorClass = propertyEditorClass;
65+
5866
Method readMethodToUse = BridgeMethodResolver.findBridgedMethod(readMethod);
5967
Method writeMethodToUse = BridgeMethodResolver.findBridgedMethod(writeMethod);
6068
if (writeMethodToUse == null && readMethodToUse != null) {
@@ -66,7 +74,22 @@ public GenericTypeAwarePropertyDescriptor(Class beanClass, String propertyName,
6674
}
6775
this.readMethod = readMethodToUse;
6876
this.writeMethod = writeMethodToUse;
69-
this.propertyEditorClass = propertyEditorClass;
77+
78+
if (this.writeMethod != null && this.readMethod == null) {
79+
// Write method not matched against read method: potentially ambiguous through
80+
// several overloaded variants, in which case an arbitrary winner has been chosen
81+
// by the JDK's JavaBeans Introspector...
82+
Set<Method> ambiguousCandidates = new HashSet<Method>();
83+
for (Method method : beanClass.getMethods()) {
84+
if (method.getName().equals(writeMethodToUse.getName()) &&
85+
!method.equals(writeMethodToUse) && !method.isBridge()) {
86+
ambiguousCandidates.add(method);
87+
}
88+
}
89+
if (!ambiguousCandidates.isEmpty()) {
90+
this.ambiguousWriteMethods = ambiguousCandidates;
91+
}
92+
}
7093
}
7194

7295

@@ -77,6 +100,13 @@ public Method getReadMethod() {
77100

78101
@Override
79102
public Method getWriteMethod() {
103+
Set<Method> ambiguousCandidates = this.ambiguousWriteMethods;
104+
if (ambiguousCandidates != null) {
105+
this.ambiguousWriteMethods = null;
106+
LogFactory.getLog(GenericTypeAwarePropertyDescriptor.class).warn("Invalid JavaBean property '" +
107+
getName() + "' being accessed! Ambiguous write methods found next to actually used [" +
108+
this.writeMethod + "]: " + ambiguousCandidates);
109+
}
80110
return this.writeMethod;
81111
}
82112

0 commit comments

Comments
 (0)