Skip to content

Commit 450eb48

Browse files
committed
Only support @OptionalParameter annotation with endpoint methods
Remove `@Nullable` support for optional endpoint method parameters in favor of only supporting `@OptionalParameter`. The annotation processor now also only supports `@OptionalParameter` detection. Closes gh-47136
1 parent e764878 commit 450eb48

File tree

12 files changed

+86
-143
lines changed

12 files changed

+86
-143
lines changed

configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
107107

108108
static final String WEB_ENDPOINT_ANNOTATION = "org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint";
109109

110-
static final String READ_OPERATION_ANNOTATION = "org.springframework.boot.actuate.endpoint.annotation.ReadOperation";
110+
static final String ENDPOINT_READ_OPERATION_ANNOTATION = "org.springframework.boot.actuate.endpoint.annotation.ReadOperation";
111111

112-
static final String OPTIONAL_PARAMETER_ANNOTATION = "org.springframework.boot.actuate.endpoint.annotation.OptionalParameter";
112+
static final String ENDPOINT_OPTIONAL_PARAMETER_ANNOTATION = "org.springframework.boot.actuate.endpoint.annotation.OptionalParameter";
113113

114114
static final String NAME_ANNOTATION = "org.springframework.boot.context.properties.bind.Name";
115115

@@ -158,16 +158,16 @@ protected Set<String> endpointAnnotations() {
158158
REST_CONTROLLER_ENDPOINT_ANNOTATION, SERVLET_ENDPOINT_ANNOTATION, WEB_ENDPOINT_ANNOTATION);
159159
}
160160

