Skip to content

Commit efb80bc

Browse files
committed
Polish "Support additional nullness signal for Actuator endpoints"
See gh-46854
1 parent 75bcc2e commit efb80bc

File tree

4 files changed

+37
-51
lines changed

4 files changed

+37
-51
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,10 @@
5252
* @author Stephane Nicoll
5353
* @author Scott Frederick
5454
* @author Moritz Halbritter
55-
* @author Wonyong Hwang
5655
*/
5756
class MetadataGenerationEnvironment {
5857

59-
private static final Set<String> NULLABLE_ANNOTATIONS = Set.of(
60-
"org.springframework.lang.Nullable",
61-
"org.jspecify.annotations.Nullable");
58+
private static final String NULLABLE_ANNOTATION = "org.jspecify.annotations.Nullable";
6259

6360
private static final Set<String> TYPE_EXCLUDES = Set.of("com.zaxxer.hikari.IConnectionCustomizer",
6461
"groovy.lang.MetaClass", "groovy.text.markup.MarkupTemplateEngine", "java.io.Writer", "java.io.PrintWriter",
@@ -268,7 +265,12 @@ AnnotationMirror getAnnotation(Element element, String type) {
268265
return annotation;
269266
}
270267
}
268+
}
269+
return null;
270+
}
271271

272+
private AnnotationMirror getTypeUseAnnotation(Element element, String type) {
273+
if (element != null) {
272274
for (AnnotationMirror annotation : element.asType().getAnnotationMirrors()) {
273275
if (type.equals(annotation.getAnnotationType().toString())) {
274276
return annotation;
@@ -377,12 +379,7 @@ AnnotationMirror getNameAnnotation(Element element) {
377379
}
378380

379381
boolean hasNullableAnnotation(Element element) {
380-
for (String nullableAnnotation : NULLABLE_ANNOTATIONS) {
381-
if (getAnnotation(element, nullableAnnotation) != null) {
382-
return true;
383-
}
384-
}
385-
return false;
382+
return getTypeUseAnnotation(element, NULLABLE_ANNOTATION) != null;
386383
}
387384

388385
boolean hasOptionalParameterAnnotation(Element element) {

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
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;
32+
import org.springframework.boot.configurationsample.endpoint.OptionalParameterEndpoint;
3133
import org.springframework.boot.configurationsample.endpoint.ReadOnlyAccessEndpoint;
3234
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint;
3335
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint2;
3436
import org.springframework.boot.configurationsample.endpoint.SimpleEndpoint3;
3537
import org.springframework.boot.configurationsample.endpoint.SpecificEndpoint;
3638
import org.springframework.boot.configurationsample.endpoint.UnrestrictedAccessEndpoint;
3739
import org.springframework.boot.configurationsample.endpoint.incremental.IncrementalEndpoint;
38-
import org.springframework.boot.configurationsample.endpoint.NullableParameterEndpoint;
39-
import org.springframework.boot.configurationsample.endpoint.OptionalParameterEndpoint;
4040

4141
import static org.assertj.core.api.Assertions.assertThat;
4242
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
@@ -196,37 +196,25 @@ void shouldFailIfEndpointWithSameIdButWithConflictingEnabledByDefaultSetting() {
196196
}
197197

198198
@Test
199-
void nullableParameterEndpoint() {
199+
void endpointWithNullableParameter() {
200200
ConfigurationMetadata metadata = compile(NullableParameterEndpoint.class);
201-
assertThat(metadata).has(Metadata.withGroup("management.endpoint.nullable").fromSource(NullableParameterEndpoint.class));
201+
assertThat(metadata)
202+
.has(Metadata.withGroup("management.endpoint.nullable").fromSource(NullableParameterEndpoint.class));
202203
assertThat(metadata).has(access("nullable", Access.UNRESTRICTED));
203204
assertThat(metadata).has(cacheTtl("nullable"));
204205
assertThat(metadata.getItems()).hasSize(3);
205206
}
206207

207208
@Test
208-
void optionalParameterEndpoint() {
209+
void endpointWithOptionalParameter() {
209210
ConfigurationMetadata metadata = compile(OptionalParameterEndpoint.class);
210-
assertThat(metadata).has(Metadata.withGroup("management.endpoint.optional").fromSource(OptionalParameterEndpoint.class));
211+
assertThat(metadata)
212+
.has(Metadata.withGroup("management.endpoint.optional").fromSource(OptionalParameterEndpoint.class));
211213
assertThat(metadata).has(access("optional", Access.UNRESTRICTED));
212214
assertThat(metadata).has(cacheTtl("optional"));
213215
assertThat(metadata.getItems()).hasSize(3);
214216
}
215217

216-
@Test
217-
void nullableAndOptionalParameterEquivalence() {
218-
ConfigurationMetadata nullableMetadata = compile(NullableParameterEndpoint.class);
219-
ConfigurationMetadata optionalMetadata = compile(OptionalParameterEndpoint.class);
220-
221-
assertThat(nullableMetadata.getItems()).hasSize(3);
222-
assertThat(optionalMetadata.getItems()).hasSize(3);
223-
224-
assertThat(nullableMetadata).has(access("nullable", Access.UNRESTRICTED));
225-
assertThat(optionalMetadata).has(access("optional", Access.UNRESTRICTED));
226-
assertThat(nullableMetadata).has(cacheTtl("nullable"));
227-
assertThat(optionalMetadata).has(cacheTtl("optional"));
228-
}
229-
230218
private Metadata.MetadataItemCondition access(String endpointId, Access defaultValue) {
231219
return defaultAccess(endpointId, endpointId, defaultValue);
232220
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
package org.springframework.boot.configurationsample.endpoint;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import org.springframework.boot.configurationsample.Endpoint;
2022
import org.springframework.boot.configurationsample.ReadOperation;
21-
import org.jspecify.annotations.Nullable;
2223

2324
/**
24-
* An endpoint with @Nullable parameter to test.
25+
* An endpoint that uses {@code Nullable} to signal an optional parameter.
2526
*
2627
* @author Wonyong Hwang
2728
*/

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.configurationsample.endpoint;
17+
package org.springframework.boot.configurationsample.endpoint;
1818

19-
import org.springframework.boot.configurationsample.Endpoint;
20-
import org.springframework.boot.configurationsample.ReadOperation;
21-
import org.springframework.boot.configurationsample.OptionalParameter;
22-
23-
/**
24-
* An endpoint with @OptionalParameter to compare with @Nullable behavior.
25-
*
26-
* @author Wonyong Hwang
27-
*/
28-
@Endpoint(id = "optional")
29-
public class OptionalParameterEndpoint {
30-
31-
@ReadOperation
32-
public String invoke(@OptionalParameter String parameter) {
33-
return "test with " + parameter;
34-
}
35-
36-
}
19+
import org.springframework.boot.configurationsample.Endpoint;
20+
import org.springframework.boot.configurationsample.OptionalParameter;
21+
import org.springframework.boot.configurationsample.ReadOperation;
22+
23+
/**
24+
* An endpoint that uses {@code OptionalParameter} to signal an optional parameter.
25+
*
26+
* @author Wonyong Hwang
27+
*/
28+
@Endpoint(id = "optional")
29+
public class OptionalParameterEndpoint {
30+
31+
@ReadOperation
32+
public String invoke(@OptionalParameter String parameter) {
33+
return "test with " + parameter;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)