From 9ae2e07b414f10728049014f64ca43166f2e382a Mon Sep 17 00:00:00 2001 From: Graeme Rocher Date: Tue, 17 Dec 2024 09:09:38 -0400 Subject: [PATCH] Process constructors regradless of builder. Fixes #11432 --- .../IntrospectedTypeElementVisitor.java | 21 +++++++------------ .../lombok/LombokIntrospectedBuilderTest.java | 21 +++++++++++++++++++ .../io/micronaut/test/lombok/RobotRecord.java | 9 ++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 test-suite/src/test/java/io/micronaut/test/lombok/RobotRecord.java diff --git a/core-processor/src/main/java/io/micronaut/inject/beans/visitor/IntrospectedTypeElementVisitor.java b/core-processor/src/main/java/io/micronaut/inject/beans/visitor/IntrospectedTypeElementVisitor.java index a7a0ff06c9..090ef931b1 100644 --- a/core-processor/src/main/java/io/micronaut/inject/beans/visitor/IntrospectedTypeElementVisitor.java +++ b/core-processor/src/main/java/io/micronaut/inject/beans/visitor/IntrospectedTypeElementVisitor.java @@ -205,7 +205,7 @@ private void processBuilderDefinition(ClassElement element, VisitorContext conte throw new ElementPostponedToNextRoundException(element); } String creatorMethod = lombokBuilder.stringValue("buildMethodName").orElse("build"); - String[] writePrefixes = lombokBuilder.stringValue("setterPrefix").map(sp -> new String[] { sp }).orElse(new String[]{""}); + String[] writePrefixes = lombokBuilder.stringValue("setterPrefix").map(sp -> new String[]{sp}).orElse(new String[]{""}); processBuilderDefinition( element, context, @@ -420,18 +420,13 @@ private void processElement(boolean metadata, List beanProperties = ce.getBeanProperties(propertyElementQuery).stream() .filter(p -> !p.isExcluded()) .toList(); - // unfortunately sometimes we don't see the Lombok transformations - // so assume if the class is annotated with Lombok builder we cannot - // access the constructor. - if (!ce.hasDeclaredAnnotation(ANN_LOMBOK_BUILDER)) { - Optional constructorElement = ce.getPrimaryConstructor(); - constructorElement.ifPresent(constructorEl -> { - if (ArrayUtils.isNotEmpty(constructorEl.getParameters())) { - writer.visitConstructor(constructorEl); - } - }); - ce.getDefaultConstructor().ifPresent(writer::visitDefaultConstructor); - } + Optional constructorElement = ce.getPrimaryConstructor(); + constructorElement.ifPresent(constructorEl -> { + if (ArrayUtils.isNotEmpty(constructorEl.getParameters())) { + writer.visitConstructor(constructorEl); + } + }); + ce.getDefaultConstructor().ifPresent(writer::visitDefaultConstructor); for (PropertyElement beanProperty : beanProperties) { if (beanProperty.isExcluded()) { diff --git a/test-suite/src/test/java/io/micronaut/test/lombok/LombokIntrospectedBuilderTest.java b/test-suite/src/test/java/io/micronaut/test/lombok/LombokIntrospectedBuilderTest.java index 76f1f54290..89f737e19e 100644 --- a/test-suite/src/test/java/io/micronaut/test/lombok/LombokIntrospectedBuilderTest.java +++ b/test-suite/src/test/java/io/micronaut/test/lombok/LombokIntrospectedBuilderTest.java @@ -1,8 +1,12 @@ package io.micronaut.test.lombok; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import io.micronaut.core.beans.BeanConstructor; import io.micronaut.core.beans.BeanIntrospection; +import io.micronaut.core.beans.BeanProperty; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -11,6 +15,23 @@ public class LombokIntrospectedBuilderTest { + @Test + void testLombokRecordBuilder() { + BeanIntrospection introspection = BeanIntrospection.getIntrospection(RobotRecord.class); + + assertTrue(introspection.hasBuilder()); + BeanProperty property = + introspection.getRequiredProperty("name", String.class); + + assertTrue(property.hasSetterOrConstructorArgument()); + BeanConstructor constructor = introspection.getConstructor(); + assertNotNull(constructor); + assertEquals(1, constructor.getArguments().length); + + RobotRecord robot = introspection.instantiate("test"); + assertEquals("test", robot.name()); + } + @Test void testLombokBuilder() { BeanIntrospection.Builder builder = BeanIntrospection.getIntrospection(RobotEntity.class) diff --git a/test-suite/src/test/java/io/micronaut/test/lombok/RobotRecord.java b/test-suite/src/test/java/io/micronaut/test/lombok/RobotRecord.java new file mode 100644 index 0000000000..55cd0d8768 --- /dev/null +++ b/test-suite/src/test/java/io/micronaut/test/lombok/RobotRecord.java @@ -0,0 +1,9 @@ +package io.micronaut.test.lombok; + +import io.micronaut.core.annotation.Introspected; +import lombok.Builder; + +@Introspected +@Builder +public record RobotRecord(String name) { +}