Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions options.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,21 @@ The names used for generated methods, classes, etc. can be changed via the follo

## Miscellaneous

| option | details |
|----------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `@RecordBuilder.Options(inheritComponentAnnotations = true/false)` | If true, any annotations (if applicable) on record components are copied to the builder methods. The default is `true`. |
| `@RecordBuilder.Options(publicBuilderConstructors = true/false)` | Makes the generated builder's constructors public. The default is `false`. |
| `@RecordBuilder.Options(builderClassModifiers = {}})` | Any additional `javax.lang.model.element.Modifier` you wish to apply to the builder. |
| `@RecordBuilder.Options(beanClassName = "Foo")` | If set, the Builder will contain an internal interface with this name. |
| `@RecordBuilder.Options(addClassRetainedGenerated = true/false)` | If true, generated classes are annotated with `RecordBuilderGenerated`. The default is `false`. |
| `@RecordBuilder.Options(addStaticBuilder = true/false)` | If true, a functional-style builder is added so that record instances can be instantiated without `new()`. The default is `true`. |
| `@RecordBuilder.Options(inheritComponentAnnotations = true/false)` | If true, any annotations (if applicable) on record components are copied to the builder methods. The default is `true`. |
| `@RecordBuilder.Options(addConcreteSettersForOptional = <mode>)` | Add non-optional setter methods for optional record components. The default is `ConcreteSettersForOptionalMode.DISABLED`. |
| `@RecordBuilder.Options(nullableAnnotationClass = "com.foo.Nullable")` | Nullability annotation to use when RecordBuilder needs to add one. |
| `@RecordBuilder.Options(useValidationApi = true/false)` | Pass built records through the Java Validation API if it's available in the classpath. The default is `false`. |
| `@RecordBuilder.Options(builderMode = BuilderMode.XXX)` | Whether to add standard builder, staged builder or both. The default is `BuilderMode.STANDARD`. |
| `@RecordBuilder.Options(onceOnlyAssignment = true/false)` | If true, attributes can be set/assigned only 1 time. Attempts to reassign/reset attributes will throw `java.lang.IllegalStateException`. The default is `false`. |
| option | details |
|------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `@RecordBuilder.Options(inheritComponentAnnotations = true/false)` | If true, any annotations (if applicable) on record components are copied to the builder methods. The default is `true`. |
| `@RecordBuilder.Options(publicBuilderConstructors = true/false)` | Makes the generated builder's constructors public. The default is `false`. |
| `@RecordBuilder.Options(builderClassModifiers = {}})` | Any additional `javax.lang.model.element.Modifier` you wish to apply to the builder. |
| `@RecordBuilder.Options(beanClassName = "Foo")` | If set, the Builder will contain an internal interface with this name. |
| `@RecordBuilder.Options(addClassRetainedGenerated = true/false)` | If true, generated classes are annotated with `RecordBuilderGenerated`. The default is `false`. |
| `@RecordBuilder.Options(addStaticBuilder = true/false)` | If true, a functional-style builder is added so that record instances can be instantiated without `new()`. The default is `true`. |
| `@RecordBuilder.Options(inheritComponentAnnotations = true/false)` | If true, any annotations (if applicable) on record components are copied to the builder methods. The default is `true`. |
| `@RecordBuilder.Options(addConcreteSettersForOptional = <mode>)` | Add non-optional setter methods for optional record components. The default is `ConcreteSettersForOptionalMode.DISABLED`. |
| `@RecordBuilder.Options(nullableAnnotationClass = "com.foo.Nullable")` | Nullability annotation to use when RecordBuilder needs to add one. |
| `@RecordBuilder.Options(useValidationApi = true/false)` | Pass built records through the Java Validation API if it's available in the classpath. The default is `false`. |
| `@RecordBuilder.Options(builderMode = BuilderMode.XXX)` | Whether to add standard builder, staged builder or both. The default is `BuilderMode.STANDARD`. |
| `@RecordBuilder.Options(onceOnlyAssignment = true/false)` | If true, attributes can be set/assigned only 1 time. Attempts to reassign/reset attributes will throw `java.lang.IllegalStateException`. The default is `false`. |
| `@RecordBuilder.Options(detectNestedRecordBuilders = true)` | If set, detects if a component is, itself, annotated with `@RecordBuilder` and, if so, adds a setter that is a `Consumer` of a builder for that record. |

