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

Prefer constructor over builder if present in introspections #11481

Merged
merged 1 commit into from
Jan 3, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,11 @@ private void writeIntrospectionClass(ClassWriterOutputVisitor classWriterOutputV
buildGetIndexedProperties(classWriter);
boolean hasBuilder = annotationMetadata != null &&
(annotationMetadata.isPresent(Introspected.class, "builder") || annotationMetadata.hasDeclaredAnnotation("lombok.Builder"));
if (defaultConstructor != null || constructor != null) {
writeBooleanMethod(classWriter, "hasConstructor", true);
}
if (defaultConstructor != null) {

writeInstantiateMethod(classWriter, defaultConstructor, "instantiate");
// in case invoked directly or via instantiateUnsafe
if (constructor == null) {
Expand Down Expand Up @@ -766,7 +770,7 @@ private void writeIntrospectionClass(ClassWriterOutputVisitor classWriterOutputV
}

private void writeBooleanMethod(ClassWriter classWriter, String methodName, boolean state) {
GeneratorAdapter booleanMethod = startPublicMethodZeroArgs(classWriter, boolean.class, methodName);
GeneratorAdapter booleanMethod = startPublicFinalMethodZeroArgs(classWriter, boolean.class, methodName);
booleanMethod.push(state);
booleanMethod.returnValue();
booleanMethod.visitMaxs(2, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private void processBuilderDefinition(ClassElement element, VisitorContext conte
creatorMethod,
writePrefixes,
methodElement,
null,
element.getDefaultConstructor().orElse(null),
returnType,
methodMetadata,
index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ public Builder<B> builder() {
}
}

/**
* Whether an accessible constructor exists.
* @return True if a default constructor exists
* @since 4.7.11
*/
protected boolean hasConstructor() {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have some default that is compatible with previously compiled beans

}

/**
* Reflection free bean instantiation implementation for the given arguments.
*
Expand Down Expand Up @@ -393,7 +402,11 @@ public AnnotationMetadata getAnnotationMetadata() {

@Override
public Argument<?>[] getConstructorArguments() {
return hasBuilder() ? getBuilderData().constructorArguments : constructorArguments;
if (hasConstructor()) {
return constructorArguments;
} else {
return hasBuilder() ? getBuilderData().constructorArguments : constructorArguments;
}
}

@NonNull
Expand Down
2 changes: 2 additions & 0 deletions test-suite/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ test {
exclude '**/classnotfound/**'
}

//compileTestJava.options.fork = true
//compileTestJava.options.forkOptions.jvmArgs = ['-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005']
17 changes: 17 additions & 0 deletions test-suite/src/test/java/io/micronaut/test/lombok/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.micronaut.test.lombok;

import io.micronaut.core.annotation.Introspected;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Introspected
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Book {
private Integer id;
private String title;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

public class LombokIntrospectedBuilderTest {

@Test
void testLombokNoArgsConstructor() {
BeanIntrospection<Book> introspection = BeanIntrospection.getIntrospection(Book.class);
assertEquals(0, introspection.getConstructorArguments().length);
Book book = introspection.instantiate();
assertNotNull(book);
}

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