Skip to content

Commit

Permalink
Merge pull request #35282 from gsmet/cleanup-bytecode-recorders
Browse files Browse the repository at this point in the history
Avoid keeping references to BytecodeRecorderImpl
  • Loading branch information
gsmet authored Aug 9, 2023
2 parents b690d05 + cb33a01 commit cefaf6d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.jboss.jandex.Type;
import org.wildfly.common.Assert;

import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.proxy.ProxyConfiguration;
import io.quarkus.deployment.proxy.ProxyFactory;
import io.quarkus.deployment.recording.AnnotationProxyProvider.AnnotationProxy;
Expand All @@ -71,7 +70,6 @@
import io.quarkus.runtime.StartupContext;
import io.quarkus.runtime.StartupTask;
import io.quarkus.runtime.annotations.IgnoreProperty;
import io.quarkus.runtime.annotations.RecordableConstructor;
import io.quarkus.runtime.annotations.RelaxedValidation;

/**
Expand Down Expand Up @@ -114,7 +112,6 @@ public class BytecodeRecorderImpl implements RecorderContext {
private final boolean staticInit;
private final ClassLoader classLoader;

private static final Map<Class<?>, ProxyFactory<?>> recordingProxyFactories = new ConcurrentHashMap<>();
private final Map<Class<?>, ProxyFactory<?>> returnValueProxy = new ConcurrentHashMap<>();

private final Map<Class<?>, Object> existingProxyClasses = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -364,29 +361,23 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
};

try {
if (recordingProxyFactories.containsKey(theClass)) {
return (T) recordingProxyFactories.get(theClass).newInstance(invocationHandler);
ProxyFactory<T> factory = RecordingProxyFactories.get(theClass);

if (factory != null) {
return factory.newInstance(invocationHandler);
}

String proxyNameSuffix = "$$RecordingProxyProxy" + COUNT.incrementAndGet();

ProxyConfiguration<T> proxyConfiguration = new ProxyConfiguration<T>()
.setSuperClass(theClass)
.setClassLoader(classLoader)
.setAnchorClass(getClass())
.setProxyNameSuffix(proxyNameSuffix);
ProxyFactory<T> factory = new ProxyFactory<T>(proxyConfiguration);
factory = new ProxyFactory<T>(proxyConfiguration);
T recordingProxy = factory.newInstance(invocationHandler);
existingProxyClasses.put(theClass, recordingProxy);
recordingProxyFactories.put(theClass, factory);

if (theClass.getClassLoader() instanceof QuarkusClassLoader) {
((QuarkusClassLoader) theClass.getClassLoader()).addCloseTask(new Runnable() {
@Override
public void run() {
recordingProxyFactories.remove(theClass);
}
});
}
RecordingProxyFactories.put(theClass, factory);
return recordingProxy;
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.deployment.recording;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.proxy.ProxyFactory;

class RecordingProxyFactories {

private static final Map<Class<?>, ProxyFactory<?>> RECORDING_PROXY_FACTORIES = new ConcurrentHashMap<>();

static <T> void put(Class<T> clazz, ProxyFactory<T> proxyFactory) {
RECORDING_PROXY_FACTORIES.put(clazz, proxyFactory);

if (clazz.getClassLoader() instanceof QuarkusClassLoader) {
((QuarkusClassLoader) clazz.getClassLoader()).addCloseTask(new Runnable() {
@Override
public void run() {
RecordingProxyFactories.RECORDING_PROXY_FACTORIES.remove(clazz);
}
});
}
}

@SuppressWarnings("unchecked")
static <T> ProxyFactory<T> get(Class<T> clazz) {
return (ProxyFactory<T>) RECORDING_PROXY_FACTORIES.get(clazz);
}
}

0 comments on commit cefaf6d

Please sign in to comment.