Skip to content

Commit

Permalink
Annotate generated classes with @generated
Browse files Browse the repository at this point in the history
This commit annotates every generated class with `@Generated` so that
build tools can recognize and ignore those types if necessary.

Closes spring-projectsgh-29484
  • Loading branch information
snicoll committed Dec 8, 2023
1 parent 3b4c7a8 commit 322460a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ public CodeBlock generateInstanceSupplierCode(GenerationContext generationContex
class DeprecationTests {

private static final TestCompiler TEST_COMPILER = TestCompiler.forSystem()
.withCompilerOptions("-Xlint:all", "-Xlint:-rawtypes", "-Werror");
.withCompilerOptions("-Xlint:all", "-Xlint:-rawtypes", "-Xlint:-processing", "-Werror");

@Test
void generateBeanDefinitionMethodWithDeprecatedTargetClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
class CodeWarningsTests {

private static final TestCompiler TEST_COMPILER = TestCompiler.forSystem()
.withCompilerOptions("-Xlint:all", "-Werror");
.withCompilerOptions("-Xlint:all", "-Xlint:-processing", "-Werror");

private final CodeWarnings codeWarnings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void generateWhenHasStaticFactoryMethodCheckedException() {
class DeprecationTests {

private static final TestCompiler TEST_COMPILER = TestCompiler.forSystem()
.withCompilerOptions("-Xlint:all", "-Xlint:-rawtypes", "-Werror");
.withCompilerOptions("-Xlint:all", "-Xlint:-rawtypes", "-Xlint:-processing", "-Werror");

@Test
@Disabled("Need to move to a separate method so that the warning can be suppressed")
Expand Down Expand Up @@ -323,7 +323,7 @@ private void compileAndCheckWarnings(BeanDefinition beanDefinition) {
class DeprecationForRemovalTests {

private static final TestCompiler TEST_COMPILER = TestCompiler.forSystem()
.withCompilerOptions("-Xlint:all", "-Xlint:-rawtypes", "-Werror");
.withCompilerOptions("-Xlint:all", "-Xlint:-rawtypes", "-Xlint:-processing", "-Werror");

@Test
@Disabled("Need to move to a separate method so that the warning can be suppressed")
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ JavaFile generateJavaFile() {

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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ void getOrAddWhenRepeatReturnsSameGeneratedClass() {
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);
Expand Down

0 comments on commit 322460a

Please sign in to comment.