diff --git a/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java b/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java index 372e05c2..952cf251 100644 --- a/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java +++ b/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java @@ -246,6 +246,12 @@ * name of that class. */ String fromWithClassName() default "_FromWith"; + + /** + * If true, a functional-style builder is added so that record instances can be instantiated + * without {@code new}. + */ + boolean addStaticBuilder() default true; } @Retention(RetentionPolicy.CLASS) 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 fe977198..75e29041 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 @@ -81,7 +81,9 @@ class InternalRecordBuilderProcessor { addBeanNestedClass(); } addDefaultConstructor(); - addStaticBuilder(); + if (metaData.addStaticBuilder()) { + addStaticBuilder(); + } if (recordComponents.size() > 0) { addAllArgsConstructor(); } diff --git a/record-builder-test/src/main/java/io/soabase/recordbuilder/test/NoStaticBuilder.java b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/NoStaticBuilder.java new file mode 100644 index 00000000..1e422173 --- /dev/null +++ b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/NoStaticBuilder.java @@ -0,0 +1,23 @@ +package io.soabase.recordbuilder.test; + +import io.soabase.recordbuilder.core.RecordBuilder; + +/** + * Copyright 2019 Jordan Zimmerman + * + * 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. + */ +@RecordBuilder.Options(addStaticBuilder = false) +@RecordBuilder +public record NoStaticBuilder(String foo) { +} diff --git a/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestCustomMethodNames.java b/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestVariousOptions.java similarity index 75% rename from record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestCustomMethodNames.java rename to record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestVariousOptions.java index b3cbe919..0ba16fd8 100644 --- a/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestCustomMethodNames.java +++ b/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestVariousOptions.java @@ -15,13 +15,14 @@ */ package io.soabase.recordbuilder.test; -import java.util.List; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.List; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; -public class TestCustomMethodNames { +public class TestVariousOptions { @Test public void builderGetsCustomSetterAndGetterNames() { @@ -54,4 +55,15 @@ public void recordHasPrefixedGetters() { assertEquals(List.of(2), obj.getTheList()); assertTrue(obj.isTheBoolean()); } + + @Test + public void noStaticBuilder() { + boolean hasStaticBuilder = Stream.of(NoStaticBuilderBuilder.class.getDeclaredMethods()) + .anyMatch(method -> method.getName().equals("NoStaticBuilder")); + assertFalse(hasStaticBuilder); + + hasStaticBuilder = Stream.of(SimpleRecordBuilder.class.getDeclaredMethods()) + .anyMatch(method -> method.getName().equals("SimpleRecord")); + assertTrue(hasStaticBuilder); + } }