This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 8.5.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/8.5.x by this push: new f8ce492251 Fix MethodExpression when accessing static method via an instance f8ce492251 is described below commit f8ce49225132131f42f8a8b89f7148c8224bf442 Author: Mark Thomas <ma...@apache.org> AuthorDate: Wed Aug 31 16:33:49 2022 +0100 Fix MethodExpression when accessing static method via an instance --- java/javax/el/Util.java | 4 +--- java/org/apache/el/util/ReflectionUtil.java | 5 ++--- test/org/apache/el/util/TestReflectionUtil.java | 25 +++++++++++++++++++++++++ webapps/docs/changelog.xml | 5 +++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/java/javax/el/Util.java b/java/javax/el/Util.java index d169839794..7e7812a102 100644 --- a/java/javax/el/Util.java +++ b/java/javax/el/Util.java @@ -546,11 +546,9 @@ class Util { */ static Method getMethod(Class<?> type, Object base, Method m) { JreCompat jreCompat = JreCompat.getInstance(); - // If base is null, method MUST be static - // If base is non-null, method may be static or non-static if (m == null || (Modifier.isPublic(type.getModifiers()) && - (jreCompat.canAccess(base, m) || base != null && jreCompat.canAccess(null, m)))) { + (Modifier.isStatic(m.getModifiers()) && jreCompat.canAccess(null, m) || jreCompat.canAccess(base, m)))) { return m; } Class<?>[] interfaces = type.getInterfaces(); diff --git a/java/org/apache/el/util/ReflectionUtil.java b/java/org/apache/el/util/ReflectionUtil.java index c1162d1897..635c906123 100644 --- a/java/org/apache/el/util/ReflectionUtil.java +++ b/java/org/apache/el/util/ReflectionUtil.java @@ -430,11 +430,10 @@ public class ReflectionUtil { */ private static Method getMethod(Class<?> type, Object base, Method m) { JreCompat jreCompat = JreCompat.getInstance(); - // If base is null, method MUST be static - // If base is non-null, method may be static or non-static if (m == null || (Modifier.isPublic(type.getModifiers()) && - (jreCompat.canAccess(base, m) || base != null && jreCompat.canAccess(null, m)))) { + (Modifier.isStatic(m.getModifiers()) && jreCompat.canAccess(null, m) + || jreCompat.canAccess(base, m)))) { return m; } Class<?>[] interfaces = type.getInterfaces(); diff --git a/test/org/apache/el/util/TestReflectionUtil.java b/test/org/apache/el/util/TestReflectionUtil.java index f39ae85467..ffac25b3bd 100644 --- a/test/org/apache/el/util/TestReflectionUtil.java +++ b/test/org/apache/el/util/TestReflectionUtil.java @@ -16,10 +16,16 @@ */ package org.apache.el.util; +import javax.el.ELContext; +import javax.el.ExpressionFactory; +import javax.el.MethodExpression; import javax.el.MethodNotFoundException; +import org.junit.Assert; import org.junit.Test; +import org.apache.jasper.el.ELContextImpl; + public class TestReflectionUtil { private static final Tester BASE = new Tester(); @@ -60,4 +66,23 @@ public class TestReflectionUtil { new Class[] {null}, new Object[] {null}); } + + @Test + public void testStaticMethodOnInstance() { + ExpressionFactory factory = ExpressionFactory.newInstance(); + ELContext context = new ELContextImpl(factory); + + MethodExpression methodExpression = + factory.createMethodExpression(context, "${\"1\".format(2)}", String.class, new Class<?>[] {}); + + try { + methodExpression.invoke(context, null); + } catch (IllegalArgumentException iae) { + // Ensure correct IllegalArgumentException is thrown + String msg = iae.getMessage(); + Assert.assertTrue(msg, msg.contains("[format]")); + return; + } + Assert.fail("No exception"); + } } diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index ccdc313a7b..38625188aa 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -189,6 +189,11 @@ type conversion fails during an EL arithmetic operation. This is an EL error so ELException seems more appropriate. (markt) </fix> + <fix> + Fix a bug in <code>MethodExpression</code> handling that triggered an + error when invoking a static method on an instance of the class rather + than directly on the class. (markt) + </fix> </changelog> </subsection> <subsection name="Cluster"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org