ASF Bugzilla – Attachment 34489 Details for
Bug 60431
EL function varargs with array
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
AstFunction patch
AstFunctionVarArgs.patch (text/plain), 4.21 KB, created by
Ben Wolfe
on 2016-11-29 21:42:17 UTC
(
hide
)
Description:
AstFunction patch
Filename:
MIME Type:
Creator:
Ben Wolfe
Created:
2016-11-29 21:42:17 UTC
Size:
4.21 KB
patch
obsolete
>Index: java/org/apache/el/lang/ELSupport.java >=================================================================== >--- java/org/apache/el/lang/ELSupport.java (revision 1771946) >+++ java/org/apache/el/lang/ELSupport.java (working copy) >@@ -24,6 +24,7 @@ > import java.security.AccessController; > import java.security.PrivilegedAction; > import java.util.Collections; >+import java.util.List; > import java.util.Map; > import java.util.Set; > >@@ -577,6 +578,11 @@ > if (type.isArray() && obj.getClass().isArray()) { > return coerceToArray(ctx, obj, type); > } >+ >+ // Handle lists >+ if (type.isArray() && List.class.isInstance(obj)) { >+ return coerceToArray(ctx, ((List)obj).toArray(), type); >+ } > > throw new ELException(MessageFactory.get("error.convert", > obj, obj.getClass(), type)); >Index: java/org/apache/el/parser/AstFunction.java >=================================================================== >--- java/org/apache/el/parser/AstFunction.java (revision 1771946) >+++ java/org/apache/el/parser/AstFunction.java (working copy) >@@ -170,6 +170,9 @@ > if (m.isVarArgs() && i == methodParameterCount - 1) { > if (inputParameterCount < methodParameterCount) { > params[i] = null; >+ } else if (inputParameterCount == methodParameterCount && paramTypes[i].isArray()) { >+ params[i] = parameters.jjtGetChild(i).getValue(ctx); >+ params[i] = coerceToType(ctx, params[i], paramTypes[i]); > } else { > Object[] varargs = > new Object[inputParameterCount - methodParameterCount + 1]; >Index: test/org/apache/el/parser/TestAstFunction.java >=================================================================== >--- test/org/apache/el/parser/TestAstFunction.java (revision 1771946) >+++ test/org/apache/el/parser/TestAstFunction.java (working copy) >@@ -38,4 +38,31 @@ > Object result = processor.getValue("valueOf(1000)", Integer.class); > Assert.assertEquals(Integer.valueOf(1000), result); > } >+ >+ @Test >+ public void testParamsToVararg() throws Exception { >+ ELProcessor processor = new ELProcessor(); >+ processor.getELManager().mapFunction("fn", "echo", TesterUtilA.class.getMethod("echo", String[].class)); >+ Object result = processor.getValue("fn:echo('a', 'b', 'c')", String.class); >+ Assert.assertEquals("a, b, c", result); >+ } >+ >+ @Test >+ public void testArrayToVararg() throws Exception { >+ ELProcessor processor = new ELProcessor(); >+ processor.getELManager().getELContext().getImportHandler() >+ .importStatic("org.apache.el.parser.TesterUtilA.toArray"); >+ processor.getELManager().mapFunction("fn", "echo", TesterUtilA.class.getMethod("echo", String[].class)); >+ Object result = processor.getValue("fn:echo(toArray('a', 'b'))", String.class); >+ Assert.assertEquals("a, b", result); >+ } >+ >+ @Test >+ public void testListToVararg() throws Exception { >+ ELProcessor processor = new ELProcessor(); >+ processor.getELManager().mapFunction("fn", "echo", TesterUtilA.class.getMethod("echo", String[].class)); >+ Object result = processor.getValue("fn:echo(['a', 'b'])", String.class); >+ Assert.assertEquals("a, b", result); >+ } >+ > } >Index: test/org/apache/el/parser/TesterUtilA.java >=================================================================== >--- test/org/apache/el/parser/TesterUtilA.java (nonexistent) >+++ test/org/apache/el/parser/TesterUtilA.java (working copy) >@@ -0,0 +1,24 @@ >+package org.apache.el.parser; >+ >+public class TesterUtilA { >+ >+ public static String echo(String...strings) { >+ if (strings == null) { >+ return null; >+ } >+ >+ StringBuilder sb = new StringBuilder(); >+ for (int i = 0; i < strings.length; i++) { >+ if (i > 0) { >+ sb.append(", "); >+ } >+ sb.append(strings[i]); >+ } >+ return sb.toString(); >+ } >+ >+ public static String[] toArray(String arg1, String arg2) { >+ return new String[] {arg1, arg2}; >+ } >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 60431
: 34489