Skip to content

Cc parent optionals fix #143

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

Merged
merged 5 commits into from
Dec 10, 2024
Merged
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
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>8</release>
<debug>true</debug>
<optimize>true</optimize>
<showDeprecation>true</showDeprecation>
Expand Down Expand Up @@ -370,6 +369,8 @@
<forceRegenerate>true</forceRegenerate>
<args>
<arg>-Ximm</arg>
<arg>-Ximm-builder</arg>
<arg>-Ximm-cc</arg>
<arg>-Ximm-optionalgetter</arg>
</args>
</configuration>
Expand Down
61 changes: 14 additions & 47 deletions src/main/java/com/github/sabomichal/immutablexjc/PluginImpl.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,6 @@
package com.github.sabomichal.immutablexjc;

import java.beans.Introspector;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JAnnotationValue;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JConditional;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JFormatter;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
import com.sun.codemodel.*;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.ClassOutline;
Expand All @@ -53,6 +12,15 @@
import org.apache.commons.lang3.reflect.FieldUtils;
import org.xml.sax.ErrorHandler;

import java.beans.Introspector;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
* IMMUTABLE-XJC plugin implementation.
*
Expand Down Expand Up @@ -213,9 +181,7 @@ public String getUsage() {
private void appendOption(StringBuilder retval, String option, String description, String n, int optionColumnWidth) {
retval.append(" ");
retval.append(option);
for (int i = option.length(); i < optionColumnWidth; i++) {
retval.append(' ');
}
retval.append(" ".repeat(Math.max(0, optionColumnWidth - option.length())));
retval.append(" : ");
retval.append(description);
retval.append(n);
Expand Down Expand Up @@ -345,7 +311,7 @@ private JMethod addBuildMethod(JDefinedClass clazz, JDefinedClass builderClass,
JBlock block = method.body();
JConditional conditional = block._if(field.eq(JExpr._null()));
conditional._then()._throw(JExpr._new(builderClass.owner().ref(NullPointerException.class))
.arg("Required field '" + field.name() + "' have to be assigned a value."));
.arg("Required field '" + field.name() + "' have to be assigned a value."));
}
constructorInvocation.arg(JExpr.ref(field.name()));
}
Expand Down Expand Up @@ -814,13 +780,14 @@ private JMethod generateCopyConstructor(final JDefinedClass clazz, final JDefine

for (JFieldVar field : superclassFields) {
String propertyName = field.name();
JType type = field.type();
JMethod getter = getGetterProperty(field, clazz);
if (isCollection(field)) {
JVar tmpVar = ctor.body().decl(0, getJavaType(field), "_" + propertyName, JExpr.invoke(o, getter));
JConditional conditional = ctor.body()._if(tmpVar.eq(JExpr._null()));
conditional._then().assign(JExpr.refthis(propertyName), getNewCollectionExpression(codeModel, getJavaType(field)));
conditional._else().assign(JExpr.refthis(propertyName), getDefensiveCopyExpression(codeModel, getJavaType(field), tmpVar));
} else if (optionalGetter && !isRequired(field)) {
ctor.body().assign(JExpr.refthis(propertyName), JExpr.invoke(o, getter).invoke("orElse").arg(JExpr._null()));
} else {
ctor.body().assign(JExpr.refthis(propertyName), JExpr.invoke(o, getter));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public void testMarshall() throws Exception {
by1.add(x1);
NameExpression y1 = new NameExpression("y");
by1.add(y1);
Declaration declaration1 = new Declaration(by1, "name", new HashMap<>(),"doc", "type");
Declaration declaration1 = new Declaration(by1, "name", "comment", new HashMap<>(),"doc", "type");
parameter.add(declaration1);

List<NameExpression> by2 = new ArrayList<>();
NameExpression x2 = new NameExpression("x");
by2.add(x2);
NameExpression y2 = new NameExpression("y");
by2.add(y2);
Declaration declaration2 = new Declaration(by2, "name", new HashMap<>(), "doc", "type");
Declaration declaration2 = new Declaration(by2, "name", "comment", new HashMap<>(), "doc", "type");
parameter.add(declaration2);

Parameters parameters = new Parameters(parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public void testCollectionsAreImmutable() {

@Test
public void testOptionalGetter() {
assertFalse(new com.github.sabomichal.immutablexjc.test.optionalgetter.Declaration(null, null, null, null, null)
assertFalse(new com.github.sabomichal.immutablexjc.test.optionalgetter.Declaration(null, null, null,null, null, null)
.getDocumentation()
.isPresent());

assertTrue(new com.github.sabomichal.immutablexjc.test.optionalgetter.Declaration(null, null, null, "documentation", null)
assertTrue(new com.github.sabomichal.immutablexjc.test.optionalgetter.Declaration(null, null, null, null, "documentation", null)
.getDocumentation()
.isPresent());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class MyDeclaration extends Declaration {
private final String myAdditionalElement;

public MyDeclaration(String myAdditionalElement) {
super(new ArrayList<>(), "name", new HashMap<>(), "doc", "type");
super(new ArrayList<>(), "name", "comment", new HashMap<>(), "doc", "type");
this.myAdditionalElement = myAdditionalElement;
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/xsd/abstract.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
<xs:element name="correlation_id" type="xs:string"/>
<xs:element name="session_id" type="xs:string"/>
<xs:element name="return_code" type="xs:string"/>
<xs:element name="min_version" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="body" type="body"/>
<xs:complexType name="body" abstract="true">
<xs:sequence/>
</xs:complexType>
</xs:schema>
</xs:schema>
1 change: 1 addition & 0 deletions src/test/xsd/basic.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<xs:element name="by" type="NameExpression" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="comment" type="xs:string" use="optional"/>
<xs:anyAttribute/>
</xs:complexType>
<xs:complexType name="NameExpression">
Expand Down
Loading