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
This commit is contained in:
Andy Wilkinson
2021-03-25 15:23:09 +00:00
parent 35aeae5a4f
commit af7e4e211c
13 changed files with 261 additions and 44 deletions

View File

@@ -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 {

View File

@@ -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) {