### Staged Builders

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@
* @see #nullablePattern
*/
boolean defaultNotNull() default false;

/**
* If true, record components that are themselves {@code @RecordBuilder} records will have builder methods that
* are nested builder consumers so that you can do, for example,
* {@code myRecord.withNestedRecord(nestedBuilder -> nestedBuilder.field1(...).field2(...))}
*/
boolean detectNestedRecordBuilders() default true;
}

@Retention(RetentionPolicy.CLASS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.palantir.javapoet.ParameterizedTypeName;
import com.palantir.javapoet.TypeName;
import com.palantir.javapoet.TypeVariableName;
import io.soabase.recordbuilder.core.RecordBuilder;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.*;
Expand Down Expand Up @@ -135,8 +136,9 @@ public static RecordClassType getRecordClassType(ProcessingEnvironment processin
List<? extends AnnotationMirror> canonicalConstructorAnnotations) {
var typeName = TypeName.get(recordComponent.asType());
var rawTypeName = TypeName.get(processingEnv.getTypeUtils().erasure(recordComponent.asType()));
return new RecordClassType(typeName, rawTypeName, recordComponent.getSimpleName().toString(),
recordComponent.getSimpleName().toString(), accessorAnnotations, canonicalConstructorAnnotations);
return new RecordClassType(recordComponent.asType().getKind(), typeName, rawTypeName,
recordComponent.getSimpleName().toString(), recordComponent.getSimpleName().toString(),
accessorAnnotations, canonicalConstructorAnnotations);
}

public static String getWithMethodName(ClassType component, String prefix) {
Expand Down Expand Up @@ -180,6 +182,12 @@ private static String getNamePrefix(Element element) {
return "";
}

public static RecordBuilder.Options getMetaData(ProcessingEnvironment processingEnv, Element element) {
var recordSpecificMetaData = element.getAnnotation(RecordBuilder.Options.class);
return (recordSpecificMetaData != null) ? recordSpecificMetaData
: RecordBuilderOptions.build(processingEnv.getOptions());
}

private ElementUtils() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ private List<RecordClassType> buildRecordComponents(TypeElement typeElement) {
.stream().filter(annotation -> !annotation.getAnnotationType().asElement().getSimpleName()
.toString().equals(DeconstructorAccessor.class.getSimpleName()))
.toList();
var type = new RecordClassType(typeName, rawTypeName, name,
executableElement.getSimpleName().toString(), annotationMirrors, List.of());
var type = new RecordClassType(executableElement.getReturnType().getKind(), typeName, rawTypeName,
name, executableElement.getSimpleName().toString(), annotationMirrors, List.of());
var orderedType = Map.entry(deconstructorAccessor.order(), type);
return Stream.of(orderedType);
}).sorted((o1, o2) -> {
Expand Down Expand Up @@ -229,9 +229,9 @@ private List<RecordClassType> buildRecordComponents(ExecutableElement executable
return executableElement.getParameters().stream().map(parameter -> {
ValidatedParameter validatedParameter = validateParameter(parameter.getSimpleName().toString(),
parameter.asType());
return new RecordClassType(validatedParameter.typeName, validatedParameter.rawTypeName,
parameter.getSimpleName().toString(), parameter.getSimpleName().toString(),
parameter.getAnnotationMirrors(), List.of());
return new RecordClassType(parameter.asType().getKind(), validatedParameter.typeName,
validatedParameter.rawTypeName, parameter.getSimpleName().toString(),
parameter.getSimpleName().toString(), parameter.getAnnotationMirrors(), List.of());
}).toList();
}

Expand Down
Loading