This is an automated email from the ASF dual-hosted git repository.
dashorst pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git
The following commit(s) were added to refs/heads/master by this push:
new a7a4a4018d Cache ByteBuddy proxy classes for the class loader they are
defined in
a7a4a4018d is described below
commit a7a4a4018d7f47b30bf317c91b74c9a274de42bf
Author: Martijn Dashorst <[email protected]>
AuthorDate: Wed Oct 28 12:33:19 2026 +0100
Cache ByteBuddy proxy classes for the class loader they are defined in
Since WICKET-7204 a proxy class is defined through a private lookup in
the class loader of the proxied type, but the cache of proxy classes was
still keyed on the application's class resolver, which defaults to the
context class loader of the current thread. A thread with another
context class loader missed the cache and defined the proxy class a
second time in the type's loader, failing with "LinkageError: attempted
duplicate class definition". Injecting a concrete class through
LazyInitProxyFactory, e.g. with @SpringBean, broke on such threads.
The cache is now keyed on the loader the proxy is defined in: the
type's own loader. A proxy for a type in a java.* package is still
renamed and defined in a wrapper loader whose parent is the
application's loader, because java.sql and other platform types have a
loader that cannot see Wicket's own interfaces; it stays cached for the
application's loader.
Applications see the same proxy class on every thread and no other
change.
Generated-by: Claude Code (Claude Opus 5.5)
Co-authored-by: Andrea Del Bene <[email protected]>
---
.../proxy/bytebuddy/ByteBuddyProxyFactory.java | 28 ++++++++-
.../proxy/bytebuddy/ByteBuddyProxyFactoryTest.java | 73 ++++++++++++++++++++++
2 files changed, 98 insertions(+), 3 deletions(-)
diff --git
a/wicket-ioc/src/main/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactory.java
b/wicket-ioc/src/main/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactory.java
index b372692de1..45b1bb1d30 100644
---
a/wicket-ioc/src/main/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactory.java
+++
b/wicket-ioc/src/main/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactory.java
@@ -102,7 +102,7 @@ public class ByteBuddyProxyFactory implements IProxyFactory
@SuppressWarnings("unchecked")
public static <T> Class<T> createOrGetProxyClass(Class<T> type)
{
- ClassLoader classLoader = resolveClassLoader();
+ ClassLoader classLoader = resolveClassLoader(type);
return (Class<T>) DYNAMIC_CLASS_CACHE.findOrInsert(classLoader,
new TypeCache.SimpleKey(type),
@@ -123,6 +123,15 @@ public class ByteBuddyProxyFactory implements IProxyFactory
DYNAMIC_CLASS_CACHE);
}
+ /**
+ * The JVM does not allow a class to be defined in a <em>java.**</em>
package, so the proxy for
+ * a type in one is renamed into another package.
+ */
+ private static boolean isInJavaPackage(String typeName)
+ {
+ return typeName.startsWith("java.");
+ }
+
/**
* The proxy has to be defined in the same runtime package as the type
it proxies, or the
* package private methods it overrides are not overridden at all. Only
a proxy for a
@@ -130,7 +139,7 @@ public class ByteBuddyProxyFactory implements IProxyFactory
*/
private static ClassLoadingStrategy<ClassLoader>
resolveLoadingStrategy(Class<?> type)
{
- if (type.getName().startsWith("java."))
+ if (isInJavaPackage(type.getName()))
{
return
ClassLoadingStrategy.Default.WRAPPER.allowExistingTypes();
}
@@ -147,6 +156,19 @@ public class ByteBuddyProxyFactory implements IProxyFactory
}
}
+ /**
+ * The class loader the proxy for the given type is defined in, and
cached for.
+ */
+ private static ClassLoader resolveClassLoader(Class<?> type)
+ {
+ if (isInJavaPackage(type.getName()))
+ {
+ return resolveClassLoader();
+ }
+
+ return type.getClassLoader();
+ }
+
private static ClassLoader resolveClassLoader()
{
ClassLoader classLoader = null;
@@ -204,7 +226,7 @@ public class ByteBuddyProxyFactory implements IProxyFactory
String packageName = prefix.substring(0, lastIdxOfDot);
String className = prefix.substring(lastIdxOfDot + 1);
String name = packageName + ".";
- if (prefix.startsWith("java."))
+ if (isInJavaPackage(prefix))
{
name = "bytebuddy_generated_wicket_proxy." +
name + className;
}
diff --git
a/wicket-ioc/src/test/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactoryTest.java
b/wicket-ioc/src/test/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactoryTest.java
new file mode 100644
index 0000000000..333d87b966
--- /dev/null
+++
b/wicket-ioc/src/test/java/org/apache/wicket/proxy/bytebuddy/ByteBuddyProxyFactoryTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.sql.Timestamp;
+
+import org.apache.wicket.proxy.util.ConcreteObject;
+import org.apache.wicket.proxy.util.MockObjectLocator;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link ByteBuddyProxyFactory}
+ */
+class ByteBuddyProxyFactoryTest
+{
+ /**
+ * A thread with another context class loader gets the proxy class that
is already defined.
+ */
+ @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
+ {
+ thread.setContextClassLoader(original);
+ }
+ }
+
+ /**
+ * A type in a <em>java.**</em> package of the platform class loader,
which cannot see the
+ * interfaces the proxy implements.
+ */
+ @Test
+ void proxiesTypeInJavaPackageOfPlatformClassLoader()
+ {
+ Timestamp proxy = new
ByteBuddyProxyFactory().createProxy(Timestamp.class,
+ new MockObjectLocator(new Timestamp(42)));
+
+ assertEquals(42, proxy.getTime());
+ }
+}