Resolve generic types in generated metadata

Closes gh-15850
This commit is contained in:
Stephane Nicoll
2019-02-15 14:13:51 +01:00
parent 51776af45d
commit 91a005f578
8 changed files with 449 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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,9 @@ import org.springframework.boot.configurationsample.endpoint.EnabledEndpoint;
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint;
import org.springframework.boot.configurationsample.endpoint.SpecificEndpoint;
import org.springframework.boot.configurationsample.endpoint.incremental.IncrementalEndpoint;
import org.springframework.boot.configurationsample.generic.AbstractGenericProperties;
import org.springframework.boot.configurationsample.generic.SimpleGenericProperties;
import org.springframework.boot.configurationsample.generic.UnresolvedGenericProperties;
import org.springframework.boot.configurationsample.incremental.BarProperties;
import org.springframework.boot.configurationsample.incremental.FooProperties;
import org.springframework.boot.configurationsample.incremental.RenamedBarProperties;
@@ -292,7 +295,7 @@ public class ConfigurationMetadataAnnotationProcessorTests {
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>>"));
"java.util.Map<java.lang.String,org.springframework.boot.configurationsample.simple.SimpleCollectionProperties$Holder<java.lang.String>>"));
}
@Test
@@ -504,6 +507,40 @@ public class ConfigurationMetadataAnnotationProcessorTests {
.withMessageContaining("Compilation failed");
}
@Test
public void simpleGenericProperties() {
ConfigurationMetadata metadata = compile(AbstractGenericProperties.class,
SimpleGenericProperties.class);
assertThat(metadata).has(
Metadata.withGroup("generic").fromSource(SimpleGenericProperties.class));
assertThat(metadata).has(Metadata.withProperty("generic.name", String.class)
.fromSource(SimpleGenericProperties.class)
.withDescription("Generic name.").withDefaultValue(null));
assertThat(metadata).has(Metadata
.withProperty("generic.mappings",
"java.util.Map<java.lang.Integer,java.time.Duration>")
.fromSource(SimpleGenericProperties.class)
.withDescription("Generic mappings.").withDefaultValue(null));
assertThat(metadata.getItems()).hasSize(3);
}
@Test
public void unresolvedGenericProperties() {
ConfigurationMetadata metadata = compile(AbstractGenericProperties.class,
UnresolvedGenericProperties.class);
assertThat(metadata).has(Metadata.withGroup("generic")
.fromSource(UnresolvedGenericProperties.class));
assertThat(metadata).has(Metadata.withProperty("generic.name", String.class)
.fromSource(UnresolvedGenericProperties.class)
.withDescription("Generic name.").withDefaultValue(null));
assertThat(metadata).has(Metadata
.withProperty("generic.mappings",
"java.util.Map<java.lang.Number,java.lang.Object>")
.fromSource(UnresolvedGenericProperties.class)
.withDescription("Generic mappings.").withDefaultValue(null));
assertThat(metadata.getItems()).hasSize(3);
}
@Test
public void genericTypes() {
ConfigurationMetadata metadata = compile(GenericConfig.class);
@@ -518,7 +555,7 @@ public class ConfigurationMetadataAnnotationProcessorTests {
assertThat(metadata).has(Metadata.withProperty("generic.foo.name")
.ofType(String.class).fromSource(GenericConfig.Foo.class));
assertThat(metadata).has(Metadata.withProperty("generic.foo.string-to-bar")
.ofType("java.util.Map<java.lang.String,org.springframework.boot.configurationsample.specific.GenericConfig.Bar<java.lang.Integer>>")
.ofType("java.util.Map<java.lang.String,org.springframework.boot.configurationsample.specific.GenericConfig$Bar<java.lang.Integer>>")
.fromSource(GenericConfig.Foo.class));
assertThat(metadata).has(Metadata.withProperty("generic.foo.string-to-integer")
.ofType("java.util.Map<java.lang.String,java.lang.Integer>")

View File

@@ -0,0 +1,136 @@
/*
* Copyright 2012-2019 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.configurationprocessor;
import java.io.IOException;
import java.time.Duration;
import java.util.Set;
import java.util.function.BiConsumer;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.configurationprocessor.TypeUtils.TypeDescriptor;
import org.springframework.boot.configurationsample.generic.AbstractGenericProperties;
import org.springframework.boot.configurationsample.generic.AbstractIntermediateGenericProperties;
import org.springframework.boot.configurationsample.generic.SimpleGenericProperties;
import org.springframework.boot.testsupport.compiler.TestCompiler;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link TypeUtils}.
*
* @author Stephane Nicoll
*/
public class TypeUtilsTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void resolveTypeDescriptorOnConcreteClass() throws IOException {
process(SimpleGenericProperties.class, (roundEnv, typeUtils) -> {
for (Element rootElement : roundEnv.getRootElements()) {
TypeDescriptor typeDescriptor = typeUtils
.resolveTypeDescriptor((TypeElement) rootElement);
assertThat(typeDescriptor.getGenerics().keySet().stream()
.map(Object::toString)).containsOnly("A", "B", "C");
assertThat(typeDescriptor.resolveGeneric("A"))
.hasToString(String.class.getName());
assertThat(typeDescriptor.resolveGeneric("B"))
.hasToString(Integer.class.getName());
assertThat(typeDescriptor.resolveGeneric("C"))
.hasToString(Duration.class.getName());
}
});
}
@Test
public void resolveTypeDescriptorOnIntermediateClass() throws IOException {
process(AbstractIntermediateGenericProperties.class, (roundEnv, typeUtils) -> {
for (Element rootElement : roundEnv.getRootElements()) {
TypeDescriptor typeDescriptor = typeUtils
.resolveTypeDescriptor((TypeElement) rootElement);
assertThat(typeDescriptor.getGenerics().keySet().stream()
.map(Object::toString)).containsOnly("A", "B", "C");
assertThat(typeDescriptor.resolveGeneric("A"))
.hasToString(String.class.getName());
assertThat(typeDescriptor.resolveGeneric("B"))
.hasToString(Integer.class.getName());
assertThat(typeDescriptor.resolveGeneric("C")).hasToString("C");
}
});
}
@Test
public void resolveTypeDescriptorWithOnlyGenerics() throws IOException {
process(AbstractGenericProperties.class, (roundEnv, typeUtils) -> {
for (Element rootElement : roundEnv.getRootElements()) {
TypeDescriptor typeDescriptor = typeUtils
.resolveTypeDescriptor((TypeElement) rootElement);
assertThat(typeDescriptor.getGenerics().keySet().stream()
.map(Object::toString)).containsOnly("A", "B", "C");
}
});
}
private void process(Class<?> target,
BiConsumer<RoundEnvironment, TypeUtils> consumer) throws IOException {
TestProcessor processor = new TestProcessor(consumer);
TestCompiler compiler = new TestCompiler(this.temporaryFolder);
compiler.getTask(target).call(processor);
}
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
private final class TestProcessor extends AbstractProcessor {
private final BiConsumer<RoundEnvironment, TypeUtils> typeUtilsConsumer;
private TypeUtils typeUtils;
private TestProcessor(BiConsumer<RoundEnvironment, TypeUtils> typeUtils) {
this.typeUtilsConsumer = typeUtils;
}
@Override
public synchronized void init(ProcessingEnvironment env) {
this.typeUtils = new TypeUtils(env);
}
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
this.typeUtilsConsumer.accept(roundEnv, this.typeUtils);
return false;
}
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2012-2019 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.generic;
import java.util.HashMap;
import java.util.Map;
/**
* A base properties class with generics.
*
* @author Stephane Nicoll
*/
public class AbstractGenericProperties<A, B, C> {
/**
* Generic name.
*/
private A name;
/**
* Generic mappings.
*/
private final Map<B, C> mappings = new HashMap<>();
public A getName() {
return this.name;
}
public void setName(A name) {
this.name = name;
}
public Map<B, C> getMappings() {
return this.mappings;
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2012-2019 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.generic;
/**
* An intermediate layer that resolves some of the generics from the parent but not all.
*
* @author Stephane Nicoll
*/
public abstract class AbstractIntermediateGenericProperties<C>
extends AbstractGenericProperties<String, Integer, C> {
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2012-2019 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.generic;
import java.time.Duration;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Simple properties with resolved generic information.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("generic")
public class SimpleGenericProperties
extends AbstractIntermediateGenericProperties<Duration> {
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2012-2019 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.generic;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Properties with unresolved generic information.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("generic")
public class UnresolvedGenericProperties<B extends Number, C>
extends AbstractGenericProperties<String, B, C> {
}