Add @Generated annotation to generated Java types (#1075)

JaCoCo automatically excludes methods and classes annotated with @Generated from the coverage reports. This is very important to us as generated code should not normally be included in the coverage report. We want to measure the coverage of the code that we actually wrote and maintain, not the code that was automatically generated by tools.

By introducing a property generatedAnnotation (default value false) one could enable writing @Generated on Java types to be generated.

Co-authored-by: Nullpointer <mike.schulze@tealium.com>
This commit is contained in:
André Rouél
2025-07-08 00:31:49 +02:00
committed by GitHub
parent dbf57280ba
commit 0973774a5d
11 changed files with 141 additions and 4 deletions

View File

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

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");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@ import org.gradle.api.tasks.SourceSet;
public interface CodeGenSpec extends ModulesSpec {
DirectoryProperty getOutputDir();
Property<Boolean> getAddGeneratedAnnotation();
Property<SourceSet> getSourceSet();
Property<String> getIndent();

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");
* you may not use this file except in compliance with the License.

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");
* you may not use this file except in compliance with the License.
@@ -19,12 +19,17 @@ import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
public abstract class CodeGenTask extends ModulesTask {
@OutputDirectory
public abstract DirectoryProperty getOutputDir();
@Input
@Optional
public abstract Property<Boolean> getGeneratedAnnotation();
@Input
public abstract Property<String> getIndent();

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");
* you may not use this file except in compliance with the License.
@@ -47,6 +47,7 @@ public abstract class JavaCodeGenTask extends CodeGenTask {
getCliBaseOptions(),
getProject().file(getOutputDir()).toPath(),
getIndent().get(),
getGeneratedAnnotation().get(),
getGenerateGetters().get(),
getGenerateJavadoc().get(),
getGenerateSpringBootConfig().get(),