Author: markt
Date: Mon Apr 27 09:45:12 2015
New Revision: 1676231

URL: http://svn.apache.org/r1676231
Log:
Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=57855
Explicitly handle the case where a MethodExpression is invoked with null or the 
wrong number of parameters. Rather than failing with 
ArrayIndexOutOfBoundsException or NullPointerException throw an 
IllegalArgumentException with a useful error message.

Modified:
    tomcat/trunk/java/org/apache/el/Messages.properties
    tomcat/trunk/java/org/apache/el/parser/AstValue.java
    tomcat/trunk/test/org/apache/el/TestMethodExpressionImpl.java

Modified: tomcat/trunk/java/org/apache/el/Messages.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/el/Messages.properties?rev=1676231&r1=1676230&r2=1676231&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/el/Messages.properties (original)
+++ tomcat/trunk/java/org/apache/el/Messages.properties Mon Apr 27 09:45:12 2015
@@ -21,6 +21,8 @@ error.unreachable.base=Target Unreachabl
 error.unreachable.property=Target Unreachable, ''{0}'' returned null
 error.resolver.unhandled=ELResolver did not handle type: {0} with property of 
''{1}''
 error.resolver.unhandled.null=ELResolver cannot handle a null base Object with 
identifier ''{0}''
+error.invoke.wrongParams=The method [{0}] was called with [{1}] parameter(s) 
when it expected [{2}]
+error.invoke.tooFewParams=The method [{0}] was called with [{1}] parameter(s) 
when it expected at least [{2}]
 
 # ValueExpressionLiteral
 error.value.literal.write=ValueExpression is a literal and not writable: {0}

Modified: tomcat/trunk/java/org/apache/el/parser/AstValue.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/el/parser/AstValue.java?rev=1676231&r1=1676230&r2=1676231&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/el/parser/AstValue.java (original)
+++ tomcat/trunk/java/org/apache/el/parser/AstValue.java Mon Apr 27 09:45:12 
2015
@@ -237,7 +237,7 @@ public final class AstValue extends Simp
         }
         m = ReflectionUtil.getMethod(ctx, t.base, t.property, types, values);
 
-        // Handle varArgs and any co-ercion required
+        // Handle varArgs and any coercion required
         values = convertArgs(ctx, values, m);
 
         Object result = null;
@@ -268,6 +268,24 @@ public final class AstValue extends Simp
 
         int paramCount = types.length;
 
+        if (paramCount > 0 && src == null ||
+                m.isVarArgs() && src.length < paramCount ||
+                !m.isVarArgs() && src.length != paramCount) {
+            String inputParamCount = null;
+            if (src != null) {
+                inputParamCount = Integer.toString(src.length);
+            }
+            String msg;
+            if (m.isVarArgs()) {
+                msg = MessageFactory.get("error.invoke.tooFewParams",
+                        m.getName(), inputParamCount, 
Integer.toString(paramCount));
+            } else {
+                msg = MessageFactory.get("error.invoke.wrongParams",
+                        m.getName(), inputParamCount, 
Integer.toString(paramCount));
+            }
+            throw new IllegalArgumentException(msg);
+        }
+
         Object[] dest = new Object[paramCount];
 
         for (int i = 0; i < paramCount - 1; i++) {

Modified: tomcat/trunk/test/org/apache/el/TestMethodExpressionImpl.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/TestMethodExpressionImpl.java?rev=1676231&r1=1676230&r2=1676231&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/el/TestMethodExpressionImpl.java (original)
+++ tomcat/trunk/test/org/apache/el/TestMethodExpressionImpl.java Mon Apr 27 
09:45:12 2015
@@ -482,4 +482,20 @@ public class TestMethodExpressionImpl {
         Object r = me.invoke(context, null);
         assertEquals("AA2Hello World!", r.toString());
     }
+
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testBug57855a() {
+        MethodExpression me = factory.createMethodExpression(context,
+                "${beanAA.echo2}", null , new Class[]{String.class});
+        me.invoke(context, new Object[0]);
+    }
+
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testBug57855b() {
+        MethodExpression me = factory.createMethodExpression(context,
+                "${beanAA.echo2}", null , new Class[]{String.class});
+        me.invoke(context, null);
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to