diff --git a/jnosql-mapping/jnosql-mapping-reflection/src/main/java/org/eclipse/jnosql/mapping/reflection/DefaultConstructorBuilder.java b/jnosql-mapping/jnosql-mapping-reflection/src/main/java/org/eclipse/jnosql/mapping/reflection/DefaultConstructorBuilder.java index 724675ac3..aa8f4ebb1 100644 --- a/jnosql-mapping/jnosql-mapping-reflection/src/main/java/org/eclipse/jnosql/mapping/reflection/DefaultConstructorBuilder.java +++ b/jnosql-mapping/jnosql-mapping-reflection/src/main/java/org/eclipse/jnosql/mapping/reflection/DefaultConstructorBuilder.java @@ -52,7 +52,17 @@ public void add(Object value) { @Override public void addEmptyParameter() { - this.values.add(null); + Constructor constructor = ((DefaultConstructorMetadata) this.metadata).constructor(); + Class type = constructor.getParameterTypes()[this.values.size()]; + if(boolean.class.equals(type)) { + this.values.add(Boolean.FALSE); + } else if(char.class.equals(type)) { + this.values.add(Character.valueOf((char)0)); + } else if(type.isPrimitive()) { + this.values.add(Byte.valueOf((byte)0)); + } else { + this.values.add(null); + } } @Override diff --git a/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/ClassGraphClassScannerTest.java b/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/ClassGraphClassScannerTest.java index fbc9e8a85..16ddf7e74 100644 --- a/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/ClassGraphClassScannerTest.java +++ b/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/ClassGraphClassScannerTest.java @@ -40,7 +40,7 @@ class ClassGraphClassScannerTest { void shouldReturnEntities() { Set> entities = classScanner.entities(); Assertions.assertNotNull(entities); - assertThat(entities).hasSize(31) + assertThat(entities).hasSize(32) .contains(Person.class); } diff --git a/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/DefaultConstructorBuilderTest.java b/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/DefaultConstructorBuilderTest.java index 9b2297e8a..8866c1ad0 100644 --- a/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/DefaultConstructorBuilderTest.java +++ b/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/DefaultConstructorBuilderTest.java @@ -21,6 +21,7 @@ import org.eclipse.jnosql.mapping.metadata.ConstructorMetadata; import org.eclipse.jnosql.mapping.metadata.EntityMetadata; import org.eclipse.jnosql.mapping.reflection.entities.constructor.BookUser; +import org.eclipse.jnosql.mapping.reflection.entities.constructor.Counter; import org.jboss.weld.junit5.auto.AddPackages; import org.jboss.weld.junit5.auto.EnableAutoWeld; import org.junit.jupiter.api.BeforeEach; @@ -34,12 +35,16 @@ class DefaultConstructorBuilderTest { private ConstructorMetadata constructor; + private ConstructorMetadata counterConstructor; @BeforeEach void setUp(){ ClassConverter converter = new ReflectionClassConverter(); EntityMetadata entityMetadata = converter.apply(BookUser.class); this.constructor = entityMetadata.constructor(); + + EntityMetadata counterMetadata = converter.apply(Counter.class); + this.counterConstructor = counterMetadata.constructor(); } @Test @@ -99,4 +104,18 @@ void shouldEqualsHashCode(){ assertThat(builder).isEqualTo(other); assertThat(builder).hasSameHashCodeAs(other); } + + @Test + void shouldHandleEmptyPrimitives() { + ConstructorBuilder builder = DefaultConstructorBuilder.of(counterConstructor); + builder.addEmptyParameter(); + builder.addEmptyParameter(); + builder.addEmptyParameter(); + builder.addEmptyParameter(); + Counter counter = builder.build(); + assertThat(counter.count()).isEqualTo(0); + assertThat(counter.active()).isFalse(); + assertThat(counter.ratio()).isEqualTo(0d); + assertThat(counter.code()).isEqualTo((char)0); + } } diff --git a/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/entities/constructor/Counter.java b/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/entities/constructor/Counter.java new file mode 100644 index 000000000..f5bdd9a19 --- /dev/null +++ b/jnosql-mapping/jnosql-mapping-reflection/src/test/java/org/eclipse/jnosql/mapping/reflection/entities/constructor/Counter.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Contributors to the Eclipse Foundation + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. + * + * You may elect to redistribute this code under either of these licenses. + * + * Contributors: + * + * Jesse Gallagher + */ +package org.eclipse.jnosql.mapping.reflection.entities.constructor; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; + +@Entity +public record Counter(@Column int count, @Column boolean active, @Column double ratio, @Column char code) { + +}