Skip to content

Commit

Permalink
if a class has a constructor default to it in introspection instead o…
Browse files Browse the repository at this point in the history
…f builder
  • Loading branch information
graemerocher committed Jan 2, 2025
1 parent 5e1be4c commit cac6bf3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
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;
}

/**
* 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

0 comments on commit cac6bf3

Please sign in to comment.