From 927c2cacfac50d3185136d8ea6ff7667582385bd Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 18 Jan 2018 13:52:12 +0100 Subject: [PATCH] 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 --- .../configurationprocessor/TypeUtils.java | 19 +++++++++++++++++-- ...ationMetadataAnnotationProcessorTests.java | 2 ++ .../simple/SimpleCollectionProperties.java | 18 +++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java index 8bf3cb28ee..8f40ce7796 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java @@ -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 diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java index d0f0bc9cfc..3496fb7ec9 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java @@ -252,6 +252,8 @@ public class ConfigurationMetadataAnnotationProcessorTests { "java.util.Collection")); assertThat(metadata).has(Metadata.withProperty("collection.doubles", "java.util.List")); + assertThat(metadata).has(Metadata.withProperty("collection.names-to-holders", + "java.util.Map>")); } @Test diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleCollectionProperties.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleCollectionProperties.java index 91d14019a2..732129ae4c 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleCollectionProperties.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/simple/SimpleCollectionProperties.java @@ -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 doubles = new ArrayList(); + private final Map> namesToHolders = new HashMap>(); + public Map getIntegersToNames() { return this.integersToNames; } @@ -81,4 +83,18 @@ public class SimpleCollectionProperties { return this.doubles; } + public Map> getNamesToHolders() { + return this.namesToHolders; + } + + public static class Holder { + + private T target; + + public void setTarget(T target) { + this.target = target; + } + + } + }