Skip to content

Commit

Permalink
Option to make builder ctor public
Browse files Browse the repository at this point in the history
`@RecordBuilder.Options(publicBuilderConstructors = true)`

Closes #157
Replaces #128
  • Loading branch information
Randgalt committed Dec 14, 2023
1 parent e7b7963 commit 2a9361c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@
* make the builder public when the record is package protect.
*/
Modifier[] builderClassModifiers() default {};

/**
* Makes the generated builder's constructors public
*/
boolean publicBuilderConstructors() default false;
}

@Retention(RetentionPolicy.CLASS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class InternalRecordBuilderProcessor {
"RecordBuilderValidator");
private static final TypeVariableName rType = TypeVariableName.get("R");
private final ProcessingEnvironment processingEnv;
private final boolean publicBuilderConstructors;

InternalRecordBuilderProcessor(ProcessingEnvironment processingEnv, TypeElement record,
RecordBuilder.Options metaData, Optional<String> packageNameOpt) {
Expand All @@ -70,6 +71,7 @@ class InternalRecordBuilderProcessor {
uniqueVarName = getUniqueVarName();
notNullPattern = Pattern.compile(metaData.interpretNotNullsPattern());
collectionBuilderUtils = new CollectionBuilderUtils(recordComponents, this.metaData);
publicBuilderConstructors = metaData.publicBuilderConstructors();

builder = TypeSpec.classBuilder(builderClassType.name()).addAnnotation(generatedRecordBuilderAnnotation)
.addModifiers(metaData.builderClassModifiers()).addTypeVariables(typeVariables);
Expand Down Expand Up @@ -317,13 +319,17 @@ private void addComponentCallsAsArguments(int index, CodeBlock.Builder codeBlock
});
}

private Modifier constructorVisibilityModifier() {
return publicBuilderConstructors ? Modifier.PUBLIC : Modifier.PRIVATE;
}

private void addDefaultConstructor() {
/*
* Adds a default constructor similar to:
*
* private MyRecordBuilder() { }
*/
var constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE)
var constructor = MethodSpec.constructorBuilder().addModifiers(constructorVisibilityModifier())
.addAnnotation(generatedRecordBuilderAnnotation).build();
builder.addMethod(constructor);
}
Expand Down Expand Up @@ -378,7 +384,7 @@ private void addAllArgsConstructor() {
*
* private MyRecordBuilder(int p1, T p2, ...) { this.p1 = p1; this.p2 = p2; ... }
*/
var constructorBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE)
var constructorBuilder = MethodSpec.constructorBuilder().addModifiers(constructorVisibilityModifier())
.addAnnotation(generatedRecordBuilderAnnotation);
recordComponents.forEach(component -> {
constructorBuilder.addParameter(component.typeName(), component.name());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

import io.soabase.recordbuilder.core.RecordBuilder;

@RecordBuilder
@RecordBuilder.Options(publicBuilderConstructors = true)
public record PublicConstructor(int i, String s) {
}

0 comments on commit 2a9361c

Please sign in to comment.