Skip to content

Commit 927c2ca

Browse files
committed
Rework type generation algorithm
The initial solution to gh-11512 was still using a plain `toString` that could potentially break with a JDK upgrade. Turns out that JDK9 actually uses the same type for AnnotatedType and ClassType so the trick of using a visitor doesn't work anymore. Retrospectively, it is quite easy to generate the full type once we have the DeclaredType as we already have some logic to get the qualified, that is raw, type and we have access to the type parameters. This commit still uses a `toString` to generate the representation of the type parameters but this looks much safer than trying to redo what such a simple `toString` should do. Also, the additional metadata that we could get on an ExecutableElement does not apply to them. Closes gh-11512
1 parent 7ea4501 commit 927c2ca

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Collection;
2020
import java.util.Collections;
2121
import java.util.HashMap;
22+
import java.util.Iterator;
2223
import java.util.Map;
2324

2425
import javax.annotation.processing.ProcessingEnvironment;
@@ -186,7 +187,21 @@ public String visitDeclared(DeclaredType type, Void none) {
186187
return getQualifiedName(enclosingElement) + "$"
187188
+ type.asElement().getSimpleName().toString();
188189
}
189-
return type.toString();
190+
StringBuilder sb = new StringBuilder();
191+
sb.append(getQualifiedName(type.asElement()));
192+
if (!type.getTypeArguments().isEmpty()) {
193+
sb.append("<");
194+
Iterator<?> it = type.getTypeArguments().iterator();
195+
while (it.hasNext()) {
196+
sb.append(it.next());
197+
if (it.hasNext()) {
198+
sb.append(",");
199+
}
200+
}
201+
sb.append(">");
202+
203+
}
204+
return sb.toString();
190205
}
191206

192207
@Override

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ public void parseCollectionConfig() throws Exception {
252252
"java.util.Collection<java.lang.Byte>"));
253253
assertThat(metadata).has(Metadata.withProperty("collection.doubles",
254254
"java.util.List<java.lang.Double>"));
255+
assertThat(metadata).has(Metadata.withProperty("collection.names-to-holders",
256+
"java.util.Map<java.lang.String,org.springframework.boot.configurationsample.simple.SimpleCollectionProperties.Holder<java.lang.String>>"));
255257
}
256258

257259
@Test

spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleCollectionProperties.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,8 @@ public class SimpleCollectionProperties {
4545

4646
private final List<Double> doubles = new ArrayList<Double>();
4747

48+
private final Map<String, Holder<String>> namesToHolders = new HashMap<String, Holder<String>>();
49+
4850
public Map<Integer, String> getIntegersToNames() {
4951
return this.integersToNames;
5052
}
@@ -81,4 +83,18 @@ public List<Double> getDoubles() {
8183
return this.doubles;
8284
}
8385

86+
public Map<String, Holder<String>> getNamesToHolders() {
87+
return this.namesToHolders;
88+
}
89+
90+
public static class Holder<T> {
91+
92+
private T target;
93+
94+
public void setTarget(T target) {
95+
this.target = target;
96+
}
97+
98+
}
99+
84100
}

0 commit comments

Comments
 (0)