Skip to content

Commit

Permalink
Revert "Use a generated class to pass pre-generated proxies to static…
Browse files Browse the repository at this point in the history
… init"

This reverts commit 3f0686351c20135b6d0f0d57617c5b94b54c2252.
  • Loading branch information
yrodiere committed Mar 22, 2022
1 parent d7984aa commit 4408a5b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 101 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.GeneratedClassGizmoAdaptor;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -116,7 +115,6 @@
import io.quarkus.deployment.util.ServiceUtil;
import io.quarkus.dev.console.DevConsoleManager;
import io.quarkus.devconsole.spi.DevConsoleRuntimeTemplateInfoBuildItem;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.hibernate.orm.PersistenceUnit;
import io.quarkus.hibernate.orm.deployment.integration.HibernateOrmIntegrationRuntimeConfiguredBuildItem;
import io.quarkus.hibernate.orm.deployment.integration.HibernateOrmIntegrationStaticConfiguredBuildItem;
Expand Down Expand Up @@ -498,7 +496,7 @@ public void defineJpaEntities(
}

@BuildStep
public void pregenProxies(
public ProxyDefinitionsBuildItem pregenProxies(
JpaModelBuildItem jpaModel,
JpaModelIndexBuildItem indexBuildItem,
TransformedClassesBuildItem transformedClassesBuildItem,
Expand All @@ -516,9 +514,7 @@ public void pregenProxies(
PreGeneratedProxies proxyDefinitions = generatedProxies(managedClassAndPackageNames,
indexBuildItem.getIndex(), transformedClassesBuildItem,
generatedClassBuildItemBuildProducer, liveReloadBuildItem);

ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClassBuildItemBuildProducer, false);
ClassGenerationHelper.generatePreGeneratedProxiesSupplier(classOutput, proxyDefinitions);
return new ProxyDefinitionsBuildItem(proxyDefinitions);
}

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
Expand Down Expand Up @@ -562,6 +558,7 @@ public void build(RecorderContext recorderContext, HibernateOrmRecorder recorder
JpaModelBuildItem jpaModel,
List<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptorBuildItems,
List<HibernateOrmIntegrationStaticConfiguredBuildItem> integrationBuildItems,
ProxyDefinitionsBuildItem proxyDefinitions,
BuildProducer<FeatureBuildItem> feature,
BuildProducer<BeanContainerListenerBuildItem> beanContainerListener,
LaunchModeBuildItem launchMode) throws Exception {
Expand Down Expand Up @@ -627,7 +624,8 @@ public void build(RecorderContext recorderContext, HibernateOrmRecorder recorder

beanContainerListener
.produce(new BeanContainerListenerBuildItem(
recorder.initMetadata(finalStagePUDescriptors, scanner, integratorClasses)));
recorder.initMetadata(finalStagePUDescriptors, scanner, integratorClasses,
proxyDefinitions.getProxies())));
}

private void validateHibernatePropertiesNotUsed() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.hibernate.orm.deployment;

import java.util.Objects;

import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.hibernate.orm.runtime.proxies.PreGeneratedProxies;

/**
* Contains the reference to the class definitions of the proxies
* that Hibernate ORM might require at runtime.
* In Quarkus such proxies are built upfront, during the build.
* This needs to be a separate build item from other components so
* to avoid cycles in the rather complex build graph required by
* this extension.
*/
public final class ProxyDefinitionsBuildItem extends SimpleBuildItem {

private final PreGeneratedProxies proxies;

public ProxyDefinitionsBuildItem(PreGeneratedProxies proxies) {
Objects.requireNonNull(proxies);
this.proxies = proxies;
}

public PreGeneratedProxies getProxies() {
return proxies;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public void setupPersistenceProvider(HibernateOrmRuntimeConfig hibernateOrmRunti
}

public BeanContainerListener initMetadata(List<QuarkusPersistenceUnitDefinition> parsedPersistenceXmlDescriptors,
Scanner scanner, Collection<Class<? extends Integrator>> additionalIntegrators) {
Scanner scanner, Collection<Class<? extends Integrator>> additionalIntegrators,
PreGeneratedProxies proxyDefinitions) {
SchemaManagementIntegrator.clearDsMap();
for (QuarkusPersistenceUnitDefinition i : parsedPersistenceXmlDescriptors) {
if (i.getDataSource().isPresent()) {
Expand All @@ -66,9 +67,7 @@ public BeanContainerListener initMetadata(List<QuarkusPersistenceUnitDefinition>
@Override
public void created(BeanContainer beanContainer) {
PersistenceUnitsHolder.initializeJpa(parsedPersistenceXmlDescriptors, scanner, additionalIntegrators,
// We need an indirection to retrieve pre-generated proxies
// to avoid cyclic dependencies in build steps.
PreGeneratedProxies.get());
proxyDefinitions);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.quarkus.hibernate.orm.runtime.proxies;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

/**
* A holder class for proxies that were generated at build time,
Expand All @@ -15,43 +13,21 @@
* In most circumstances these will be used for every entity, however
* in some corner cases it may still be necessary to generate proxies
* at static init time.
*
* This class is bytecode recordable.
*/
public class PreGeneratedProxies {

public static final String PRE_GENERATED_PROXIES_SUPPLIER_CLASS = PreGeneratedProxies.class.getName()
+ "_PreGeneratedProxiesSupplier";

/**
* This is a hack to access information about proxies generated at build time,
* without relying on recorders.
*
* We can't generate new class definitions at runtime,
* and relying on recorders to pass the information
* leads to cyclic dependencies in the rather complex build graph required by this extension.
*
* See
* https://quarkusio.zulipchat.com/#narrow/stream/187038-dev/topic/Recorder.20introducing.20cyclic.20dependency.20between.20build.20steps
*
* @return The pre-generated proxies.
*/
@SuppressWarnings("unchecked")
public static PreGeneratedProxies get() {
try {
return ((Supplier<PreGeneratedProxies>) Class.forName(PRE_GENERATED_PROXIES_SUPPLIER_CLASS)
.getConstructor().newInstance())
.get();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
| NoSuchMethodException | ClassNotFoundException e) {
throw new RuntimeException("Unable to retrieve pre-generated proxies", e);
}
}

private Map<String, ProxyClassDetailsHolder> proxies = new HashMap<>();

public Map<String, ProxyClassDetailsHolder> getProxies() {
return proxies;
}

public void setProxies(Map<String, ProxyClassDetailsHolder> proxies) {
this.proxies = proxies;
}

public static class ProxyClassDetailsHolder {

private String className;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* Runtime proxies are used by Hibernate ORM to handle a number of corner cases;
* in particular Enhanced Proxies need special consideration in Quarkus as
* they aren't generated by the enhancers during the build.
* Since we can't generate new class definitions at runtime, this value holder
* class is meant to be created at build time and hold onto those class definitions.
*
* Implementors of a custom {@link org.hibernate.bytecode.spi.ProxyFactoryFactory} are
* then able to lookup such class definitions at runtime to create new instances of the
Expand Down

0 comments on commit 4408a5b

Please sign in to comment.