Skip to content

Commit

Permalink
Merge pull request #26188 from metacosm/fix-custom-resource-registration
Browse files Browse the repository at this point in the history
Properly register classes linked to `CustomResource` extensions
  • Loading branch information
gsmet committed Jun 16, 2022
2 parents 0dcb162 + 938ebda commit 0795d3a
Showing 1 changed file with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ public void process(ApplicationIndexBuildItem applicationIndex, CombinedIndexBui

// register fully (and not weakly) for reflection watchers, informers and custom resources
final Set<DotName> watchedClasses = new HashSet<>();
findWatchedClasses(WATCHER, applicationIndex, combinedIndexBuildItem, watchedClasses, 1);
findWatchedClasses(RESOURCE_EVENT_HANDLER, applicationIndex, combinedIndexBuildItem, watchedClasses, 1);
findWatchedClasses(CUSTOM_RESOURCE, applicationIndex, combinedIndexBuildItem, watchedClasses, 2);
findWatchedClasses(WATCHER, applicationIndex, combinedIndexBuildItem, watchedClasses, 1,
true);
findWatchedClasses(RESOURCE_EVENT_HANDLER, applicationIndex, combinedIndexBuildItem, watchedClasses, 1,
true);
findWatchedClasses(CUSTOM_RESOURCE, applicationIndex, combinedIndexBuildItem, watchedClasses, 2,
false);

Predicate<DotName> reflectionIgnorePredicate = ReflectiveHierarchyBuildItem.DefaultIgnoreTypePredicate.INSTANCE
.or(IS_OKHTTP_CLASS);
Expand Down Expand Up @@ -200,29 +203,36 @@ public void process(ApplicationIndexBuildItem applicationIndex, CombinedIndexBui
sslNativeSupport.produce(new ExtensionSslNativeSupportBuildItem(Feature.KUBERNETES_CLIENT));
}

private void findWatchedClasses(final DotName implementor, final ApplicationIndexBuildItem applicationIndex,
private void findWatchedClasses(final DotName implementedOrExtendedClass, final ApplicationIndexBuildItem applicationIndex,
final CombinedIndexBuildItem combinedIndexBuildItem, final Set<DotName> watchedClasses,
final int expectedGenericTypeCardinality) {
applicationIndex.getIndex().getAllKnownImplementors(implementor)
.forEach(c -> {
try {
final List<Type> watcherGenericTypes = JandexUtil.resolveTypeParameters(c.name(),
implementor, combinedIndexBuildItem.getIndex());
if (watcherGenericTypes.size() == expectedGenericTypeCardinality) {
watcherGenericTypes.forEach(t -> watchedClasses.add(t.name()));
}
} catch (IllegalArgumentException ignored) {
// when the class has no subclasses and we were not able to determine the generic types,
// it's likely that the class might be able to get deserialized
if (applicationIndex.getIndex().getAllKnownSubclasses(c.name()).isEmpty()) {
log.warnv("{0} '{1}' will most likely not work correctly in native mode. " +
"Consider specifying the generic type of '{2}' that this class handles. "
+
"See https://quarkus.io/guides/kubernetes-client#note-on-generic-types for more details",
implementor.local(), c.name(), implementor);
}
}
});
final int expectedGenericTypeCardinality, boolean isTargetClassAnInterface) {
final var index = applicationIndex.getIndex();
final var implementors = isTargetClassAnInterface ? index
.getAllKnownImplementors(implementedOrExtendedClass) : index.getAllKnownSubclasses(implementedOrExtendedClass);
implementors.forEach(c -> {
try {
final List<Type> watcherGenericTypes = JandexUtil.resolveTypeParameters(c.name(),
implementedOrExtendedClass, combinedIndexBuildItem.getIndex());
if (!isTargetClassAnInterface) {
// add the class itself: for example, in the case of CustomResource, we want to
// register the class that extends CustomResource in addition to its type parameters
watchedClasses.add(c.name());
}
if (watcherGenericTypes.size() == expectedGenericTypeCardinality) {
watcherGenericTypes.forEach(t -> watchedClasses.add(t.name()));
}
} catch (IllegalArgumentException ignored) {
// when the class has no subclasses and we were not able to determine the generic types,
// it's likely that the class might be able to get deserialized
if (index.getAllKnownSubclasses(c.name()).isEmpty()) {
log.warnv("{0} '{1}' will most likely not work correctly in native mode. " +
"Consider specifying the generic type of '{2}' that this class handles. "
+
"See https://quarkus.io/guides/kubernetes-client#note-on-generic-types for more details",
implementedOrExtendedClass.local(), c.name(), implementedOrExtendedClass);
}
}
});
}

}

0 comments on commit 0795d3a

Please sign in to comment.