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

Sourcegen Builder Guide #1454

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
@@ -0,0 +1,49 @@
package io.micronaut.guides.feature;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.starter.application.ApplicationType;
import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.build.RequiresMavenLocal;
import io.micronaut.starter.build.dependencies.Dependency;
import io.micronaut.starter.build.dependencies.Scope;
import io.micronaut.starter.feature.Feature;
import io.micronaut.starter.options.Language;
import jakarta.inject.Singleton;

@Singleton
public class SourcegenJava implements RequiresMavenLocal {
public static final String ARTIFACT_ID_SOURCEGEN_ANNOTATIONS = "micronaut-sourcegen-annotations";
public static final String ARTIFACT_ID_SOURCEGEN_GENERATOR_JAVA = "micronaut-sourcegen-generator-java";
public static final String ARTIFACT_ID_SOURCEGEN_GENERATOR_KOTLIN = "micronaut-sourcegen-generator-kotlin";

@Override
public @NonNull String getName() {
return "sourcegen-generator";
}

@Override
public boolean supports(ApplicationType applicationType) {
return true;
}

@Override
public void apply(GeneratorContext generatorContext) {
addDependencies(generatorContext);
}

protected void addDependencies(GeneratorContext generatorContext) {
generatorContext.addDependency(Dependency.builder()
.lookupArtifactId(ARTIFACT_ID_SOURCEGEN_ANNOTATIONS)
.scope(Scope.COMPILE));
if (generatorContext.getLanguage() == Language.JAVA) {
generatorContext.addDependency(Dependency.builder()
.lookupArtifactId(ARTIFACT_ID_SOURCEGEN_GENERATOR_JAVA)
.scope(Scope.ANNOTATION_PROCESSOR));
} else if (generatorContext.getLanguage() == Language.KOTLIN) {
generatorContext.addDependency(Dependency.builder()
.lookupArtifactId(ARTIFACT_ID_SOURCEGEN_GENERATOR_KOTLIN)
.scope(Scope.ANNOTATION_PROCESSOR));
}
}
}

15 changes: 15 additions & 0 deletions buildSrc/src/main/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.micronaut.sourcegen</groupId>
<artifactId>micronaut-sourcegen-annotations</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.micronaut.sourcegen</groupId>
<artifactId>micronaut-sourcegen-generator-java</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.micronaut.sourcegen</groupId>
<artifactId>micronaut-sourcegen-generator-kotlin</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package example.micronaut;

import io.micronaut.sourcegen.annotations.Builder;

@Builder
public record Widget(String name, int id) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package example.micronaut;

import org.junit.jupiter.api.Test;

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

class WidgetTest {

@Test
void widgetBuilder() {
Widget testWidget = WidgetBuilder.builder()
.name("foo")
.id(1)
.build();
assertEquals("foo", testWidget.name());
assertEquals(1, testWidget.id());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package example.micronaut

import io.micronaut.sourcegen.annotations.Builder

@Builder
data class Widget(val name: String, val id: Int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package example.micronaut

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test


class WidgetTest {

@Test
fun widgetBuilder() {
val testWidget = WidgetBuilder.builder()
.name("foo")
.id(1)
.build()
assertEquals("foo", testWidget.name)
assertEquals(1, testWidget.id)
}
}
16 changes: 16 additions & 0 deletions guides/micronaut-sourcegen-builder/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"title": "Micronaut Source generation @Builder",
"intro": "Learn how to generate a Builder with Micronaut Sourcegen and the @Builder annotation.",
"authors": ["Sergio del Amo"],
"tags": [],
"categories": ["Beyond the Basics"],
"publicationDate": "2024-04-24",
"languages": ["java"],
"apps": [
{
"validateLicense": false,
"name": "default",
"features": ["sourcegen-generator"]
}
]
}
Empty file.
Loading