Skip to content

Commit

Permalink
Merge branch '2.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Feb 20, 2019
2 parents 0c45019 + 91a005f commit 34f28b4
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -292,7 +292,7 @@ private void processSimpleTypes(String prefix, TypeElement element,
boolean isNested = isNested(returnTypeElement, field, element);
boolean isCollection = this.typeUtils.isCollectionOrMap(returnType);
if (!isExcluded && !isNested && (setter != null || isCollection)) {
String dataType = this.typeUtils.getType(returnType);
String dataType = this.typeUtils.getType(element, returnType);
String sourceType = this.typeUtils.getQualifiedName(element);
String description = this.typeUtils.getJavaDoc(field);
Object defaultValue = fieldValues.get(name);
Expand Down Expand Up @@ -335,7 +335,7 @@ private void processSimpleLombokTypes(String prefix, TypeElement element,
boolean isCollection = this.typeUtils.isCollectionOrMap(returnType);
boolean hasSetter = hasLombokSetter(field, element);
if (!isExcluded && !isNested && (hasSetter || isCollection)) {
String dataType = this.typeUtils.getType(returnType);
String dataType = this.typeUtils.getType(element, returnType);
String sourceType = this.typeUtils.getQualifiedName(element);
String description = this.typeUtils.getJavaDoc(field);
Object defaultValue = fieldValues.get(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,7 +21,9 @@
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -33,8 +35,10 @@
import javax.lang.model.type.PrimitiveType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.lang.model.util.SimpleTypeVisitor8;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic.Kind;

/**
* Type Utilities.
Expand Down Expand Up @@ -73,18 +77,22 @@ class TypeUtils {

private final ProcessingEnvironment env;

private final Types types;

private final TypeExtractor typeExtractor;

private final TypeMirror collectionType;

private final TypeMirror mapType;

private final Map<TypeElement, TypeDescriptor> typeDescriptors = new HashMap<>();

TypeUtils(ProcessingEnvironment env) {
this.env = env;
Types types = env.getTypeUtils();
this.typeExtractor = new TypeExtractor(types);
this.collectionType = getDeclaredType(types, Collection.class, 1);
this.mapType = getDeclaredType(types, Map.class, 2);
this.types = env.getTypeUtils();
this.typeExtractor = new TypeExtractor(this.types);
this.collectionType = getDeclaredType(this.types, Collection.class, 1);
this.mapType = getDeclaredType(this.types, Map.class, 2);
}

private TypeMirror getDeclaredType(Types types, Class<?> typeClass,
Expand Down Expand Up @@ -115,14 +123,15 @@ public String getQualifiedName(Element element) {
/**
* Return the type of the specified {@link TypeMirror} including all its generic
* information.
* @param element the {@link TypeElement} in which this {@code type} is declared
* @param type the type to handle
* @return a representation of the type including all its generic information
*/
public String getType(TypeMirror type) {
public String getType(TypeElement element, TypeMirror type) {
if (type == null) {
return null;
}
return type.accept(this.typeExtractor, null);
return type.accept(this.typeExtractor, createTypeDescriptor(element));
}

public boolean isCollectionOrMap(TypeMirror type) {
Expand Down Expand Up @@ -160,11 +169,49 @@ private TypeKind getPrimitiveFor(TypeMirror type) {
return WRAPPER_TO_PRIMITIVE.get(type.toString());
}

TypeDescriptor resolveTypeDescriptor(TypeElement element) {
if (this.typeDescriptors.containsKey(element)) {
return this.typeDescriptors.get(element);
}
return createTypeDescriptor(element);
}

private TypeDescriptor createTypeDescriptor(TypeElement element) {
TypeDescriptor descriptor = new TypeDescriptor();
process(descriptor, element.asType());
this.typeDescriptors.put(element, descriptor);
return descriptor;
}

private void process(TypeDescriptor descriptor, TypeMirror type) {
try {
if (type.getKind() == TypeKind.DECLARED) {
DeclaredType declaredType = (DeclaredType) type;
DeclaredType freshType = (DeclaredType) this.env.getElementUtils()
.getTypeElement(this.types.asElement(type).toString()).asType();
List<? extends TypeMirror> arguments = declaredType.getTypeArguments();
for (int i = 0; i < arguments.size(); i++) {
TypeMirror specificType = arguments.get(i);
TypeMirror signatureType = freshType.getTypeArguments().get(i);
descriptor.registerIfNecessary(signatureType, specificType);
}
TypeElement element = (TypeElement) this.types.asElement(type);
process(descriptor, element.getSuperclass());
}
}
catch (Exception ex) {
this.env.getMessager().printMessage(Kind.WARNING,
"Failed to generated type descriptor for " + type,
this.types.asElement(type));
}
}

/**
* A visitor that extracts the fully qualified name of a type, including generic
* information.
*/
private static class TypeExtractor extends SimpleTypeVisitor8<String, Void> {
private static class TypeExtractor
extends SimpleTypeVisitor8<String, TypeDescriptor> {

private final Types types;

Expand All @@ -173,34 +220,60 @@ private static class TypeExtractor extends SimpleTypeVisitor8<String, Void> {
}

@Override
public String visitDeclared(DeclaredType type, Void none) {
public String visitDeclared(DeclaredType type, TypeDescriptor descriptor) {
TypeElement enclosingElement = getEnclosingTypeElement(type);
if (enclosingElement != null) {
return getQualifiedName(enclosingElement) + "$"
+ type.asElement().getSimpleName();
}
String qualifiedName = getQualifiedName(type.asElement());
String qualifiedName = determineQualifiedName(type, enclosingElement);
if (type.getTypeArguments().isEmpty()) {
return qualifiedName;
}
StringBuilder name = new StringBuilder();
name.append(qualifiedName);
name.append("<").append(type.getTypeArguments().stream()
.map(TypeMirror::toString).collect(Collectors.joining(",")))
.map((t) -> visit(t, descriptor)).collect(Collectors.joining(",")))
.append(">");
return name.toString();
}

private String determineQualifiedName(DeclaredType type,
TypeElement enclosingElement) {
if (enclosingElement != null) {
return getQualifiedName(enclosingElement) + "$"
+ type.asElement().getSimpleName();
}
return getQualifiedName(type.asElement());
}

@Override
public String visitTypeVariable(TypeVariable t, TypeDescriptor descriptor) {
TypeMirror typeMirror = descriptor.resolveGeneric(t);
if (typeMirror != null) {
if (typeMirror instanceof TypeVariable) {
// Still unresolved, let's use upper bound
return visit(((TypeVariable) typeMirror).getUpperBound(), descriptor);
}
else {
return visit(typeMirror, descriptor);
}
}
// Unresolved generics, use upper bound
return visit(t.getUpperBound(), descriptor);
}

@Override
public String visitArray(ArrayType t, Void none) {
return t.getComponentType().accept(this, none) + "[]";
public String visitArray(ArrayType t, TypeDescriptor descriptor) {
return t.getComponentType().accept(this, descriptor) + "[]";
}

@Override
public String visitPrimitive(PrimitiveType t, Void none) {
public String visitPrimitive(PrimitiveType t, TypeDescriptor descriptor) {
return this.types.boxedClass(t).getQualifiedName().toString();
}

@Override
protected String defaultAction(TypeMirror t, TypeDescriptor descriptor) {
return t.toString();
}

public String getQualifiedName(Element element) {
if (element == null) {
return null;
Expand Down Expand Up @@ -230,4 +303,42 @@ private TypeElement getEnclosingTypeElement(TypeMirror type) {

}

/**
* Descriptor for a given type.
*/
static class TypeDescriptor {

private final Map<TypeVariable, TypeMirror> generics = new HashMap<>();

public Map<TypeVariable, TypeMirror> getGenerics() {
return Collections.unmodifiableMap(this.generics);
}

public TypeMirror resolveGeneric(TypeVariable typeVariable) {
return resolveGeneric(getParameterName(typeVariable));
}

public TypeMirror resolveGeneric(String parameterName) {
return this.generics.entrySet().stream()
.filter((e) -> getParameterName(e.getKey()).equals(parameterName))
.findFirst().map(Entry::getValue).orElse(null);
}

private void registerIfNecessary(TypeMirror variable, TypeMirror resolution) {
if (variable instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) variable;
if (this.generics.keySet().stream()
.noneMatch((candidate) -> getParameterName(candidate)
.equals(getParameterName(typeVariable)))) {
this.generics.put(typeVariable, resolution);
}
}
}

private String getParameterName(TypeVariable typeVariable) {
return typeVariable.asElement().getSimpleName().toString();
}

}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -45,6 +45,9 @@
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;
Expand Down Expand Up @@ -292,7 +295,7 @@ public void parseCollectionConfig() {
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
Expand Down Expand Up @@ -504,6 +507,40 @@ public void invalidDoubleRegistration() {
.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);
Expand All @@ -518,7 +555,7 @@ public void genericTypes() {
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>")
Expand Down
Loading

0 comments on commit 34f28b4

Please sign in to comment.