From 9e2e94baf4c8a4b176043d7b21b3c9fbc6485cc5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 11 Nov 2009 23:27:34 +0000 Subject: [PATCH] log a warning in case of ambiguous setter methods (SPR-4931) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2362 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../GenericTypeAwarePropertyDescriptor.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java b/org.springframework.beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java index b1d21cfa63b..404b3399d1e 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java @@ -19,6 +19,10 @@ package org.springframework.beans; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.logging.LogFactory; import org.springframework.core.BridgeMethodResolver; import org.springframework.core.GenericTypeResolver; @@ -44,6 +48,8 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor { private final Class propertyEditorClass; + private volatile Set ambiguousWriteMethods; + private Class propertyType; private MethodParameter writeMethodParameter; @@ -55,6 +61,8 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor { super(propertyName, null, null); this.beanClass = beanClass; + this.propertyEditorClass = propertyEditorClass; + Method readMethodToUse = BridgeMethodResolver.findBridgedMethod(readMethod); Method writeMethodToUse = BridgeMethodResolver.findBridgedMethod(writeMethod); if (writeMethodToUse == null && readMethodToUse != null) { @@ -66,7 +74,22 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor { } this.readMethod = readMethodToUse; this.writeMethod = writeMethodToUse; - this.propertyEditorClass = propertyEditorClass; + + if (this.writeMethod != null && this.readMethod == null) { + // Write method not matched against read method: potentially ambiguous through + // several overloaded variants, in which case an arbitrary winner has been chosen + // by the JDK's JavaBeans Introspector... + Set ambiguousCandidates = new HashSet(); + for (Method method : beanClass.getMethods()) { + if (method.getName().equals(writeMethodToUse.getName()) && + !method.equals(writeMethodToUse) && !method.isBridge()) { + ambiguousCandidates.add(method); + } + } + if (!ambiguousCandidates.isEmpty()) { + this.ambiguousWriteMethods = ambiguousCandidates; + } + } } @@ -77,6 +100,13 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor { @Override public Method getWriteMethod() { + Set ambiguousCandidates = this.ambiguousWriteMethods; + if (ambiguousCandidates != null) { + this.ambiguousWriteMethods = null; + LogFactory.getLog(GenericTypeAwarePropertyDescriptor.class).warn("Invalid JavaBean property '" + + getName() + "' being accessed! Ambiguous write methods found next to actually used [" + + this.writeMethod + "]: " + ambiguousCandidates); + } return this.writeMethod; }