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


##########
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 check stays, as Andrea says: `java.sql` and `java.net.http` types sit in 
the platform class loader, and a wrapper loader under it cannot see Wicket's 
interfaces. With the suggested simplification, proxying `java.sql.Timestamp` 
fails with `NoClassDefFoundError: 
org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactory$InterceptorMutator`. 
76935f9795 adds `proxiesTypeInJavaPackageOfPlatformClassLoader`, which covers 
that case.



##########
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:
   Covered with a test rather than a comment: 
`proxiesTypeInJavaPackageOfPlatformClassLoader` in 76935f9795 proxies 
`java.sql.Timestamp` end to end.



-- 
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