Skip to content

Commit

Permalink
Add exception and failure analyzer for missing AOT initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Apr 30, 2024
1 parent 90b1c18 commit 3c935c9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2012-2024 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.boot;

/**
* Thrown when the AOT initializer couldn't be found.
* @author Moritz Halbritter
*/
class AotInitializerNotFoundException extends RuntimeException {
private final Class<?> mainClass;

private final String initializerClassName;

AotInitializerNotFoundException(Class<?> mainClass, String initializerClassName) {
this.mainClass = mainClass;
this.initializerClassName = initializerClassName;
}

Class<?> getMainClass() {
return this.mainClass;
}

String getInitializerClassName() {
return this.initializerClassName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2012-2024 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.boot;

import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

/**
* An {@link AbstractFailureAnalyzer} that performs analysis of failures caused by a
* {@link AotInitializerNotFoundException}.
*
* @author Moritz Halbritter
*/
class AotInitializerNotFoundFailureAnalyzer extends AbstractFailureAnalyzer<AotInitializerNotFoundException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, AotInitializerNotFoundException cause) {
return new FailureAnalysis("Startup with AOT mode enabled failed: AOT initializer %s could not be found".formatted(cause.getInitializerClassName()),
"Consider the following:\n"
+ "\tDid you build the application with enabled AOT processing?\n"
+ "\tIs the main class %s correct?\n".formatted(cause.getMainClass().getName())
+ "\tIf you want to run the application in regular mode, remove the system property 'spring.aot.enabled'",
cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,9 @@ private void addAotGeneratedInitializerIfNecessary(List<ApplicationContextInitia
initializers.stream().filter(AotApplicationContextInitializer.class::isInstance).toList());
if (aotInitializers.isEmpty()) {
String initializerClassName = this.mainApplicationClass.getName() + "__ApplicationContextInitializer";
Assert.state(ClassUtils.isPresent(initializerClassName, getClassLoader()),
"You are starting the application with AOT mode enabled but AOT processing hasn't happened. "
+ "Please build your application with enabled AOT processing first, "
+ "or remove the system property 'spring.aot.enabled' to run the application in regular mode");
if (!ClassUtils.isPresent(initializerClassName, getClassLoader())) {
throw new AotInitializerNotFoundException(this.mainApplicationClass, initializerClassName);
}
aotInitializers.add(AotApplicationContextInitializer.forInitializerClasses(initializerClassName));
}
initializers.removeAll(aotInitializers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ org.springframework.boot.reactor.ReactorEnvironmentPostProcessor

# Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.AotInitializerNotFoundFailureAnalyzer,\
org.springframework.boot.context.config.ConfigDataNotFoundFailureAnalyzer,\
org.springframework.boot.context.properties.IncompatibleConfigurationFailureAnalyzer,\
org.springframework.boot.context.properties.NotConstructorBoundInjectionFailureAnalyzer,\
Expand Down

0 comments on commit 3c935c9

Please sign in to comment.