You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by li...@apache.org on 2010/08/24 19:45:46 UTC

svn commit: r988633 - /incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java

Author: linsun
Date: Tue Aug 24 17:45:46 2010
New Revision: 988633

URL: http://svn.apache.org/viewvc?rev=988633&view=rev
Log:
ARIES-387 Ensure there is one setter that matches the type of the getter

Modified:
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java?rev=988633&r1=988632&r2=988633&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java (original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java Tue Aug 24 17:45:46 2010
@@ -431,16 +431,28 @@ public class ReflectionUtils {
             return result;
         }
         
-        private Method findMethodByClass(Class<?> arg) throws ComponentDefinitionException {
+        private Method findMethodByClass(Class<?> arg)
+                throws ComponentDefinitionException {
             Method result = null;
+
+            if (!hasSameTypeSetter()) {
+                throw new ComponentDefinitionException(
+                        "At least one Setter method has to match the type of the Getter method for property "
+                                + getName());
+            }
+
+            if (setters.size() == 1) {
+                return setters.iterator().next();
+            }
             
             for (Method m : setters) {
                 Class<?> paramType = m.getParameterTypes()[0];
-                
-                if ((arg == null && Object.class.isAssignableFrom(paramType)) 
+
+                if ((arg == null && Object.class.isAssignableFrom(paramType))
                         || (arg != null && paramType.isAssignableFrom(arg))) {
-                    
-                    // pick the method that has the more specific parameter if any
+
+                    // pick the method that has the more specific parameter if
+                    // any
                     if (result != null) {
                         Class<?> oldParamType = result.getParameterTypes()[0];
                         if (paramType.isAssignableFrom(oldParamType)) {
@@ -449,18 +461,35 @@ public class ReflectionUtils {
                             result = m;
                         } else {
                             throw new ComponentDefinitionException(
-                                    "Ambiguous setter method for property "+getName()+
-                                    ". More than one method matches the  parameter type "+arg);
+                                    "Ambiguous setter method for property "
+                                            + getName()
+                                            + ". More than one method matches the  parameter type "
+                                            + arg);
                         }
                     } else {
                         result = m;
                     }
                 }
-            }            
-            
+            }
+
             return result;
         }
         
+        // ensure there is a setter that matches the type of the getter
+        private boolean hasSameTypeSetter() {
+            if (getter == null) {
+                return true;
+            }
+            Iterator<Method> it = setters.iterator();
+            while (it.hasNext()) {
+                Method m = it.next();
+                if (m.getParameterTypes()[0].equals(getter.getReturnType())) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
         private Method findMethodWithConversion(Object value) throws ComponentDefinitionException {
             ExecutionContext ctx = ExecutionContext.Holder.getContext();
             List<Method> matchingMethods = new ArrayList<Method>();