Skip to content

Commit

Permalink
Process constructors regradless of builder. Fixes #11432
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Dec 17, 2024
1 parent 40478d2 commit 9ae2e07
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -420,18 +420,13 @@ private void processElement(boolean metadata,
List<PropertyElement> 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<MethodElement> constructorElement = ce.getPrimaryConstructor();
constructorElement.ifPresent(constructorEl -> {
if (ArrayUtils.isNotEmpty(constructorEl.getParameters())) {
writer.visitConstructor(constructorEl);
}
});
ce.getDefaultConstructor().ifPresent(writer::visitDefaultConstructor);
}
Optional<MethodElement> 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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,6 +15,23 @@

public class LombokIntrospectedBuilderTest {

@Test
void testLombokRecordBuilder() {
BeanIntrospection<RobotRecord> introspection = BeanIntrospection.getIntrospection(RobotRecord.class);

assertTrue(introspection.hasBuilder());
BeanProperty<RobotRecord, String> property =
introspection.getRequiredProperty("name", String.class);

assertTrue(property.hasSetterOrConstructorArgument());
BeanConstructor<RobotRecord> constructor = introspection.getConstructor();
assertNotNull(constructor);
assertEquals(1, constructor.getArguments().length);

RobotRecord robot = introspection.instantiate("test");
assertEquals("test", robot.name());
}

@Test
void testLombokBuilder() {
BeanIntrospection.Builder<RobotEntity> builder = BeanIntrospection.getIntrospection(RobotEntity.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}

0 comments on commit 9ae2e07

Please sign in to comment.