Skip to content

Commit

Permalink
Fixup quarkusio#36479 JAXB unmarshalling fails in native mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Jan 16, 2024
1 parent 67e59aa commit 730bd66
Showing 1 changed file with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -415,15 +420,33 @@ public static Stream<Path> safeWalk(Path p) {
}
}

private void addReflectiveHierarchyClass(DotName className,
BuildProducer<ReflectiveHierarchyBuildItem> 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<String> 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<ReflectiveClassBuildItem> reflectiveClass, boolean methods, boolean fields,
Expand Down

0 comments on commit 730bd66

Please sign in to comment.