Skip to content

Commit a6dd8b7

Browse files
committed
Add exception and failure analyzer for missing AOT initializer
The diagnostics text include the name of the AOT initializer class and the main class name. It also tells the user how to switch of the AOT mode. Closes gh-38645
1 parent 883810b commit a6dd8b7

File tree

6 files changed

+137
-6
lines changed

6 files changed

+137
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
/**
20+
* Exception thrown when the AOT initializer couldn't be found.
21+
*
22+
* @author Moritz Halbritter
23+
* @since 3.2.6
24+
*/
25+
public class AotInitializerNotFoundException extends RuntimeException {
26+
27+
private final Class<?> mainClass;
28+
29+
public AotInitializerNotFoundException(Class<?> mainClass, String initializerClassName) {
30+
super("Startup with AOT mode enabled failed: AOT initializer %s could not be found"
31+
.formatted(initializerClassName));
32+
this.mainClass = mainClass;
33+
}
34+
35+
public Class<?> getMainClass() {
36+
return this.mainClass;
37+
}
38+
39+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,9 @@ private void addAotGeneratedInitializerIfNecessary(List<ApplicationContextInitia
439439
initializers.stream().filter(AotApplicationContextInitializer.class::isInstance).toList());
440440
if (aotInitializers.isEmpty()) {
441441
String initializerClassName = this.mainApplicationClass.getName() + "__ApplicationContextInitializer";
442-
Assert.state(ClassUtils.isPresent(initializerClassName, getClassLoader()),
443-
"You are starting the application with AOT mode enabled but AOT processing hasn't happened. "
444-
+ "Please build your application with enabled AOT processing first, "
445-
+ "or remove the system property 'spring.aot.enabled' to run the application in regular mode");
442+
if (!ClassUtils.isPresent(initializerClassName, getClassLoader())) {
443+
throw new AotInitializerNotFoundException(this.mainApplicationClass, initializerClassName);
444+
}
446445
aotInitializers.add(AotApplicationContextInitializer.forInitializerClasses(initializerClassName));
447446
}
448447
initializers.removeAll(aotInitializers);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.diagnostics.analyzer;
18+
19+
import org.springframework.boot.AotInitializerNotFoundException;
20+
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
21+
import org.springframework.boot.diagnostics.FailureAnalysis;
22+
23+
/**
24+
* An {@link AbstractFailureAnalyzer} that performs analysis of failures caused by a
25+
* {@link AotInitializerNotFoundException}.
26+
*
27+
* @author Moritz Halbritter
28+
*/
29+
class AotInitializerNotFoundFailureAnalyzer extends AbstractFailureAnalyzer<AotInitializerNotFoundException> {
30+
31+
@Override
32+
protected FailureAnalysis analyze(Throwable rootFailure, AotInitializerNotFoundException cause) {
33+
return new FailureAnalysis(cause.getMessage(), "Consider the following:\n"
34+
+ "\tDid you build the application with enabled AOT processing?\n"
35+
+ "\tIs the main class %s correct?\n".formatted(cause.getMainClass().getName())
36+
+ "\tIf you want to run the application in regular mode, remove the system property 'spring.aot.enabled'",
37+
cause);
38+
}
39+
40+
}

spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ org.springframework.boot.diagnostics.FailureAnalyzer=\
6464
org.springframework.boot.context.config.ConfigDataNotFoundFailureAnalyzer,\
6565
org.springframework.boot.context.properties.IncompatibleConfigurationFailureAnalyzer,\
6666
org.springframework.boot.context.properties.NotConstructorBoundInjectionFailureAnalyzer,\
67+
org.springframework.boot.diagnostics.analyzer.AotInitializerNotFoundFailureAnalyzer,\
6768
org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\
6869
org.springframework.boot.diagnostics.analyzer.BeanDefinitionOverrideFailureAnalyzer,\
6970
org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1445,8 +1445,8 @@ void shouldReportFriendlyErrorIfAotInitializerNotFound() {
14451445
application.setMainApplicationClass(TestSpringApplication.class);
14461446
System.setProperty(AotDetector.AOT_ENABLED, "true");
14471447
try {
1448-
assertThatIllegalStateException().isThrownBy(application::run)
1449-
.withMessageContaining("but AOT processing hasn't happened");
1448+
assertThatExceptionOfType(AotInitializerNotFoundException.class).isThrownBy(application::run)
1449+
.withMessageMatching("^.+AOT initializer .+ could not be found$");
14501450
}
14511451
finally {
14521452
System.clearProperty(AotDetector.AOT_ENABLED);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.diagnostics.analyzer;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.AotInitializerNotFoundException;
22+
import org.springframework.boot.diagnostics.FailureAnalysis;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link AotInitializerNotFoundFailureAnalyzer}.
28+
*
29+
* @author Moritz Halbritter
30+
*/
31+
class AotInitializerNotFoundFailureAnalyzerTests {
32+
33+
@Test
34+
void shouldAnalyze() {
35+
FailureAnalysis analysis = analyze();
36+
assertThat(analysis.getDescription()).isEqualTo(
37+
"Startup with AOT mode enabled failed: AOT initializer class org.springframework.boot.diagnostics.analyzer.AotInitializerNotFoundFailureAnalyzerTests__ApplicationContextInitializer could not be found");
38+
assertThat(analysis.getAction()).isEqualTo(
39+
"""
40+
Consider the following:
41+
\tDid you build the application with enabled AOT processing?
42+
\tIs the main class org.springframework.boot.diagnostics.analyzer.AotInitializerNotFoundFailureAnalyzerTests correct?
43+
\tIf you want to run the application in regular mode, remove the system property 'spring.aot.enabled'""");
44+
}
45+
46+
private FailureAnalysis analyze() {
47+
return new AotInitializerNotFoundFailureAnalyzer()
48+
.analyze(new AotInitializerNotFoundException(AotInitializerNotFoundFailureAnalyzerTests.class,
49+
AotInitializerNotFoundFailureAnalyzerTests.class + "__ApplicationContextInitializer"));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)