Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add java versions starting from 11 to build pipeline #28

Merged
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
20 changes: 10 additions & 10 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ jobs:
build:
strategy:
matrix:
java: [ '17', '21' ]
java: [ '11', '16', '17', '21' ]
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java }}
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B -ntp verify --file pom.xml
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java }}
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B -ntp verify --file pom.xml
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
<flatten-maven-plugin.version>1.5.0</flatten-maven-plugin.version>
<surefire-maven-plugin.version>3.1.2</surefire-maven-plugin.version>

<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

<jreleaser.version>1.9.0</jreleaser.version>
<java-poet.version>1.13.0</java-poet.version>
Expand Down Expand Up @@ -102,6 +102,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
90 changes: 79 additions & 11 deletions processor/src/main/java/io/jonasg/bob/BuildableField.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,97 @@
package io.jonasg.bob;

import java.util.Objects;
import java.util.Optional;

import javax.lang.model.type.TypeMirror;

/**
* Represents a field that is buildable
*
* @param name the name of the field as declared in the type that will be built
* @param isConstructorArgument indicates if the field can be set through the constructor
* @param setterMethodName the name of the setter method to access the field.
* @param type the type of the field
* {@link BuildableField#name} the name of the field as declared in the type
* that will be built
* {@link BuildableField#isConstructorArgument} indicates if the field can be
* set through the constructor
* {@link BuildableField#setterMethodName} the name of the setter method to
* access the field.
* {@link BuildableField#type} the type of the field
*/
public record BuildableField(
String name,
boolean isConstructorArgument,
boolean isMandatory,
Optional<String> setterMethodName,
TypeMirror type) {
public final class BuildableField {
private final String name;
private final boolean isConstructorArgument;
private final boolean isMandatory;
private final Optional<String> setterMethodName;
private final TypeMirror type;

public BuildableField(
String name,
boolean isConstructorArgument,
boolean isMandatory,
Optional<String> setterMethodName,
TypeMirror type) {
this.name = name;
this.isConstructorArgument = isConstructorArgument;
this.isMandatory = isMandatory;
this.setterMethodName = setterMethodName;
this.type = type;
}

public static BuildableField fromConstructor(String fieldName, TypeMirror type) {
return new BuildableField(fieldName, true, false, Optional.empty(), type);
}

public static BuildableField fromSetter(String fieldName, boolean fieldIsMandatory, String setterMethodName, TypeMirror type) {
public static BuildableField fromSetter(String fieldName, boolean fieldIsMandatory, String setterMethodName,
TypeMirror type) {
return new BuildableField(fieldName, false, fieldIsMandatory, Optional.of(setterMethodName), type);
}

public String name() {
return name;
}

public boolean isConstructorArgument() {
return isConstructorArgument;
}

public boolean isMandatory() {
return isMandatory;
}

public Optional<String> setterMethodName() {
return setterMethodName;
}

public TypeMirror type() {
return type;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (BuildableField) obj;
return Objects.equals(this.name, that.name) &&
this.isConstructorArgument == that.isConstructorArgument &&
this.isMandatory == that.isMandatory &&
Objects.equals(this.setterMethodName, that.setterMethodName) &&
Objects.equals(this.type, that.type);
}

@Override
public int hashCode() {
return Objects.hash(name, isConstructorArgument, isMandatory, setterMethodName, type);
}

@Override
public String toString() {
return "BuildableField[" +
"name=" + name + ", " +
"isConstructorArgument=" + isConstructorArgument + ", " +
"isMandatory=" + isMandatory + ", " +
"setterMethodName=" + setterMethodName + ", " +
"type=" + type + ']';
}

}
39 changes: 23 additions & 16 deletions processor/src/main/java/io/jonasg/bob/BuilderTypeSpecFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ protected BuilderTypeSpecFactory(TypeDefinition typeDefinition, Buildable builda
private List<BuildableField> extractBuildableFieldsFrom(TypeDefinition typeDefinition) {
var fieldNames = typeDefinition.fields().stream()
.map(FieldDefinition::name)
.toList();
.collect(Collectors.toList());
List<ParameterDefinition> eligibleConstructorParams = this.constructorDefinition.parameters()
.stream()
.filter(p -> fieldNames.contains(p.name()))
.toList();
.collect(Collectors.toList());
Stream<BuildableField> constructorBuildableFields = this.constructorDefinition.parameters()
.stream()
.filter(p -> fieldNames.contains(p.name()))
Expand All @@ -84,13 +84,13 @@ private List<BuildableField> extractBuildableFieldsFrom(TypeDefinition typeDefin
|| p.field().isAnnotatedWith(Buildable.Mandatory.class);
return BuildableField.fromSetter(p.field().name(), fieldIsMandatory, p.methodName(), p.type());
});
return Stream.concat(constructorBuildableFields, setterBuildableFields).toList();
return Stream.concat(constructorBuildableFields, setterBuildableFields).collect(Collectors.toList());
}

private ConstructorDefinition extractConstructorDefinitionFrom(TypeDefinition typeDefinition) {
var buildableConstructors = typeDefinition.constructors().stream()
.filter(c -> c.isAnnotatedWith(Buildable.Constructor.class))
.toList();
.collect(Collectors.toList());
if (buildableConstructors.size() > 1) {
throw new IllegalArgumentException("Only one constructor can be annotated with @Buildable.Constructor");
}
Expand Down Expand Up @@ -132,7 +132,7 @@ private List<MethodSpec> generateSetters() {
return this.buildableFields.stream()
.filter(this::notExcluded)
.map(this::generateSetterForField)
.toList();
.collect(Collectors.toList());
}

protected MethodSpec generateSetterForField(BuildableField field) {
Expand Down Expand Up @@ -166,7 +166,7 @@ private boolean isEnforcedAllowNullsConstructorPolicy() {
private List<FieldSpec> generateFields() {
return buildableFields.stream()
.map(this::generateField)
.toList();
.collect(Collectors.toList());
}

protected FieldSpec generateField(BuildableField field) {
Expand Down Expand Up @@ -249,21 +249,28 @@ protected CodeBlock generateFieldAssignment(BuildableField field) {
.build();
} else {
return CodeBlock.builder()
.addStatement("instance.%s(this.%s)".formatted(setterName(field.setterMethodName().orElseThrow()),
field.name()))
.addStatement(
String.format("instance.%s(this.%s)", setterName(field.setterMethodName().orElseThrow()),
field.name()))
.build();
}
}

protected String defaultForType(TypeMirror type) {
return switch (type.toString()) {
case "int" -> "0";
case "long" -> "0L";
case "float" -> "0.0f";
case "double" -> "0.0d";
case "boolean" -> "false";
default -> "null";
};
switch (type.toString()) {
case "int":
return "0";
case "long":
return "0L";
case "float":
return "0.0f";
case "double":
return "0.0d";
case "boolean":
return "false";
default:
return "null";
}
}

private MethodSpec of() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import javax.lang.model.element.Modifier;

Expand Down Expand Up @@ -40,7 +42,45 @@ public StepBuilderInterfaceTypeSpecFactory(TypeDefinition typeDefinition,
this.packageName = packageName;
}

record BuilderDetails(TypeSpec typeSpec, Set<TypeName> interfaces) {
final class BuilderDetails {
private final TypeSpec typeSpec;
private final Set<TypeName> interfaces;

BuilderDetails(TypeSpec typeSpec, Set<TypeName> interfaces) {
this.typeSpec = typeSpec;
this.interfaces = interfaces;
}

public TypeSpec typeSpec() {
return typeSpec;
}

public Set<TypeName> interfaces() {
return interfaces;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (BuilderDetails) obj;
return Objects.equals(this.typeSpec, that.typeSpec) &&
Objects.equals(this.interfaces, that.interfaces);
}

@Override
public int hashCode() {
return Objects.hash(typeSpec, interfaces);
}

@Override
public String toString() {
return "BuilderDetails[" +
"typeSpec=" + typeSpec + ", " +
"interfaces=" + interfaces + ']';
}

}

Expand Down Expand Up @@ -88,7 +128,7 @@ BuilderDetails typeSpec(String builderImplName) {
.stream()
.filter(field -> (field.isConstructorArgument() && isEnforcedConstructorPolicy())
|| field.isMandatory())
.toList();
.collect(Collectors.toList());
mandatoryFields
.subList(0, mandatoryFields.size() - 1)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.lang.model.element.Modifier;

Expand Down Expand Up @@ -84,7 +85,46 @@ public TypeSpec build() {

}

record Argument(TypeName typeName, String name) {
final class Argument {
private final TypeName typeName;
private final String name;

Argument(TypeName typeName, String name) {
this.typeName = typeName;
this.name = name;
}

public TypeName typeName() {
return typeName;
}

public String name() {
return name;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (Argument) obj;
return Objects.equals(this.typeName, that.typeName) &&
Objects.equals(this.name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(typeName, name);
}

@Override
public String toString() {
return "Argument[" +
"typeName=" + typeName + ", " +
"name=" + name + ']';
}

}
}
}
Loading