diff --git a/record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/InternalRecordBuilderProcessor.java b/record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/InternalRecordBuilderProcessor.java index f47ebaf1..a52d5a3a 100644 --- a/record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/InternalRecordBuilderProcessor.java +++ b/record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/InternalRecordBuilderProcessor.java @@ -196,10 +196,6 @@ private void addOnceOnlySupport() { } private void addStagedBuilderClasses() { - if (recordComponents.size() < 2) { - return; - } - IntStream.range(0, recordComponents.size()).forEach(index -> { Optional nextComponent = ((index + 1) < recordComponents.size()) ? Optional.of(recordComponents.get(index + 1)) : Optional.empty(); @@ -723,10 +719,6 @@ private void addStaticDefaultBuilderMethod() { } private void addStaticStagedBuilderMethod(String builderMethodName) { - if (recordComponents.size() < 2) { - return; - } - /* * Adds the staged builder method similar to: * @@ -757,11 +749,12 @@ private void addStaticStagedBuilderMethod(String builderMethodName) { codeBlock.addStatement(")"); } + var returnType = stagedBuilderType(recordComponents.isEmpty() ? builderClassType : recordComponents.get(0)); + var methodSpec = MethodSpec.methodBuilder(builderMethodName) .addJavadoc("Return the first stage of a staged builder\n") .addModifiers(Modifier.PUBLIC, Modifier.STATIC).addAnnotation(generatedRecordBuilderAnnotation) - .addTypeVariables(typeVariables).returns(stagedBuilderType(recordComponents.get(0)).typeName()) - .addCode(codeBlock.build()).build(); + .addTypeVariables(typeVariables).returns(returnType.typeName()).addCode(codeBlock.build()).build(); builder.addMethod(methodSpec); } diff --git a/record-builder-test/src/main/java/io/soabase/recordbuilder/test/staged/NoFieldsStaged.java b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/staged/NoFieldsStaged.java new file mode 100644 index 00000000..64fe0313 --- /dev/null +++ b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/staged/NoFieldsStaged.java @@ -0,0 +1,23 @@ +/* + * Copyright 2019 The original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.soabase.recordbuilder.test.staged; + +import io.soabase.recordbuilder.core.RecordBuilder; + +@RecordBuilder +@RecordBuilder.Options(builderMode = RecordBuilder.BuilderMode.STAGED) +public record NoFieldsStaged() { +} diff --git a/record-builder-test/src/main/java/io/soabase/recordbuilder/test/staged/SingleFieldStaged.java b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/staged/SingleFieldStaged.java new file mode 100644 index 00000000..6a173fb7 --- /dev/null +++ b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/staged/SingleFieldStaged.java @@ -0,0 +1,25 @@ +/* + * Copyright 2019 The original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.soabase.recordbuilder.test.staged; + +import io.soabase.recordbuilder.core.RecordBuilder; + +import java.time.Instant; + +@RecordBuilder +@RecordBuilder.Options(builderMode = RecordBuilder.BuilderMode.STAGED) +public record SingleFieldStaged(int i) { +} diff --git a/record-builder-test/src/test/java/io/soabase/recordbuilder/test/staged/TestStagedBuilder.java b/record-builder-test/src/test/java/io/soabase/recordbuilder/test/staged/TestStagedBuilder.java index 7554ae27..9b5ea6c2 100644 --- a/record-builder-test/src/test/java/io/soabase/recordbuilder/test/staged/TestStagedBuilder.java +++ b/record-builder-test/src/test/java/io/soabase/recordbuilder/test/staged/TestStagedBuilder.java @@ -61,4 +61,16 @@ void testGenericCombined() { .aT(new GenericStaged<>("other", builder.build(), BigInteger.TEN)).theUThing(BigDecimal.ONE).build(); assertEquals(obj1, obj2); } + + @Test + void testSingleField() { + SingleFieldStaged obj = SingleFieldStagedBuilder.builder().i(1).build(); + assertEquals(new SingleFieldStaged(1), obj); + } + + @Test + void testNoFields() { + NoFieldsStaged obj = NoFieldsStagedBuilder.builder().build(); + assertEquals(new NoFieldsStaged(), obj); + } }