This is an automated email from the ASF dual-hosted git repository. bitstorm pushed a commit to branch WICKET-7204-regression in repository https://gitbox.apache.org/repos/asf/wicket.git
commit 865039c3dbdf7cf56672dad224ba8b669505dfa6 Author: Andrea Del Bene <[email protected]> AuthorDate: Tue Oct 27 13:03:01 2026 +0100 Fix to avoid a separate ClassLoader for proxied classes --- .../proxy/bytebuddy/ByteBuddyProxyFactory.java | 33 +++++++++++- .../proxy/bytebuddy/ByteBuddyProxyFactoryTest.java | 61 ++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) 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..162363448c 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), @@ -147,6 +147,37 @@ public class ByteBuddyProxyFactory implements IProxyFactory } } + /** + * 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) + { + if (!type.getName().startsWith("java.")) + { + ClassLoader typeClassLoader = type.getClassLoader(); + if (typeClassLoader != null) + { + return typeClassLoader; + } + } + + return resolveClassLoader(); + } + private static ClassLoader resolveClassLoader() { ClassLoader classLoader = null; 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..9af2c5999f --- /dev/null +++ b/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 + { + thread.setContextClassLoader(original); + } + } +}
