Fix annotation processor to deal with relocated @ConstructorBinding

Update `ConfigurationMetadataAnnotationProcessor` to use the correct
location for the `@ConstructorBinding` annotation and to deal with
finding it as a meta-annotation.

Closes gh-32660
This commit is contained in:
Phillip Webb
2022-10-20 10:17:48 -07:00
parent bdedae21c0
commit c53c8c84b8
6 changed files with 134 additions and 4 deletions

View File

@@ -78,7 +78,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
static final String DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.context.properties.DeprecatedConfigurationProperty";
static final String CONSTRUCTOR_BINDING_ANNOTATION = "org.springframework.boot.context.properties.ConstructorBinding";
static final String CONSTRUCTOR_BINDING_ANNOTATION = "org.springframework.boot.context.properties.bind.ConstructorBinding";
static final String AUTOWIRED_ANNOTATION = "org.springframework.beans.factory.annotation.Autowired";

View File

@@ -36,6 +36,7 @@ import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
@@ -184,7 +185,7 @@ class MetadataGenerationEnvironment {
}
boolean hasConstructorBindingAnnotation(ExecutableElement element) {
return hasAnnotation(element, this.constructorBindingAnnotation);
return hasAnnotation(element, this.constructorBindingAnnotation, true);
}
boolean hasAutowiredAnnotation(ExecutableElement element) {
@@ -192,7 +193,40 @@ class MetadataGenerationEnvironment {
}
boolean hasAnnotation(Element element, String type) {
return getAnnotation(element, type) != null;
return hasAnnotation(element, type, false);
}
boolean hasAnnotation(Element element, String type, boolean considerMetaAnnotations) {
if (element != null) {
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
if (type.equals(annotation.getAnnotationType().toString())) {
return true;
}
}
if (considerMetaAnnotations) {
Set<Element> seen = new HashSet<>();
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
if (hasMetaAnnotation(annotation.getAnnotationType().asElement(), type, seen)) {
return true;
}
}
}
}
return false;
}
private boolean hasMetaAnnotation(Element annotationElement, String type, Set<Element> seen) {
if (seen.add(annotationElement)) {
for (AnnotationMirror annotation : annotationElement.getAnnotationMirrors()) {
DeclaredType annotationType = annotation.getAnnotationType();
if (type.equals(annotationType.toString())
|| hasMetaAnnotation(annotationType.asElement(), type, seen)) {
return true;
}
}
}
return false;
}
AnnotationMirror getAnnotation(Element element, String type) {