From 969351a1b00479a73a973b421a9c805633c43105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Sol=C3=B3rzano?= Date: Mon, 22 Jul 2024 02:26:07 +0200 Subject: [PATCH] Register fields for reflection in kubernetes-client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jorge Solórzano --- .../deployment/KubernetesClientProcessor.java | 65 ++++--------------- 1 file changed, 14 insertions(+), 51 deletions(-) diff --git a/extensions/kubernetes-client/deployment/src/main/java/io/quarkus/kubernetes/client/deployment/KubernetesClientProcessor.java b/extensions/kubernetes-client/deployment/src/main/java/io/quarkus/kubernetes/client/deployment/KubernetesClientProcessor.java index dc8370dfd862a..664d35fd6f90e 100644 --- a/extensions/kubernetes-client/deployment/src/main/java/io/quarkus/kubernetes/client/deployment/KubernetesClientProcessor.java +++ b/extensions/kubernetes-client/deployment/src/main/java/io/quarkus/kubernetes/client/deployment/KubernetesClientProcessor.java @@ -1,6 +1,5 @@ package io.quarkus.kubernetes.client.deployment; -import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -10,16 +9,12 @@ import java.util.function.Predicate; import java.util.stream.Collectors; -import org.jboss.jandex.AnnotationInstance; -import org.jboss.jandex.AnnotationTarget; import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.IndexView; import org.jboss.jandex.Type; import org.jboss.logging.Logger; -import com.fasterxml.jackson.annotation.JsonFormat; - import io.fabric8.kubernetes.api.builder.VisitableBuilder; import io.fabric8.kubernetes.api.model.AnyType; import io.fabric8.kubernetes.api.model.IntOrString; @@ -75,9 +70,7 @@ public class KubernetesClientProcessor { private static final DotName KUBE_SCHEMA = DotName.createSimple(KubeSchema.class.getName()); private static final DotName VISITABLE_BUILDER = DotName.createSimple(VisitableBuilder.class.getName()); private static final DotName CUSTOM_RESOURCE = DotName.createSimple(CustomResource.class.getName()); - private static final String SERVICE_ACCOUNT = "ServiceAccount"; - private static final DotName JSON_FORMAT = DotName.createSimple(JsonFormat.class.getName()); private static final String[] EMPTY_STRINGS_ARRAY = new String[0]; @BuildStep @@ -159,30 +152,21 @@ public void process(ApplicationIndexBuildItem applicationIndex, CombinedIndexBui Collection visitableBuilderImpls = fullIndex.getAllKnownImplementors(VISITABLE_BUILDER); // default sizes determined experimentally - these are only set in order to prevent continuous expansion of the array list - List withoutFieldsRegistration = new ArrayList<>( - kubernetesResourceImpls.size() + kubernetesResourceListImpls.size()); - List withFieldsRegistration = new ArrayList<>(2); - List ignoreJsonDeserialization = new ArrayList<>( - kubernetesResourceImpls.size() + kubernetesResourceListImpls.size()); + int defSize = kubernetesResourceImpls.size() + kubernetesResourceListImpls.size() + visitableBuilderImpls.size(); + List withFieldsRegistration = new ArrayList<>(defSize); + List ignoreJsonDeserialization = new ArrayList<>(defSize); populateReflectionRegistrationLists(kubernetesResourceImpls, watchedClasses, ignoreJsonDeserialization, - withoutFieldsRegistration, withFieldsRegistration); populateReflectionRegistrationLists(kubernetesResourceListImpls, watchedClasses, ignoreJsonDeserialization, - withoutFieldsRegistration, withFieldsRegistration); populateReflectionRegistrationLists(visitableBuilderImpls, watchedClasses, ignoreJsonDeserialization, - withoutFieldsRegistration, withFieldsRegistration); if (!withFieldsRegistration.isEmpty()) { reflectiveClasses.produce(ReflectiveClassBuildItem - .builder(withFieldsRegistration.toArray(EMPTY_STRINGS_ARRAY)).weak(true).methods().fields() - .build()); - } - if (!withoutFieldsRegistration.isEmpty()) { - reflectiveClasses.produce(ReflectiveClassBuildItem - .builder(withoutFieldsRegistration.toArray(EMPTY_STRINGS_ARRAY)).weak(true).methods() + .builder(withFieldsRegistration.toArray(EMPTY_STRINGS_ARRAY)) + .weak().methods().fields() .build()); } @@ -233,9 +217,8 @@ public void process(ApplicationIndexBuildItem applicationIndex, CombinedIndexBui .sorted() .collect(Collectors.joining("\n")); log.debugv("Watched Classes:\n{0}", watchedClassNames); - List modelClasses = new ArrayList<>(withFieldsRegistration.size() + withoutFieldsRegistration.size()); + List modelClasses = new ArrayList<>(withFieldsRegistration.size()); modelClasses.addAll(withFieldsRegistration); - modelClasses.addAll(withoutFieldsRegistration); Collections.sort(modelClasses); log.debugv("Model Classes:\n{0}", String.join("\n", modelClasses)); } @@ -254,35 +237,15 @@ public void process(ApplicationIndexBuildItem applicationIndex, CombinedIndexBui private static void populateReflectionRegistrationLists(Collection kubernetesResourceImpls, Set watchedClasses, List ignoredJsonDeserializationClasses, - List withoutFieldsRegistration, List withFieldsRegistration) { - kubernetesResourceImpls - .stream() - .peek(c -> { - // we need to make sure that the Jackson extension does not try to fully register the model classes - // since we are going to register them weakly - ignoredJsonDeserializationClasses.add(c.name()); - }) - .filter(c -> !watchedClasses.contains(c.name())) - .map(c -> { - boolean registerFields = false; - List jsonFormatInstances = c.annotationsMap().get(JSON_FORMAT); - if (jsonFormatInstances != null) { - for (AnnotationInstance jsonFormatInstance : jsonFormatInstances) { - if (jsonFormatInstance.target().kind() == AnnotationTarget.Kind.FIELD) { - registerFields = true; - break; - } - } - } - return new AbstractMap.SimpleEntry<>(c.name(), registerFields); - }).forEach(e -> { - if (e.getValue()) { - withFieldsRegistration.add(e.getKey().toString()); - } else { - withoutFieldsRegistration.add(e.getKey().toString()); - } - }); + for (ClassInfo resource : kubernetesResourceImpls) { + // we need to make sure that the Jackson extension does not try to fully + // register the model classes since we are going to register them weakly + ignoredJsonDeserializationClasses.add(resource.name()); + if (!watchedClasses.contains(resource.name())) { + withFieldsRegistration.add(resource.name().toString()); + } + } } private void findWatchedClasses(final DotName implementedOrExtendedClass,