diff --git a/src/main/java/com/github/sabomichal/immutablexjc/PluginImpl.java b/src/main/java/com/github/sabomichal/immutablexjc/PluginImpl.java index 3e030a4..a6b2b79 100644 --- a/src/main/java/com/github/sabomichal/immutablexjc/PluginImpl.java +++ b/src/main/java/com/github/sabomichal/immutablexjc/PluginImpl.java @@ -341,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)) @@ -939,6 +939,10 @@ 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; } diff --git a/src/test/java/com/github/sabomichal/immutablexjc/test/TestMiscOptions.java b/src/test/java/com/github/sabomichal/immutablexjc/test/TestMiscOptions.java index aafe28c..6d6687a 100644 --- a/src/test/java/com/github/sabomichal/immutablexjc/test/TestMiscOptions.java +++ b/src/test/java/com/github/sabomichal/immutablexjc/test/TestMiscOptions.java @@ -4,6 +4,8 @@ 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; @@ -103,9 +105,13 @@ public void testOptionalGetter() { .getDocumentation() .isPresent()); - com.github.sabomichal.immutablexjc.test.optionalgetter.DecimalExtensionType type = new com.github.sabomichal.immutablexjc.test.optionalgetter.DecimalExtensionType(BigDecimal.valueOf(1), "s"); + 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()); } } diff --git a/src/test/xsd/basic.xsd b/src/test/xsd/basic.xsd index cd76ce4..e9f620b 100644 --- a/src/test/xsd/basic.xsd +++ b/src/test/xsd/basic.xsd @@ -51,4 +51,9 @@ + + + + + \ No newline at end of file