Add @Name support to the annotation processor

Update the `ConfigurationMetadataAnnotationProcessor` so that `@Name`
annotated parameters generate the correct meta-data.

Closes gh-22492
This commit is contained in:
Phillip Webb
2020-07-23 05:35:38 -07:00
parent 3aed03b9f9
commit 33b48c8bac
9 changed files with 168 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -81,6 +81,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.actuate."
+ "endpoint.annotation.ReadOperation";
static final String NAME_ANNOTATION = "org.springframework.boot.context.properties.bind.Name";
private static final Set<String> SUPPORTED_OPTIONS = Collections
.unmodifiableSet(Collections.singleton(ADDITIONAL_METADATA_LOCATIONS_OPTION));
@@ -118,6 +120,10 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return READ_OPERATION_ANNOTATION;
}
protected String nameAnnotation() {
return NAME_ANNOTATION;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
@@ -136,7 +142,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
this.metadataEnv = new MetadataGenerationEnvironment(env, configurationPropertiesAnnotation(),
nestedConfigurationPropertyAnnotation(), deprecatedConfigurationPropertyAnnotation(),
constructorBindingAnnotation(), defaultValueAnnotation(), endpointAnnotation(),
readOperationAnnotation());
readOperationAnnotation(), nameAnnotation());
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -95,10 +95,12 @@ class MetadataGenerationEnvironment {
private final String readOperationAnnotation;
private final String nameAnnotation;
MetadataGenerationEnvironment(ProcessingEnvironment environment, String configurationPropertiesAnnotation,
String nestedConfigurationPropertyAnnotation, String deprecatedConfigurationPropertyAnnotation,
String constructorBindingAnnotation, String defaultValueAnnotation, String endpointAnnotation,
String readOperationAnnotation) {
String readOperationAnnotation, String nameAnnotation) {
this.typeUtils = new TypeUtils(environment);
this.elements = environment.getElementUtils();
this.messager = environment.getMessager();
@@ -110,6 +112,7 @@ class MetadataGenerationEnvironment {
this.defaultValueAnnotation = defaultValueAnnotation;
this.endpointAnnotation = endpointAnnotation;
this.readOperationAnnotation = readOperationAnnotation;
this.nameAnnotation = nameAnnotation;
}
private static FieldValuesParser resolveFieldValuesParser(ProcessingEnvironment env) {
@@ -275,6 +278,10 @@ class MetadataGenerationEnvironment {
return getAnnotation(element, this.readOperationAnnotation);
}
AnnotationMirror getNameAnnotation(Element element) {
return getAnnotation(element, this.nameAnnotation);
}
boolean hasNullableAnnotation(Element element) {
return getAnnotation(element, NULLABLE_ANNOTATION) != null;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -22,6 +22,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.NestingKind;
import javax.lang.model.element.TypeElement;
@@ -76,7 +77,7 @@ class PropertyDescriptorResolver {
TypeElementMembers members, ExecutableElement constructor) {
Map<String, PropertyDescriptor<?>> candidates = new LinkedHashMap<>();
constructor.getParameters().forEach((parameter) -> {
String name = parameter.getSimpleName().toString();
String name = getParameterName(parameter);
TypeMirror propertyType = parameter.asType();
ExecutableElement getter = members.getPublicGetter(name, propertyType);
ExecutableElement setter = members.getPublicSetter(name, propertyType);
@@ -87,6 +88,14 @@ class PropertyDescriptorResolver {
return candidates.values().stream();
}
private String getParameterName(VariableElement parameter) {
AnnotationMirror nameAnnotation = this.environment.getNameAnnotation(parameter);
if (nameAnnotation != null) {
return (String) this.environment.getAnnotationElementValues(nameAnnotation).get("value");
}
return parameter.getSimpleName().toString();
}
Stream<PropertyDescriptor<?>> resolveJavaBeanProperties(TypeElement type, ExecutableElement factoryMethod,
TypeElementMembers members) {
// First check if we have regular java bean properties there