Author: cbrisson Date: Fri Apr 14 10:20:25 2017 New Revision: 1791334 URL: http://svn.apache.org/viewvc?rev=1791334&view=rev Log: [engine] Fix vararg methods disambiguation
Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/VarargMethodsTestCase.java Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java?rev=1791334&r1=1791333&r2=1791334&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java Fri Apr 14 10:20:25 2017 @@ -329,8 +329,8 @@ public class MethodMap */ private int compare(Class[] c1, Class[] c2) { - boolean c1MoreSpecific = false; - boolean c2MoreSpecific = false; + boolean c1IsVararag = false; + boolean c2IsVararag = false; boolean fixedLengths = false; // compare lengths to handle comparisons where the size of the arrays @@ -351,10 +351,12 @@ public class MethodMap if (itemClass == null) { /* by construct, we have c1.length = l2 + 1 */ - c2[c1.length - 1] = c1[c1.length - 1]; + c1IsVararag = true; + c2[c1.length - 1] = null; } else { + c2IsVararag = true; for (int i = l2 - 1; i < c1.length; ++i) { /* also overwrite the vaargs itself */ @@ -378,10 +380,12 @@ public class MethodMap if (itemClass == null) { /* by construct, we have c2.length = l1 + 1 */ - c1[c2.length - 1] = c2[c2.length - 1]; + c2IsVararag = true; + c1[c2.length - 1] = null; } else { + c1IsVararag = true; for (int i = l1 - 1; i < c2.length; ++i) { /* also overwrite the vaargs itself */ @@ -468,8 +472,8 @@ public class MethodMap * If one method accepts varargs and the other does not, * call the non-vararg one more specific. */ - boolean last1Array = !fixedLengths && c1[c1.length - 1].isArray(); - boolean last2Array = !fixedLengths && c2[c2.length - 1].isArray(); + boolean last1Array = c1IsVararag || !fixedLengths && c1[c1.length - 1].isArray(); + boolean last2Array = c2IsVararag || !fixedLengths && c2[c2.length - 1].isArray(); if (last1Array && !last2Array) { return LESS_SPECIFIC; Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/VarargMethodsTestCase.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/VarargMethodsTestCase.java?rev=1791334&r1=1791333&r2=1791334&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/VarargMethodsTestCase.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/VarargMethodsTestCase.java Fri Apr 14 10:20:25 2017 @@ -130,7 +130,10 @@ public class VarargMethodsTestCase exten assertEvalEquals("String,List", "$nasty.test651('test',['TEST'])"); } - + public void testMax() + { + assertEvalEquals("4", "$nasty.max(4, 3.5)"); + } public static class NiceTool { @@ -248,6 +251,26 @@ public class VarargMethodsTestCase exten return "String,List"; } + public Number max(Number n1, Number n2) { return max(new Number[] { n1, n2 }); } + + public Number max(Number[] numbers) + { + if (numbers.length == 0) return null; + int minindex = -1, i = 0; + double val = Double.MIN_VALUE; + for (Number n : numbers) + { + if (n.floatValue() > val) + { + minindex = i; + val = n.floatValue(); + } + ++i; + } + if (minindex < 0) minindex = 0; + return numbers[minindex]; + } + } }