Suppress warnings in JsonMixin AOT-generated code

Closes gh-48564
This commit is contained in:
Stéphane Nicoll
2025-12-17 09:41:09 +01:00
parent 8273bf4aa0
commit 2754aaef57
2 changed files with 20 additions and 10 deletions
@@ -33,6 +33,7 @@ import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments;
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragmentsDecorator;
import org.springframework.beans.factory.aot.CodeWarnings;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.CodeBlock;
@@ -87,9 +88,15 @@ class JsonMixinModuleEntriesBeanRegistrationAotProcessor implements BeanRegistra
method.addModifiers(Modifier.PRIVATE, Modifier.STATIC);
method.returns(BEAN_TYPE);
CodeBlock.Builder code = CodeBlock.builder();
code.add("return $T.create(", JsonMixinModuleEntries.class).beginControlFlow("(mixins) ->");
entries.doWithEntry(this.classLoader, (type, mixin) -> addEntryCode(code, type, mixin));
CodeWarnings codeWarnings = new CodeWarnings();
codeWarnings.detectDeprecation(BEAN_TYPE);
code.add("return $T.create(", BEAN_TYPE).beginControlFlow("(mixins) ->");
entries.doWithEntry(this.classLoader, (type, mixin) -> {
codeWarnings.detectDeprecation(type, mixin);
addEntryCode(code, type, mixin);
});
code.endControlFlow(")");
codeWarnings.suppress(method);
method.addCode(code.build());
});
return generatedMethod.toMethodReference().toCodeBlock();
@@ -109,14 +109,17 @@ class JsonMixinModuleEntriesBeanRegistrationAotProcessorTests {
@SuppressWarnings("unchecked")
private void compile(BiConsumer<GenericApplicationContext, Compiled> result) {
ClassName className = processAheadOfTime();
TestCompiler.forSystem().with(this.generationContext).compile((compiled) -> {
GenericApplicationContext freshApplicationContext = new GenericApplicationContext();
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
.getInstance(ApplicationContextInitializer.class, className.toString());
initializer.initialize(freshApplicationContext);
freshApplicationContext.refresh();
result.accept(freshApplicationContext, compiled);
});
TestCompiler.forSystem()
.withCompilerOptions("-Xlint:all", "-Werror")
.with(this.generationContext)
.compile((compiled) -> {
GenericApplicationContext freshApplicationContext = new GenericApplicationContext();
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
.getInstance(ApplicationContextInitializer.class, className.toString());
initializer.initialize(freshApplicationContext);
freshApplicationContext.refresh();
result.accept(freshApplicationContext, compiled);
});
}
private void registerEntries(Class<?>... basePackageClasses) {