This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new 786b43d522 Fix MethodExpression when accessing static method via an instance 786b43d522 is described below commit 786b43d52229b8db14b343ac6022368fc01785ac 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/jakarta/el/Util.java | 4 +--- java/org/apache/el/util/ReflectionUtil.java | 4 +--- test/org/apache/el/util/TestReflectionUtil.java | 24 ++++++++++++++++++++++++ webapps/docs/changelog.xml | 5 +++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/java/jakarta/el/Util.java b/java/jakarta/el/Util.java index badccfb892..b0a995c59b 100644 --- a/java/jakarta/el/Util.java +++ b/java/jakarta/el/Util.java @@ -544,11 +544,9 @@ class Util { * making changes keep the code in sync. */ static Method getMethod(Class<?> type, Object base, Method m) { - // 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()) && - (canAccess(base, m) || base != null && canAccess(null, m)))) { + (Modifier.isStatic(m.getModifiers()) && canAccess(null, m) || 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 70e7e41056..fd6680dd8f 100644 --- a/java/org/apache/el/util/ReflectionUtil.java +++ b/java/org/apache/el/util/ReflectionUtil.java @@ -429,11 +429,9 @@ public class ReflectionUtil { * the code in sync. */ private static Method getMethod(Class<?> type, Object base, Method m) { - // 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()) && - (m.canAccess(base) || base != null && m.canAccess(null)))) { + (Modifier.isStatic(m.getModifiers()) && m.canAccess(null) || m.canAccess(base)))) { 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 43b519fbc6..4362fc255e 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 jakarta.el.ELContext; +import jakarta.el.ExpressionFactory; +import jakarta.el.MethodExpression; import jakarta.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,22 @@ 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 6d053ef776..e43b8f5c60 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -239,6 +239,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