Fix detection of JSpecify annotations on parameters

See gh-28797
This commit is contained in:
Sébastien Deleuze
2025-01-03 17:29:49 +01:00
parent f136e27ccf
commit 57c0d95007
2 changed files with 25 additions and 7 deletions

View File

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

View File

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