Skip to content

Commit 75bde00

Browse files
committed
Generate auto-configuration OnWebApplication data
Update the auto-configuration annotation processor to generate properties for `@OnWebApplication`. See gh-13328
1 parent c2f8398 commit 75bde00

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/java/org/springframework/boot/autoconfigureprocessor/AutoConfigureAnnotationProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"org.springframework.boot.autoconfigure.condition.ConditionalOnClass",
5454
"org.springframework.boot.autoconfigure.condition.ConditionalOnBean",
5555
"org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate",
56+
"org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication",
5657
"org.springframework.boot.autoconfigure.AutoConfigureBefore",
5758
"org.springframework.boot.autoconfigure.AutoConfigureAfter",
5859
"org.springframework.boot.autoconfigure.AutoConfigureOrder" })
@@ -85,6 +86,8 @@ protected void addAnnotations(Map<String, String> annotations) {
8586
"org.springframework.boot.autoconfigure.condition.ConditionalOnBean");
8687
annotations.put("ConditionalOnSingleCandidate",
8788
"org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate");
89+
annotations.put("ConditionalOnWebApplication",
90+
"org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication");
8891
annotations.put("AutoConfigureBefore",
8992
"org.springframework.boot.autoconfigure.AutoConfigureBefore");
9093
annotations.put("AutoConfigureAfter",
@@ -99,6 +102,7 @@ private void addValueExtractors(Map<String, ValueExtractor> attributes) {
99102
attributes.put("ConditionalOnBean", new OnBeanConditionValueExtractor());
100103
attributes.put("ConditionalOnSingleCandidate",
101104
new OnBeanConditionValueExtractor());
105+
attributes.put("ConditionalOnWebApplication", ValueExtractor.allFrom("type"));
102106
attributes.put("AutoConfigureBefore", ValueExtractor.allFrom("value", "name"));
103107
attributes.put("AutoConfigureAfter", ValueExtractor.allFrom("value", "name"));
104108
attributes.put("AutoConfigureOrder", ValueExtractor.allFrom("value"));

spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/test/java/org/springframework/boot/autoconfigureprocessor/AutoConfigureAnnotationProcessorTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void createCompiler() throws IOException {
5050
@Test
5151
public void annotatedClass() throws Exception {
5252
Properties properties = compile(TestClassConfiguration.class);
53-
assertThat(properties).hasSize(5);
53+
assertThat(properties).hasSize(6);
5454
assertThat(properties).containsEntry(
5555
"org.springframework.boot.autoconfigureprocessor."
5656
+ "TestClassConfiguration.ConditionalOnClass",
@@ -73,6 +73,10 @@ public void annotatedClass() throws Exception {
7373
"org.springframework.boot.autoconfigureprocessor."
7474
+ "TestClassConfiguration.ConditionalOnSingleCandidate",
7575
"java.io.OutputStream");
76+
assertThat(properties).containsEntry(
77+
"org.springframework.boot.autoconfigureprocessor."
78+
+ "TestClassConfiguration.ConditionalOnWebApplication",
79+
"SERVLET");
7680
}
7781

7882
@Test
@@ -124,7 +128,7 @@ public void annotatedClassWithOrder() throws Exception {
124128
}
125129

126130
private Properties compile(Class<?>... types) throws IOException {
127-
TestConditionMetadataAnnotationProcessor processor = new TestConditionMetadataAnnotationProcessor(
131+
TestAutoConfigureAnnotationProcessor processor = new TestAutoConfigureAnnotationProcessor(
128132
this.compiler.getOutputLocation());
129133
this.compiler.getTask(types).call(processor);
130134
return processor.getWrittenProperties();
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@
3434
"org.springframework.boot.autoconfigureprocessor.TestConditionalOnClass",
3535
"org.springframework.boot.autoconfigure.condition.TestConditionalOnBean",
3636
"org.springframework.boot.autoconfigure.condition.TestConditionalOnSingleCandidate",
37+
"org.springframework.boot.autoconfigure.condition.TestConditionalOnWebApplication",
3738
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigureBefore",
3839
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigureAfter",
3940
"org.springframework.boot.autoconfigureprocessor.TestAutoConfigureOrder" })
40-
public class TestConditionMetadataAnnotationProcessor
41+
public class TestAutoConfigureAnnotationProcessor
4142
extends AutoConfigureAnnotationProcessor {
4243

4344
private final File outputLocation;
4445

45-
public TestConditionMetadataAnnotationProcessor(File outputLocation) {
46+
public TestAutoConfigureAnnotationProcessor(File outputLocation) {
4647
this.outputLocation = outputLocation;
4748
}
4849

@@ -53,6 +54,8 @@ protected void addAnnotations(Map<String, String> annotations) {
5354
put(annotations, "ConditionalOnBean", TestConditionalOnBean.class);
5455
put(annotations, "ConditionalOnSingleCandidate",
5556
TestConditionalOnSingleCandidate.class);
57+
put(annotations, "ConditionalOnWebApplication",
58+
TestConditionalOnWebApplication.class);
5659
put(annotations, "AutoConfigureBefore", TestAutoConfigureBefore.class);
5760
put(annotations, "AutoConfigureAfter", TestAutoConfigureAfter.class);
5861
put(annotations, "AutoConfigureOrder", TestAutoConfigureOrder.class);

spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/test/java/org/springframework/boot/autoconfigureprocessor/TestClassConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigureprocessor;
1818

19+
import org.springframework.boot.autoconfigureprocessor.TestConditionalOnWebApplication.Type;
20+
1921
/**
2022
* Test configuration with an annotated class.
2123
*
@@ -25,6 +27,7 @@
2527
@TestConditionalOnClass(name = "java.io.InputStream", value = TestClassConfiguration.Nested.class)
2628
@TestConditionalOnBean(type = "java.io.OutputStream")
2729
@TestConditionalOnSingleCandidate(type = "java.io.OutputStream")
30+
@TestConditionalOnWebApplication(type = Type.SERVLET)
2831
public class TestClassConfiguration {
2932

3033
@TestAutoConfigureOrder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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.autoconfigureprocessor;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* Alternative to Spring Boot's {@code @ConditionalOnWebApplication} for testing (removes
27+
* the need for a dependency on the real annotation).
28+
*
29+
* @author Phillip Webb
30+
*/
31+
@Target({ ElementType.TYPE, ElementType.METHOD })
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Documented
34+
public @interface TestConditionalOnWebApplication {
35+
36+
Type type() default Type.ANY;
37+
38+
enum Type {
39+
40+
ANY, SERVLET, REACTIVE
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)