Make the functional static builder optional (#105)

Closes #100
This commit is contained in:
Jordan Zimmerman
2022-04-08 08:33:46 +01:00
committed by GitHub
parent 7b6ad4d7ba
commit 86093b6bad
4 changed files with 48 additions and 5 deletions

View File

@@ -246,6 +246,12 @@ public @interface RecordBuilder {
* name of that class. * name of that class.
*/ */
String fromWithClassName() default "_FromWith"; 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) @Retention(RetentionPolicy.CLASS)

View File

@@ -81,7 +81,9 @@ class InternalRecordBuilderProcessor {
addBeanNestedClass(); addBeanNestedClass();
} }
addDefaultConstructor(); addDefaultConstructor();
addStaticBuilder(); if (metaData.addStaticBuilder()) {
addStaticBuilder();
}
if (recordComponents.size() > 0) { if (recordComponents.size() > 0) {
addAllArgsConstructor(); addAllArgsConstructor();
} }

View File

@@ -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) {
}

View File

@@ -15,13 +15,14 @@
*/ */
package io.soabase.recordbuilder.test; package io.soabase.recordbuilder.test;
import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.stream.Stream;
public class TestCustomMethodNames { import static org.junit.jupiter.api.Assertions.*;
public class TestVariousOptions {
@Test @Test
public void builderGetsCustomSetterAndGetterNames() { public void builderGetsCustomSetterAndGetterNames() {
@@ -54,4 +55,15 @@ public class TestCustomMethodNames {
assertEquals(List.of(2), obj.getTheList()); assertEquals(List.of(2), obj.getTheList());
assertTrue(obj.isTheBoolean()); 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);
}
} }