diff --git a/README.md b/README.md index 739009e..16d8442 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/annotations/src/main/java/io/jonasg/bob/Buildable.java b/annotations/src/main/java/io/jonasg/bob/Buildable.java index 1d7fb58..c5d12d3 100644 --- a/annotations/src/main/java/io/jonasg/bob/Buildable.java +++ b/annotations/src/main/java/io/jonasg/bob/Buildable.java @@ -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 { + } + } diff --git a/annotations/src/main/java/io/jonasg/bob/BuildableConstructor.java b/annotations/src/main/java/io/jonasg/bob/BuildableConstructor.java deleted file mode 100644 index e323f40..0000000 --- a/annotations/src/main/java/io/jonasg/bob/BuildableConstructor.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.jonasg.bob; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 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) -public @interface BuildableConstructor { -} diff --git a/processor/src/main/java/io/jonasg/bob/TypeSpecFactory.java b/processor/src/main/java/io/jonasg/bob/TypeSpecFactory.java index 5ee0ce3..910a8df 100644 --- a/processor/src/main/java/io/jonasg/bob/TypeSpecFactory.java +++ b/processor/src/main/java/io/jonasg/bob/TypeSpecFactory.java @@ -71,10 +71,10 @@ private List 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() @@ -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()); @@ -123,7 +123,7 @@ private List generateFields() { .toList(); } - private MethodSpec genereateBuildMethod() { + private MethodSpec generateBuildMethod() { Builder builder = MethodSpec.methodBuilder("build") .addModifiers(Modifier.PUBLIC) .returns(className(this.typeDefinition)); diff --git a/processor/src/main/java/io/jonasg/bob/definitions/ConstructorDefinition.java b/processor/src/main/java/io/jonasg/bob/definitions/ConstructorDefinition.java index 7243375..239b2aa 100644 --- a/processor/src/main/java/io/jonasg/bob/definitions/ConstructorDefinition.java +++ b/processor/src/main/java/io/jonasg/bob/definitions/ConstructorDefinition.java @@ -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; @@ -32,6 +33,7 @@ public List 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())); } } diff --git a/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinition.java b/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinition.java index 8349915..3b607f5 100644 --- a/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinition.java +++ b/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinition.java @@ -20,14 +20,6 @@ public class TypeDefinition extends SimpleTypeDefinition { private List methods; - private TypeDefinition(String typeName, String packageName, String enclosedIn, List fields, - List constructors) { - super(typeName, packageName); - this.parent = enclosedIn; - this.fields = fields; - this.constructors = constructors; - } - public TypeDefinition() { super(); } @@ -36,18 +28,6 @@ public List fields() { return fields; } - public List fields(Predicate predicate) { - return fields().stream() - .filter(predicate) - .collect(Collectors.toList()); - } - - public List methods(Predicate predicate) { - return methods.stream() - .filter(predicate) - .toList(); - } - public String nestedIn() { return parent; } diff --git a/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinitionFactory.java b/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinitionFactory.java index cc600bd..f7f7648 100644 --- a/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinitionFactory.java +++ b/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinitionFactory.java @@ -65,7 +65,7 @@ private List toTypeDefinitions(List 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)); } } diff --git a/processor/src/test/java/io/jonasg/bob/BobFeaturesTests.java b/processor/src/test/java/io/jonasg/bob/BobFeaturesTests.java index ce1d0fd..9681208 100644 --- a/processor/src/test/java/io/jonasg/bob/BobFeaturesTests.java +++ b/processor/src/test/java/io/jonasg/bob/BobFeaturesTests.java @@ -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(); } diff --git a/processor/src/test/resources/tests/failing-compilation/MultipleBuildableConstructorAnnotationsPresent/MultipleBuildableConstructorAnnotationsPresent.java b/processor/src/test/resources/tests/failing-compilation/MultipleBuildableConstructorAnnotationsPresent/MultipleBuildableConstructorAnnotationsPresent.java index a54e136..a0ef967 100644 --- a/processor/src/test/resources/tests/failing-compilation/MultipleBuildableConstructorAnnotationsPresent/MultipleBuildableConstructorAnnotationsPresent.java +++ b/processor/src/test/resources/tests/failing-compilation/MultipleBuildableConstructorAnnotationsPresent/MultipleBuildableConstructorAnnotationsPresent.java @@ -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 { @@ -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; @@ -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; diff --git a/processor/src/test/resources/tests/successful-compilation/UseConstructorAnnotatedWithBuildableConstructor/UseConstructorAnnotatedWithBuildableConstructor.java b/processor/src/test/resources/tests/successful-compilation/UseConstructorAnnotatedWithBuildableConstructor/UseConstructorAnnotatedWithBuildableConstructor.java index 977298c..7891fae 100644 --- a/processor/src/test/resources/tests/successful-compilation/UseConstructorAnnotatedWithBuildableConstructor/UseConstructorAnnotatedWithBuildableConstructor.java +++ b/processor/src/test/resources/tests/successful-compilation/UseConstructorAnnotatedWithBuildableConstructor/UseConstructorAnnotatedWithBuildableConstructor.java @@ -1,7 +1,6 @@ package io.jonasg.bob.test; import io.jonasg.bob.Buildable; -import io.jonasg.bob.BuildableConstructor; @Buildable public class UseConstructorAnnotatedWithBuildableConstructor { @@ -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;