Revert "Support constructor binding on 3rd party classes"

This commit reverts the support of constructor binding on 3rd party
classes using @ImportConfigurationPropertiesBean

See gh-23172

Closes gh-23593
This commit is contained in:
Stephane Nicoll
2020-10-06 16:06:10 +02:00
parent 2595258494
commit 1296b4dfe6
39 changed files with 139 additions and 1413 deletions

View File

@@ -22,7 +22,6 @@ import java.io.StringWriter;
import java.time.Duration;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -41,7 +40,6 @@ import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic.Kind;
@@ -82,10 +80,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
static final String NAME_ANNOTATION = "org.springframework.boot.context.properties.bind.Name";
static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION = "org.springframework.boot.context.properties.ImportAsConfigurationPropertiesBean";
static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION = "org.springframework.boot.context.properties.ImportAsConfigurationPropertiesBeans";
private static final Set<String> SUPPORTED_OPTIONS = Collections
.unmodifiableSet(Collections.singleton(ADDITIONAL_METADATA_LOCATIONS_OPTION));
@@ -127,14 +121,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return NAME_ANNOTATION;
}
protected String importAsConfigurationPropertiesBeanAnnotation() {
return IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION;
}
protected String importAsConfigurationPropertiesBeansAnnotation() {
return IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
@@ -153,16 +139,22 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
this.metadataEnv = new MetadataGenerationEnvironment(env, configurationPropertiesAnnotation(),
nestedConfigurationPropertyAnnotation(), deprecatedConfigurationPropertyAnnotation(),
constructorBindingAnnotation(), defaultValueAnnotation(), endpointAnnotation(),
readOperationAnnotation(), nameAnnotation(), importAsConfigurationPropertiesBeanAnnotation(),
importAsConfigurationPropertiesBeansAnnotation());
readOperationAnnotation(), nameAnnotation());
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
this.metadataCollector.processing(roundEnv);
processConfigurationProperties(roundEnv);
processEndpoint(roundEnv);
processImportAsConfigurationProperties(roundEnv);
TypeElement annotationType = this.metadataEnv.getConfigurationPropertiesAnnotationElement();
if (annotationType != null) { // Is @ConfigurationProperties available
for (Element element : roundEnv.getElementsAnnotatedWith(annotationType)) {
processElement(element);
}
}
TypeElement endpointType = this.metadataEnv.getEndpointAnnotationElement();
if (endpointType != null) { // Is @Endpoint available
getElementsAnnotatedOrMetaAnnotatedWith(roundEnv, endpointType).forEach(this::processEndpoint);
}
if (roundEnv.processingOver()) {
try {
writeMetaData();
@@ -174,40 +166,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return false;
}
private void processConfigurationProperties(RoundEnvironment roundEnv) {
TypeElement annotationType = this.metadataEnv.getConfigurationPropertiesAnnotationElement();
if (annotationType != null) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotationType)) {
processElement(element);
}
}
}
private void processEndpoint(RoundEnvironment roundEnv) {
TypeElement endpointType = this.metadataEnv.getEndpointAnnotationElement();
if (endpointType != null) {
getElementsAnnotatedOrMetaAnnotatedWith(roundEnv, endpointType).forEach(this::processEndpoint);
}
}
private void processImportAsConfigurationProperties(RoundEnvironment roundEnv) {
TypeElement importAsConfigurationPropertiesBeanType = this.metadataEnv
.getImportAsConfigurationPropertiesBeansAnnotation();
TypeElement importAsConfigurationPropertiesBeansType = this.metadataEnv
.getImportAsConfigurationPropertiesBeansAnnotationElement();
if (importAsConfigurationPropertiesBeanType == null && importAsConfigurationPropertiesBeansType == null) {
return;
}
Set<Element> elements = new LinkedHashSet<>();
if (importAsConfigurationPropertiesBeanType != null) {
elements.addAll(roundEnv.getElementsAnnotatedWith(importAsConfigurationPropertiesBeanType));
}
if (importAsConfigurationPropertiesBeansType != null) {
elements.addAll(roundEnv.getElementsAnnotatedWith(importAsConfigurationPropertiesBeansType));
}
elements.forEach(this::processImportAsConfigurationPropertiesBean);
}
private Map<Element, List<Element>> getElementsAnnotatedOrMetaAnnotatedWith(RoundEnvironment roundEnv,
TypeElement annotation) {
Map<Element, List<Element>> result = new LinkedHashMap<>();
@@ -226,7 +184,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
if (annotation != null) {
String prefix = getPrefix(annotation);
if (element instanceof TypeElement) {
processAnnotatedTypeElement(prefix, (TypeElement) element, false, new Stack<>());
processAnnotatedTypeElement(prefix, (TypeElement) element, new Stack<>());
}
else if (element instanceof ExecutableElement) {
processExecutableElement(prefix, (ExecutableElement) element, new Stack<>());
@@ -238,11 +196,10 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
}
}
private void processAnnotatedTypeElement(String prefix, TypeElement element, boolean fromImport,
Stack<TypeElement> seen) {
private void processAnnotatedTypeElement(String prefix, TypeElement element, Stack<TypeElement> seen) {
String type = this.metadataEnv.getTypeUtils().getQualifiedName(element);
this.metadataCollector.add(ItemMetadata.newGroup(prefix, type, type, null));
processTypeElement(prefix, element, fromImport, null, seen);
processTypeElement(prefix, element, null, seen);
}
private void processExecutableElement(String prefix, ExecutableElement element, Stack<TypeElement> seen) {
@@ -260,26 +217,25 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
}
else {
this.metadataCollector.add(group);
processTypeElement(prefix, (TypeElement) returns, false, element, seen);
processTypeElement(prefix, (TypeElement) returns, element, seen);
}
}
}
}
private void processTypeElement(String prefix, TypeElement element, boolean fromImport, ExecutableElement source,
private void processTypeElement(String prefix, TypeElement element, ExecutableElement source,
Stack<TypeElement> seen) {
if (!seen.contains(element)) {
seen.push(element);
new PropertyDescriptorResolver(this.metadataEnv).resolve(element, fromImport, source)
.forEach((descriptor) -> {
this.metadataCollector.add(descriptor.resolveItemMetadata(prefix, this.metadataEnv));
if (descriptor.isNested(this.metadataEnv)) {
TypeElement nestedTypeElement = (TypeElement) this.metadataEnv.getTypeUtils()
.asElement(descriptor.getType());
String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, descriptor.getName());
processTypeElement(nestedPrefix, nestedTypeElement, false, source, seen);
}
});
new PropertyDescriptorResolver(this.metadataEnv).resolve(element, source).forEach((descriptor) -> {
this.metadataCollector.add(descriptor.resolveItemMetadata(prefix, this.metadataEnv));
if (descriptor.isNested(this.metadataEnv)) {
TypeElement nestedTypeElement = (TypeElement) this.metadataEnv.getTypeUtils()
.asElement(descriptor.getType());
String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, descriptor.getName());
processTypeElement(nestedPrefix, nestedTypeElement, source, seen);
}
});
seen.pop();
}
}
@@ -316,31 +272,6 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
}
}
private void processImportAsConfigurationPropertiesBean(Element element) {
this.metadataEnv.getImportAsConfigurationPropertiesBeanAnnotations(element)
.forEach(this::processImportAsConfigurationPropertiesBean);
}
@SuppressWarnings("unchecked")
private void processImportAsConfigurationPropertiesBean(AnnotationMirror annotation) {
String prefix = getPrefix(annotation);
processImportAsConfigurationPropertiesBeanTypes(prefix,
(List<TypeMirror>) this.metadataEnv.getAnnotationElementValues(annotation).get("type"));
processImportAsConfigurationPropertiesBeanTypes(prefix,
(List<TypeMirror>) this.metadataEnv.getAnnotationElementValues(annotation).get("value"));
}
private void processImportAsConfigurationPropertiesBeanTypes(String prefix, List<TypeMirror> types) {
if (types != null) {
for (TypeMirror type : types) {
Element element = this.metadataEnv.getTypeUtils().asElement(type);
AnnotationMirror annotation = this.metadataEnv.getConfigurationPropertiesAnnotation(element);
prefix = (annotation != null) ? getPrefix(annotation) : prefix;
processAnnotatedTypeElement(prefix, (TypeElement) element, true, new Stack<>());
}
}
}
private boolean hasMainReadOperation(TypeElement element) {
for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())) {
if (this.metadataEnv.getReadOperationAnnotation(method) != null

View File

@@ -97,15 +97,10 @@ class MetadataGenerationEnvironment {
private final String nameAnnotation;
private final String importAsConfigurationPropertiesBeanAnnotation;
private final String importAsConfigurationPropertiesBeansAnnotation;
MetadataGenerationEnvironment(ProcessingEnvironment environment, String configurationPropertiesAnnotation,
String nestedConfigurationPropertyAnnotation, String deprecatedConfigurationPropertyAnnotation,
String constructorBindingAnnotation, String defaultValueAnnotation, String endpointAnnotation,
String readOperationAnnotation, String nameAnnotation, String importAsConfigurationPropertiesBeanAnnotation,
String importAsConfigurationPropertiesBeansAnnotation) {
String readOperationAnnotation, String nameAnnotation) {
this.typeUtils = new TypeUtils(environment);
this.elements = environment.getElementUtils();
this.messager = environment.getMessager();
@@ -118,8 +113,6 @@ class MetadataGenerationEnvironment {
this.endpointAnnotation = endpointAnnotation;
this.readOperationAnnotation = readOperationAnnotation;
this.nameAnnotation = nameAnnotation;
this.importAsConfigurationPropertiesBeanAnnotation = importAsConfigurationPropertiesBeanAnnotation;
this.importAsConfigurationPropertiesBeansAnnotation = importAsConfigurationPropertiesBeansAnnotation;
}
private static FieldValuesParser resolveFieldValuesParser(ProcessingEnvironment env) {
@@ -265,14 +258,6 @@ class MetadataGenerationEnvironment {
return this.elements.getTypeElement(this.configurationPropertiesAnnotation);
}
TypeElement getImportAsConfigurationPropertiesBeansAnnotation() {
return this.elements.getTypeElement(this.importAsConfigurationPropertiesBeanAnnotation);
}
TypeElement getImportAsConfigurationPropertiesBeansAnnotationElement() {
return this.elements.getTypeElement(this.importAsConfigurationPropertiesBeansAnnotation);
}
AnnotationMirror getConfigurationPropertiesAnnotation(Element element) {
return getAnnotation(element, this.configurationPropertiesAnnotation);
}
@@ -297,22 +282,6 @@ class MetadataGenerationEnvironment {
return getAnnotation(element, this.nameAnnotation);
}
List<AnnotationMirror> getImportAsConfigurationPropertiesBeanAnnotations(Element element) {
List<AnnotationMirror> annotations = new ArrayList<>();
AnnotationMirror importBean = getAnnotation(element, this.importAsConfigurationPropertiesBeanAnnotation);
if (importBean != null) {
annotations.add(importBean);
}
AnnotationMirror importBeans = getAnnotation(element, this.importAsConfigurationPropertiesBeansAnnotation);
if (importBeans != null) {
AnnotationValue value = importBeans.getElementValues().values().iterator().next();
for (Object contained : (List<?>) value.getValue()) {
annotations.add((AnnotationMirror) contained);
}
}
return Collections.unmodifiableList(annotations);
}
boolean hasNullableAnnotation(Element element) {
return getAnnotation(element, NULLABLE_ANNOTATION) != null;
}

View File

@@ -49,19 +49,16 @@ class PropertyDescriptorResolver {
* specified {@link TypeElement type} based on the specified {@link ExecutableElement
* factory method}, if any.
* @param type the target type
* @param fromImport it the type was imported via a
* {@code @ImportAsConfigurationPropertiesBean}
* @param factoryMethod the method that triggered the metadata for that {@code type}
* or {@code null}
* @return the candidate properties for metadata generation
*/
Stream<PropertyDescriptor<?>> resolve(TypeElement type, boolean fromImport, ExecutableElement factoryMethod) {
Stream<PropertyDescriptor<?>> resolve(TypeElement type, ExecutableElement factoryMethod) {
TypeElementMembers members = new TypeElementMembers(this.environment, type);
if (factoryMethod != null) {
return resolveJavaBeanProperties(type, factoryMethod, members);
}
return resolve(ConfigurationPropertiesTypeElement.of(type, fromImport, this.environment), factoryMethod,
members);
return resolve(ConfigurationPropertiesTypeElement.of(type, this.environment), factoryMethod, members);
}
private Stream<PropertyDescriptor<?>> resolve(ConfigurationPropertiesTypeElement type,
@@ -181,29 +178,20 @@ class PropertyDescriptorResolver {
return boundConstructor;
}
static ConfigurationPropertiesTypeElement of(TypeElement type, boolean fromImport,
MetadataGenerationEnvironment env) {
static ConfigurationPropertiesTypeElement of(TypeElement type, MetadataGenerationEnvironment env) {
boolean constructorBoundType = isConstructorBoundType(type, env);
List<ExecutableElement> constructors = ElementFilter.constructorsIn(type.getEnclosedElements());
List<ExecutableElement> boundConstructors = constructors.stream()
.filter(env::hasConstructorBindingAnnotation).collect(Collectors.toList());
boolean constructorBoundType = isConstructorBoundType(type, fromImport, constructors, env);
return new ConfigurationPropertiesTypeElement(type, constructorBoundType, constructors, boundConstructors);
}
private static boolean isConstructorBoundType(TypeElement type, boolean fromImport,
List<ExecutableElement> constructors, MetadataGenerationEnvironment env) {
private static boolean isConstructorBoundType(TypeElement type, MetadataGenerationEnvironment env) {
if (env.hasConstructorBindingAnnotation(type)) {
return true;
}
if (type.getNestingKind() == NestingKind.MEMBER) {
return isConstructorBoundType((TypeElement) type.getEnclosingElement(), false, constructors, env);
}
if (fromImport) {
for (ExecutableElement constructor : constructors) {
if (!constructor.getParameters().isEmpty()) {
return true;
}
}
return isConstructorBoundType((TypeElement) type.getEnclosingElement(), env);
}
return false;
}