Skip to content

Commit

Permalink
build: split annotation processor and annotation into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Mar 23, 2024
1 parent af636f0 commit ed80489
Show file tree
Hide file tree
Showing 43 changed files with 235 additions and 119 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ Bob generates a builder in the form of pure Java source code.
```xml
<dependency>
<groupId>io.jonasg</groupId>
<artifactId>bob</artifactId>
<artifactId>bob-annotations</artifactId>
<version>${bob.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.jonasg</groupId>
<artifactId>bob-processor</artifactId>
<version>${bob.version}</version>
<scope>compile</scope>
</dependency>
```
### Gradle
```groovy
dependencies {
annotationProcessor "io.jonasg:bob:" + bobVersion
compileOnly "io.jonasg:bob:" + bobVersion
annotationProcessor "io.jonasg:bob-processor:" + bobVersion
compileOnly "io.jonasg:bob-annotations:" + bobVersion
}
```

Expand Down Expand Up @@ -57,6 +64,10 @@ For parameters
that do not have a corresponding field, the default value for that type will be used.
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.

```java
@Buildable
public class Car {
Expand Down Expand Up @@ -91,14 +102,14 @@ For the car example this will be `my.garage.CarBuilder`
The location of the builder can be changed:

```java
@Buildable(package = "my.other.garage")
@Buildable(packageName = "my.other.garage")
public class Car {
```

### Field exclusion

```java
@Buildable(excludes = {"brand", "color"})
@Buildable(excludeFields = {"brand", "color"})
public class Car {
```

Expand All @@ -108,7 +119,7 @@ By default Bob will generated setter methods consisting out of *new style setter
If you want to change the prefix of those setter methods you can:

```java
@Buildable(prefix = "with")
@Buildable(setterPrefix = "with")
public class Car {
```

Expand Down
20 changes: 20 additions & 0 deletions annotations/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.jonasg</groupId>
<artifactId>bob</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>bob-annotations</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* The builder will have a build method
* that will create an instance of the annotated class.
*/
@SuppressWarnings("unused")
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface Buildable {
Expand All @@ -19,16 +20,16 @@
*
* @return the list of fields to be excluded to be included in the builder.
*/
String[] excludes() default {};
String[] excludeFields() default {};

/**
* The prefix for the generated setters.
* The setterPrefix for the generated setters.
* For example, "with" or "set"
* Defaults to no prefix.
* Defaults to no setterPrefix.
*
* @return the prefix for the generated setters.
* @return the setterPrefix for the generated setters.
*/
String prefix() default "";
String setterPrefix() default "";

/**
* The package name of the generated builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* using the selected constructor as opposed to the one with the most
* parameters.
*/
@SuppressWarnings("unused")
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.CONSTRUCTOR)
public @interface BuildableConstructor {
Expand Down
52 changes: 6 additions & 46 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<name>bob</name>
<description>Builder generator for Java</description>
<url>https://github.com/jonas-grgt/bob</url>
<modules>
<module>annotations</module>
<module>processor</module>
</modules>
<groupId>io.jonasg</groupId>

<developers>
Expand All @@ -24,7 +28,7 @@
</licenses>

<artifactId>bob</artifactId>
<packaging>jar</packaging>
<packaging>pom</packaging>
<version>0.1.0-SNAPSHOT</version>

<properties>
Expand Down Expand Up @@ -147,55 +151,11 @@
<java>
<eclipse>
<version>4.26</version>
<file>${project.basedir}/eclipse-formatter.xml</file>
<file>eclipse-formatter.xml</file>
</eclipse>
</java>
</configuration>
</plugin>
<plugin>
<groupId>org.jreleaser</groupId>
<artifactId>jreleaser-maven-plugin</artifactId>
<inherited>false</inherited>
<configuration>
<jreleaser>
<project>
<description>Bob - Builder generator</description>
<links>
<homepage>https://github.com/jonasgeiregat/bob</homepage>
</links>
<license>APACHE-2.0</license>
<authors>Jonas Geiregat</authors>
<copyright>2024 Jonas Geiregat</copyright>
</project>
<release>
<github>
<changelog>
<formatted>ALWAYS</formatted>
<preset>conventional-commits</preset>
</changelog>
</github>
</release>
<signing>
<active>ALWAYS</active>
<armored>true</armored>
</signing>
<deploy>
<maven>
<nexus2>
<maven-central>
<active>ALWAYS</active>
<url>https://s01.oss.sonatype.org/service/local</url>
<snapshotUrl>https://s01.oss.sonatype.org/content/repositories/snapshots/</snapshotUrl>
<closeRepository>true</closeRepository>
<releaseRepository>true</releaseRepository>
<stagingRepositories>target/staging-deploy</stagingRepositories>
</maven-central>
</nexus2>
</maven>
</deploy>
</jreleaser>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
28 changes: 28 additions & 0 deletions processor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.jonasg</groupId>
<artifactId>bob</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>bob-processor</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.jonasg</groupId>
<artifactId>bob-annotations</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ private List<TypeName> simpleClassNames(List<SimpleTypeDefinition> definitions)
}

private String fieldName(String name) {
if (buildable.prefix().isEmpty()) {
if (buildable.setterPrefix().isEmpty()) {
return name;
}
return Formatter.format("$prefix$name", buildable.prefix(),
return Formatter.format("$setterPrefix$name", buildable.setterPrefix(),
name.substring(0, 1).toUpperCase() + name.substring(1));
}

private boolean notExcluded(ParameterDefinition field) {
return !Arrays.asList(buildable.excludes()).contains(field.name());
return !Arrays.asList(buildable.excludeFields()).contains(field.name());
}

private MethodSpec constructor() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,42 @@ void useConstructorAnnotatedWithBuildableConstructor() {
}

@Test
void useConstructorWithTheGreatestNumberOfParameters() {
void useConstructorWithTheMostNumberOfParameters() {
Cute.blackBoxTest()
.given()
.processors(List.of(BuildableProcessor.class))
.andSourceFiles(
"/tests/successful-compilation/UseConstructorWithTheGreatestNumberOfParameters/UseConstructorWithTheGreatestNumberOfParameters.java")
"/tests/successful-compilation/UseConstructorWithTheMostNumberOfParameters/UseConstructorWithTheMostNumberOfParameters.java")
.whenCompiled()
.thenExpectThat()
.compilationSucceeds()
.andThat()
.generatedSourceFile(
"io.jonasg.bob.test.builder.UseConstructorWithTheGreatestNumberOfParametersBuilder")
"io.jonasg.bob.test.builder.UseConstructorWithTheMostNumberOfParametersBuilder")
.matches(
CuteApi.ExpectedFileObjectMatcherKind.BINARY,
JavaFileObjectUtils.readFromResource(
"/tests/successful-compilation/UseConstructorWithTheGreatestNumberOfParameters/Expected_UseConstructorWithTheGreatestNumberOfParameters.java"))
"/tests/successful-compilation/UseConstructorWithTheMostNumberOfParameters/Expected_UseConstructorWithTheMostNumberOfParameters.java"))
.executeTest();
}

@Test
void useFirstConstructorWithTheMostNumberOfParameters() {
Cute.blackBoxTest()
.given()
.processors(List.of(BuildableProcessor.class))
.andSourceFiles(
"/tests/successful-compilation/UseFirstConstructorWithTheMostNumberOfParameters/UseFirstConstructorWithTheMostNumberOfParameters.java")
.whenCompiled()
.thenExpectThat()
.compilationSucceeds()
.andThat()
.generatedSourceFile(
"io.jonasg.bob.test.builder.UseFirstConstructorWithTheMostNumberOfParametersBuilder")
.matches(
CuteApi.ExpectedFileObjectMatcherKind.BINARY,
JavaFileObjectUtils.readFromResource(
"/tests/successful-compilation/UseFirstConstructorWithTheMostNumberOfParameters/Expected_UseFirstConstructorWithTheMostNumberOfParameters.java"))
.executeTest();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.jonasg.bob.Buildable;

@Buildable(prefix = "with")
@Buildable(setterPrefix = "with")
public class SetterWithCustomPrefix {
private String make;

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

import io.jonasg.bob.test.UseConstructorWithTheMostNumberOfParameters;
import java.lang.String;

public final class UseConstructorWithTheMostNumberOfParametersBuilder {
private String make;

private int year;

private double engineSize;

private boolean isElectric;

private float fuelEfficiency;

public UseConstructorWithTheMostNumberOfParametersBuilder() {
}

public UseConstructorWithTheMostNumberOfParametersBuilder make(String make) {
this.make = make;
return this;
}

public UseConstructorWithTheMostNumberOfParametersBuilder year(int year) {
this.year = year;
return this;
}

public UseConstructorWithTheMostNumberOfParametersBuilder engineSize(double engineSize) {
this.engineSize = engineSize;
return this;
}

public UseConstructorWithTheMostNumberOfParametersBuilder isElectric(boolean isElectric) {
this.isElectric = isElectric;
return this;
}

public UseConstructorWithTheMostNumberOfParametersBuilder fuelEfficiency(float fuelEfficiency) {
this.fuelEfficiency = fuelEfficiency;
return this;
}

public UseConstructorWithTheMostNumberOfParameters build() {
return new UseConstructorWithTheMostNumberOfParameters(make, year, engineSize, isElectric, fuelEfficiency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.jonasg.bob.Buildable;

@Buildable
public class UseConstructorWithTheGreatestNumberOfParameters {
public class UseConstructorWithTheMostNumberOfParameters {
private String make;

private int year;
Expand All @@ -14,24 +14,24 @@ public class UseConstructorWithTheGreatestNumberOfParameters {

private float fuelEfficiency;

public UseConstructorWithTheGreatestNumberOfParameters() {
public UseConstructorWithTheMostNumberOfParameters() {
}

public UseConstructorWithTheGreatestNumberOfParameters(double engineSize, boolean isElectric, float fuelEfficiency) {
public UseConstructorWithTheMostNumberOfParameters(double engineSize, boolean isElectric, float fuelEfficiency) {
this.engineSize = engineSize;
this.isElectric = isElectric;
this.fuelEfficiency = fuelEfficiency;
}

public UseConstructorWithTheGreatestNumberOfParameters(String make, int year, double engineSize, boolean isElectric, float fuelEfficiency) {
public UseConstructorWithTheMostNumberOfParameters(String make, int year, double engineSize, boolean isElectric, float fuelEfficiency) {
this.make = make;
this.year = year;
this.engineSize = engineSize;
this.isElectric = isElectric;
this.fuelEfficiency = fuelEfficiency;
}

public UseConstructorWithTheGreatestNumberOfParameters(String make, int year) {
public UseConstructorWithTheMostNumberOfParameters(String make, int year) {
this.make = make;
this.year = year;
}
Expand Down
Loading

0 comments on commit ed80489

Please sign in to comment.