forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce NegatableSpringBootCondition for negating condition
Closes spring-projectsGH-39614
- Loading branch information
Showing
6 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...n/java/org/springframework/boot/autoconfigure/condition/NegatableSpringBootCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.autoconfigure.condition; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Map; | ||
|
||
import org.springframework.context.annotation.Condition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.ResolvableType; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Base of all negatable {@link Condition} implementations used with Spring Boot. | ||
* | ||
* @author Yanming Zhou | ||
*/ | ||
public abstract class NegatableSpringBootCondition<T extends Annotation> extends SpringBootCondition { | ||
|
||
public static final String NEGATING_ATTRIBUTE_NAME = "negating"; | ||
|
||
@Override | ||
public final boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
boolean result = super.matches(context, metadata); | ||
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(getAnnotationName()); | ||
if (annotationAttributes != null && annotationAttributes.containsKey(NEGATING_ATTRIBUTE_NAME)) { | ||
if ((Boolean) annotationAttributes.get(NEGATING_ATTRIBUTE_NAME)) { | ||
result = !result; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
protected String getAnnotationName() { | ||
Class<?> clazz = ResolvableType.forClass(getClass()) | ||
.as(NegatableSpringBootCondition.class) | ||
.getGeneric() | ||
.resolve(); | ||
Assert.state(clazz != null, "Type argument of NegatableSpringBootCondition should be present"); | ||
return clazz.getName(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...a/org/springframework/boot/autoconfigure/condition/NegatableSpringBootConditionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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.autoconfigure.condition; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link NegatableSpringBootCondition}. | ||
* | ||
* @author Yanming Zhou | ||
*/ | ||
class NegatableSpringBootConditionTests { | ||
|
||
@Test | ||
void notNegating() { | ||
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); | ||
assertThat(context.containsBean("foo")).isTrue(); | ||
} | ||
|
||
@Test | ||
void negating() { | ||
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); | ||
assertThat(context.containsBean("bar")).isFalse(); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class Config { | ||
|
||
@Bean | ||
@AlwaysTrueConditional | ||
String foo() { | ||
return "bean"; | ||
} | ||
|
||
@Bean | ||
@AlwaysTrueConditional(negating = true) | ||
String bar() { | ||
return "bean"; | ||
} | ||
|
||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Conditional(AlwaysTrueCondition.class) | ||
public @interface AlwaysTrueConditional { | ||
|
||
boolean negating() default false; | ||
|
||
} | ||
|
||
static class AlwaysTrueCondition extends NegatableSpringBootCondition<AlwaysTrueConditional> { | ||
|
||
@Override | ||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
return ConditionOutcome.match(); | ||
} | ||
|
||
} | ||
|
||
} |