Skip to content

Commit

Permalink
add characterEscapeHandler and TypedXmlWriter xmlseeAlso support and …
Browse files Browse the repository at this point in the history
…fix separator in jaxb index file
  • Loading branch information
dufoli committed Aug 25, 2021
1 parent dc686fb commit d182c44
Showing 1 changed file with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package io.quarkus.jaxb.deployment;

import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import javax.inject.Inject;
Expand Down Expand Up @@ -47,8 +52,11 @@

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;

import com.sun.xml.bind.v2.model.annotation.Locatable;

Expand Down Expand Up @@ -108,6 +116,7 @@ class JaxbProcessor {
private static final DotName XML_SCHEMA = DotName.createSimple(XmlSchema.class.getName());
private static final DotName XML_JAVA_TYPE_ADAPTER = DotName.createSimple(XmlJavaTypeAdapter.class.getName());
private static final DotName XML_ANY_ELEMENT = DotName.createSimple(XmlAnyElement.class.getName());
private static final DotName XML_SEE_ALSO = DotName.createSimple(XmlSeeAlso.class.getName());

private static final List<DotName> JAXB_ROOT_ANNOTATIONS = Arrays.asList(XML_ROOT_ELEMENT, XML_TYPE, XML_REGISTRY);

Expand Down Expand Up @@ -173,6 +182,18 @@ void processAnnotationsAndIndexFiles(
proxyDefinitions.produce(new NativeImageProxyDefinitionBuildItem(className, Locatable.class.getName()));
addReflectiveClass(reflectiveClass, true, false, className);
});
produceProxyIfExist(proxyDefinitions, "com.sun.xml.bind.marshaller.CharacterEscapeHandler");
produceProxyIfExist(proxyDefinitions, "com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler");
produceProxyIfExist(proxyDefinitions, "org.glassfish.jaxb.core.marshaller.CharacterEscapeHandler");
produceProxyIfExist(proxyDefinitions, "com.sun.xml.txw2.output.CharacterEscapeHandler");
produceProxyIfExist(proxyDefinitions, "org.glassfish.jaxb.characterEscapeHandler");
produceProxyIfExist(proxyDefinitions, "org.glassfish.jaxb.marshaller.CharacterEscapeHandler");

proxyDefinitions.produce(new NativeImageProxyDefinitionBuildItem("com.sun.xml.txw2.TypedXmlWriter"));
Set<String> proxiesCreated = new HashSet<>();
DotName typedXmlWriterDN = DotName.createSimple("com.sun.xml.txw2.TypedXmlWriter");
// getAllKnownDirectImplementors skip interface, so use own recursion
produceRecursiveProxies(index, typedXmlWriterDN, proxyDefinitions, proxiesCreated);

for (JaxbFileRootBuildItem i : fileRoots) {
try (Stream<Path> stream = iterateResources(i.getFileRoot())) {
Expand All @@ -182,6 +203,50 @@ void processAnnotationsAndIndexFiles(
}
}

private void produceProxyIfExist(BuildProducer<NativeImageProxyDefinitionBuildItem> proxies, String interfaceName) {
try {
Class.forName(interfaceName);
proxies.produce(new NativeImageProxyDefinitionBuildItem(interfaceName));
} catch (ClassNotFoundException e) {
//silent fail
}
}

@BuildStep
void seeAlso(CombinedIndexBuildItem combinedIndexBuildItem,
BuildProducer<ReflectiveClassBuildItem> reflectiveItems) {
IndexView index = combinedIndexBuildItem.getIndex();
for (AnnotationInstance xmlSeeAlsoAnn : index.getAnnotations(XML_SEE_ALSO)) {
AnnotationValue value = xmlSeeAlsoAnn.value();
Type[] types = value.asClassArray();
for (Type t : types) {
reflectiveItems.produce(new ReflectiveClassBuildItem(false, false, t.name().toString()));
}
}
}

void produceRecursiveProxies(IndexView index,
DotName interfaceDN,
BuildProducer<NativeImageProxyDefinitionBuildItem> proxies, Set<String> proxiesCreated) {
index.getKnownDirectImplementors(interfaceDN).stream()
.filter(classinfo -> Modifier.isInterface(classinfo.flags()))
.map(ClassInfo::name)
.forEach((className) -> {
if (!proxiesCreated.contains(className.toString())) {
proxies.produce(new NativeImageProxyDefinitionBuildItem(className.toString()));
produceRecursiveProxies(index, className, proxies, proxiesCreated);
proxiesCreated.add(className.toString());
}
});

}

@BuildStep
void addDependencies(BuildProducer<IndexDependencyBuildItem> indexDependency) {
indexDependency.produce(new IndexDependencyBuildItem("org.glassfish.jaxb", "txw2"));
indexDependency.produce(new IndexDependencyBuildItem("org.glassfish.jaxb", "jaxb-runtime"));
}

@BuildStep
void ignoreWarnings(BuildProducer<ReflectiveHierarchyIgnoreWarningBuildItem> ignoreWarningProducer) {
for (DotName type : IGNORE_TYPES) {
Expand Down Expand Up @@ -221,7 +286,8 @@ private void handleJaxbFile(Path p, BuildProducer<NativeImageResourceBuildItem>
BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
try {
String path = p.toAbsolutePath().toString().substring(1);
String pkg = p.toAbsolutePath().getParent().toString().substring(1).replace("/", ".") + ".";
String pkg = p.toAbsolutePath().getParent().toString().substring(1)
.replace(File.separator, ".") + ".";

resource.produce(new NativeImageResourceBuildItem(path));

Expand Down

0 comments on commit d182c44

Please sign in to comment.