With interface shouldn't use prefix option (#132)

This commit is contained in:
Jordan Zimmerman
2023-03-29 10:03:17 +01:00
committed by GitHub
parent 183ab67c1a
commit 141362845e
5 changed files with 25 additions and 19 deletions

View File

@@ -63,7 +63,7 @@ public @interface RecordBuilder {
}
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE, ElementType.PACKAGE})
@Target({ ElementType.TYPE, ElementType.PACKAGE })
@Inherited
@interface Options {
/**

View File

@@ -173,8 +173,7 @@ class InternalRecordBuilderProcessor {
if (metaData.addClassRetainedGenerated()) {
classBuilder.addAnnotation(recordBuilderGeneratedAnnotation);
}
recordComponents
.forEach(component -> addNestedGetterMethod(classBuilder, component, prefixedName(component, true)));
recordComponents.forEach(component -> addNestedGetterMethod(classBuilder, component, component.name()));
addWithBuilderMethod(classBuilder);
addWithSuppliedBuilderMethod(classBuilder);
IntStream.range(0, recordComponents.size())
@@ -235,7 +234,7 @@ class InternalRecordBuilderProcessor {
*/
var codeBlockBuilder = CodeBlock.builder().add("return new $L$L(", builderClassType.name(),
typeVariables.isEmpty() ? "" : "<>");
addComponentCallsAsArguments(-1, codeBlockBuilder);
addComponentCallsAsArguments(-1, codeBlockBuilder, false);
codeBlockBuilder.add(");");
var methodSpec = MethodSpec.methodBuilder(metaData.withClassMethodPrefix())
.addAnnotation(generatedRecordBuilderAnnotation)
@@ -268,7 +267,7 @@ class InternalRecordBuilderProcessor {
codeBlockBuilder.add("$T.validate(", validatorTypeName);
}
codeBlockBuilder.add("new $T(", recordClassType.typeName());
addComponentCallsAsArguments(index, codeBlockBuilder);
addComponentCallsAsArguments(index, codeBlockBuilder, false);
codeBlockBuilder.add(")");
if (metaData.useValidationApi()) {
codeBlockBuilder.add(")");
@@ -303,7 +302,7 @@ class InternalRecordBuilderProcessor {
classBuilder.addMethod(methodSpec);
}
private void addComponentCallsAsArguments(int index, CodeBlock.Builder codeBlockBuilder) {
private void addComponentCallsAsArguments(int index, CodeBlock.Builder codeBlockBuilder, boolean usePrefixedName) {
IntStream.range(0, recordComponents.size()).forEach(parameterIndex -> {
if (parameterIndex > 0) {
codeBlockBuilder.add(", ");
@@ -312,7 +311,8 @@ class InternalRecordBuilderProcessor {
if (parameterIndex == index) {
collectionBuilderUtils.addShimCall(codeBlockBuilder, parameterComponent);
} else {
codeBlockBuilder.add("$L()", prefixedName(parameterComponent, true));
codeBlockBuilder.add("$L()",
usePrefixedName ? prefixedName(parameterComponent, true) : parameterComponent.name());
}
});
}
@@ -546,8 +546,8 @@ class InternalRecordBuilderProcessor {
IntStream.range(0, recordComponents.size()).forEach(index -> {
var component = recordComponents.get(index);
MethodSpec methodSpec = MethodSpec.methodBuilder(prefixedName(component, true))
.returns(component.typeName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC)
MethodSpec methodSpec = MethodSpec.methodBuilder(component.name()).returns(component.typeName())
.addAnnotation(Override.class).addModifiers(Modifier.PUBLIC)
.addStatement("return from.$L()", component.name()).addAnnotation(generatedRecordBuilderAnnotation)
.build();
fromWithClassBuilder.addMethod(methodSpec);
@@ -914,7 +914,7 @@ class InternalRecordBuilderProcessor {
methodBuilder.addJavadoc("Perform an operation on record components");
}
codeBlockBuilder.add("proc.apply(");
addComponentCallsAsArguments(-1, codeBlockBuilder);
addComponentCallsAsArguments(-1, codeBlockBuilder, true);
codeBlockBuilder.add(");");
methodBuilder.addCode(codeBlockBuilder.build());
return methodBuilder.build();

View File

@@ -16,10 +16,13 @@
package io.soabase.recordbuilder.test;
import java.util.List;
import java.util.Map;
import io.soabase.recordbuilder.core.RecordBuilder;
import io.soabase.recordbuilder.test.CustomMethodNamesBuilder.Bean;
@RecordBuilder
@RecordBuilder.Options(setterPrefix = "set", getterPrefix = "get", booleanPrefix = "is", beanClassName = "Bean")
public record CustomMethodNames(int theValue, List<Integer> theList, boolean theBoolean) implements Bean {
public record CustomMethodNames<K, V>(Map<K, V> kvMap, int theValue, List<Integer> theList, boolean theBoolean)
implements Bean, CustomMethodNamesBuilder.With {
}

View File

@@ -25,7 +25,8 @@ import org.junit.jupiter.api.Test;
class TestOptionsOnPackage {
@Test
void testOptionsOnInclude() throws IOException {
String text= Files.readString(Path.of("target/generated-sources/annotations/io/soabase/recordbuilder/test/foo/PairBuilder.java"));
Assertions.assertTrue(text.contains("// MyLicense - Auto generated"));
String text = Files.readString(
Path.of("target/generated-sources/annotations/io/soabase/recordbuilder/test/foo/PairBuilder.java"));
Assertions.assertTrue(text.contains("// MyLicense - Auto generated"));
}
}

View File

@@ -18,6 +18,7 @@ package io.soabase.recordbuilder.test;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
@@ -26,25 +27,26 @@ public class TestVariousOptions {
@Test
public void builderGetsCustomSetterAndGetterNames() {
var obj = CustomMethodNamesBuilder.builder().setTheValue(1).setTheList(List.of(2)).setTheBoolean(true);
var obj = CustomMethodNamesBuilder.builder().setKvMap(Map.of(1, "one")).setTheValue(1).setTheList(List.of(2))
.setTheBoolean(true);
assertEquals(1, obj.getTheValue());
assertEquals(List.of(2), obj.getTheList());
assertTrue(obj.isTheBoolean());
assertEquals(new CustomMethodNames(1, List.of(2), true), obj.build());
assertEquals(new CustomMethodNames<>(Map.of(1, "one"), 1, List.of(2), true), obj.build());
}
@Test
public void withBuilderGetsCustomSetterAndGetterNames() {
var obj = CustomMethodNamesBuilder.from(
CustomMethodNamesBuilder.builder().setTheValue(1).setTheList(List.of(2)).setTheBoolean(true).build());
assertEquals(1, obj.getTheValue());
assertEquals(List.of(2), obj.getTheList());
assertTrue(obj.isTheBoolean());
assertEquals(1, obj.theValue());
assertEquals(List.of(2), obj.theList());
assertTrue(obj.theBoolean());
}
@Test
public void recordHasPrefixedGetters() {
var obj = new CustomMethodNames(1, List.of(2), true);
var obj = new CustomMethodNames<>(Map.of(1, "one"), 1, List.of(2), true);
assertEquals(1, obj.getTheValue());
assertEquals(List.of(2), obj.getTheList());
assertTrue(obj.isTheBoolean());