Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make access to the class loader for Develocity types CC-compatible #61

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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 @@ -14,11 +14,11 @@
public final class ProxyFactory {

public static <T> T createProxy(Object target, Class<T> targetInterface) {
return createProxy(target, targetInterface, target.getClass().getClassLoader());
return createProxy(target, targetInterface, target);
}

private static <T> T createProxy(Object target, Class<T> targetInterface, ClassLoader develocityTypesClassLoader) {
return newProxyInstance(targetInterface, new ProxyingInvocationHandler(target, develocityTypesClassLoader));
private static <T> T createProxy(Object target, Class<T> targetInterface, Object topLevelDevelocityTarget) {
return newProxyInstance(targetInterface, new ProxyingInvocationHandler(target, topLevelDevelocityTarget));
}

@SuppressWarnings("unchecked")
Expand All @@ -29,16 +29,19 @@ private static <T> T newProxyInstance(Class<T> targetInterface, InvocationHandle
private static final class ProxyingInvocationHandler implements InvocationHandler {

private final Object target;
private final ClassLoader develocityTypesClassLoader;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 Storing the reference to the class loader is not CC-compatible.

// this is the top-level Develocity extension instance that we proxy to.
// We pass it around to be able to access a classloader for Develocity types in a configuration cache compatible manner
private final Object topLevelDevelocityTarget;

private ProxyingInvocationHandler(Object target, ClassLoader develocityTypesClassLoader) {
private ProxyingInvocationHandler(Object target, Object topLevelDevelocityTarget) {
this.target = target;
this.develocityTypesClassLoader = develocityTypesClassLoader;
this.topLevelDevelocityTarget = topLevelDevelocityTarget;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) {
try {
ClassLoader develocityTypesClassLoader = topLevelDevelocityTarget.getClass().getClassLoader();
Method targetMethod = target.getClass().getMethod(method.getName(), convertTypes(method.getParameterTypes(), develocityTypesClassLoader));
Object[] targetArgs = toTargetArgs(args, develocityTypesClassLoader);

Expand All @@ -51,7 +54,7 @@ public Object invoke(Object proxy, Method method, Object[] args) {
} else if (result instanceof Enum) {
return adaptEnumArg((Enum<?>) result, develocityTypesClassLoader);
}
return createProxy(result, method.getReturnType(), develocityTypesClassLoader);
return createProxy(result, method.getReturnType(), topLevelDevelocityTarget);
} catch (Throwable e) {
throw new RuntimeException("Failed to invoke " + method + " on " + target + " with args " + Arrays.toString(args), e);
}
Expand Down Expand Up @@ -108,7 +111,7 @@ private Object createLocalProxy(Object target) {
return Proxy.newProxyInstance(
localClassLoader,
convertTypes(collectInterfaces(target.getClass()), localClassLoader),
new ProxyingInvocationHandler(target, develocityTypesClassLoader)
new ProxyingInvocationHandler(target, topLevelDevelocityTarget)
);
}

Expand Down