Title: [2077] trunk/xstream/src: Ensure that provided argument sequence is taken into account for constructor selection.
Revision
2077
Author
joehni
Date
2013-06-29 17:01:10 -0500 (Sat, 29 Jun 2013)

Log Message

Ensure that provided argument sequence is taken into account for constructor selection.

Modified Paths

Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/core/util/DependencyInjectionFactory.java (2076 => 2077)


--- trunk/xstream/src/java/com/thoughtworks/xstream/core/util/DependencyInjectionFactory.java	2013-06-25 22:53:10 UTC (rev 2076)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/core/util/DependencyInjectionFactory.java	2013-06-29 22:01:10 UTC (rev 2077)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2009, 2010, 2011, 2012 XStream Committers.
+ * Copyright (c) 2007, 2009, 2010, 2011, 2012, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -119,8 +119,7 @@
                 }
 
                 // first approach: test the ctor params against the dependencies in the sequence
-                // of the parameter
-                // declaration
+                // of the parameter declaration
                 matchingDependencies.clear();
                 clear(usedDependencies);
                 for (int j = 0, k = 0; j < parameterTypes.length
@@ -137,8 +136,8 @@
                     }
                 }
 
-                if (bestMatchingCtor == null && possibleCtor == null) {
-                    possibleCtor = constructor; // assumption
+                if (bestMatchingCtor == null) {
+                    boolean possible = true; // assumption
 
                     // try to match all dependencies in the sequence of the parameter
                     // declaration
@@ -173,12 +172,29 @@
                             }
                             deps[assignable] = null; // do not match same dep twice
                         } else {
-                            possibleCtor = null;
+                            possible = false;
                             break;
                         }
                     }
                     
-                    if (possibleCtor != null) {
+                    if (possible) {
+                        if (possibleCtor != null) {
+                            int j = 0;
+                            for(; j < parameterTypes.length; ++j) {
+                                boolean a = possibleUsedDependencies.get(j);
+                                boolean b = usedDependencies.get(j);
+                                if ((a && !b) ||(b && !a)) {
+                                    if (b) {
+                                        j = parameterTypes.length; 
+                                    }
+                                    break;
+                                }
+                            }
+                            if (j < parameterTypes.length) {
+                                continue;
+                            }
+                        }
+                        possibleCtor = constructor;
                         possibleMatchingDependencies = (List)matchingDependencies.clone();
                         if (usedDependencies != null) {
                             possibleUsedDependencies = (BitSet)usedDependencies.clone();

Modified: trunk/xstream/src/test/com/thoughtworks/xstream/core/util/DependencyInjectionFactoryTest.java (2076 => 2077)


--- trunk/xstream/src/test/com/thoughtworks/xstream/core/util/DependencyInjectionFactoryTest.java	2013-06-25 22:53:10 UTC (rev 2076)
+++ trunk/xstream/src/test/com/thoughtworks/xstream/core/util/DependencyInjectionFactoryTest.java	2013-06-29 22:01:10 UTC (rev 2077)
@@ -175,4 +175,26 @@
         assertTrue(used.get(0));
         assertTrue(used.get(1));
     }
+    
+    public void testWillSelectMatchingConstructorForFirstMatchingArguments() {
+        BitSet used = new BitSet();
+        Thing thing = (Thing)DependencyInjectionFactory.newInstance(
+            Thing.class, new Object[]{this, new Integer(1), "foo"}, used);
+        assertSame(this, thing.getTestCase());
+        assertEquals(1, thing.getFirst());
+        assertEquals(4, thing.getSecond());
+        assertTrue(used.get(0));
+        assertTrue(used.get(1));
+        assertFalse(used.get(2));
+        
+        used = new BitSet();
+        thing = (Thing)DependencyInjectionFactory.newInstance(
+            Thing.class, new Object[]{this, "foo", new Integer(1)}, used);
+        assertSame(this, thing.getTestCase());
+        assertEquals(3, thing.getFirst());
+        assertEquals(12, thing.getSecond());
+        assertTrue(used.get(0));
+        assertTrue(used.get(1));
+        assertFalse(used.get(2));
+    }
 }

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to