Add Kotlin support for "addGeneratedAnnotation" flag (#1115)

This adds logic so that the Kotlin code generator also supports the
"addGeneratedAnnotation" flag.

Also:
* Add Antora documentation
* Adjust names (generated-annotation -> add-generated-annotation)
* Adjust doc comments
This commit is contained in:
Daniel Chao
2025-07-08 14:05:15 -07:00
committed by GitHub
parent 3a35be6311
commit 48ad4386c8
14 changed files with 76 additions and 15 deletions

View File

@@ -29,6 +29,13 @@ Default: (not set) +
Flag that indicates to generate classes that implement `java.io.Serializable`. Flag that indicates to generate classes that implement `java.io.Serializable`.
==== ====
.--add-generated-annotation
[%collapsible]
====
Default: (not set) +
Flag that indicates to add the `org.pkl.config.java.Generated` annotation to generated types.
====
.--rename .--rename
[%collapsible] [%collapsible]
==== ====

View File

@@ -44,6 +44,14 @@ Example: `implementSerializable = true` +
Whether to generate classes that implement `java.io.Serializable`. Whether to generate classes that implement `java.io.Serializable`.
==== ====
.addGeneratedAnnotation: Property<Boolean>
[%collapsible]
====
Default: `false` +
Example: `addGeneratedAnnotation = true` +
Whether to add the `org.pkl.config.java.Generated` annotation to generated types.
====
.renames: MapProperty<String, String> .renames: MapProperty<String, String>
[%collapsible] [%collapsible]
==== ====

View File

@@ -29,8 +29,8 @@ data class CliJavaCodeGeneratorOptions(
/** The characters to use for indenting generated source code. */ /** The characters to use for indenting generated source code. */
val indent: String = " ", val indent: String = " ",
/** Whether to add a <code>@Generated</code> annotation to the types to be generated. */ /** Whether to add a `@Generated` annotation to the types to be generated. */
val generatedAnnotation: Boolean = false, val addGeneratedAnnotation: Boolean = false,
/** /**
* Whether to generate public getter methods and private/protected fields instead of public * Whether to generate public getter methods and private/protected fields instead of public
@@ -85,7 +85,7 @@ data class CliJavaCodeGeneratorOptions(
internal fun toJavaCodeGeneratorOptions() = internal fun toJavaCodeGeneratorOptions() =
JavaCodeGeneratorOptions( JavaCodeGeneratorOptions(
indent, indent,
generatedAnnotation, addGeneratedAnnotation,
generateGetters, generateGetters,
generateJavadoc, generateJavadoc,
generateSpringBootConfig, generateSpringBootConfig,

View File

@@ -47,8 +47,8 @@ data class JavaCodeGeneratorOptions(
/** The characters to use for indenting generated Java code. */ /** The characters to use for indenting generated Java code. */
val indent: String = " ", val indent: String = " ",
/** Whether to add a <code>@Generated</code> annotation to the types to be generated. */ /** Adds the `org.pkl.config.java.Generated` annotation to the classes to be generated. */
val generatedAnnotation: Boolean = false, val addGeneratedAnnotation: Boolean = false,
/** /**
* Whether to generate public getter methods and protected final fields instead of public final * Whether to generate public getter methods and protected final fields instead of public final
@@ -563,7 +563,7 @@ class JavaCodeGenerator(
fun generateClass(): TypeSpec.Builder { fun generateClass(): TypeSpec.Builder {
val builder = val builder =
TypeSpec.classBuilder(javaPoetClassName.simpleName()).addModifiers(Modifier.PUBLIC) TypeSpec.classBuilder(javaPoetClassName.simpleName()).addModifiers(Modifier.PUBLIC)
if (codegenOptions.generatedAnnotation) { if (codegenOptions.addGeneratedAnnotation) {
val name = ClassName.get("org.pkl.config.java", "Generated") val name = ClassName.get("org.pkl.config.java", "Generated")
val generated = AnnotationSpec.builder(name).build() val generated = AnnotationSpec.builder(name).build()
builder.addAnnotation(generated) builder.addAnnotation(generated)

View File

@@ -55,9 +55,9 @@ class PklJavaCodegenCommand : ModulesCommand(name = "pkl-codegen-java", helpLink
) )
.default(defaults.indent) .default(defaults.indent)
private val generatedAnnotation: Boolean by private val addGeneratedAnnotation: Boolean by
option( option(
names = arrayOf("--generated-annotation"), names = arrayOf("--add-generated-annotation"),
help = "Whether to add a @Generated annotation to the types to be generated.", help = "Whether to add a @Generated annotation to the types to be generated.",
) )
.flag() .flag()
@@ -139,7 +139,7 @@ class PklJavaCodegenCommand : ModulesCommand(name = "pkl-codegen-java", helpLink
base = baseOptions.baseOptions(modules, projectOptions), base = baseOptions.baseOptions(modules, projectOptions),
outputDir = outputDir, outputDir = outputDir,
indent = indent, indent = indent,
generatedAnnotation = generatedAnnotation, addGeneratedAnnotation = addGeneratedAnnotation,
generateGetters = generateGetters, generateGetters = generateGetters,
generateJavadoc = generateJavadoc, generateJavadoc = generateJavadoc,
generateSpringBootConfig = generateSpringBoot, generateSpringBootConfig = generateSpringBoot,

View File

@@ -871,7 +871,7 @@ class JavaCodeGeneratorTest {
} }
""" """
.trimIndent(), .trimIndent(),
JavaCodeGeneratorOptions(generatedAnnotation = true), JavaCodeGeneratorOptions(addGeneratedAnnotation = true),
) )
assertThat(javaCode).compilesSuccessfully().isEqualToResourceFile("GeneratedAnnotation.jva") assertThat(javaCode).compilesSuccessfully().isEqualToResourceFile("GeneratedAnnotation.jva")

View File

@@ -38,6 +38,9 @@ data class CliKotlinCodeGeneratorOptions(
/** Whether generated classes should implement [java.io.Serializable]. */ /** Whether generated classes should implement [java.io.Serializable]. */
val implementSerializable: Boolean = false, val implementSerializable: Boolean = false,
/** Whether to add the `@Generated` annotation to types */
val addGeneratedAnnotation: Boolean = false,
/** /**
* A rename mapping for class names. * A rename mapping for class names.
* *
@@ -57,6 +60,7 @@ data class CliKotlinCodeGeneratorOptions(
generateKdoc, generateKdoc,
generateSpringBootConfig, generateSpringBootConfig,
implementSerializable, implementSerializable,
addGeneratedAnnotation,
renames, renames,
) )
} }

View File

@@ -40,6 +40,9 @@ data class KotlinCodeGeneratorOptions(
/** Whether to generate classes that implement [java.io.Serializable]. */ /** Whether to generate classes that implement [java.io.Serializable]. */
val implementSerializable: Boolean = false, val implementSerializable: Boolean = false,
/** Whether to add the `@Generated` to generated types. */
val addGeneratedAnnotation: Boolean = false,
/** /**
* A mapping from Pkl module name prefixes to their replacements. * A mapping from Pkl module name prefixes to their replacements.
* *
@@ -455,6 +458,9 @@ class KotlinCodeGenerator(
if (options.generateSpringBootConfig) { if (options.generateSpringBootConfig) {
generateSpringBootAnnotations(builder) generateSpringBootAnnotations(builder)
} }
if (options.addGeneratedAnnotation) {
builder.addAnnotation(ClassName("org.pkl.config.java", "Generated"))
}
builder.primaryConstructor(generateConstructor()) builder.primaryConstructor(generateConstructor())
@@ -498,6 +504,9 @@ class KotlinCodeGenerator(
if (options.generateSpringBootConfig) { if (options.generateSpringBootConfig) {
generateSpringBootAnnotations(builder) generateSpringBootAnnotations(builder)
} }
if (options.addGeneratedAnnotation) {
builder.addAnnotation(ClassName("org.pkl.config.java", "Generated"))
}
builder.primaryConstructor(generateConstructor()) builder.primaryConstructor(generateConstructor())

View File

@@ -79,6 +79,12 @@ class PklKotlinCodegenCommand : ModulesCommand(name = "pkl-codegen-kotlin", help
) )
.flag() .flag()
private val addGeneratedAnnotation: Boolean by
option(
names = arrayOf("--add-generated-annotation"),
help = "Whether to add a @Generated annotation to the types to be generated.",
)
.flag()
private val renames: Map<String, String> by private val renames: Map<String, String> by
option( option(
names = arrayOf("--rename"), names = arrayOf("--rename"),
@@ -105,6 +111,7 @@ class PklKotlinCodegenCommand : ModulesCommand(name = "pkl-codegen-kotlin", help
generateKdoc = generateKdoc, generateKdoc = generateKdoc,
generateSpringBootConfig = generateSpringboot, generateSpringBootConfig = generateSpringboot,
implementSerializable = implementSerializable, implementSerializable = implementSerializable,
addGeneratedAnnotation = addGeneratedAnnotation,
renames = renames, renames = renames,
) )
CliKotlinCodeGenerator(options).run() CliKotlinCodeGenerator(options).run()

View File

@@ -2029,6 +2029,30 @@ class KotlinCodeGeneratorTest {
) )
} }
@Test
fun `add generated annotation`() {
val files =
KotlinCodeGeneratorOptions(addGeneratedAnnotation = true)
.generateFiles("com.example.MyModule" to "foo: String")
assertThat(files).containsKey("kotlin/com/example/MyModule.kt")
assertThat(files["kotlin/com/example/MyModule.kt"])
.isEqualTo(
"""
package com.example
import kotlin.String
import org.pkl.config.java.Generated
@Generated
data class MyModule(
val foo: String
)
"""
.trimIndent()
)
}
private fun Map<String, String>.validateContents( private fun Map<String, String>.validateContents(
vararg assertions: kotlin.Pair<String, List<String>> vararg assertions: kotlin.Pair<String, List<String>>
) { ) {

View File

@@ -190,7 +190,6 @@ public class PklPlugin implements Plugin<Project> {
configureBaseSpec(spec); configureBaseSpec(spec);
configureCodeGenSpec(spec); configureCodeGenSpec(spec);
spec.getAddGeneratedAnnotation().convention(false);
spec.getGenerateGetters().convention(false); spec.getGenerateGetters().convention(false);
spec.getGenerateJavadoc().convention(false); spec.getGenerateJavadoc().convention(false);
// Not using `convention()` so that users can disable generation of // Not using `convention()` so that users can disable generation of
@@ -207,7 +206,6 @@ public class PklPlugin implements Plugin<Project> {
.configure( .configure(
task -> { task -> {
configureCodeGenTask(task, spec); configureCodeGenTask(task, spec);
task.getGeneratedAnnotation().set(spec.getAddGeneratedAnnotation());
task.getGenerateGetters().set(spec.getGenerateGetters()); task.getGenerateGetters().set(spec.getGenerateGetters());
task.getGenerateJavadoc().set(spec.getGenerateJavadoc()); task.getGenerateJavadoc().set(spec.getGenerateJavadoc());
task.getParamsAnnotation().set(spec.getParamsAnnotation()); task.getParamsAnnotation().set(spec.getParamsAnnotation());
@@ -354,6 +352,8 @@ public class PklPlugin implements Plugin<Project> {
spec.getImplementSerializable().convention(false); spec.getImplementSerializable().convention(false);
spec.getAddGeneratedAnnotation().convention(false);
configureCodeGenSpecModulePath(spec); configureCodeGenSpecModulePath(spec);
} }
@@ -454,6 +454,7 @@ public class PklPlugin implements Plugin<Project> {
task.getOutputDir().set(spec.getOutputDir()); task.getOutputDir().set(spec.getOutputDir());
task.getGenerateSpringBootConfig().set(spec.getGenerateSpringBootConfig()); task.getGenerateSpringBootConfig().set(spec.getGenerateSpringBootConfig());
task.getImplementSerializable().set(spec.getImplementSerializable()); task.getImplementSerializable().set(spec.getImplementSerializable());
task.getAddGeneratedAnnotation().set(spec.getAddGeneratedAnnotation());
task.getRenames().set(spec.getRenames()); task.getRenames().set(spec.getRenames());
} }

View File

@@ -28,7 +28,7 @@ public abstract class CodeGenTask extends ModulesTask {
@Input @Input
@Optional @Optional
public abstract Property<Boolean> getGeneratedAnnotation(); public abstract Property<Boolean> getAddGeneratedAnnotation();
@Input @Input
public abstract Property<String> getIndent(); public abstract Property<String> getIndent();

View File

@@ -47,7 +47,7 @@ public abstract class JavaCodeGenTask extends CodeGenTask {
getCliBaseOptions(), getCliBaseOptions(),
getProject().file(getOutputDir()).toPath(), getProject().file(getOutputDir()).toPath(),
getIndent().get(), getIndent().get(),
getGeneratedAnnotation().get(), getAddGeneratedAnnotation().get(),
getGenerateGetters().get(), getGenerateGetters().get(),
getGenerateJavadoc().get(), getGenerateJavadoc().get(),
getGenerateSpringBootConfig().get(), getGenerateSpringBootConfig().get(),

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. * Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@ public abstract class KotlinCodeGenTask extends CodeGenTask {
getGenerateKdoc().get(), getGenerateKdoc().get(),
getGenerateSpringBootConfig().get(), getGenerateSpringBootConfig().get(),
getImplementSerializable().get(), getImplementSerializable().get(),
getAddGeneratedAnnotation().get(),
getRenames().get())) getRenames().get()))
.run(); .run();
} }