Skip to content

Commit

Permalink
Fixing Jaxb unmarshalling error with native compilation
Browse files Browse the repository at this point in the history
(cherry picked from commit 24b321e)
  • Loading branch information
rysurd authored and gsmet committed Jan 15, 2024
1 parent df32d25 commit 3c52567
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageSystemPropertyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyIgnoreWarningBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
Expand Down Expand Up @@ -186,6 +187,7 @@ void processAnnotationsAndIndexFiles(
BuildProducer<NativeImageProxyDefinitionBuildItem> proxyDefinitions,
CombinedIndexBuildItem combinedIndexBuildItem,
List<JaxbFileRootBuildItem> fileRoots,
BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchies,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<NativeImageResourceBuildItem> resource,
BuildProducer<NativeImageResourceBundleBuildItem> resourceBundle,
Expand All @@ -202,10 +204,11 @@ void processAnnotationsAndIndexFiles(
for (DotName jaxbRootAnnotation : JAXB_ROOT_ANNOTATIONS) {
for (AnnotationInstance jaxbRootAnnotationInstance : index
.getAnnotations(jaxbRootAnnotation)) {
if (jaxbRootAnnotationInstance.target().kind() == Kind.CLASS) {
String className = jaxbRootAnnotationInstance.target().asClass().name().toString();
reflectiveClass.produce(ReflectiveClassBuildItem.builder(className).methods().fields().build());
classesToBeBound.add(className);
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());
jaxbRootAnnotationsDetected = true;
}
}
Expand Down Expand Up @@ -412,6 +415,17 @@ 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 void addReflectiveClass(BuildProducer<ReflectiveClassBuildItem> reflectiveClass, boolean methods, boolean fields,
String... className) {
reflectiveClass.produce(new ReflectiveClassBuildItem(methods, fields, className));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.it.jaxb;

import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlTransient;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
public abstract class BookIBANField {
@XmlElement
private String IBAN;

public BookIBANField() {
}

public void setIBAN(String IBAN) {
this.IBAN = IBAN;
}

public String getIBAN() {
return IBAN;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.it.jaxb;

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlType(propOrder = { "IBAN", "title" })
public class BookWithParent extends BookIBANField {
@XmlElement
private String title;

public BookWithParent() {
}

public BookWithParent(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@ public io.quarkus.it.jaxb.Response seeAlso() {
return response;
}

//Test for Jaxb with parent class field
@Path("/bookwithparent")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getBookWithParent(@QueryParam("name") String name, @QueryParam("iban") String iban) throws JAXBException {
BookWithParent bookWithParent = new BookWithParent();
bookWithParent.setTitle(name);
bookWithParent.setIBAN(iban);
JAXBContext context = JAXBContext.newInstance(bookWithParent.getClass());
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(bookWithParent, sw);
return sw.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
package io.quarkus.it.jaxb;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class JaxbIT extends JaxbTest {

//We have to test native executable of Jaxb
@Test
public void bookWithParent() {
given().when()
.param("name", "Foundation")
.param("iban", "4242")
.get("/jaxb/bookwithparent")
.then()
.statusCode(200)
.body(is(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><bookWithParent><IBAN>4242</IBAN><title>Foundation</title></bookWithParent>"));
}
}

0 comments on commit 3c52567

Please sign in to comment.