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

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new d5a02603c5 Fix MethodExpression when accessing static method via an 
instance
d5a02603c5 is described below

commit d5a02603c5391eed353d5b0afd7973081fcb6183
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 bb1601ee4d..8fdc2bb22f 100644
--- a/java/javax/el/Util.java
+++ b/java/javax/el/Util.java
@@ -544,11 +544,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 9322c11152..eb0f1efd52 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -242,6 +242,11 @@
       <fix>
         <bug>66246</bug>: Fix JspC generates invalid web.xml due to incorrect 
header. (lihan)
       </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

Reply via email to