utafrali commented on code in PR #1722:
URL: https://github.com/apache/wicket/pull/1722#discussion_r4388588237


##########
wicket-ioc/src/main/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactory.java:
##########
@@ -147,6 +147,37 @@ private static ClassLoadingStrategy<ClassLoader> 
resolveLoadingStrategy(Class<?>
                }
        }
 
+       /**
+        * Resolves the class loader the proxy class for the given type is 
defined in. It is also the
+        * class loader the proxy class is cached for.
+        * <p>
+        * Unless the type is a <em>java.**</em> one (see
+        * {@link #resolveLoadingStrategy(Class)}), the proxy class is defined 
by lookup in the class
+        * loader of the proxied type, no matter which class loader the current 
thread resolves. The
+        * cache has to be keyed on that same class loader: if it was keyed on 
the class loader of the
+        * current thread, another thread (e.g. a servlet container request 
thread, whose context class
+        * loader differs from the one of the thread that initialized the 
application) would miss the
+        * cached class and try to define it a second time in the class loader 
of the type, failing with
+        * a {@link LinkageError} (duplicate class definition).
+        *
+        * @param type
+        *            the type to proxy
+        * @return the class loader the proxy class is defined in and cached for
+        */
+       private static ClassLoader resolveClassLoader(Class<?> type)

Review Comment:
   The outer `if (!type.getName().startsWith("java."))` guard is redundant with 
the inner `if (typeClassLoader != null)` check. For any `java.*` type, 
`type.getClassLoader()` returns `null` (bootstrap ClassLoader), so it already 
falls through to `resolveClassLoader()` without the outer guard. The simplified 
form is behaviorally identical:
   
   ```java
   private static ClassLoader resolveClassLoader(Class<?> type)
   {
       ClassLoader typeClassLoader = type.getClassLoader();
       return typeClassLoader != null ? typeClassLoader : resolveClassLoader();
   }
   ```
   
   If keeping the explicit `java.*` branch is intentional to mirror 
`resolveLoadingStrategy` visually, a one-line inline comment explaining that 
(e.g. `// java.* types use WRAPPER strategy; see resolveLoadingStrategy`) would 
help future readers understand why, since the Javadoc above describes the 
*result* rather than calling this relationship out directly.



##########
wicket-ioc/src/test/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactoryTest.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.proxy.bytebuddy;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+import org.apache.wicket.proxy.util.ConcreteObject;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link ByteBuddyProxyFactory}
+ */
+class ByteBuddyProxyFactoryTest
+{
+       /**
+        * The proxy class is defined in the class loader of the proxied type, 
so it must be cached
+        * for that class loader, whatever class loader the current thread 
resolves. Otherwise a
+        * thread with another context class loader (e.g. a servlet container 
request thread) does
+        * not find the cached class and tries to define it a second time, 
which fails with a
+        * {@link LinkageError} (duplicate class definition).
+        */
+       @Test
+       void proxyClassIsReusedAcrossContextClassLoaders() throws Exception
+       {
+               Thread thread = Thread.currentThread();
+               ClassLoader original = thread.getContextClassLoader();
+
+               try (URLClassLoader other = new URLClassLoader(new URL[0], 
original))
+               {
+                       Class<ConcreteObject> first = ByteBuddyProxyFactory
+                               .createOrGetProxyClass(ConcreteObject.class);
+
+                       thread.setContextClassLoader(other);
+                       Class<ConcreteObject> second = ByteBuddyProxyFactory
+                               .createOrGetProxyClass(ConcreteObject.class);
+
+                       assertSame(first, second);
+               }
+               finally

Review Comment:
   Good regression test that directly exercises the broken scenario (two calls 
with different thread context ClassLoaders must return the same `Class` 
object). One gap worth noting: the `java.*` fallback path is not covered. While 
proxying a `java.*` class is an extreme edge case in Wicket's IoC context, it 
would be worth at least a brief comment on the test class explaining the scope 
("covers non-java.* types; java.* types fall back to the application/thread 
ClassLoader and are not exercised here") so future contributors know this 
branch exists.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to