Skip to content

Commit 71ab5dd

Browse files
committed
Restore proper handling of array types
Closes gh-11512
1 parent 97a51dd commit 71ab5dd

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.annotation.processing.ProcessingEnvironment;
2626
import javax.lang.model.element.Element;
2727
import javax.lang.model.element.TypeElement;
28+
import javax.lang.model.type.ArrayType;
2829
import javax.lang.model.type.DeclaredType;
2930
import javax.lang.model.type.PrimitiveType;
3031
import javax.lang.model.type.TypeKind;
@@ -204,6 +205,11 @@ public String visitDeclared(DeclaredType type, Void none) {
204205
return sb.toString();
205206
}
206207

208+
@Override
209+
public String visitArray(ArrayType t, Void none) {
210+
return t.getComponentType().accept(this, none) + "[]";
211+
}
212+
207213
@Override
208214
public String visitPrimitive(PrimitiveType t, Void none) {
209215
return this.types.boxedClass(t).getQualifiedName().toString();

spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.boot.configurationsample.simple.DeprecatedSingleProperty;
5454
import org.springframework.boot.configurationsample.simple.HierarchicalProperties;
5555
import org.springframework.boot.configurationsample.simple.NotAnnotated;
56+
import org.springframework.boot.configurationsample.simple.SimpleArrayProperties;
5657
import org.springframework.boot.configurationsample.simple.SimpleCollectionProperties;
5758
import org.springframework.boot.configurationsample.simple.SimplePrefixValueProperties;
5859
import org.springframework.boot.configurationsample.simple.SimpleProperties;
@@ -69,6 +70,7 @@
6970
import org.springframework.boot.configurationsample.specific.InvalidAccessorProperties;
7071
import org.springframework.boot.configurationsample.specific.InvalidDoubleRegistrationProperties;
7172
import org.springframework.boot.configurationsample.specific.SimplePojo;
73+
import org.springframework.boot.configurationsample.specific.WildcardConfig;
7274
import org.springframework.boot.junit.compiler.TestCompiler;
7375
import org.springframework.util.FileCopyUtils;
7476

@@ -256,6 +258,22 @@ public void parseCollectionConfig() throws Exception {
256258
"java.util.Map<java.lang.String,org.springframework.boot.configurationsample.simple.SimpleCollectionProperties.Holder<java.lang.String>>"));
257259
}
258260

261+
@Test
262+
public void parseArrayConfig() throws Exception {
263+
ConfigurationMetadata metadata = compile(SimpleArrayProperties.class);
264+
assertThat(metadata).has(Metadata.withGroup("array")
265+
.ofType(SimpleArrayProperties.class));
266+
assertThat(metadata).has(Metadata.withProperty("array.primitive",
267+
"java.lang.Integer[]"));
268+
assertThat(metadata).has(Metadata.withProperty("array.simple",
269+
"java.lang.String[]"));
270+
assertThat(metadata).has(Metadata.withProperty("array.inner",
271+
"org.springframework.boot.configurationsample.simple.SimpleArrayProperties$Holder[]"));
272+
assertThat(metadata).has(Metadata.withProperty("array.name-to-integer",
273+
"java.util.Map<java.lang.String,java.lang.Integer>[]"));
274+
assertThat(metadata.getItems()).hasSize(5);
275+
}
276+
259277
@Test
260278
public void simpleMethodConfig() throws Exception {
261279
ConfigurationMetadata metadata = compile(SimpleMethodConfig.class);
@@ -443,6 +461,20 @@ public void genericTypes() throws IOException {
443461
assertThat(metadata.getItems()).hasSize(9);
444462
}
445463

464+
@Test
465+
public void wildcardTypes() throws IOException {
466+
ConfigurationMetadata metadata = compile(WildcardConfig.class);
467+
assertThat(metadata).has(Metadata.withGroup("wildcard")
468+
.ofType(WildcardConfig.class));
469+
assertThat(metadata).has(Metadata.withProperty("wildcard.string-to-number")
470+
.ofType("java.util.Map<java.lang.String,? extends java.lang.Number>")
471+
.fromSource(WildcardConfig.class));
472+
assertThat(metadata).has(Metadata.withProperty("wildcard.integers")
473+
.ofType("java.util.List<? super java.lang.Integer>")
474+
.fromSource(WildcardConfig.class));
475+
assertThat(metadata.getItems()).hasSize(3);
476+
}
477+
446478
@Test
447479
public void lombokDataProperties() throws Exception {
448480
ConfigurationMetadata metadata = compile(LombokSimpleDataProperties.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.configurationsample.simple;
18+
19+
import java.util.Map;
20+
21+
import org.springframework.boot.configurationsample.ConfigurationProperties;
22+
23+
/**
24+
* Properties with array.
25+
*
26+
* @author Stephane Nicoll
27+
*/
28+
@ConfigurationProperties("array")
29+
public class SimpleArrayProperties {
30+
31+
private int[] primitive;
32+
33+
private String[] simple;
34+
35+
private Holder[] inner;
36+
37+
private Map<String, Integer>[] nameToInteger;
38+
39+
public int[] getPrimitive() {
40+
return this.primitive;
41+
}
42+
43+
public void setPrimitive(int[] primitive) {
44+
this.primitive = primitive;
45+
}
46+
47+
public String[] getSimple() {
48+
return this.simple;
49+
}
50+
51+
public void setSimple(String[] simple) {
52+
this.simple = simple;
53+
}
54+
55+
public Holder[] getInner() {
56+
return this.inner;
57+
}
58+
59+
public void setInner(Holder[] inner) {
60+
this.inner = inner;
61+
}
62+
63+
public Map<String, Integer>[] getNameToInteger() {
64+
return this.nameToInteger;
65+
}
66+
67+
public void setNameToInteger(Map<String, Integer>[] nameToInteger) {
68+
this.nameToInteger = nameToInteger;
69+
}
70+
71+
public static class Holder {
72+
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.configurationsample.specific;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.configurationsample.ConfigurationProperties;
23+
24+
/**
25+
* Demonstrate properties with a wildcard type.
26+
*
27+
* @author Stephane Nicoll
28+
*/
29+
@ConfigurationProperties("wildcard")
30+
public class WildcardConfig {
31+
32+
private Map<String, ? extends Number> stringToNumber;
33+
34+
private List<? super Integer> integers;
35+
36+
public Map<String, ? extends Number> getStringToNumber() {
37+
return this.stringToNumber;
38+
}
39+
40+
public void setStringToNumber(Map<String, ? extends Number> stringToNumber) {
41+
this.stringToNumber = stringToNumber;
42+
}
43+
44+
public List<? super Integer> getIntegers() {
45+
return this.integers;
46+
}
47+
48+
public void setIntegers(List<? super Integer> integers) {
49+
this.integers = integers;
50+
}
51+
52+
}

0 commit comments

Comments
 (0)