Skip to content

Commit

Permalink
Make the functional static builder optional (#105)
Browse files Browse the repository at this point in the history
Closes #100
  • Loading branch information
Randgalt authored Apr 8, 2022
1 parent 7b6ad4d commit 86093b6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class InternalRecordBuilderProcessor {
addBeanNestedClass();
}
addDefaultConstructor();
addStaticBuilder();
if (metaData.addStaticBuilder()) {
addStaticBuilder();
}
if (recordComponents.size() > 0) {
addAllArgsConstructor();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 86093b6

Please sign in to comment.