You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2021/06/03 15:42:14 UTC

[commons-jexl] 07/13: Fix and test for JEXL-124

This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to tag 2.1.1
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git

commit 3da34ebcc115f1f1611bbbb70861995138ee9e2b
Author: Henri Biestro <he...@apache.org>
AuthorDate: Fri Dec 16 07:13:26 2011 +0000

    Fix and test for JEXL-124
    
    git-svn-id: https://svn-us.apache.org/repos/asf/commons/proper/jexl/branches/2.0@1215052 13f79535-47bb-0310-9956-ffa450edef68
---
 .../org/apache/commons/jexl2/internal/MethodExecutor.java | 12 ++++++++----
 src/test/java/org/apache/commons/jexl2/MethodTest.java    | 15 +++++++++++++++
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl2/internal/MethodExecutor.java b/src/main/java/org/apache/commons/jexl2/internal/MethodExecutor.java
index d6487f2..f39542a 100644
--- a/src/main/java/org/apache/commons/jexl2/internal/MethodExecutor.java
+++ b/src/main/java/org/apache/commons/jexl2/internal/MethodExecutor.java
@@ -131,12 +131,16 @@ public final class MethodExecutor extends AbstractExecutor.Method {
         // if no values are being passed into the vararg, size == 0
         if (size == 1) {
             // if one non-null value is being passed into the vararg,
+            // and that arg is not the sole argument and not an array of the expected type,
             // make the last arg an array of the expected type
             if (actual[index] != null) {
-                // create a 1-length array to hold and replace the last argument
-                Object lastActual = Array.newInstance(type, 1);
-                Array.set(lastActual, 0, actual[index]);
-                actual[index] = lastActual;
+                Class<?> aclazz = actual[index].getClass();
+                if (!aclazz.isArray() || !aclazz.getComponentType().equals(type)) {
+                    // create a 1-length array to hold and replace the last argument
+                    Object lastActual = Array.newInstance(type, 1);
+                    Array.set(lastActual, 0, actual[index]);
+                    actual[index] = lastActual;
+                }
             }
             // else, the vararg is null and used as is, considered as T[]
         } else {
diff --git a/src/test/java/org/apache/commons/jexl2/MethodTest.java b/src/test/java/org/apache/commons/jexl2/MethodTest.java
index 9a0b4b8..137141f 100644
--- a/src/test/java/org/apache/commons/jexl2/MethodTest.java
+++ b/src/test/java/org/apache/commons/jexl2/MethodTest.java
@@ -66,6 +66,19 @@ public class MethodTest extends JexlTestCase {
             }
             return mixed + ":" + result;
         }
+        
+        public String concat(String... strs) {
+            if (strs.length > 0) {
+                StringBuilder strb = new StringBuilder(strs[0]);
+                for(int s = 1; s < strs.length; ++s) {
+                    strb.append(", ");
+                    strb.append(strs[s]);
+                }
+                return strb.toString();
+            } else {
+                return "";
+            }
+        }
     }
 
     public static class Functor {
@@ -118,6 +131,8 @@ public class MethodTest extends JexlTestCase {
         asserter.assertExpression("test.callInts()", "Varargs:0");
         asserter.assertExpression("test.callInts(1)", "Varargs:1");
         asserter.assertExpression("test.callInts(1,2,3,4,5)", "Varargs:15");
+        asserter.assertExpression("test.concat(['1', '2', '3'])", "1, 2, 3");
+        asserter.assertExpression("test.concat('1', '2', '3')", "1, 2, 3");
     }
 
     public void testCallMixedVarArgMethod() throws Exception {