This commit is contained in:
Phillip Webb
2019-04-11 13:39:26 -07:00
parent dabe75a281
commit 2dfd916c96
34 changed files with 517 additions and 451 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.configurationprocessor;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
@@ -61,30 +62,36 @@ class ConstructorParameterPropertyDescriptor extends PropertyDescriptor<Variable
private Object getDefaultValueFromAnnotation(
MetadataGenerationEnvironment environment, Element element) {
AnnotationMirror defaultValueAnnotation = environment
.getDefaultValueAnnotation(element);
if (defaultValueAnnotation != null) {
List<String> defaultValue = (List<String>) environment
.getAnnotationElementValues(defaultValueAnnotation).get("value");
if (defaultValue != null) {
try {
TypeMirror specificType = determineSpecificType(environment);
if (defaultValue.size() == 1) {
return coerceValue(specificType, defaultValue.get(0));
}
return defaultValue.stream()
.map((value) -> coerceValue(specificType, value))
.collect(Collectors.toList());
}
catch (IllegalArgumentException ex) {
environment.getMessager().printMessage(Kind.ERROR, ex.getMessage(),
element, defaultValueAnnotation);
AnnotationMirror annotation = environment.getDefaultValueAnnotation(element);
List<String> defaultValue = getDefaultValue(environment, annotation);
if (defaultValue != null) {
try {
TypeMirror specificType = determineSpecificType(environment);
if (defaultValue.size() == 1) {
return coerceValue(specificType, defaultValue.get(0));
}
return defaultValue.stream()
.map((value) -> coerceValue(specificType, value))
.collect(Collectors.toList());
}
catch (IllegalArgumentException ex) {
environment.getMessager().printMessage(Kind.ERROR, ex.getMessage(),
element, annotation);
}
}
return null;
}
@SuppressWarnings("unchecked")
private List<String> getDefaultValue(MetadataGenerationEnvironment environment,
AnnotationMirror annotation) {
if (annotation == null) {
return null;
}
Map<String, Object> values = environment.getAnnotationElementValues(annotation);
return (List<String>) values.get("value");
}
private TypeMirror determineSpecificType(MetadataGenerationEnvironment environment) {
TypeMirror candidate = getSource().asType();
TypeMirror elementCandidate = environment.getTypeUtils()

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.configurationprocessor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
@@ -49,7 +50,24 @@ class MetadataGenerationEnvironment {
private static final String NULLABLE_ANNOTATION = "org.springframework.lang.Nullable";
private final Set<String> typeExcludes;
private static final Set<String> TYPE_EXCLUDES;
static {
Set<String> excludes = new HashSet<>();
excludes.add("com.zaxxer.hikari.IConnectionCustomizer");
excludes.add("groovy.text.markup.MarkupTemplateEngine");
excludes.add("java.io.Writer");
excludes.add("java.io.PrintWriter");
excludes.add("java.lang.ClassLoader");
excludes.add("java.util.concurrent.ThreadFactory");
excludes.add("javax.jms.XAConnectionFactory");
excludes.add("javax.sql.DataSource");
excludes.add("javax.sql.XADataSource");
excludes.add("org.apache.tomcat.jdbc.pool.PoolConfiguration");
excludes.add("org.apache.tomcat.jdbc.pool.Validator");
excludes.add("org.flywaydb.core.api.callback.FlywayCallback");
excludes.add("org.flywaydb.core.api.resolver.MigrationResolver");
TYPE_EXCLUDES = Collections.unmodifiableSet(excludes);
}
private final TypeUtils typeUtils;
@@ -79,7 +97,6 @@ class MetadataGenerationEnvironment {
String deprecatedConfigurationPropertyAnnotation,
String defaultValueAnnotation, String endpointAnnotation,
String readOperationAnnotation) {
this.typeExcludes = determineTypeExcludes();
this.typeUtils = new TypeUtils(environment);
this.elements = environment.getElementUtils();
this.messager = environment.getMessager();
@@ -92,24 +109,6 @@ class MetadataGenerationEnvironment {
this.readOperationAnnotation = readOperationAnnotation;
}
private static Set<String> determineTypeExcludes() {
Set<String> excludes = new HashSet<>();
excludes.add("com.zaxxer.hikari.IConnectionCustomizer");
excludes.add("groovy.text.markup.MarkupTemplateEngine");
excludes.add("java.io.Writer");
excludes.add("java.io.PrintWriter");
excludes.add("java.lang.ClassLoader");
excludes.add("java.util.concurrent.ThreadFactory");
excludes.add("javax.jms.XAConnectionFactory");
excludes.add("javax.sql.DataSource");
excludes.add("javax.sql.XADataSource");
excludes.add("org.apache.tomcat.jdbc.pool.PoolConfiguration");
excludes.add("org.apache.tomcat.jdbc.pool.Validator");
excludes.add("org.flywaydb.core.api.callback.FlywayCallback");
excludes.add("org.flywaydb.core.api.resolver.MigrationResolver");
return excludes;
}
private static FieldValuesParser resolveFieldValuesParser(ProcessingEnvironment env) {
try {
return new JavaCompilerFieldValuesParser(env);
@@ -147,7 +146,7 @@ class MetadataGenerationEnvironment {
if (typeName.endsWith("[]")) {
typeName = typeName.substring(0, typeName.length() - 2);
}
return this.typeExcludes.contains(typeName);
return TYPE_EXCLUDES.contains(typeName);
}
public boolean isDeprecated(Element element) {

View File

@@ -164,9 +164,8 @@ class TypeUtils {
return this.types.getDeclaredType(this.env.getElementUtils()
.getTypeElement(Object.class.getName()));
}
else { // return type argument to Collection<...>
return declaredType.getTypeArguments().get(0);
}
// return type argument to Collection<...>
return declaredType.getTypeArguments().get(0);
}
// recursively walk the supertypes, looking for Collection<...>