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

Add an ability to introspect builders #135

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -32,4 +32,13 @@
@Retention(RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
public @interface Builder {

/**
* If true, an {@link io.micronaut.core.annotation.Introspected} annotation will be added
* on the builder so that introspection can be created for it.
*
* @return Whether the builder needs to be introspected
*/
boolean introspected() default true;

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@
@Retention(RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
public @interface SuperBuilder {

/**
* If true, an {@link io.micronaut.core.annotation.Introspected} annotation will be added
* on the builder so that introspection can be created for it.
*
* @return Whether the builder needs to be introspected
*/
boolean introspected() default true;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package io.micronaut.sourcegen.generator.visitors;

import io.micronaut.core.annotation.Creator;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.bind.annotation.Bindable;
Expand Down Expand Up @@ -71,6 +73,8 @@
@Internal
public final class BuilderAnnotationVisitor implements TypeElementVisitor<Builder, Object> {

public static final String BUILDER_INTROSPECTED_MEMBER = "introspected";

private final Set<String> processed = new HashSet<>();

@Override
Expand Down Expand Up @@ -101,6 +105,9 @@ public void visitClass(ClassElement element, VisitorContext context) {

ClassDef.ClassDefBuilder builder = ClassDef.builder(builderClassName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
if (element.booleanValue(Builder.class, BUILDER_INTROSPECTED_MEMBER).orElse(true)) {
builder.addAnnotation(Introspected.class);
}

List<PropertyElement> properties = element.getBeanProperties();
for (PropertyElement beanProperty : properties) {
Expand Down Expand Up @@ -151,7 +158,8 @@ public void visitClass(ClassElement element, VisitorContext context) {
}

private MethodDef createAllPropertiesConstructor(ClassTypeDef builderType, List<PropertyElement> properties) {
MethodDef.MethodDefBuilder builder = MethodDef.constructor();
MethodDef.MethodDefBuilder builder = MethodDef.constructor()
.addAnnotation(Creator.class);
VariableDef.This self = new VariableDef.This(builderType);
for (PropertyElement parameter : properties) {
ParameterDef parameterDef = ParameterDef.of(parameter.getName(), TypeDef.of(parameter.getType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.sourcegen.generator.visitors;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.inject.ast.ClassElement;
import io.micronaut.inject.ast.PropertyElement;
Expand All @@ -25,6 +26,7 @@
import io.micronaut.sourcegen.annotations.SuperBuilder;
import io.micronaut.sourcegen.generator.SourceGenerator;
import io.micronaut.sourcegen.generator.SourceGenerators;
import io.micronaut.sourcegen.model.AnnotationDef;
import io.micronaut.sourcegen.model.ClassDef;
import io.micronaut.sourcegen.model.ClassTypeDef;
import io.micronaut.sourcegen.model.MethodDef;
Expand All @@ -46,6 +48,8 @@
@Internal
public final class SuperBuilderAnnotationVisitor implements TypeElementVisitor<SuperBuilder, Object> {

public static final String SUPER_BUILDER_INTROSPECTED_MEMBER = "introspected";

private final Set<String> processed = new HashSet<>();

@Override
Expand Down Expand Up @@ -148,6 +152,10 @@ public void visitClass(ClassElement element, VisitorContext context) {
)
)
);
if (element.booleanValue(SuperBuilder.class, SUPER_BUILDER_INTROSPECTED_MEMBER).orElse(true)) {
builder.addAnnotation(AnnotationDef.builder(Introspected.class)
.addMember("withPrefix", "").build());
}

builder.addMethod(createSelfMethod());
builder.addMethod(BuilderAnnotationVisitor.createBuildMethod(element));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
package io.micronaut.sourcegen.example;

import java.lang.reflect.Modifier;

import io.micronaut.core.beans.BeanIntrospection;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class AnimalSuperBuilderTest {
Expand Down Expand Up @@ -61,6 +64,13 @@ public void testDog() {
}
//end::test[]

@Test
public void dogIntrospection() {
var introspection = BeanIntrospection.getIntrospection(DogSuperBuilder.class);
assertNotNull(introspection);
assertEquals(6, introspection.getBeanProperties().size());
graemerocher marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void internalTest() {
assertTrue(Modifier.isPublic(CatSuperBuilder.class.getModifiers()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package io.micronaut.sourcegen.example;

import io.micronaut.core.beans.BeanIntrospection;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class PersonBuilderTest {

Expand All @@ -36,6 +38,14 @@ public void buildsPerson() {
}
//end::test[]

@Test
public void personIntrospection() {
var introspection = BeanIntrospection.getIntrospection(PersonBuilder.class);
assertNotNull(introspection);
assertEquals(0, introspection.getBeanProperties().size());
assertEquals(3, introspection.getConstructorArguments().length);
}

@Test
public void buildsPersonWithPrimitiveDefaults() {
var person = Person2Builder.builder()
Expand Down
Loading