Skip to content

Commit

Permalink
JAXB toString strategy for more succinct output
Browse files Browse the repository at this point in the history
Outputs only simple class names instead of fully qualified, and omits fields
set to null.

Enabled using the -XtoString argument, followed by
-XtoString-toStringStrategyClass=no.digipost.org.jvnet.jaxb.lang.SuccinctToStringStrategy
for JAXB 3.0+ (Jakarta)

or
-XtoString-toStringStrategyClass=no.digipost.org.jvnet.jaxb2_commons.lang.SuccinctToStringStrategy
for JAXB 2.x (javax)

https://github.com/highsource/jaxb-tools/wiki/JAXB2-Basic
https://github.com/highsource/jaxb-tools/wiki/JAXB2-ToString-Plugin
  • Loading branch information
runeflobakk committed Sep 11, 2024
1 parent eb5f2e7 commit 6462ae2
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
1 change: 1 addition & 0 deletions bind-jakarta/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/main/java/no/digipost/jakarta
src/main/java/no/digipost/javax
src/main/java/no/digipost/org/jvnet
6 changes: 6 additions & 0 deletions bind-jakarta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-plugins-runtime</artifactId>
<version>4.0.8</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 8 additions & 0 deletions bind-jakarta/rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ recipeList:
- org.openrewrite.java.ChangePackage:
oldPackageName: no.digipost.javax
newPackageName: no.digipost.jakarta
recursive: true
- org.openrewrite.java.ChangePackage:
oldPackageName: no.digipost.org.jvnet.jaxb2_commons.lang
newPackageName: no.digipost.org.jvnet.jaxb.lang
recursive: true
- org.openrewrite.java.ChangePackage:
oldPackageName: org.jvnet.jaxb2_commons
newPackageName: org.jvnet.jaxb
recursive: true
65 changes: 65 additions & 0 deletions bind-javax/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,70 @@
<artifactId>java-8-matchers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>2.0.14</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>no.digipost</groupId>
<artifactId>digg</artifactId>
<scope>test</scope>
</dependency>
</dependencies>



<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<version>2.0.14</version>
<configuration>
<extension>true</extension>
<strict>false</strict>
<schemaDirectory>src/test/resources/xsd</schemaDirectory>
<bindingDirectory>src/main/jaxb</bindingDirectory>
<enableIntrospection>true</enableIntrospection>
<generateDirectory>${project.build.directory}/generated-test-sources/xjc</generateDirectory>
<generatePackage>no.digipost.xml.alieninvasion</generatePackage>
<addCompileSourceRoot>false</addCompileSourceRoot>
<addTestCompileSourceRoot>true</addTestCompileSourceRoot>
<args>
<arg>-Xvalue-constructor</arg>
<arg>-Xsetters</arg>
<arg>-XsimpleEquals</arg>
<arg>-XsimpleHashCode</arg>
<arg>-XtoString</arg>
<arg>-XtoString-toStringStrategyClass=no.digipost.org.jvnet.jaxb2_commons.lang.SuccinctToStringStrategy</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>2.0.14</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package no.digipost.org.jvnet.jaxb2_commons.lang;

import org.jvnet.jaxb2_commons.lang.DefaultToStringStrategy;
import org.jvnet.jaxb2_commons.locator.ObjectLocator;

public class SuccinctToStringStrategy extends DefaultToStringStrategy {

@Override
public boolean isUseIdentityHashCode() {
return false;
}

@Override
public boolean isUseDefaultFieldValueMarkers() {
return false;
}

@Override
protected void appendClassName(StringBuilder toString, Object object) {
if (object != null) {
toString.append(object.getClass().getSimpleName());
}
}

@Override
public StringBuilder appendField(ObjectLocator parentLocator, Object parent, String fieldName, StringBuilder buffer, Object value, boolean valueSet) {
if (valueSet) {
return super.appendField(parentLocator, parent, fieldName, buffer, value, valueSet);
}
return buffer;
}

private static final SuccinctToStringStrategy INSTANCE = new SuccinctToStringStrategy();

public static SuccinctToStringStrategy getInstance() {
return INSTANCE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package no.digipost.xml.alieninvasion;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Stream;

import static java.lang.Integer.toHexString;
import static java.lang.reflect.Modifier.isStatic;
import static java.util.stream.Collectors.toList;
import static no.digipost.DiggExceptions.applyUnchecked;
import static no.digipost.xml.alieninvasion.DiplomaticAgreement.FOE;
import static no.digipost.xml.alieninvasion.DiplomaticAgreement.FRIEND;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertAll;
import static uk.co.probablyfine.matchers.Java8Matchers.where;

class SuccinctToStringStrategyTest {

@Test
void emptyObject() {
Alien alien = new Alien();
assertThat("toString outputs simple class name instead of fully qualified",
alien, where(Alien::toString, startsWith(Alien.class.getSimpleName())));
assertThat("toString outputs no key-value pairs for its properties",
alien, where(Alien::toString, not(containsString("="))));
}

@Test
void doesNotIncludehashCode() {
Alien alien = new Alien("Blarg", new Planet("Alpha Centauri b", "Centaurus"), FOE);
assertThat("toString does not include hashCode",
alien, where(Alien::toString, not(containsString(toHexString(alien.hashCode())))));
}

@Test
void onlyIncludesNonNullFields() {
Alien homelessAlien = new Alien("Blarg", null, FRIEND);
List<Field> alienFields = Stream.of(Alien.class.getDeclaredFields())
.filter(field -> !isStatic(field.getModifiers()))
.map(field -> {
field.setAccessible(true);
return field;
})
.collect(toList());

assertAll("toString includes all non-null field values", alienFields.stream()
.filter(field -> applyUnchecked(field::get, homelessAlien) != null)
.map(Field::getName)
.map(fieldName -> () -> assertThat(homelessAlien, where(Alien::toString, containsString(fieldName)))));

assertAll("toString excludes all null field values", alienFields.stream()
.filter(field -> applyUnchecked(field::get, homelessAlien) == null)
.map(Field::getName)
.map(fieldName -> () -> assertThat(homelessAlien, where(Alien::toString, not(containsString(fieldName))))));
}

}
29 changes: 29 additions & 0 deletions bind-javax/src/test/resources/xsd/my-schema.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" targetNamespace="https://digipost.no/digipost-xml-bind"
xmlns="https://digipost.no/digipost-xml-bind"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:complexType name="Alien">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1" />
<xsd:element name="home" type="Planet" minOccurs="1" maxOccurs="1" />
<xsd:element name="diplomatic-agreement" type="diplomatic-agreement" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Planet">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="constellation-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>

<xsd:simpleType name="diplomatic-agreement">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="friend" />
<xsd:enumeration value="foe" />
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<artifactId>java-8-matchers</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>no.digipost</groupId>
<artifactId>digg</artifactId>
<version>0.35</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 6462ae2

Please sign in to comment.