From 730bd662b524771542c678d8be1ade63c3d46852 Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Tue, 16 Jan 2024 15:07:02 +0100 Subject: [PATCH] Fixup #36479 JAXB unmarshalling fails in native mode --- .../jaxb/deployment/JaxbProcessor.java | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/extensions/jaxb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbProcessor.java b/extensions/jaxb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbProcessor.java index 3d488e9d012d9..653d957acd040 100644 --- a/extensions/jaxb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbProcessor.java +++ b/extensions/jaxb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbProcessor.java @@ -49,6 +49,7 @@ import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget.Kind; import org.jboss.jandex.AnnotationValue; +import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.IndexView; import org.jboss.jandex.Type; @@ -204,11 +205,15 @@ void processAnnotationsAndIndexFiles( for (DotName jaxbRootAnnotation : JAXB_ROOT_ANNOTATIONS) { for (AnnotationInstance jaxbRootAnnotationInstance : index .getAnnotations(jaxbRootAnnotation)) { - if (jaxbRootAnnotationInstance.target().kind() == Kind.CLASS - && !JAXB_ANNOTATIONS.contains(jaxbRootAnnotationInstance.target().asClass().getClass())) { - DotName targetClass = jaxbRootAnnotationInstance.target().asClass().name(); - addReflectiveHierarchyClass(targetClass, reflectiveHierarchies, index); - classesToBeBound.add(targetClass.toString()); + if (jaxbRootAnnotationInstance.target().kind() == Kind.CLASS) { + final ClassInfo target = jaxbRootAnnotationInstance.target().asClass(); + reflectiveClass.produce( + ReflectiveClassBuildItem + .builder(listAncestors(target, index)) + .methods() + .fields() + .build()); + classesToBeBound.add(target.name().toString()); jaxbRootAnnotationsDetected = true; } } @@ -415,15 +420,33 @@ public static Stream safeWalk(Path p) { } } - private void addReflectiveHierarchyClass(DotName className, - BuildProducer reflectiveHierarchy, - IndexView index) { - Type jandexType = Type.create(className, Type.Kind.CLASS); - reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem.Builder() - .type(jandexType) - .index(index) - .source(getClass().getSimpleName() + " > " + jandexType.name().toString()) - .build()); + private static String[] listAncestors(ClassInfo classInfo, IndexView index) { + final List result = new ArrayList<>(); + result.add(classInfo.name().toString()); + + DotName superName = null; + while ((superName = classInfo.superName()) != null) { + result.add(superName.toString()); + classInfo = index.getClassByName(superName); + if (classInfo == null) { + /* + * The given class is not available in the index + * Fallback to java.lang.Class API + */ + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + try { + Class cl = tccl.loadClass(superName.toString()); + while ((cl = cl.getSuperclass()) != null) { + result.add(cl.getName()); + } + } catch (ClassNotFoundException e) { + throw new IllegalStateException("Could not find " + superName + + " in Jandex nor load it using Quarkus build time context class loader"); + } + break; + } + } + return result.toArray(new String[0]); } private void addReflectiveClass(BuildProducer reflectiveClass, boolean methods, boolean fields,