Skip to content

Commit

Permalink
feat!: Merge @BuildableConstructor into @buildable ending up with @Bu…
Browse files Browse the repository at this point in the history
…ildable.Constructor
  • Loading branch information
jonas-grgt committed Apr 8, 2024
1 parent 754e23e commit 81726b5
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ In example `null` for `Integer` and zero for `int`.

If your class contains multiple constructors that tie for having the most parameters,
the first one will be selected.
See `@BuildableConstructor` if you want to change this behavior.
See `@Buildable.Constructor` if you want to change this behavior.

```java
@Buildable
Expand All @@ -94,7 +94,7 @@ new Car(null, 0, "red", BigDecimal.ZERO);

### Different constructor

If you want to use a different constructor instead of the default selected one, annotated it with `@BuildableConstructor`
If you want to use a different constructor instead of the default selected one, annotated it with `@Buildable.Constructor`

### Package

Expand Down
13 changes: 13 additions & 0 deletions annotations/src/main/java/io/jonasg/bob/Buildable.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@
* @return the package name of the generated builder
*/
String packageName() default "";

/**
* Marks a constructor as buildable.
* This means that a builder will be generated
* using the selected constructor as opposed to the one with the most
* parameters.
*/
@SuppressWarnings("unused")
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.CONSTRUCTOR)
@interface Constructor {
}

}
18 changes: 0 additions & 18 deletions annotations/src/main/java/io/jonasg/bob/BuildableConstructor.java

This file was deleted.

8 changes: 4 additions & 4 deletions processor/src/main/java/io/jonasg/bob/TypeSpecFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ private List<BuildableField> extractBuildableFieldsFrom(TypeDefinition typeDefin

private ConstructorDefinition extractConstructorDefinitionFrom(TypeDefinition typeDefinition) {
var buildableConstructors = typeDefinition.constructors().stream()
.filter(c -> c.isAnnotatedWith(BuildableConstructor.class))
.filter(c -> c.isAnnotatedWith(Buildable.Constructor.class))
.toList();
if (buildableConstructors.size() > 1) {
throw new IllegalArgumentException("Only one constructor can be annotated with @BuildableConstructor");
throw new IllegalArgumentException("Only one constructor can be annotated with @Buildable.Constructor");
}
if (buildableConstructors.isEmpty()) {
return typeDefinition.constructors().stream()
Expand All @@ -95,7 +95,7 @@ private TypeSpec typeSpec() {
builder.addTypeVariables(toTypeVariableNames(this.typeDefinition));
builder.addMethods(generateSetters());
builder.addFields(generateFields());
builder.addMethod(genereateBuildMethod());
builder.addMethod(generateBuildMethod());
builder.addMethod(generateConstructor());
if (!this.typeDefinition.genericParameters().isEmpty()) {
builder.addMethod(of());
Expand Down Expand Up @@ -123,7 +123,7 @@ private List<FieldSpec> generateFields() {
.toList();
}

private MethodSpec genereateBuildMethod() {
private MethodSpec generateBuildMethod() {
Builder builder = MethodSpec.methodBuilder("build")
.addModifiers(Modifier.PUBLIC)
.returns(className(this.typeDefinition));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jonasg.bob.definitions;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import javax.lang.model.element.AnnotationMirror;
Expand Down Expand Up @@ -32,6 +33,7 @@ public List<ParameterDefinition> parameters() {

public boolean isAnnotatedWith(Class<?> type) {
return annotations.stream()
.anyMatch(a -> a.getAnnotationType().toString().equals(type.getName()));
.anyMatch(a -> Objects.equals(type.getName().replaceAll("\\$", "."),
a.getAnnotationType().asElement().asType().toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ public class TypeDefinition extends SimpleTypeDefinition {

private List<MethodDefinition> methods;

private TypeDefinition(String typeName, String packageName, String enclosedIn, List<FieldDefinition> fields,
List<ConstructorDefinition> constructors) {
super(typeName, packageName);
this.parent = enclosedIn;
this.fields = fields;
this.constructors = constructors;
}

public TypeDefinition() {
super();
}
Expand All @@ -36,18 +28,6 @@ public List<FieldDefinition> fields() {
return fields;
}

public List<FieldDefinition> fields(Predicate<FieldDefinition> predicate) {
return fields().stream()
.filter(predicate)
.collect(Collectors.<FieldDefinition>toList());
}

public List<MethodDefinition> methods(Predicate<MethodDefinition> predicate) {
return methods.stream()
.filter(predicate)
.toList();
}

public String nestedIn() {
return parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private List<SimpleTypeDefinition> toTypeDefinitions(List<? extends TypeMirror>
String name = parts.get(0);
parts.remove(0);
Collections.reverse(parts);
String packageName = join(parts.toArray(new String[parts.size()]), ".");
String packageName = join(parts.toArray(new String[0]), ".");
definitions.add(new SimpleTypeDefinition(name, packageName));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void failWhenMultipleConstructorAreAnnotatedWithBuildableConstructor() {
.andThat()
.compilerMessage()
.ofKindError()
.contains("Only one constructor can be annotated with @BuildableConstructor")
.contains("Only one constructor can be annotated with @Buildable.Constructor")
.executeTest();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.jonasg.bob.test;

import io.jonasg.bob.Buildable;
import io.jonasg.bob.BuildableConstructor;
import io.jonasg.bob.Buildable.Constructor;

@Buildable
public class MultipleBuildableConstructorAnnotationsPresent {
Expand All @@ -24,7 +24,7 @@ public MultipleBuildableConstructorAnnotationsPresent(double engineSize, boolean
this.fuelEfficiency = fuelEfficiency;
}

@BuildableConstructor
@Buildable.Constructor
public MultipleBuildableConstructorAnnotationsPresent(String make, int year, double engineSize, boolean isElectric, float fuelEfficiency) {
this.make = make;
this.year = year;
Expand All @@ -33,7 +33,7 @@ public MultipleBuildableConstructorAnnotationsPresent(String make, int year, dou
this.fuelEfficiency = fuelEfficiency;
}

@BuildableConstructor
@Buildable.Constructor
public MultipleBuildableConstructorAnnotationsPresent(String make, int year) {
this.make = make;
this.year = year;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.jonasg.bob.test;

import io.jonasg.bob.Buildable;
import io.jonasg.bob.BuildableConstructor;

@Buildable
public class UseConstructorAnnotatedWithBuildableConstructor {
Expand Down Expand Up @@ -32,7 +31,7 @@ public UseConstructorAnnotatedWithBuildableConstructor(String make, int year, do
this.fuelEfficiency = fuelEfficiency;
}

@BuildableConstructor
@Buildable.Constructor
public UseConstructorAnnotatedWithBuildableConstructor(String make, int year) {
this.make = make;
this.year = year;
Expand Down

0 comments on commit 81726b5

Please sign in to comment.