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
pull/11790/head
Stephane Nicoll 7 years ago
parent 7ea4501fd5
commit 927c2cacfa

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* 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.
@ -19,6 +19,7 @@ package org.springframework.boot.configurationprocessor;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.annotation.processing.ProcessingEnvironment;
@ -186,7 +187,21 @@ class TypeUtils {
return getQualifiedName(enclosingElement) + "$"
+ type.asElement().getSimpleName().toString();
}
return type.toString();
StringBuilder sb = new StringBuilder();
sb.append(getQualifiedName(type.asElement()));
if (!type.getTypeArguments().isEmpty()) {
sb.append("<");
Iterator<?> it = type.getTypeArguments().iterator();
while (it.hasNext()) {
sb.append(it.next());
if (it.hasNext()) {
sb.append(",");
}
}
sb.append(">");
}
return sb.toString();
}
@Override

@ -252,6 +252,8 @@ public class ConfigurationMetadataAnnotationProcessorTests {
"java.util.Collection<java.lang.Byte>"));
assertThat(metadata).has(Metadata.withProperty("collection.doubles",
"java.util.List<java.lang.Double>"));
assertThat(metadata).has(Metadata.withProperty("collection.names-to-holders",
"java.util.Map<java.lang.String,org.springframework.boot.configurationsample.simple.SimpleCollectionProperties.Holder<java.lang.String>>"));
}
@Test

@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* 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.
@ -45,6 +45,8 @@ public class SimpleCollectionProperties {
private final List<Double> doubles = new ArrayList<Double>();
private final Map<String, Holder<String>> namesToHolders = new HashMap<String, Holder<String>>();
public Map<Integer, String> getIntegersToNames() {
return this.integersToNames;
}
@ -81,4 +83,18 @@ public class SimpleCollectionProperties {
return this.doubles;
}
public Map<String, Holder<String>> getNamesToHolders() {
return this.namesToHolders;
}
public static class Holder<T> {
private T target;
public void setTarget(T target) {
this.target = target;
}
}
}

Loading…
Cancel
Save