You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rz...@apache.org on 2026/04/30 10:10:51 UTC

(tomee) branch TOMEE-4603 updated: TOMEE-4603 - Recover from duplicate proxy class definition in ClassDefiner

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

rzo1 pushed a commit to branch TOMEE-4603
in repository https://gitbox.apache.org/repos/asf/tomee.git


The following commit(s) were added to refs/heads/TOMEE-4603 by this push:
     new 77993d8e93 TOMEE-4603 - Recover from duplicate proxy class definition in ClassDefiner
77993d8e93 is described below

commit 77993d8e9376a6f0bcbb53c6291baa267a701786
Author: Richard Zowalla <rz...@apache.org>
AuthorDate: Thu Apr 30 12:10:35 2026 +0200

    TOMEE-4603 - Recover from duplicate proxy class definition in ClassDefiner
    
    When the JVM rejects a defineClass with LinkageError because that class
    name is already loaded in this ClassLoader, re-resolve the existing
    class via Class.forName instead of failing. Mirrors the
    Unsafe.handleLinkageError fallback in OpenWebBeans, which only kicks in
    when no DefiningClassService is registered. Without this defence
    OpenWebBeans 4.0.x can generate the same proxy class FQN for two
    distinct Bean<?> instances of the same erased type (e.g. MyFaces'
    @FacesScoped Map producers requestScope/param/header/...) and the
    LinkageError propagates to the user. Logs a WARNING so the recovery
    remains visible.
---
 .../apache/openejb/util/proxy/ClassDefiner.java    | 40 ++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ClassDefiner.java b/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ClassDefiner.java
index 49791ce954..a082c543a9 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ClassDefiner.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ClassDefiner.java
@@ -18,6 +18,8 @@
  */
 package org.apache.openejb.util.proxy;
 
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
 import org.apache.webbeans.spi.DefiningClassService;
 import org.apache.webbeans.spi.InstantiatingClassService;
 
@@ -26,6 +28,8 @@ import java.lang.reflect.Method;
 import java.security.ProtectionDomain;
 
 public class ClassDefiner implements DefiningClassService, InstantiatingClassService {
+    private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB, ClassDefiner.class);
+
     private static final Method CLASS_LOADER_DEFINE_CLASS;
     private static final Method GET_MODULE;
     private static final Method CAN_READ;
@@ -95,6 +99,10 @@ public class ClassDefiner implements DefiningClassService, InstantiatingClassSer
             return (Class<?>) CLASS_LOADER_DEFINE_CLASS.invoke(
                     loader, className, b, 0, b.length, protectionDomain);
         } catch (final Exception e) {
+            final Class<?> existing = handleLinkageError(e, className, loader);
+            if (existing != null) {
+                return existing;
+            }
             throw e instanceof RuntimeException ? ((RuntimeException) e) : new RuntimeException(e);
         }
     }
@@ -115,10 +123,42 @@ public class ClassDefiner implements DefiningClassService, InstantiatingClassSer
             final Object lookup = PRIVATE_LOOKUP_IN.invoke(null, originalClass, MethodHandles.lookup());
             return (Class<?>) DEFINE_CLASS.invoke(lookup, b);
         } catch (final Exception e) {
+            final Class<?> existing = handleLinkageError(e, className, loader);
+            if (existing != null) {
+                return existing;
+            }
             throw new RuntimeException(e);
         }
     }
 
+    // Mirrors org.apache.webbeans.proxy.Unsafe#handleLinkageError: when the
+    // JVM rejects a defineClass with LinkageError because that class name is
+    // already loaded in this ClassLoader, re-resolve the existing class via
+    // Class#forName instead of failing. OpenWebBeans 4.0.x can generate the
+    // same proxy class FQN for two distinct Bean<?> instances of the same
+    // erased type (see TOMEE-4603 / OWB reproducer); without this fallback
+    // the second resolution propagates a LinkageError out to the user.
+    private static Class<?> handleLinkageError(final Throwable thrown, final String className, final ClassLoader loader) {
+        Throwable t = thrown;
+        while (t != null) {
+            if (t instanceof LinkageError) {
+                try {
+                    final Class<?> existing = Class.forName(className.replace('/', '.'), true, loader);
+                    LOGGER.warning("Recovered from duplicate proxy class definition for '" + className
+                            + "' in classloader " + loader
+                            + " by re-resolving the existing class. This usually indicates an upstream proxy "
+                            + "factory generated the same class name twice (see TOMEE-4603). "
+                            + "Original error: " + t);
+                    return existing;
+                } catch (final ClassNotFoundException cnfe) {
+                    return null;
+                }
+            }
+            t = t.getCause();
+        }
+        return null;
+    }
+
     public static <T> T allocateProxy(final Class<T> proxyClass) {
         // let's still use Unsafe for the moment so we avoid calling the constructor
         return (T) LocalBeanProxyFactory.Unsafe.allocateInstance(proxyClass);