161-
protected String readOperationAnnotation() {
162-
return READ_OPERATION_ANNOTATION;
161+
protected String endpointReadOperationAnnotation() {
162+
return ENDPOINT_READ_OPERATION_ANNOTATION;
163163
}
164164

165165
protected String nameAnnotation() {
166166
return NAME_ANNOTATION;
167167
}
168168

169-
protected String optionalParameterAnnotation() {
170-
return OPTIONAL_PARAMETER_ANNOTATION;
169+
protected String endpointOptionalParameterAnnotation() {
170+
return ENDPOINT_OPTIONAL_PARAMETER_ANNOTATION;
171171
}
172172

173173
protected String endpointAccessEnum() {
@@ -194,8 +194,8 @@ public synchronized void init(ProcessingEnvironment env) {
194194
this.metadataEnv = new MetadataGenerationEnvironment(env, configurationPropertiesAnnotation(),
195195
configurationPropertiesSourceAnnotation(), nestedConfigurationPropertyAnnotation(),
196196
deprecatedConfigurationPropertyAnnotation(), constructorBindingAnnotation(), autowiredAnnotation(),
197-
defaultValueAnnotation(), endpointAnnotations(), readOperationAnnotation(),
198-
optionalParameterAnnotation(), nameAnnotation());
197+
defaultValueAnnotation(), endpointAnnotations(), endpointReadOperationAnnotation(),
198+
endpointOptionalParameterAnnotation(), nameAnnotation());
199199
}
200200

201201
@Override
@@ -271,8 +271,7 @@ private void processAnnotatedTypeElement(String prefix, TypeElement element, Deq
271271
}
272272

273273
private void processExecutableElement(String prefix, ExecutableElement element, Deque<TypeElement> seen) {
274-
if ((!element.getModifiers().contains(Modifier.PRIVATE))
275-
&& (TypeKind.VOID != element.getReturnType().getKind())) {
274+
if ((!element.getModifiers().contains(Modifier.PRIVATE)) && returnsVoid(element)) {
276275
Element returns = this.processingEnv.getTypeUtils().asElement(element.getReturnType());
277276
if (returns instanceof TypeElement typeElement) {
278277
ItemMetadata group = ItemMetadata.newGroup(prefix,
@@ -354,7 +353,7 @@ private void processEndpoint(AnnotationMirror annotation, TypeElement element) {
354353
"Permitted level of access for the %s endpoint.".formatted(endpointId), defaultAccess, null);
355354
this.metadataCollector.add(accessProperty,
356355
(existing) -> checkDefaultAccessValueMatchesExisting(existing, defaultAccess, type));
357-
if (hasMainReadOperation(element)) {
356+
if (isCachableEndpoint(element)) {
358357
this.metadataCollector.addIfAbsent(ItemMetadata.newProperty(endpointKey, "cache.time-to-live",
359358
Duration.class.getName(), type, null, "Maximum time that a response can be cached.", "0ms", null));
360359
}
@@ -371,28 +370,27 @@ private void checkDefaultAccessValueMatchesExisting(ItemMetadata existing, Strin
371370
}
372371
}
373372

374-
private boolean hasMainReadOperation(TypeElement element) {
373+
private boolean isCachableEndpoint(TypeElement element) {
375374
for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())) {
376-
if (this.metadataEnv.getReadOperationAnnotation(method) != null
377-
&& (TypeKind.VOID != method.getReturnType().getKind()) && hasNoOrOptionalParameters(method)) {
375+
if (this.metadataEnv.isEndpointReadOperation(method) && returnsVoid(method)
376+
&& !hasMandatoryEndpointParameter(method)) {
378377
return true;
379378
}
380379
}
381380
return false;
382381
}
383382

384-
private boolean hasNoOrOptionalParameters(ExecutableElement method) {
383+
private boolean returnsVoid(ExecutableElement method) {
384+
return TypeKind.VOID != method.getReturnType().getKind();
385+
}
386+
387+
private boolean hasMandatoryEndpointParameter(ExecutableElement method) {
385388
for (VariableElement parameter : method.getParameters()) {
386-
if (!isOptionalParameter(parameter)) {
387-
return false;
389+
if (!this.metadataEnv.hasEndpointOptionalParameterAnnotation(parameter)) {
390+
return true;
388391
}
389392
}
390-
return true;
391-
}
392-
393-
private boolean isOptionalParameter(VariableElement parameter) {
394-
return this.metadataEnv.hasNullableAnnotation(parameter)
395-
|| this.metadataEnv.hasOptionalParameterAnnotation(parameter);
393+
return false;
396394
}
397395

398396
private String getPrefix(AnnotationMirror annotation) {

configuration-metadata/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ class MetadataGenerationEnvironment {
9696

9797
private final Set<String> endpointAnnotations;
9898

99-
private final String readOperationAnnotation;
99+
private final String endpointReadOperationAnnotation;
100100

101-
private final String optionalParameterAnnotation;
101+
private final String endpointOptionalParameterAnnotation;
102102

103103
private final String nameAnnotation;
104104

@@ -108,7 +108,7 @@ class MetadataGenerationEnvironment {
108108
String configurationPropertiesSourceAnnotation, String nestedConfigurationPropertyAnnotation,
109109
String deprecatedConfigurationPropertyAnnotation, String constructorBindingAnnotation,
110110
String autowiredAnnotation, String defaultValueAnnotation, Set<String> endpointAnnotations,
111-
String readOperationAnnotation, String optionalParameterAnnotation, String nameAnnotation) {
111+
String endpointReadOperationAnnotation, String endpointOptionalParameterAnnotation, String nameAnnotation) {
112112
this.typeUtils = new TypeUtils(environment);
113113
this.elements = environment.getElementUtils();
114114
this.messager = environment.getMessager();
@@ -122,8 +122,8 @@ class MetadataGenerationEnvironment {
122122
this.autowiredAnnotation = autowiredAnnotation;
123123
this.defaultValueAnnotation = defaultValueAnnotation;
124124
this.endpointAnnotations = endpointAnnotations;
125-
this.readOperationAnnotation = readOperationAnnotation;
126-
this.optionalParameterAnnotation = optionalParameterAnnotation;
125+
this.endpointReadOperationAnnotation = endpointReadOperationAnnotation;
126+
this.endpointOptionalParameterAnnotation = endpointOptionalParameterAnnotation;
127127
this.nameAnnotation = nameAnnotation;
128128
}
129129

@@ -370,8 +370,8 @@ Set<TypeElement> getEndpointAnnotationElements() {
370370
.collect(Collectors.toSet());
371371
}
372372

373-
AnnotationMirror getReadOperationAnnotation(Element element) {
374-
return getAnnotation(element, this.readOperationAnnotation);
373+
boolean isEndpointReadOperation(Element element) {
374+
return getAnnotation(element, this.endpointReadOperationAnnotation) != null;
375375
}
376376

377377
AnnotationMirror getNameAnnotation(Element element) {
@@ -382,8 +382,8 @@ boolean hasNullableAnnotation(Element element) {
382382
return getTypeUseAnnotation(element, NULLABLE_ANNOTATION) != null;
383383
}
384384

385-
boolean hasOptionalParameterAnnotation(Element element) {
386-
return getAnnotation(element, this.optionalParameterAnnotation) != null;
385+
boolean hasEndpointOptionalParameterAnnotation(Element element) {
386+
return getAnnotation(element, this.endpointOptionalParameterAnnotation) != null;
387387
}
388388

389389
private boolean isElementDeprecated(Element element) {

configuration-metadata/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/EndpointMetadataGenerationTests.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.boot.configurationsample.endpoint.CustomPropertiesEndpoint;
2929
import org.springframework.boot.configurationsample.endpoint.EnabledEndpoint;
3030
import org.springframework.boot.configurationsample.endpoint.NoAccessEndpoint;
31-
import org.springframework.boot.configurationsample.endpoint.NullableParameterEndpoint;
3231
import org.springframework.boot.configurationsample.endpoint.OptionalParameterEndpoint;
3332
import org.springframework.boot.configurationsample.endpoint.ReadOnlyAccessEndpoint;
3433
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint;
@@ -195,16 +194,6 @@ void shouldFailIfEndpointWithSameIdButWithConflictingEnabledByDefaultSetting() {
195194
"Existing property 'management.endpoint.simple.access' from type org.springframework.boot.configurationsample.endpoint.SimpleEndpoint has a conflicting value. Existing value: unrestricted, new value from type org.springframework.boot.configurationsample.endpoint.SimpleEndpoint3: none");
196195
}
197196

198-
@Test
199-
void endpointWithNullableParameter() {
200-
ConfigurationMetadata metadata = compile(NullableParameterEndpoint.class);
201-
assertThat(metadata)
202-
.has(Metadata.withGroup("management.endpoint.nullable").fromSource(NullableParameterEndpoint.class));
203-
assertThat(metadata).has(access("nullable", Access.UNRESTRICTED));
204-
assertThat(metadata).has(cacheTtl("nullable"));
205-
assertThat(metadata.getItems()).hasSize(3);
206-
}
207-
208197
@Test
209198
void endpointWithOptionalParameter() {
210199
ConfigurationMetadata metadata = compile(OptionalParameterEndpoint.class);

configuration-metadata/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestConfigurationMetadataAnnotationProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ protected Set<String> endpointAnnotations() {
126126
}
127127

128128
@Override
129-
protected String readOperationAnnotation() {
129+
protected String endpointReadOperationAnnotation() {
130130
return READ_OPERATION_ANNOTATION;
131131
}
132132

133133
@Override
134-
protected String optionalParameterAnnotation() {
134+
protected String endpointOptionalParameterAnnotation() {
135135
return OPTIONAL_PARAMETER_ANNOTATION;
136136
}
137137

configuration-metadata/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/endpoint/NullableParameterEndpoint.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

module/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/invoke/reflect/OperationMethod.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @since 2.0.0
3535
* @see ReflectiveOperationInvoker
3636
*/
37-
public class OperationMethod {
37+
public abstract class OperationMethod {
3838

3939
private static final ParameterNameDiscoverer DEFAULT_PARAMETER_NAME_DISCOVERER = new DefaultParameterNameDiscoverer();
4040

@@ -44,26 +44,14 @@ public class OperationMethod {
4444

4545
private final OperationParameters operationParameters;
4646

47-
/**
48-
* Create a new {@link OperationMethod} instance.
49-
* @param method the source method
50-
* @param operationType the operation type
51-
* @deprecated since 4.0.0 for removal in 4.2.0 in favor of
52-
* {@link #OperationMethod(Method, OperationType, Predicate)}
53-
*/
54-
@Deprecated(since = "4.0.0", forRemoval = true)
55-
public OperationMethod(Method method, OperationType operationType) {
56-
this(method, operationType, (parameter) -> false);
57-
}
58-
5947
/**
6048
* Create a new {@link OperationMethod} instance.
6149
* @param method the source method
6250
* @param operationType the operation type
6351
* @param optionalParameters predicate to test if a parameter is optional
6452
* @since 4.0.0
6553
*/
66-
public OperationMethod(Method method, OperationType operationType, Predicate<Parameter> optionalParameters) {
54+
protected OperationMethod(Method method, OperationType operationType, Predicate<Parameter> optionalParameters) {
6755
Assert.notNull(method, "'method' must not be null");
6856
Assert.notNull(operationType, "'operationType' must not be null");
6957
this.method = method;

module/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/invoke/reflect/OperationMethodParameter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.function.Predicate;
2222

2323
import org.springframework.boot.actuate.endpoint.invoke.OperationParameter;
24-
import org.springframework.core.Nullness;
2524

2625
/**
2726
* {@link OperationParameter} created from an {@link OperationMethod}.
@@ -61,11 +60,7 @@ public Class<?> getType() {
6160

6261
@Override
6362
public boolean isMandatory() {
64-
return !isOptional();
65-
}
66-
67-
private boolean isOptional() {
68-
return Nullness.NULLABLE == Nullness.forParameter(this.parameter) || this.optional.test(this.parameter);
63+
return !this.optional.test(this.parameter);
6964
}
7065

7166
@Override

module/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/invoke/reflect/OperationMethodParameterTests.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ class OperationMethodParameterTests {
4343

4444
private final Method example = ReflectionUtils.findMethod(getClass(), "example", String.class, String.class);
4545

46-
private final Method exampleJSpecifyNullable = ReflectionUtils.findMethod(getClass(), "exampleJSpecifyNullable",
47-
String.class, String.class);
48-
49-
private final Method exampleSpringNullable = ReflectionUtils.findMethod(getClass(), "exampleSpringNullable",
50-
String.class, String.class);
51-
5246
private Method exampleAnnotation = ReflectionUtils.findMethod(getClass(), "exampleAnnotation", String.class);
5347

5448
@Test
@@ -79,20 +73,6 @@ void isMandatoryWhenOptionalAnnotationShouldReturnFalse() {
7973
assertThat(parameter.isMandatory()).isFalse();
8074
}
8175

82-
@Test
83-
void isMandatoryWhenJSpecifyNullableAnnotationShouldReturnFalse() {
84-
OperationMethodParameter parameter = new OperationMethodParameter("name",
85-
this.exampleJSpecifyNullable.getParameters()[1], this::isOptionalParameter);
86-
assertThat(parameter.isMandatory()).isFalse();
87-
}
88-
89-
@Test
90-
void isMandatoryWhenSpringNullableAnnotationShouldReturnFalse() {
91-
OperationMethodParameter parameter = new OperationMethodParameter("name",
92-
this.exampleSpringNullable.getParameters()[1], this::isOptionalParameter);
93-
assertThat(parameter.isMandatory()).isFalse();
94-
}
95-
9676
@Test
9777
void getAnnotationShouldReturnAnnotation() {
9878
OperationMethodParameter parameter = new OperationMethodParameter("name",
@@ -109,13 +89,6 @@ private boolean isOptionalParameter(Parameter parameter) {
10989
void example(String one, @TestOptional String two) {
11090
}
11191

112-
void exampleJSpecifyNullable(String one, @org.jspecify.annotations.Nullable String two) {
113-
}
114-
115-
@SuppressWarnings("deprecation")
116-
void exampleSpringNullable(String one, @org.springframework.lang.Nullable String two) {
117-
}
118-
11992
void exampleAnnotation(@Selector(match = Match.ALL_REMAINING) String allRemaining) {
12093
}
12194

module/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/invoke/reflect/OperationMethodTests.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package org.springframework.boot.actuate.endpoint.invoke.reflect;
1818

1919
import java.lang.reflect.Method;
20-
import java.lang.reflect.Parameter;
21-
import java.util.function.Predicate;
2220

2321
import org.junit.jupiter.api.Test;
2422

@@ -36,39 +34,35 @@
3634
*/
3735
class OperationMethodTests {
3836

39-
private static final Predicate<Parameter> NON_OPTIONAL = (parameter) -> false;
40-
4137
private final Method exampleMethod = ReflectionUtils.findMethod(getClass(), "example", String.class);
4238

4339
@Test
4440
void createWhenMethodIsNullShouldThrowException() {
45-
assertThatIllegalArgumentException()
46-
.isThrownBy(() -> new OperationMethod(null, OperationType.READ, NON_OPTIONAL))
41+
assertThatIllegalArgumentException().isThrownBy(() -> new TestOperationMethod(null, OperationType.READ))
4742
.withMessageContaining("'method' must not be null");
4843
}
4944

5045
@Test
5146
void createWhenOperationTypeIsNullShouldThrowException() {
52-
assertThatIllegalArgumentException()
53-
.isThrownBy(() -> new OperationMethod(this.exampleMethod, null, NON_OPTIONAL))
47+
assertThatIllegalArgumentException().isThrownBy(() -> new TestOperationMethod(this.exampleMethod, null))
5448
.withMessageContaining("'operationType' must not be null");
5549
}
5650

5751
@Test
5852
void getMethodShouldReturnMethod() {
59-
OperationMethod operationMethod = new OperationMethod(this.exampleMethod, OperationType.READ, NON_OPTIONAL);
53+
OperationMethod operationMethod = new TestOperationMethod(this.exampleMethod, OperationType.READ);
6054
assertThat(operationMethod.getMethod()).isEqualTo(this.exampleMethod);
6155
}
6256

6357
@Test
6458
void getOperationTypeShouldReturnOperationType() {
65-
OperationMethod operationMethod = new OperationMethod(this.exampleMethod, OperationType.READ, NON_OPTIONAL);
59+
OperationMethod operationMethod = new TestOperationMethod(this.exampleMethod, OperationType.READ);
6660
assertThat(operationMethod.getOperationType()).isEqualTo(OperationType.READ);
6761
}
6862

6963
@Test
7064
void getParametersShouldReturnParameters() {
71-
OperationMethod operationMethod = new OperationMethod(this.exampleMethod, OperationType.READ, NON_OPTIONAL);
65+
OperationMethod operationMethod = new TestOperationMethod(this.exampleMethod, OperationType.READ);
7266
OperationParameters parameters = operationMethod.getParameters();
7367
assertThat(parameters.getParameterCount()).isOne();
7468
assertThat(parameters.iterator().next().getName()).isEqualTo("name");

0 commit comments

Comments
 (0)