Support all known endpoint types

Previously, the configuration metadata annotation processor only
declared support for `@Endpoint` and none of the other more
specialized `@…Endpoint` annotations that are meta-annotated with
`@Endpoint` such as `@WebEndpoint` and `@JmxEndpoint. This would
result in missing metadata if a full or incremental build only
compiled classes annotated with one of the more specialized
`@…Endpoint` annotations as the annotation processor would not be
called.

This commit updates the processor's supported annotation types to
include every known `@…Endpoint` annotation. The test processor has
also been similarly updated to align its behaviour with that of the
main processor.

Fixes gh-25388
pull/26153/head
Andy Wilkinson 4 years ago
parent 35aeae5a4f
commit af7e4e211c

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -20,7 +20,9 @@ import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -58,7 +60,12 @@ import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
* @since 1.2.0
*/
@SupportedAnnotationTypes({ ConfigurationMetadataAnnotationProcessor.CONFIGURATION_PROPERTIES_ANNOTATION,
ConfigurationMetadataAnnotationProcessor.CONTROLLER_ENDPOINT_ANNOTATION,
ConfigurationMetadataAnnotationProcessor.ENDPOINT_ANNOTATION,
ConfigurationMetadataAnnotationProcessor.JMX_ENDPOINT_ANNOTATION,
ConfigurationMetadataAnnotationProcessor.REST_CONTROLLER_ENDPOINT_ANNOTATION,
ConfigurationMetadataAnnotationProcessor.SERVLET_ENDPOINT_ANNOTATION,
ConfigurationMetadataAnnotationProcessor.WEB_ENDPOINT_ANNOTATION,
"org.springframework.context.annotation.Configuration" })
public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor {
@ -74,8 +81,18 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
static final String DEFAULT_VALUE_ANNOTATION = "org.springframework.boot.context.properties.bind.DefaultValue";
static final String CONTROLLER_ENDPOINT_ANNOTATION = "org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoint";
static final String ENDPOINT_ANNOTATION = "org.springframework.boot.actuate.endpoint.annotation.Endpoint";
static final String JMX_ENDPOINT_ANNOTATION = "org.springframework.boot.actuate.endpoint.jmx.annotation.JmxEndpoint";
static final String REST_CONTROLLER_ENDPOINT_ANNOTATION = "org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint";
static final String SERVLET_ENDPOINT_ANNOTATION = "org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint";
static final String WEB_ENDPOINT_ANNOTATION = "org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint";
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";
@ -109,8 +126,9 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return DEFAULT_VALUE_ANNOTATION;
}
protected String endpointAnnotation() {
return ENDPOINT_ANNOTATION;
protected Set<String> endpointAnnotations() {
return new HashSet<>(Arrays.asList(CONTROLLER_ENDPOINT_ANNOTATION, ENDPOINT_ANNOTATION, JMX_ENDPOINT_ANNOTATION,
REST_CONTROLLER_ENDPOINT_ANNOTATION, SERVLET_ENDPOINT_ANNOTATION, WEB_ENDPOINT_ANNOTATION));
}
protected String readOperationAnnotation() {
@ -138,7 +156,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
this.metadataCollector = new MetadataCollector(env, this.metadataStore.readMetadata());
this.metadataEnv = new MetadataGenerationEnvironment(env, configurationPropertiesAnnotation(),
nestedConfigurationPropertyAnnotation(), deprecatedConfigurationPropertyAnnotation(),
constructorBindingAnnotation(), defaultValueAnnotation(), endpointAnnotation(),
constructorBindingAnnotation(), defaultValueAnnotation(), endpointAnnotations(),
readOperationAnnotation(), nameAnnotation());
}
@ -151,9 +169,11 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
processElement(element);
}
}
TypeElement endpointType = this.metadataEnv.getEndpointAnnotationElement();
if (endpointType != null) { // Is @Endpoint available
getElementsAnnotatedOrMetaAnnotatedWith(roundEnv, endpointType).forEach(this::processEndpoint);
Set<TypeElement> endpointTypes = this.metadataEnv.getEndpointAnnotationElements();
if (!endpointTypes.isEmpty()) { // Are endpoint annotations available
for (TypeElement endpointType : endpointTypes) {
getElementsAnnotatedOrMetaAnnotatedWith(roundEnv, endpointType).forEach(this::processEndpoint);
}
}
if (roundEnv.processingOver()) {
try {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -24,7 +24,9 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
@ -91,7 +93,7 @@ class MetadataGenerationEnvironment {
private final String defaultValueAnnotation;
private final String endpointAnnotation;
private final Set<String> endpointAnnotations;
private final String readOperationAnnotation;
@ -99,7 +101,7 @@ class MetadataGenerationEnvironment {
MetadataGenerationEnvironment(ProcessingEnvironment environment, String configurationPropertiesAnnotation,
String nestedConfigurationPropertyAnnotation, String deprecatedConfigurationPropertyAnnotation,
String constructorBindingAnnotation, String defaultValueAnnotation, String endpointAnnotation,
String constructorBindingAnnotation, String defaultValueAnnotation, Set<String> endpointAnnotations,
String readOperationAnnotation, String nameAnnotation) {
this.typeUtils = new TypeUtils(environment);
this.elements = environment.getElementUtils();
@ -110,7 +112,7 @@ class MetadataGenerationEnvironment {
this.deprecatedConfigurationPropertyAnnotation = deprecatedConfigurationPropertyAnnotation;
this.constructorBindingAnnotation = constructorBindingAnnotation;
this.defaultValueAnnotation = defaultValueAnnotation;
this.endpointAnnotation = endpointAnnotation;
this.endpointAnnotations = endpointAnnotations;
this.readOperationAnnotation = readOperationAnnotation;
this.nameAnnotation = nameAnnotation;
}
@ -270,8 +272,9 @@ class MetadataGenerationEnvironment {
return getAnnotation(element, this.defaultValueAnnotation);
}
TypeElement getEndpointAnnotationElement() {
return this.elements.getTypeElement(this.endpointAnnotation);
Set<TypeElement> getEndpointAnnotationElements() {
return this.endpointAnnotations.stream().map(this.elements::getTypeElement).filter(Objects::nonNull)
.collect(Collectors.toSet());
}
AnnotationMirror getReadOperationAnnotation(Element element) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -74,13 +74,18 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
assertThat(new ConfigurationMetadataAnnotationProcessor().getSupportedAnnotationTypes())
.containsExactlyInAnyOrder("org.springframework.boot.context.properties.ConfigurationProperties",
"org.springframework.context.annotation.Configuration",
"org.springframework.boot.actuate.endpoint.annotation.Endpoint");
"org.springframework.boot.actuate.endpoint.annotation.Endpoint",
"org.springframework.boot.actuate.endpoint.jmx.annotation.JmxEndpoint",
"org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoint",
"org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint",
"org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint",
"org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint");
}
@Test
void notAnnotated() {
ConfigurationMetadata metadata = compile(NotAnnotated.class);
assertThat(metadata.getItems()).isEmpty();
assertThat(metadata).isNull();
}
@Test

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -69,8 +69,7 @@ class IncrementalBuildMetadataGenerationTests extends AbstractMetadataGeneration
assertThat(metadata).has(Metadata.withProperty("bar.counter").withDefaultValue(0));
project.replaceText(BarProperties.class, "@ConfigurationProperties", "//@ConfigurationProperties");
metadata = project.incrementalBuild(BarProperties.class);
assertThat(metadata).has(Metadata.withProperty("foo.counter").withDefaultValue(0));
assertThat(metadata).isNotEqualTo(Metadata.withProperty("bar.counter"));
assertThat(metadata).isNull();
}
@Test

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -16,6 +16,9 @@
package org.springframework.boot.configurationprocessor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.processing.ProcessingEnvironment;
@ -31,13 +34,18 @@ class MetadataGenerationEnvironmentFactory implements Function<ProcessingEnviron
@Override
public MetadataGenerationEnvironment apply(ProcessingEnvironment environment) {
Set<String> endpointAnnotations = new HashSet<>(
Arrays.asList(TestConfigurationMetadataAnnotationProcessor.CONTROLLER_ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.REST_CONTROLLER_ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.SERVLET_ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.WEB_ENDPOINT_ANNOTATION));
return new MetadataGenerationEnvironment(environment,
TestConfigurationMetadataAnnotationProcessor.CONFIGURATION_PROPERTIES_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.NESTED_CONFIGURATION_PROPERTY_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.CONSTRUCTOR_BINDING_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.DEFAULT_VALUE_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.DEFAULT_VALUE_ANNOTATION, endpointAnnotations,
TestConfigurationMetadataAnnotationProcessor.READ_OPERATION_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.NAME_ANNOTATION);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -20,6 +20,9 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
@ -37,7 +40,14 @@ import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
* @author Andy Wilkinson
* @author Kris De Volder
*/
@SupportedAnnotationTypes({ "*" })
@SupportedAnnotationTypes({ TestConfigurationMetadataAnnotationProcessor.CONFIGURATION_PROPERTIES_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.CONTROLLER_ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.JMX_ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.REST_CONTROLLER_ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.SERVLET_ENDPOINT_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.WEB_ENDPOINT_ANNOTATION,
"org.springframework.context.annotation.Configuration" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationMetadataAnnotationProcessor {
@ -51,8 +61,18 @@ public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationM
public static final String DEFAULT_VALUE_ANNOTATION = "org.springframework.boot.configurationsample.DefaultValue";
public static final String CONTROLLER_ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.ControllerEndpoint";
public static final String ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.Endpoint";
public static final String JMX_ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.JmxEndpoint";
public static final String REST_CONTROLLER_ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.RestControllerEndpoint";
public static final String SERVLET_ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.ServletEndpoint";
public static final String WEB_ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.WebEndpoint";
public static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.configurationsample.ReadOperation";
public static final String NAME_ANNOTATION = "org.springframework.boot.configurationsample.Name";
@ -91,8 +111,9 @@ public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationM
}
@Override
protected String endpointAnnotation() {
return ENDPOINT_ANNOTATION;
protected Set<String> endpointAnnotations() {
return new HashSet<>(Arrays.asList(CONTROLLER_ENDPOINT_ANNOTATION, ENDPOINT_ANNOTATION, JMX_ENDPOINT_ANNOTATION,
REST_CONTROLLER_ENDPOINT_ANNOTATION, SERVLET_ENDPOINT_ANNOTATION, WEB_ENDPOINT_ANNOTATION));
}
@Override

@ -0,0 +1,40 @@
/*
* Copyright 2012-2021 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 {@code @ControllerEndpoint} for testing (removes the need
* for a dependency on the real annotation).
*
* @author Andy Wilkinson
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ControllerEndpoint {
String id() default "";
boolean enableByDefault() default true;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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,18 +22,19 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* Alternative to Spring Boot's {@code @JmxEndpoint} for testing (removes the need for a
* dependency on the real annotation).
*
* @author Andy Wilkinson
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Endpoint
public @interface MetaEndpoint {
public @interface JmxEndpoint {
@AliasFor(annotation = Endpoint.class)
String id();
String id() default "";
@AliasFor(annotation = Endpoint.class)
boolean enableByDefault() default true;
}

@ -0,0 +1,40 @@
/*
* Copyright 2012-2021 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 {@code @RestControllerEndpoint} for testing (removes the
* need for a dependency on the real annotation).
*
* @author Andy Wilkinson
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RestControllerEndpoint {
String id() default "";
boolean enableByDefault() default true;
}

@ -0,0 +1,40 @@
/*
* Copyright 2012-2021 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 {@code @ServletEndpoint} for testing (removes the need for
* a dependency on the real annotation).
*
* @author Andy Wilkinson
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ServletEndpoint {
String id() default "";
boolean enableByDefault() default true;
}

@ -0,0 +1,40 @@
/*
* Copyright 2012-2021 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 {@code @WebEndpoint} for testing (removes the need for a
* dependency on the real annotation).
*
* @author Andy Wilkinson
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebEndpoint {
String id() default "";
boolean enableByDefault() default true;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@ -16,17 +16,17 @@
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.MetaEndpoint;
import org.springframework.boot.configurationsample.ReadOperation;
import org.springframework.boot.configurationsample.WebEndpoint;
import org.springframework.lang.Nullable;
/**
* A meta-annotated endpoint similar to {@code @WebEndpoint} or {@code @JmxEndpoint} in
* Spring Boot. Also with a package private read operation that has an optional argument.
* A meta-annotated endpoint. Also with a package private read operation that has an
* optional argument.
*
* @author Stephane Nicoll
*/
@MetaEndpoint(id = "specific", enableByDefault = true)
@WebEndpoint(id = "specific", enableByDefault = true)
public class SpecificEndpoint {
@ReadOperation

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@ -16,15 +16,15 @@
package org.springframework.boot.configurationsample.endpoint.incremental;
import org.springframework.boot.configurationsample.MetaEndpoint;
import org.springframework.boot.configurationsample.JmxEndpoint;
/**
* A meta-annotated endpoint similar to {@code @WebEndpoint} or {@code @JmxEndpoint} in
* Spring Boot.
* A meta-annotated endpoint.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
*/
@MetaEndpoint(id = "incremental")
@JmxEndpoint(id = "incremental")
public class IncrementalSpecificEndpoint {
}

Loading…
Cancel
Save