Skip to content

Commit

Permalink
Make methods of reachable hibernate services reflectively queryable
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Jan 16, 2025
1 parent ad0a86b commit bfc5fc8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import io.quarkus.deployment.builditem.LogCategoryBuildItem;
import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.deployment.builditem.SystemPropertyBuildItem;
import io.quarkus.deployment.builditem.TransformedClassesBuildItem;
Expand Down Expand Up @@ -125,6 +126,7 @@
import io.quarkus.hibernate.orm.runtime.boot.xml.RecordableXmlMapping;
import io.quarkus.hibernate.orm.runtime.config.DialectVersions;
import io.quarkus.hibernate.orm.runtime.dev.HibernateOrmDevIntegrator;
import io.quarkus.hibernate.orm.runtime.graal.RegisterServicesForReflectionFeature;
import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationStaticDescriptor;
import io.quarkus.hibernate.orm.runtime.migration.MultiTenancyStrategy;
import io.quarkus.hibernate.orm.runtime.proxies.PreGeneratedProxies;
Expand Down Expand Up @@ -160,6 +162,11 @@ public final class HibernateOrmProcessor {

private static final String INTEGRATOR_SERVICE_FILE = "META-INF/services/org.hibernate.integrator.spi.Integrator";

@BuildStep
NativeImageFeatureBuildItem registerServicesForReflection() {
return new NativeImageFeatureBuildItem(RegisterServicesForReflectionFeature.class);
}

@BuildStep
void registerHibernateOrmMetadataForCoreDialects(
BuildProducer<DatabaseKindDialectBuildItem> producer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.hibernate.orm.runtime.graal;

import java.util.HashSet;
import java.util.Set;

import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeReflection;

/**
* Makes methods of reachable hibernate services accessible through {@link Class#getMethods()}.
*
* See <a href="https://github.com/quarkusio/quarkus/issues/45525">Github issue #45525</a>.
*/
public class RegisterServicesForReflectionFeature implements Feature {

@Override
public String getDescription() {
return "Makes methods of reachable hibernate services accessible through getMethods()`";
}

// The {@code duringAnalysis} method is invoked multiple times and increases the set of reachable types, thus we
// need to invoke {@link DuringAnalysisAccess#requireAnalysisIteration()} each time we register new methods.
private static final int ANTICIPATED_SERVICES = 100;
private static final Set<Class<?>> registeredClasses = new HashSet<>(ANTICIPATED_SERVICES);

@Override
public void duringAnalysis(DuringAnalysisAccess access) {
for (Class<?> service : access.reachableSubtypes(org.hibernate.service.Service.class)) {
if (registeredClasses.add(service)) {
RuntimeReflection.registerAllMethods(service);
access.requireAnalysisIteration();
}
}
}
}

0 comments on commit bfc5fc8

Please sign in to comment.