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 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 * 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; this.age = age;
return this; return this;
} }
@Override
public String toString() {
return "NameAndAgeBuilder[name=" + name + ", age=" + age + "]";
}
} }
``` ```

View File

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

View File

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

View File

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

View File

@@ -65,9 +65,9 @@ public class RecordBuilderProcessor extends AbstractProcessor {
addDefaultConstructor(builder); addDefaultConstructor(builder);
addAllArgsConstructor(builder, recordComponents); addAllArgsConstructor(builder, recordComponents);
addStaticDefaultBuilderMethod(builder, builderClassType, typeVariables, metaData); addStaticDefaultBuilderMethod(builder, builderClassType, typeVariables, metaData);
addStaticAllArgsBuilderMethod(builder, builderClassType, recordComponents, typeVariables, metaData);
addStaticCopyMethod(builder, builderClassType, recordClassType, recordComponents, typeVariables, metaData); addStaticCopyMethod(builder, builderClassType, recordClassType, recordComponents, typeVariables, metaData);
addBuildMethod(builder, recordClassType, recordComponents, metaData); addBuildMethod(builder, recordClassType, recordComponents, metaData);
addToStringMethod(builder, builderClassType, recordComponents);
recordComponents.forEach(component -> { recordComponents.forEach(component -> {
add1Field(builder, component); add1Field(builder, component);
add1SetterMethod(builder, component, builderClassType); add1SetterMethod(builder, component, builderClassType);
@@ -120,6 +120,33 @@ public class RecordBuilderProcessor extends AbstractProcessor {
builder.addMethod(constructorBuilder.build()); 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) { private void addBuildMethod(TypeSpec.Builder builder, ClassType recordClassType, List<ClassType> recordComponents, RecordBuilderMetaData metaData) {
/* /*
Adds the build method that generates the record similar to: Adds the build method that generates the record similar to:
@@ -174,33 +201,6 @@ public class RecordBuilderProcessor extends AbstractProcessor {
builder.addMethod(methodSpec); 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) { private void addStaticDefaultBuilderMethod(TypeSpec.Builder builder, ClassType builderClassType, List<TypeVariableName> typeVariables, RecordBuilderMetaData metaData) {
/* /*
Adds a the default builder method similar to: Adds a the default builder method similar to:

View File

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