From 57c0d95007cee57a8f6141da48cb770f01283397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Fri, 3 Jan 2025 17:29:49 +0100 Subject: [PATCH] Fix detection of JSpecify annotations on parameters See gh-28797 --- .../springframework/core/MethodParameter.java | 3 +- .../core/MethodParameterTests.java | 29 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/MethodParameter.java b/spring-core/src/main/java/org/springframework/core/MethodParameter.java index 068360e441..5639cf6351 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -412,7 +412,8 @@ public class MethodParameter { return true; } } - for (AnnotatedType annotatedType : this.executable.getAnnotatedParameterTypes()) { + if (this.parameterIndex >= 0) { + AnnotatedType annotatedType = this.executable.getAnnotatedParameterTypes()[this.parameterIndex]; for (Annotation ann : annotatedType.getAnnotations()) { if ("Nullable".equals(ann.annotationType().getSimpleName())) { return true; diff --git a/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java b/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java index 715d887881..35b25c87e9 100644 --- a/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java +++ b/spring-core/src/test/java/org/springframework/core/MethodParameterTests.java @@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException * @author Juergen Hoeller * @author Sam Brannen * @author Phillip Webb + * @author Sebastien Deleuze */ class MethodParameterTests { @@ -51,8 +52,12 @@ class MethodParameterTests { private MethodParameter jspecifyNullableParameter; + private MethodParameter jspecifyNonNullParameter; + private MethodParameter springNullableParameter; + private MethodParameter springNonNullParameter; + @BeforeEach void setup() throws NoSuchMethodException { @@ -60,10 +65,12 @@ class MethodParameterTests { stringParameter = new MethodParameter(method, 0); longParameter = new MethodParameter(method, 1); intReturnType = new MethodParameter(method, -1); - Method jspecifyNullableMethod = getClass().getMethod("jspecifyNullableMethod", String.class); + Method jspecifyNullableMethod = getClass().getMethod("jspecifyNullableMethod", String.class, String.class); jspecifyNullableParameter = new MethodParameter(jspecifyNullableMethod, 0); - Method springNullableMethod = getClass().getMethod("springNullableMethod", String.class); + jspecifyNonNullParameter = new MethodParameter(jspecifyNullableMethod, 1); + Method springNullableMethod = getClass().getMethod("springNullableMethod", String.class, String.class); springNullableParameter = new MethodParameter(springNullableMethod, 0); + springNonNullParameter = new MethodParameter(springNullableMethod, 1); } @@ -250,23 +257,33 @@ class MethodParameterTests { assertThat(jspecifyNullableParameter.isOptional()).isTrue(); } + @Test + void jspecifyNonNullParameter() { + assertThat(jspecifyNonNullParameter.isOptional()).isFalse(); + } + @Test void springNullableParameter() { assertThat(springNullableParameter.isOptional()).isTrue(); } + @Test + void springNonNullParameter() { + assertThat(springNonNullParameter.isOptional()).isFalse(); + } + public int method(String p1, long p2) { return 42; } - public @org.jspecify.annotations.Nullable String jspecifyNullableMethod(@org.jspecify.annotations.Nullable String parameter) { - return parameter; + public @org.jspecify.annotations.Nullable String jspecifyNullableMethod(@org.jspecify.annotations.Nullable String nullableParameter, String nonNullParameter) { + return nullableParameter; } @SuppressWarnings("deprecation") @org.springframework.lang.Nullable - public String springNullableMethod(@org.springframework.lang.Nullable String parameter) { - return parameter; + public String springNullableMethod(@org.springframework.lang.Nullable String nullableParameter, String nonNullParameter) { + return nullableParameter; } @SuppressWarnings("unused")