Skip to content

Commit

Permalink
Merge pull request #125 from RoanH/requiredvalues
Browse files Browse the repository at this point in the history
XML values are always required
  • Loading branch information
sabomichal authored Apr 12, 2024
2 parents 678a051 + 830a0c6 commit 3a96ffa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/com/github/sabomichal/immutablexjc/PluginImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.sun.tools.xjc.outline.Outline;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlValue;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.xml.sax.ErrorHandler;
Expand Down Expand Up @@ -340,7 +341,7 @@ private JMethod addBuildMethod(JDefinedClass clazz, JDefinedClass builderClass,
}
for (JFieldVar field : declaredFields) {
if (mustAssign(field)) {
if (isRequired(field)) {
if (isRequired(field) && !field.type().isPrimitive()) {
JBlock block = method.body();
JConditional conditional = block._if(field.eq(JExpr._null()));
conditional._then()._throw(JExpr._new(builderClass.owner().ref(NullPointerException.class))
Expand Down Expand Up @@ -938,6 +939,14 @@ private boolean isMap(JClass clazz) {
}

private boolean isRequired(JFieldVar field) {
if (field.type().isPrimitive()) {
return true;
}

if (getAnnotation(field.annotations(), XmlValue.class.getCanonicalName()).isPresent()) {
return true;
}

return Stream.of(XmlElement.class, XmlAttribute.class)
.map(annotationType ->
getAnnotation(field.annotations(), annotationType.getCanonicalName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import com.github.sabomichal.immutablexjc.test.misc.Model;
import com.github.sabomichal.immutablexjc.test.misc.NameExpression;
import com.github.sabomichal.immutablexjc.test.misc.Parameters;
import com.github.sabomichal.immutablexjc.test.optionalgetter.DecimalExtensionType;
import com.github.sabomichal.immutablexjc.test.optionalgetter.NoOptionalForPrimitive;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Marshaller;
import org.junit.jupiter.api.Test;


import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.*;


Expand Down Expand Up @@ -100,5 +104,14 @@ public void testOptionalGetter() {
assertTrue(new com.github.sabomichal.immutablexjc.test.optionalgetter.Declaration(null, null, null, "documentation", null)
.getDocumentation()
.isPresent());

DecimalExtensionType type = new DecimalExtensionType(BigDecimal.valueOf(1), "s");
assertNotNull(type.getValue());
assertEquals(BigDecimal.class, type.getValue().getClass());
assertTrue(type.getUnit().isPresent());

NoOptionalForPrimitive primitive = new NoOptionalForPrimitive(1);
assertNotNull(primitive.getIndex());
assertEquals(1, primitive.getIndex());
}
}
19 changes: 19 additions & 0 deletions src/test/xsd/basic.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,23 @@
<xs:complexType name="NameExpression">
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
<xs:element name="DecimalExtensionType">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="BaseType">
<xs:attribute name="unit" fixed="s"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name="BaseType">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="3"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="NoOptionalForPrimitive">
<xs:sequence>
<xs:element name="index" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

0 comments on commit 3a96ffa

Please sign in to comment.