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
pull/32840/head
Phillip Webb 2 years ago
parent bdedae21c0
commit c53c8c84b8

@ -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 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"; static final String AUTOWIRED_ANNOTATION = "org.springframework.beans.factory.annotation.Autowired";

@ -36,6 +36,7 @@ import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement; import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements; import javax.lang.model.util.Elements;
@ -184,7 +185,7 @@ class MetadataGenerationEnvironment {
} }
boolean hasConstructorBindingAnnotation(ExecutableElement element) { boolean hasConstructorBindingAnnotation(ExecutableElement element) {
return hasAnnotation(element, this.constructorBindingAnnotation); return hasAnnotation(element, this.constructorBindingAnnotation, true);
} }
boolean hasAutowiredAnnotation(ExecutableElement element) { boolean hasAutowiredAnnotation(ExecutableElement element) {
@ -192,7 +193,40 @@ class MetadataGenerationEnvironment {
} }
boolean hasAnnotation(Element element, String type) { 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) { AnnotationMirror getAnnotation(Element element, String type) {

@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata; import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.test.RoundEnvironmentTester; import org.springframework.boot.configurationprocessor.test.RoundEnvironmentTester;
import org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor; import org.springframework.boot.configurationprocessor.test.TestableAnnotationProcessor;
import org.springframework.boot.configurationsample.immutable.DeprecatedImmutableMultiConstructorProperties;
import org.springframework.boot.configurationsample.immutable.ImmutableClassConstructorBindingProperties; import org.springframework.boot.configurationsample.immutable.ImmutableClassConstructorBindingProperties;
import org.springframework.boot.configurationsample.immutable.ImmutableDeducedConstructorBindingProperties; import org.springframework.boot.configurationsample.immutable.ImmutableDeducedConstructorBindingProperties;
import org.springframework.boot.configurationsample.immutable.ImmutableMultiConstructorProperties; import org.springframework.boot.configurationsample.immutable.ImmutableMultiConstructorProperties;
@ -147,6 +148,16 @@ class PropertyDescriptorResolverTests {
.allMatch((predicate) -> predicate instanceof ConstructorParameterPropertyDescriptor))); .allMatch((predicate) -> predicate instanceof ConstructorParameterPropertyDescriptor)));
} }
@Test
@Deprecated(since = "3.0.0", forRemoval = true)
@SuppressWarnings("removal")
void propertiesWithMultiConstructorAndDeprecatedAnnotation() throws IOException {
process(DeprecatedImmutableMultiConstructorProperties.class,
propertyNames((stream) -> assertThat(stream).containsExactly("name", "description")));
process(DeprecatedImmutableMultiConstructorProperties.class, properties((stream) -> assertThat(stream)
.allMatch((predicate) -> predicate instanceof ConstructorParameterPropertyDescriptor)));
}
@Test @Test
void propertiesWithMultiConstructorNoDirective() throws IOException { void propertiesWithMultiConstructorNoDirective() throws IOException {
process(TwoConstructorsExample.class, propertyNames((stream) -> assertThat(stream).containsExactly("name"))); process(TwoConstructorsExample.class, propertyNames((stream) -> assertThat(stream).containsExactly("name")));

@ -28,7 +28,7 @@ import java.lang.annotation.Target;
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@Target(ElementType.CONSTRUCTOR) @Target({ ElementType.CONSTRUCTOR, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
public @interface ConstructorBinding { public @interface ConstructorBinding {

@ -0,0 +1,39 @@
/*
* Copyright 2012-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Alternative to Spring Boot's deprecated
* {@code @org.springframework.boot.context.properties.ConstructorBinding} for testing
* (removes the need for a dependency on the real annotation).
*
* @author Stephane Nicoll
*/
@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ConstructorBinding
@Deprecated(since = "3.0.0", forRemoval = true)
public @interface DeprecatedConstructorBinding {
}

@ -0,0 +1,46 @@
/*
* Copyright 2012-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.immutable;
/**
* Simple immutable properties with several constructors.
*
* @author Stephane Nicoll
*/
@SuppressWarnings("unused")
@Deprecated(since = "3.0.0", forRemoval = true)
public class DeprecatedImmutableMultiConstructorProperties {
private final String name;
/**
* Test description.
*/
private final String description;
public DeprecatedImmutableMultiConstructorProperties(String name) {
this(name, null);
}
@SuppressWarnings("removal")
@org.springframework.boot.configurationsample.DeprecatedConstructorBinding
public DeprecatedImmutableMultiConstructorProperties(String name, String description) {
this.name = name;
this.description = description;
}
}
Loading…
Cancel
Save