Skip to content

Commit

Permalink
Enhance exclude-classes property to exclude packages from the default…
Browse files Browse the repository at this point in the history
… JAXBContext
  • Loading branch information
gnieser committed Mar 17, 2023
1 parent d08d31f commit 2ab2cf5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import io.quarkus.builder.item.SimpleBuildItem;
Expand Down Expand Up @@ -33,11 +34,20 @@ public List<Class<?>> getClasses() {

public static class Builder {
private final Set<String> classNames = new LinkedHashSet<>();
private final Set<String> classNameExcludes = new LinkedHashSet<>();
private final Set<Predicate<String>> classNamePredicateExcludes = new LinkedHashSet<>();

public Builder classNameExcludes(Collection<String> classNameExcludes) {
for (String className : classNameExcludes) {
this.classNameExcludes.add(className);
final String packMatch = ".*";

for (String classNameExclude : classNameExcludes) {
if (classNameExclude.endsWith(packMatch)) {
// Package ends with ".*"
final String packageName = classNameExclude.substring(0, classNameExclude.length() - packMatch.length());
this.classNamePredicateExcludes.add((className) -> className.startsWith(packageName));
} else {
// Standard class name
this.classNamePredicateExcludes.add(classNameExclude::equals);
}
}
return this;
}
Expand All @@ -51,7 +61,7 @@ public Builder classNames(Collection<String> classNames) {

public FilteredJaxbClassesToBeBoundBuildItem build() {
final List<Class<?>> classes = classNames.stream()
.filter(className -> !this.classNameExcludes.contains(className))
.filter(className -> this.classNamePredicateExcludes.stream().noneMatch(p -> p.test(className)))
.map(FilteredJaxbClassesToBeBoundBuildItem::getClassByName)
.filter(JaxbType::isValidType)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.quarkus.jaxb.deployment;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.StringWriter;

import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public abstract class AbstractJaxbContextTest {

@Inject
JAXBContext jaxbContext;

@Inject
Marshaller marshaller;

@Inject
Unmarshaller unmarshaller;

@Test
@ActivateRequestContext
public void shouldInjectJaxbBeans() {
assertThat(jaxbContext).isNotNull();
assertThat(marshaller).isNotNull();
assertThat(unmarshaller).isNotNull();
}

@Test
@ActivateRequestContext
public void marshalModelOne() throws JAXBException {
io.quarkus.jaxb.deployment.one.Model model = new io.quarkus.jaxb.deployment.one.Model();
model.setName1("name1");

StringWriter sw = new StringWriter();
marshaller.marshal(model, sw);

assertThat(sw.toString()).isEqualTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<model><name1>name1</name1></model>");
}

@Test
@ActivateRequestContext
public void marshalModelTwo() throws JAXBException {
io.quarkus.jaxb.deployment.two.Model model = new io.quarkus.jaxb.deployment.two.Model();
model.setName2("name2");
Assertions.assertThatExceptionOfType(JAXBException.class)
.isThrownBy(() -> marshaller.marshal(model, new StringWriter()))
.withMessage("class io.quarkus.jaxb.deployment.two.Model nor any of its super class is known to this context.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.jaxb.deployment;

import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class ClassExcludeTest extends AbstractJaxbContextTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(
io.quarkus.jaxb.deployment.one.Model.class,
io.quarkus.jaxb.deployment.two.Model.class)
.addPackage("io.quarkus.jaxb.deployment.info"))
.overrideConfigKey("quarkus.jaxb.exclude-classes", "io.quarkus.jaxb.deployment.two.Model");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.jaxb.deployment;

import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class PackageExcludeTest extends AbstractJaxbContextTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(
io.quarkus.jaxb.deployment.one.Model.class,
io.quarkus.jaxb.deployment.two.Model.class)
.addPackage("io.quarkus.jaxb.deployment.info"))
.overrideConfigKey("quarkus.jaxb.exclude-classes", "io.quarkus.jaxb.deployment.two.*");
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class JaxbConfig {

/**
* Exclude classes to automatically be bound to the default JAXB context.
* Values with suffix {@code .*}, i.e. {@code org.acme.*}, are considered packages and exclude all classes that are members
* of these packages
*/
@ConfigItem
public Optional<List<String>> excludeClasses;
Expand Down

0 comments on commit 2ab2cf5

Please sign in to comment.