Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public <T> T createProxy(final Class<T> type, final IProxyTargetLocator locator)
@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),
Expand All @@ -123,14 +123,23 @@ public static <T> Class<T> createOrGetProxyClass(Class<T> type)
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
* <em>java.**</em> type is renamed into another package, and needs a class loader of its own.
*/
private static ClassLoadingStrategy<ClassLoader> resolveLoadingStrategy(Class<?> type)
{
if (type.getName().startsWith("java."))
if (isInJavaPackage(type.getName()))
{
return ClassLoadingStrategy.Default.WRAPPER.allowExistingTypes();
}
Expand All @@ -147,6 +156,19 @@ private static ClassLoadingStrategy<ClassLoader> resolveLoadingStrategy(Class<?>
}
}

/**
* 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;
Expand Down Expand Up @@ -204,7 +226,7 @@ protected String name(TypeDescription superClass) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading