Skip to content

Commit

Permalink
fix: clean up reflection registrations (#997)
Browse files Browse the repository at this point in the history
* fix: clean up todos around reflections registrations in OperatorSDKProcessor

Signed-off-by: xstefank <xstefank122@gmail.com>

* refactor: package-only visibility

Signed-off-by: Chris Laprun <claprun@redhat.com>

* refactor: remove now unneeded step

Signed-off-by: Chris Laprun <claprun@redhat.com>

---------

Signed-off-by: xstefank <xstefank122@gmail.com>
Signed-off-by: Chris Laprun <claprun@redhat.com>
Co-authored-by: Chris Laprun <claprun@redhat.com>
  • Loading branch information
xstefank and metacosm authored Nov 12, 2024
1 parent eb62624 commit 8c4532f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import jakarta.inject.Singleton;

import org.jboss.jandex.DotName;
import org.jboss.jandex.Type;
import org.jboss.logging.Logger;

import io.javaoperatorsdk.operator.api.config.ConfigurationService;
Expand All @@ -37,7 +36,6 @@
import io.quarkus.deployment.builditem.*;
import io.quarkus.deployment.builditem.nativeimage.ForceNonWeakReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyIgnoreWarningBuildItem;
import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem;
import io.quarkus.gizmo.AssignableResultHandle;
import io.quarkus.gizmo.MethodCreator;
Expand Down Expand Up @@ -190,28 +188,6 @@ AnnotationConfigurablesBuildItem gatherAnnotationConfigurables(
return new AnnotationConfigurablesBuildItem(configurableInfos);
}

/**
* Gathers the CustomResource implementations that are not part of the application index because they are part of an
* external, reusable module for example.
*
* <p>
* Note that this will be obsolete once <a href="https://github.com/quarkusio/quarkus/pull/38586">Quarkus #38586</a> is
* usable
* </p>
*/
@BuildStep
void gatherOutOfAppCustomResourceImplementations(CombinedIndexBuildItem combinedIndexBuildItem,
ApplicationIndexBuildItem applicationIndexBuildItem,
BuildProducer<QOSDKReflectiveClassBuildItem> reflectiveClassProducer) {
final var combinedIndex = combinedIndexBuildItem.getIndex();
final var appIndex = applicationIndexBuildItem.getIndex();

// only add the CRs found in the combined index that were not already in the application one since Quarkus should already handle these
final var crsFromCombined = combinedIndex.getAllKnownSubclasses(Constants.CUSTOM_RESOURCE);
crsFromCombined.removeAll(appIndex.getAllKnownSubclasses(Constants.CUSTOM_RESOURCE));
crsFromCombined.forEach(ci -> reflectiveClassProducer.produce(new QOSDKReflectiveClassBuildItem(ci.name().toString())));
}

@BuildStep
void registerClassesForReflection(
List<QOSDKReflectiveClassBuildItem> toRegister,
Expand Down Expand Up @@ -267,56 +243,13 @@ void initializeRuntimeNamespacesFromBuildTimeValues(

}

/**
* Ignore warnings related to non-indexed classes in the reflective hierarchy. At this point, we cannot know
* if they are actually needed for native compilation.
*
* <p>
* This could probably be removed once <a href=
* "https://github.com/quarkiverse/quarkus-operator-sdk/issues/941">https://github.com/quarkiverse/quarkus-operator-sdk/issues/941</a>
* is resolved.
* </p>
*
*/
@BuildStep
void ignoreNonIndexedClassesWarningsInReflectiveHierarchy(
BuildProducer<ReflectiveHierarchyIgnoreWarningBuildItem> reflectiveHierarchyIgnoreWarningBuildItemBuildProducer) {
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(DotName.createSimple(io.vertx.core.Vertx.class.getName())));
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(
DotName.createSimple(io.vertx.core.http.HttpClient.class.getName())));
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(
DotName.createSimple(io.vertx.core.http.WebSocket.class.getName())));
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(
DotName.createSimple(io.vertx.core.net.ProxyType.class.getName())));
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(
DotName.createSimple("okhttp3.OkHttpClient")));
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(
DotName.createSimple("okhttp3.OkHttpClient$Builder")));
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(
DotName.createSimple("okhttp3.Request$Builder")));
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(
DotName.createSimple("okhttp3.WebSocket")));
reflectiveHierarchyIgnoreWarningBuildItemBuildProducer.produce(
new ReflectiveHierarchyIgnoreWarningBuildItem(
DotName.createSimple("okio.BufferedSource")));

}

private void registerAssociatedClassesForReflection(BuildProducer<ReflectiveHierarchyBuildItem> reflectionClasses,
BuildProducer<ForceNonWeakReflectiveClassBuildItem> forcedReflectionClasses,
Set<String> classNamesToRegister) {
// todo: use builder API when/if https://github.com/quarkusio/quarkus/pull/38679 is available
classNamesToRegister.forEach(cn -> {
reflectionClasses.produce(new ReflectiveHierarchyBuildItem.Builder()
.type(Type.create(DotName.createSimple(cn), Type.Kind.CLASS)).build());
reflectionClasses.produce(ReflectiveHierarchyBuildItem.builder(DotName.createSimple(cn))
.ignoreTypePredicate(ReflectionRegistrations.IGNORE_TYPE_FOR_REFLECTION_PREDICATE)
.build());
forcedReflectionClasses.produce(
new ForceNonWeakReflectiveClassBuildItem(cn));
log.infov("Registered ''{0}'' for reflection", cn);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkiverse.operatorsdk.deployment;

import java.util.function.Predicate;

import org.jboss.jandex.DotName;

import io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyBuildItem;

public class ReflectionRegistrations {

private static final String[] PACKAGES_IGNORED_FOR_REFLECTION = {
// Vert.x
"io.vertx.core.",
// OkHttp3
"okhttp3.",
// Okio
"okio."
};

static final IgnoreTypeForReflectionPredicate IGNORE_TYPE_FOR_REFLECTION_PREDICATE = new IgnoreTypeForReflectionPredicate();

private static class IgnoreTypeForReflectionPredicate implements Predicate<DotName> {

@Override
public boolean test(DotName dotName) {
if (ReflectiveHierarchyBuildItem.DefaultIgnoreTypePredicate.INSTANCE.test(dotName)) {
return true;
}
String name = dotName.toString();
for (String packageName : PACKAGES_IGNORED_FOR_REFLECTION) {
if (name.startsWith(packageName)) {
return true;
}
}
return false;
}
}
}

0 comments on commit 8c4532f

Please sign in to comment.