Skip to content

Commit

Permalink
fix empty builder generation (#129)
Browse files Browse the repository at this point in the history
Prior to this change if a record or java bean had no properties a duplicate constructor was generated, one for the default constructor and another for an empty list properties. This resulted in a compilation error for duplicate constructors. This fixes that.
  • Loading branch information
graemerocher authored Aug 8, 2024
1 parent cc86a52 commit 222acd8
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public void visitClass(ClassElement element, VisitorContext context) {
}

builder.addMethod(MethodDef.constructor().build());
builder.addMethod(createAllPropertiesConstructor(builderType, properties));
if (!properties.isEmpty()) {
builder.addMethod(createAllPropertiesConstructor(builderType, properties));
}

builder.addMethod(createBuilderMethod(builderType));
builder.addMethod(createBuildMethod(element));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public void visitClass(ClassElement element, VisitorContext context) {

builder.addMethod(createSelfMethod());
builder.addMethod(BuilderAnnotationVisitor.createBuildMethod(element));
builder.addMethod(createBuilderMethod(builderType));

ClassDef builderDef = builder.build();
context.visitGeneratedSourceFile(builderDef.getPackageName(), builderDef.getSimpleName(), element).ifPresent(sourceFile -> {
Expand Down Expand Up @@ -178,6 +179,14 @@ public void visitClass(ClassElement element, VisitorContext context) {
}
}

private MethodDef createBuilderMethod(ClassTypeDef builderType) {
return MethodDef.builder("builder")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(builderType)
.addStatement(builderType.instantiate().returning())
.build();
}

private MethodDef createSelfMethod() {
return MethodDef.builder("self")
.addModifiers(Modifier.PUBLIC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,22 @@ class BuilderAnnotationVisitorSpec extends AbstractTypeElementSpec {
walrus.age == 1
}

void "test empty builder"() {
given:
var classLoader = buildClassLoader("test.Walrus", """
package test;
import io.micronaut.sourcegen.annotations.Builder;
@Builder
public record Walrus() {
}
""")
var walrusBuilderClass = classLoader.loadClass("test.WalrusBuilder")

expect:
var walrusBuilder = walrusBuilderClass.newInstance(new Object[]{})
var walrus = walrusBuilder.build()
walrus != null
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.sourcegen.example;


import io.micronaut.sourcegen.annotations.SuperBuilder;

@SuperBuilder
public class EmptyChild extends EmptyParent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.sourcegen.example;

import io.micronaut.sourcegen.annotations.SuperBuilder;

@SuperBuilder
public class EmptyParent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.sourcegen.example;

import io.micronaut.sourcegen.annotations.Builder;

@Builder
public class EmptyThing {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.micronaut.sourcegen.example;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class EmptyTest {

@Test
void testEmptyBuilder() {
EmptyThing built = EmptyThingBuilder.builder().build();
Assertions.assertNotNull(built);
}

@Test
void testEmptySuperBuilder() {
EmptyChild built = EmptyChildSuperBuilder.builder().build();
Assertions.assertNotNull(built);
}
}

0 comments on commit 222acd8

Please sign in to comment.