Prevent AotDetector to be initialized at build-time

This commit moves the condition used by `@DisabledInAotMode` to a
concrete implementation rather than using `@DisabledIf` as it causes
build initialization in a native image.

Closes gh-31705
This commit is contained in:
Stéphane Nicoll
2023-11-28 15:46:31 +01:00
parent 755fd75512
commit c92a0bd493
2 changed files with 45 additions and 3 deletions

View File

@@ -22,7 +22,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.condition.DisabledIf;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* {@code @DisabledInAotMode} signals that an annotated test class is <em>disabled</em>
@@ -48,6 +48,7 @@ import org.junit.jupiter.api.condition.DisabledIf;
* annotation.
*
* @author Sam Brannen
* @author Stephane Nicoll
* @since 6.1
* @see org.springframework.aot.AotDetector#useGeneratedArtifacts() AotDetector.useGeneratedArtifacts()
* @see org.junit.jupiter.api.condition.EnabledInNativeImage @EnabledInNativeImage
@@ -56,7 +57,6 @@ import org.junit.jupiter.api.condition.DisabledIf;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DisabledIf(value = "org.springframework.aot.AotDetector#useGeneratedArtifacts",
disabledReason = "Disabled in Spring AOT mode")
@ExtendWith(DisabledInAotModeCondition.class)
public @interface DisabledInAotMode {
}

View File

@@ -0,0 +1,42 @@
/*
* 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.test.context.aot;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.aot.AotDetector;
/**
* {@link ExecutionCondition} implementation for {@link DisabledInAotMode}.
*
* @author Stephane Nicoll
* @since 6.1.2
*/
class DisabledInAotModeCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
boolean aotEnabled = AotDetector.useGeneratedArtifacts();
if (aotEnabled) {
return ConditionEvaluationResult.disabled("Disabled in Spring AOT mode");
}
return ConditionEvaluationResult.enabled("Spring AOT mode disabled");
}
}