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

markt-asf 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 436246bd3a Refactor to use ClassValue</code> to cache 
Class.getMethods()
436246bd3a is described below

commit 436246bd3a8d1cc5cbb1f58eb6ccf1e7f8163a52
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Nov 17 17:39:04 2026 +0000

    Refactor to use ClassValue</code> to cache Class.getMethods()
    
    Co-authored by: Claude / Sonnet 5.5
---
 .../catalina/loader/WebappClassLoaderBase.java     |  4 ----
 .../org/apache/tomcat/util/IntrospectionUtils.java | 24 +++++++++++-----------
 .../catalina/tribes/demos/ChannelCreator.java      |  1 -
 webapps/docs/changelog.xml                         |  5 +++++
 4 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/java/org/apache/catalina/loader/WebappClassLoaderBase.java 
b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
index d196ebc113..a550e462df 100644
--- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java
+++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java
@@ -67,7 +67,6 @@ import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.InstrumentableClassLoader;
 import org.apache.tomcat.util.ExceptionUtils;
-import org.apache.tomcat.util.IntrospectionUtils;
 import org.apache.tomcat.util.buf.ToStringUtil;
 import org.apache.tomcat.util.collections.ConcurrentLruCache;
 import org.apache.tomcat.util.compat.JreCompat;
@@ -1503,9 +1502,6 @@ public abstract class WebappClassLoaderBase extends 
URLClassLoader
             clearReferencesRmiTargets();
         }
 
-        // Clear the IntrospectionUtils cache.
-        IntrospectionUtils.clear();
-
         // Clear the classloader reference in common-logging
         if (clearReferencesLogFactoryRelease) {
             LogFactory.release(this);
diff --git a/java/org/apache/tomcat/util/IntrospectionUtils.java 
b/java/org/apache/tomcat/util/IntrospectionUtils.java
index 784c91e4f2..050bb9da83 100644
--- a/java/org/apache/tomcat/util/IntrospectionUtils.java
+++ b/java/org/apache/tomcat/util/IntrospectionUtils.java
@@ -21,8 +21,6 @@ import java.lang.reflect.Method;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.Hashtable;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
@@ -450,12 +448,21 @@ public final class IntrospectionUtils {
     // -------------------- other utils --------------------
     /**
      * Clear the internal method cache.
+     *
+     * @deprecated No longer required. The cache is now held in a {@link 
ClassValue} so entries are released with
+     *                 their {@link Class}. This method does nothing and will 
be removed in Tomcat 12.
      */
+    @Deprecated
     public static void clear() {
-        objectMethods.clear();
+        // NO-OP
     }
 
-    private static final Map<Class<?>,Method[]> objectMethods = new 
ConcurrentHashMap<>();
+    private static final ClassValue<Method[]> objectMethods = new 
ClassValue<>() {
+        @Override
+        protected Method[] computeValue(Class<?> type) {
+            return type.getMethods();
+        }
+    };
 
     /**
      * Find all public methods of the given class, using a cache for 
performance.
@@ -464,14 +471,7 @@ public final class IntrospectionUtils {
      * @return the array of methods
      */
     public static Method[] findMethods(Class<?> c) {
-        Method[] methods = objectMethods.get(c);
-        if (methods != null) {
-            return methods;
-        }
-
-        methods = c.getMethods();
-        objectMethods.put(c, methods);
-        return methods;
+        return objectMethods.get(c);
     }
 
     /**
diff --git a/test/org/apache/catalina/tribes/demos/ChannelCreator.java 
b/test/org/apache/catalina/tribes/demos/ChannelCreator.java
index 77009ca86e..4c1305ee08 100644
--- a/test/org/apache/catalina/tribes/demos/ChannelCreator.java
+++ b/test/org/apache/catalina/tribes/demos/ChannelCreator.java
@@ -173,7 +173,6 @@ public class ChannelCreator {
             String key = (String) o;
             IntrospectionUtils.setProperty(sender, key, 
transportProperties.getProperty(key));
         }
-        IntrospectionUtils.clear();
         ps.setTransport(sender);
 
         McastService service = new McastService();
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 2ac5ed00fd..67c353e016 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -232,6 +232,11 @@
         Stricter parsing of the digest authentication header. Pull request
         <pr>1109</pr> provided by Arturo Bernal. (markt)
       </fix>
+      <scode>
+        Refactor Tomcat's internal <code>IntrospectionUtils</code> to use
+        <code>ClassValue</code> to cache the result of calling
+        <code>Class.getMethods()</code>. (markt)
+      </scode>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to