Restore proper handling of array types

Closes gh-11512
This commit is contained in:
Stephane Nicoll 2018-01-18 18:00:41 +01:00
parent 97a51ddcaa
commit 71ab5dd748
4 changed files with 165 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import java.util.Map;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.PrimitiveType;
import javax.lang.model.type.TypeKind;
@ -204,6 +205,11 @@ class TypeUtils {
return sb.toString();
}
@Override
public String visitArray(ArrayType t, Void none) {
return t.getComponentType().accept(this, none) + "[]";
}
@Override
public String visitPrimitive(PrimitiveType t, Void none) {
return this.types.boxedClass(t).getQualifiedName().toString();

View File

@ -53,6 +53,7 @@ import org.springframework.boot.configurationsample.simple.ClassWithNestedProper
import org.springframework.boot.configurationsample.simple.DeprecatedSingleProperty;
import org.springframework.boot.configurationsample.simple.HierarchicalProperties;
import org.springframework.boot.configurationsample.simple.NotAnnotated;
import org.springframework.boot.configurationsample.simple.SimpleArrayProperties;
import org.springframework.boot.configurationsample.simple.SimpleCollectionProperties;
import org.springframework.boot.configurationsample.simple.SimplePrefixValueProperties;
import org.springframework.boot.configurationsample.simple.SimpleProperties;
@ -69,6 +70,7 @@ import org.springframework.boot.configurationsample.specific.InnerClassRootConfi
import org.springframework.boot.configurationsample.specific.InvalidAccessorProperties;
import org.springframework.boot.configurationsample.specific.InvalidDoubleRegistrationProperties;
import org.springframework.boot.configurationsample.specific.SimplePojo;
import org.springframework.boot.configurationsample.specific.WildcardConfig;
import org.springframework.boot.junit.compiler.TestCompiler;
import org.springframework.util.FileCopyUtils;
@ -256,6 +258,22 @@ public class ConfigurationMetadataAnnotationProcessorTests {
"java.util.Map<java.lang.String,org.springframework.boot.configurationsample.simple.SimpleCollectionProperties.Holder<java.lang.String>>"));
}
@Test
public void parseArrayConfig() throws Exception {
ConfigurationMetadata metadata = compile(SimpleArrayProperties.class);
assertThat(metadata).has(Metadata.withGroup("array")
.ofType(SimpleArrayProperties.class));
assertThat(metadata).has(Metadata.withProperty("array.primitive",
"java.lang.Integer[]"));
assertThat(metadata).has(Metadata.withProperty("array.simple",
"java.lang.String[]"));
assertThat(metadata).has(Metadata.withProperty("array.inner",
"org.springframework.boot.configurationsample.simple.SimpleArrayProperties$Holder[]"));
assertThat(metadata).has(Metadata.withProperty("array.name-to-integer",
"java.util.Map<java.lang.String,java.lang.Integer>[]"));
assertThat(metadata.getItems()).hasSize(5);
}
@Test
public void simpleMethodConfig() throws Exception {
ConfigurationMetadata metadata = compile(SimpleMethodConfig.class);
@ -443,6 +461,20 @@ public class ConfigurationMetadataAnnotationProcessorTests {
assertThat(metadata.getItems()).hasSize(9);
}
@Test
public void wildcardTypes() throws IOException {
ConfigurationMetadata metadata = compile(WildcardConfig.class);
assertThat(metadata).has(Metadata.withGroup("wildcard")
.ofType(WildcardConfig.class));
assertThat(metadata).has(Metadata.withProperty("wildcard.string-to-number")
.ofType("java.util.Map<java.lang.String,? extends java.lang.Number>")
.fromSource(WildcardConfig.class));
assertThat(metadata).has(Metadata.withProperty("wildcard.integers")
.ofType("java.util.List<? super java.lang.Integer>")
.fromSource(WildcardConfig.class));
assertThat(metadata.getItems()).hasSize(3);
}
@Test
public void lombokDataProperties() throws Exception {
ConfigurationMetadata metadata = compile(LombokSimpleDataProperties.class);

View File

@ -0,0 +1,75 @@
/*
* Copyright 2012-2018 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
*
* http://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.configurationsample.simple;
import java.util.Map;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Properties with array.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("array")
public class SimpleArrayProperties {
private int[] primitive;
private String[] simple;
private Holder[] inner;
private Map<String, Integer>[] nameToInteger;
public int[] getPrimitive() {
return this.primitive;
}
public void setPrimitive(int[] primitive) {
this.primitive = primitive;
}
public String[] getSimple() {
return this.simple;
}
public void setSimple(String[] simple) {
this.simple = simple;
}
public Holder[] getInner() {
return this.inner;
}
public void setInner(Holder[] inner) {
this.inner = inner;
}
public Map<String, Integer>[] getNameToInteger() {
return this.nameToInteger;
}
public void setNameToInteger(Map<String, Integer>[] nameToInteger) {
this.nameToInteger = nameToInteger;
}
public static class Holder {
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2012-2018 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
*
* http://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.configurationsample.specific;
import java.util.List;
import java.util.Map;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Demonstrate properties with a wildcard type.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("wildcard")
public class WildcardConfig {
private Map<String, ? extends Number> stringToNumber;
private List<? super Integer> integers;
public Map<String, ? extends Number> getStringToNumber() {
return this.stringToNumber;
}
public void setStringToNumber(Map<String, ? extends Number> stringToNumber) {
this.stringToNumber = stringToNumber;
}
public List<? super Integer> getIntegers() {
return this.integers;
}
public void setIntegers(List<? super Integer> integers) {
this.integers = integers;
}
}