From 2eba3510f714cb8f0373a72cee32091a349adb83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Fri, 8 Dec 2023 11:51:14 +0100 Subject: [PATCH] Annotate generated classes with @Generated This commit annotates every generated class with `@Generated` so that build tools can recognize and ignore those types if necessary. Closes gh-30824 --- .../modules/ROOT/pages/core/aot.adoc | 4 +++ .../aot/generate/Generated.java | 35 +++++++++++++++++++ .../aot/generate/GeneratedClass.java | 1 + .../aot/generate/GeneratedClassTests.java | 8 +++++ 4 files changed, 48 insertions(+) create mode 100644 spring-core/src/main/java/org/springframework/aot/generate/Generated.java diff --git a/framework-docs/modules/ROOT/pages/core/aot.adoc b/framework-docs/modules/ROOT/pages/core/aot.adoc index cba6785e6d..03b5bcd43e 100644 --- a/framework-docs/modules/ROOT/pages/core/aot.adoc +++ b/framework-docs/modules/ROOT/pages/core/aot.adoc @@ -156,6 +156,7 @@ Java:: /** * Bean definitions for {@link DataSourceConfiguration} */ + @Generated public class DataSourceConfiguration__BeanDefinitions { /** * Get the bean definition for 'dataSourceConfiguration' @@ -190,6 +191,9 @@ Java:: NOTE: The exact code generated may differ depending on the exact nature of your bean definitions. +TIP: Each generated class is annotated with `org.springframework.aot.generate.Generated` to +identify them if they need to be excluded, for instance by static analysis tools. + The generated code above creates bean definitions equivalent to the `@Configuration` class, but in a direct way and without the use of reflection if at all possible. There is a bean definition for `dataSourceConfiguration` and one for `dataSourceBean`. When a `datasource` instance is required, a `BeanInstanceSupplier` is called. diff --git a/spring-core/src/main/java/org/springframework/aot/generate/Generated.java b/spring-core/src/main/java/org/springframework/aot/generate/Generated.java new file mode 100644 index 0000000000..95574e2480 --- /dev/null +++ b/spring-core/src/main/java/org/springframework/aot/generate/Generated.java @@ -0,0 +1,35 @@ +/* + * Copyright 2002-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.aot.generate; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Indicate that the type has been generated ahead of time. + * + * @author Stephane Nicoll + * @since 6.1.2 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Generated { +} diff --git a/spring-core/src/main/java/org/springframework/aot/generate/GeneratedClass.java b/spring-core/src/main/java/org/springframework/aot/generate/GeneratedClass.java index 0cf0507e4b..2b1caa6d13 100644 --- a/spring-core/src/main/java/org/springframework/aot/generate/GeneratedClass.java +++ b/spring-core/src/main/java/org/springframework/aot/generate/GeneratedClass.java @@ -142,6 +142,7 @@ public final class GeneratedClass { private TypeSpec.Builder apply() { TypeSpec.Builder type = getBuilder(this.type); + type.addAnnotation(Generated.class); this.methods.doWithMethodSpecs(type::addMethod); this.declaredClasses.values().forEach(declaredClass -> type.addType(declaredClass.apply().build())); diff --git a/spring-core/src/test/java/org/springframework/aot/generate/GeneratedClassTests.java b/spring-core/src/test/java/org/springframework/aot/generate/GeneratedClassTests.java index 23802ed069..0f8adeb35f 100644 --- a/spring-core/src/test/java/org/springframework/aot/generate/GeneratedClassTests.java +++ b/spring-core/src/test/java/org/springframework/aot/generate/GeneratedClassTests.java @@ -94,6 +94,14 @@ class GeneratedClassTests { assertThat(innerGeneratedClass).isSameAs(innerGeneratedClass2).isSameAs(innerGeneratedClass3); } + @Test + void generateJavaFileIsAnnotatedWithGenerated() { + GeneratedClass generatedClass = createGeneratedClass(TEST_CLASS_NAME); + assertThat(generatedClass.generateJavaFile().toString()) + .contains("@Generated") + .contains("import " + Generated.class.getName() + ";"); + } + @Test void generateJavaFileIncludesGeneratedMethods() { GeneratedClass generatedClass = createGeneratedClass(TEST_CLASS_NAME);