Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate JAXB v4. #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target/
playground/
.project
.classpath
.settings/
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,10 @@ The plugin flow consists of the following parts:

There are many pitfalls in JAXB Code Model API which are forcing the developer to use dirty tricks (like accessing private fields) in order to implement the manipulation of code model. Among others:

* [JAXB-784](https://github.com/javaee/jaxb-v2/issues/784) is about NPE in `JAnnotationUse#getAnnotationMembers()` method.
* [JAXB-884](https://github.com/javaee/jaxb-v2/issues/884) is about ClassCastException in `JAnnotationArrayMember#annotations()` method.
* [JAXB-878](https://github.com/javaee/jaxb-v2/issues/878) and [JAXB-879](https://github.com/javaee/jaxb-v2/issues/879) describe the lack of public getters for class fields.
* [JAXB-957](https://github.com/javaee/jaxb-v2/issues/957) mentions what need to be added to make it possible for the inner class to be moved to another class or package.
* [JAXB-883](https://github.com/javaee/jaxb-v2/issues/883) does not allow to learn if "simpleMode" setting is enabled, which in its turn controls plural form for collection property names. There are however some more difficulties to overcome.
* [JAXB-1107](https://github.com/javaee/jaxb-v2/issues/1107) – marshalling of text nodes for mixed-mode contents
* [issue #957](https://github.com/eclipse-ee4j/jaxb-ri/issues/957) mentions what need to be added to make it possible for the inner class to be moved to another class or package.
* [issue #883](https://github.com/eclipse-ee4j/jaxb-ri/issues/883) does not allow to learn if "simpleMode" setting is enabled, which in its turn controls plural form for collection property names. There are however some more difficulties to overcome.
* [issue #1677](https://github.com/eclipse-ee4j/jaxb-ri/issues/1677) add/implement JVar.annotate(JAnnotationUse annotation).
* [issue #1679](https://github.com/eclipse-ee4j/jaxb-ri/issues/1679) add/implement JAnnotationUse.param(String name, JAnnotationValue annotationValue).

## Authors

Expand All @@ -612,6 +610,7 @@ Original code by [Bjarne Hansen](http://www.conspicio.dk/blog/bjarne/jaxb-xmlele
* [Colin Fairless](https://github.com/colin-yell/)
* [Patrick Crocker](https://github.com/patrickcrocker/)
* [Kermit The Frog](https://github.com/kermit-the-frog/)
* [Dominik Broj](https://github.com/thetric)
* and others...

## License
Expand Down
101 changes: 35 additions & 66 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
</licenses>

<properties>
<jaxb-api.version>3.0.1</jaxb-api.version>
<jaxb-core.version>3.0.1</jaxb-core.version>
<jaxb-api.version>4.0.0</jaxb-api.version>
<jaxb-core.version>4.0.1</jaxb-core.version>

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -113,60 +113,6 @@
</plugins>
</build>
</profile>
<profile>
<id>test-coverage</id>
<activation>
<jdk>[,9)</jdk>
</activation>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>xml</format>
<format>html</format>
</formats>
<check />
</configuration>
<executions>
<execution>
<id>cobertura-report</id>
<phase>verify</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- This profile publishes code coverage report generated by Cobertura on https://coveralls.io. -->
<profile>
<id>coveralls</id>

<build>
<plugins>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.4.1</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<developers>
Expand All @@ -189,12 +135,11 @@

<dependencyManagement>
<dependencies>
<!-- Remove this dependency management after https://github.com/highsource/jaxb2-basics/pull/119 is resolved -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.8.2</version>
<type>pom</type>
<scope>import</scope>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down Expand Up @@ -242,23 +187,24 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<version>3.24.2</version>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-assertj3</artifactId>
<version>2.9.0</version>
<version>2.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-jakarta-jaxb-impl</artifactId>
<version>2.9.0</version>
<version>2.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -290,5 +236,28 @@
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>create-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 1 addition & 1 deletion samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<packaging>pom</packaging>

<properties>
<jaxb-xew-plugin.version>2.0-SNAPSHOT</jaxb-xew-plugin.version>
<jaxb-xew-plugin.version>2.0</jaxb-xew-plugin.version>
<jakarta-api.version>3.0.1</jakarta-api.version>
<jaxb2-basics.version>3.0.0</jaxb2-basics.version>
<jaxb2-maven-plugin.version>3.1.0</jaxb2-maven-plugin.version>
Expand Down
80 changes: 33 additions & 47 deletions src/main/java/com/sun/tools/xjc/addon/xew/Candidate.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package com.sun.tools.xjc.addon.xew;

import static com.sun.tools.xjc.addon.xew.CommonUtils.generableToString;
import static com.sun.tools.xjc.addon.xew.CommonUtils.getAnnotation;
import static com.sun.tools.xjc.addon.xew.CommonUtils.getAnnotationMemberExpression;
import static com.sun.tools.xjc.addon.xew.CommonUtils.getAnnotationMemberValue;
import static com.sun.tools.xjc.addon.xew.CommonUtils.getXsdDeclaration;
import static com.sun.tools.xjc.addon.xew.CommonUtils.isHiddenClass;
import static com.sun.tools.xjc.addon.xew.XmlElementWrapperPlugin.FACTORY_CLASS_NAME;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.tools.xjc.model.CClassInfo;
Expand All @@ -28,46 +25,44 @@
* of objects.
*/
public final class Candidate {
private final JDefinedClass candidateClass;
private final JDefinedClass candidateClass;

private final JFieldVar field;
private final JFieldVar field;

private final CPropertyInfo fieldPropertyInfo;
private final CPropertyInfo fieldPropertyInfo;

private final String fieldTargetNamespace;
private final String fieldTargetNamespace;

private final JDefinedClass fieldParametrisationClass;

private final JDefinedClass fieldParametrisationImpl;
private final Collection<ParametrisationInfo> parametrisationInfos;

// Order matters (value Object Factory is first):
private final Map<String, JDefinedClass> objectFactoryClasses = new LinkedHashMap<>();
private final Map<String, JDefinedClass> objectFactoryClasses = new LinkedHashMap<>();

private final boolean valueObjectDisabled;
private final boolean valueObjectDisabled;

private final Map<String, ScopedElementInfo> scopedElementInfos = new HashMap<>();
// Order matters as it affects the order of generated methods in Object Factory:
private final Map<JMethod, ScopedMethodInfo> scopedFactoryMethods = new LinkedHashMap<>();

/**
* By default the candidate is marked for removal unless something prevents it from being removed.
*/
private boolean markedForRemoval = true;
private boolean markedForRemoval = true;

/**
* Number of times this candidate has been substituted in the model.
*/
private int substitutionsCount;
private int substitutionsCount;

Candidate(JDefinedClass candidateClass, CClassInfo candidateClassInfo, JFieldVar field,
JDefinedClass fieldParametrizationClass, JDefinedClass fieldParametrisationImpl,
JClass xmlElementDeclModelClass, JClass xmlSchemaModelClass) {
Collection<ParametrisationInfo> parametrisationInfos, JClass xmlElementDeclModelClass,
JClass xmlSchemaModelClass) {
this.candidateClass = candidateClass;
this.field = field;
this.fieldPropertyInfo = candidateClassInfo.getProperty(field.name());
this.fieldParametrisationClass = fieldParametrizationClass;
this.fieldParametrisationImpl = fieldParametrisationImpl;
this.parametrisationInfos = parametrisationInfos;
this.valueObjectDisabled = addObjectFactoryForClass(candidateClass);
this.fieldTargetNamespace = getTargetNamespace(candidateClassInfo, xmlSchemaModelClass);
collectScopedElementInfos(xmlElementDeclModelClass);
collectScopedFactoryMethods(xmlElementDeclModelClass);
}

private String getTargetNamespace(CClassInfo candidateClassInfo, JClass xmlSchemaModelClass) {
Expand All @@ -81,32 +76,32 @@ private String getTargetNamespace(CClassInfo candidateClassInfo, JClass xmlSchem
// see com.sun.tools.xjc.generator.bean.PackageOutlineImpl#calcDefaultValues()
for (JDefinedClass objectFactoryClass : objectFactoryClasses.values()) {
JAnnotationUse schemaAnnotation = getAnnotation(objectFactoryClass.getPackage(), xmlSchemaModelClass);
JExpression elementFormDefault = getAnnotationMemberExpression(schemaAnnotation, "elementFormDefault");
String elementFormDefault = getAnnotationMemberValue(schemaAnnotation, "elementFormDefault");

if (elementFormDefault != null && generableToString(elementFormDefault).endsWith(".QUALIFIED")) {
return generableToString(getAnnotationMemberExpression(schemaAnnotation, "namespace"));
if (elementFormDefault != null && elementFormDefault.endsWith(".QUALIFIED")) {
return getAnnotationMemberValue(schemaAnnotation, "namespace");
}
}
}

return null;
}

private void collectScopedElementInfos(JClass xmlElementDeclModelClass) {
private void collectScopedFactoryMethods(JClass xmlElementDeclModelClass) {
String dotClazz = candidateClass.fullName() + ".class";

// Only value Object Factory methods are inspected:
for (JMethod method : objectFactoryClasses.values().iterator().next().methods()) {
JAnnotationUse xmlElementDeclAnnotation = getAnnotation(method, xmlElementDeclModelClass);
JExpression scope = getAnnotationMemberExpression(xmlElementDeclAnnotation, "scope");
String scope = getAnnotationMemberValue(xmlElementDeclAnnotation, "scope");

if (scope == null || !dotClazz.equals(generableToString(scope))) {
if (scope == null || !dotClazz.equals(scope)) {
continue;
}

scopedElementInfos.put(method.name(),
new ScopedElementInfo(getAnnotationMemberExpression(xmlElementDeclAnnotation, "name"),
getAnnotationMemberExpression(xmlElementDeclAnnotation, "namespace"),
scopedFactoryMethods.put(method,
new ScopedMethodInfo(getAnnotationMemberValue(xmlElementDeclAnnotation, "name"),
getAnnotationMemberValue(xmlElementDeclAnnotation, "namespace"),
method.params().get(0).type()));
}
}
Expand Down Expand Up @@ -140,7 +135,7 @@ public String getFieldName() {
}

/**
* The class of the only field in container class (collection interface or concrete implementation).
* The class of the only field in container class (collection interface or particular implementation).
*/
public JClass getFieldClass() {
return (JClass) field.type();
Expand All @@ -161,29 +156,20 @@ public String getFieldTargetNamespace() {
}

/**
* The only parametrisation class of the field (collection type). In case of basic parametrisation like
* {@code List<String>} this property is {@code null}.
*/
public JDefinedClass getFieldParametrisationClass() {
return fieldParametrisationClass;
}

/**
* If {@link #getFieldParametrisationClass()} is an interface, then this holds the same value. Otherwise it holds
* the implementation (value object) of {@link #getFieldParametrisationClass()}. In case of basic parametrisation
* like {@code List<String>} this property is {@code null}.
* The list of parametrisation classes of the field (collection types). In case of basic parametrisation like
* {@code List<String>} this collection is empty.
*/
public JDefinedClass getFieldParametrisationImpl() {
return fieldParametrisationImpl;
public Collection<ParametrisationInfo> getParametrisationInfos() {
return parametrisationInfos;
}

/**
* Return information about scoped elements, that have this candidate as a scope.
* Return information about scoped methods that have this candidate as a scope.
*
* @return object factory method name -to- element info map
*/
public Map<String, ScopedElementInfo> getScopedElementInfos() {
return scopedElementInfos;
public Map<JMethod, ScopedMethodInfo> getScopedFactoryMethods() {
return scopedFactoryMethods;
}

/**
Expand Down
Loading