Replace Actuator @Nullable with @OptionalParameter

Closes gh-45389
This commit is contained in:
Phillip Webb
2025-05-07 14:01:22 -07:00
parent a0944b00d0
commit 36fb1e9b4b
28 changed files with 309 additions and 93 deletions

View File

@@ -106,6 +106,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.actuate.endpoint.annotation.ReadOperation";
static final String OPTIONAL_PARAMETER_ANNOTATION = "org.springframework.boot.actuate.endpoint.annotation.OptionalParameter";
static final String NAME_ANNOTATION = "org.springframework.boot.context.properties.bind.Name";
static final String ENDPOINT_ACCESS_ENUM = "org.springframework.boot.actuate.endpoint.Access";
@@ -155,6 +157,10 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return NAME_ANNOTATION;
}
protected String optionalParameterAnnotation() {
return OPTIONAL_PARAMETER_ANNOTATION;
}
protected String endpointAccessEnum() {
return ENDPOINT_ACCESS_ENUM;
}
@@ -177,7 +183,7 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
this.metadataEnv = new MetadataGenerationEnvironment(env, configurationPropertiesAnnotation(),
nestedConfigurationPropertyAnnotation(), deprecatedConfigurationPropertyAnnotation(),
constructorBindingAnnotation(), autowiredAnnotation(), defaultValueAnnotation(), endpointAnnotations(),
readOperationAnnotation(), nameAnnotation());
readOperationAnnotation(), optionalParameterAnnotation(), nameAnnotation());
}
@Override
@@ -355,13 +361,18 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
private boolean hasNoOrOptionalParameters(ExecutableElement method) {
for (VariableElement parameter : method.getParameters()) {
if (!this.metadataEnv.hasNullableAnnotation(parameter)) {
if (!isOptionalParameter(parameter)) {
return false;
}
}
return true;
}
private boolean isOptionalParameter(VariableElement parameter) {
return this.metadataEnv.hasNullableAnnotation(parameter)
|| this.metadataEnv.hasOptionalParameterAnnotation(parameter);
}
private String getPrefix(AnnotationMirror annotation) {
String prefix = this.metadataEnv.getAnnotationElementStringValue(annotation, "prefix");
if (prefix != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@@ -91,6 +91,8 @@ class MetadataGenerationEnvironment {
private final String readOperationAnnotation;
private final String optionalParameterAnnotation;
private final String nameAnnotation;
private final String autowiredAnnotation;
@@ -98,7 +100,8 @@ class MetadataGenerationEnvironment {
MetadataGenerationEnvironment(ProcessingEnvironment environment, String configurationPropertiesAnnotation,
String nestedConfigurationPropertyAnnotation, String deprecatedConfigurationPropertyAnnotation,
String constructorBindingAnnotation, String autowiredAnnotation, String defaultValueAnnotation,
Set<String> endpointAnnotations, String readOperationAnnotation, String nameAnnotation) {
Set<String> endpointAnnotations, String readOperationAnnotation, String optionalParameterAnnotation,
String nameAnnotation) {
this.typeUtils = new TypeUtils(environment);
this.elements = environment.getElementUtils();
this.messager = environment.getMessager();
@@ -111,6 +114,7 @@ class MetadataGenerationEnvironment {
this.defaultValueAnnotation = defaultValueAnnotation;
this.endpointAnnotations = endpointAnnotations;
this.readOperationAnnotation = readOperationAnnotation;
this.optionalParameterAnnotation = optionalParameterAnnotation;
this.nameAnnotation = nameAnnotation;
}
@@ -337,6 +341,10 @@ class MetadataGenerationEnvironment {
return getAnnotation(element, NULLABLE_ANNOTATION) != null;
}
boolean hasOptionalParameterAnnotation(Element element) {
return getAnnotation(element, this.optionalParameterAnnotation) != null;
}
private boolean isElementDeprecated(Element element) {
return hasAnnotation(element, "java.lang.Deprecated")
|| hasAnnotation(element, this.deprecatedConfigurationPropertyAnnotation);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@@ -171,7 +171,7 @@ class EndpointMetadataGenerationTests extends AbstractMetadataGenerationTests {
assertThat(metadata).has(access("incremental", Access.UNRESTRICTED));
assertThat(metadata).has(cacheTtl("incremental"));
assertThat(metadata.getItems()).hasSize(4);
project.replaceText(IncrementalEndpoint.class, "@Nullable String param", "String param");
project.replaceText(IncrementalEndpoint.class, "@OptionalParameter String param", "String param");
metadata = project.compile();
assertThat(metadata)
.has(Metadata.withGroup("management.endpoint.incremental").fromSource(IncrementalEndpoint.class));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2025 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.
@@ -48,6 +48,7 @@ class MetadataGenerationEnvironmentFactory implements Function<ProcessingEnviron
TestConfigurationMetadataAnnotationProcessor.AUTOWIRED_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.DEFAULT_VALUE_ANNOTATION, endpointAnnotations,
TestConfigurationMetadataAnnotationProcessor.READ_OPERATION_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.OPTIONAL_PARAMETER_ANNOTATION,
TestConfigurationMetadataAnnotationProcessor.NAME_ANNOTATION);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@@ -72,6 +72,8 @@ public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationM
public static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.configurationsample.ReadOperation";
public static final String OPTIONAL_PARAMETER_ANNOTATION = "org.springframework.boot.configurationsample.OptionalParameter";
public static final String NAME_ANNOTATION = "org.springframework.boot.configurationsample.Name";
public static final String ENDPOINT_ACCESS_ENUM = "org.springframework.boot.configurationsample.Access";
@@ -120,6 +122,11 @@ public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationM
return READ_OPERATION_ANNOTATION;
}
@Override
protected String optionalParameterAnnotation() {
return OPTIONAL_PARAMETER_ANNOTATION;
}
@Override
protected String nameAnnotation() {
return NAME_ANNOTATION;

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2012-2025 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 @OptionalParameter} for testing (removes the need
* for a dependency on the real annotation).
*
* @author Phillip Webb
*/
@Target({ ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OptionalParameter {
}

View File

@@ -16,9 +16,9 @@
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.OptionalParameter;
import org.springframework.boot.configurationsample.ReadOperation;
import org.springframework.boot.configurationsample.WebEndpoint;
import org.springframework.lang.Nullable;
/**
* A meta-annotated endpoint. Also with a package private read operation that has an
@@ -31,7 +31,7 @@ import org.springframework.lang.Nullable;
public class SpecificEndpoint {
@ReadOperation
String invoke(@Nullable String param) {
String invoke(@OptionalParameter String param) {
return "test";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2025 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.
@@ -17,8 +17,8 @@
package org.springframework.boot.configurationsample.endpoint.incremental;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.OptionalParameter;
import org.springframework.boot.configurationsample.ReadOperation;
import org.springframework.lang.Nullable;
/**
* An endpoint that is enabled by default.
@@ -29,7 +29,7 @@ import org.springframework.lang.Nullable;
public class IncrementalEndpoint {
@ReadOperation
public String invoke(@Nullable String param) {
public String invoke(@OptionalParameter String param) {
return "test";
}