Compare commits

...

6 Commits

6 changed files with 38 additions and 40 deletions

View File

@@ -50,13 +50,6 @@ public class NameAndAgeBuilder {
return new NameAndAgeBuilder();
}
/**
* Return a new builder with all fields set to the given values
*/
public static NameAndAgeBuilder builder(String name, int age) {
return new NameAndAgeBuilder(name, age);
}
/**
* Return a new builder with all fields set to the values taken from the given record instance
*/
@@ -86,6 +79,11 @@ public class NameAndAgeBuilder {
this.age = age;
return this;
}
@Override
public String toString() {
return "NameAndAgeBuilder[name=" + name + ", age=" + age + "]";
}
}
```

View File

@@ -5,7 +5,7 @@
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder</artifactId>
<packaging>pom</packaging>
<version>1.0.ea</version>
<version>1.2.ea</version>
<modules>
<module>record-builder-core</module>
@@ -70,7 +70,7 @@
<url>https://github.com/randgalt/record-builder</url>
<connection>scm:git:https://github.com/randgalt/record-builder.git</connection>
<developerConnection>scm:git:git@github.com:randgalt/record-builder.git</developerConnection>
<tag>record-builder-1.0.ea</tag>
<tag>record-builder-1.2.ea</tag>
</scm>
<issueManagement>

View File

@@ -3,7 +3,7 @@
<parent>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder</artifactId>
<version>1.0.ea</version>
<version>1.2.ea</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -3,7 +3,7 @@
<parent>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder</artifactId>
<version>1.0.ea</version>
<version>1.2.ea</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@@ -65,9 +65,9 @@ public class RecordBuilderProcessor extends AbstractProcessor {
addDefaultConstructor(builder);
addAllArgsConstructor(builder, recordComponents);
addStaticDefaultBuilderMethod(builder, builderClassType, typeVariables, metaData);
addStaticAllArgsBuilderMethod(builder, builderClassType, recordComponents, typeVariables, metaData);
addStaticCopyMethod(builder, builderClassType, recordClassType, recordComponents, typeVariables, metaData);
addBuildMethod(builder, recordClassType, recordComponents, metaData);
addToStringMethod(builder, builderClassType, recordComponents);
recordComponents.forEach(component -> {
add1Field(builder, component);
add1SetterMethod(builder, component, builderClassType);
@@ -120,6 +120,33 @@ public class RecordBuilderProcessor extends AbstractProcessor {
builder.addMethod(constructorBuilder.build());
}
private void addToStringMethod(TypeSpec.Builder builder, ClassType builderClassType, List<ClassType> recordComponents) {
/*
add a toString() method similar to:
public String toString() {
return "MyRecord[p1=blah, p2=blah]";
}
*/
var codeBuilder = CodeBlock.builder().add("return \"$L[", builderClassType.name());
IntStream.range(0, recordComponents.size()).forEach(index -> {
if (index > 0) {
codeBuilder.add(", ");
}
String name = recordComponents.get(index).name();
codeBuilder.add("$L=\" + $L + \"", name, name);
});
codeBuilder.add("]\"");
var methodSpec = MethodSpec.methodBuilder("toString")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.returns(String.class)
.addStatement(codeBuilder.build())
.build();
builder.addMethod(methodSpec);
}
private void addBuildMethod(TypeSpec.Builder builder, ClassType recordClassType, List<ClassType> recordComponents, RecordBuilderMetaData metaData) {
/*
Adds the build method that generates the record similar to:
@@ -174,33 +201,6 @@ public class RecordBuilderProcessor extends AbstractProcessor {
builder.addMethod(methodSpec);
}
private void addStaticAllArgsBuilderMethod(TypeSpec.Builder builder, ClassType builderClassType, List<ClassType> recordComponents, List<TypeVariableName> typeVariables, RecordBuilderMetaData metaData) {
/*
Adds an all-args builder method that pre-fills the builder with values similar to:
public static MyRecordBuilder builder(int p1, T p2, ...) {
return new MyRecordBuilder(p1, p2, ...);
}
*/
var methodSpecBuilder = MethodSpec.methodBuilder(metaData.builderMethodName())
.addJavadoc("Return a new builder with all fields set to the given values\n")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addTypeVariables(typeVariables)
.returns(builderClassType.typeName());
var codeBuilder = CodeBlock.builder().add("return new $T(", builderClassType.typeName());
IntStream.range(0, recordComponents.size()).forEach(index -> {
ClassType component = recordComponents.get(index);
methodSpecBuilder.addParameter(component.typeName(), component.name());
if (index > 0) {
codeBuilder.add(", ");
}
codeBuilder.add("$L", component.name());
});
codeBuilder.add(")");
methodSpecBuilder.addStatement(codeBuilder.build());
builder.addMethod(methodSpecBuilder.build());
}
private void addStaticDefaultBuilderMethod(TypeSpec.Builder builder, ClassType builderClassType, List<TypeVariableName> typeVariables, RecordBuilderMetaData metaData) {
/*
Adds a the default builder method similar to:

View File

@@ -3,7 +3,7 @@
<parent>
<groupId>io.soabase.record-builder</groupId>
<artifactId>record-builder</artifactId>
<version>1.0.ea</version>
<version>1.2.ea</version>
</parent>
<modelVersion>4.0.0</modelVersion>