Skip to content

NullnessMethodInvocationValidator.MethodNullness#isNullableReturn does not work for Java classes #3316

Open
@fjakop

Description

@fjakop

We switched to spring-data 3.5.1 recently and encountered errors on formerly (spring-data 3.4.4) working code

java.lang.NullPointerException: Return value is null but must not be null
	at org.springframework.data.util.NullnessMethodInvocationValidator.returnValueIsNull(NullnessMethodInvocationValidator.java:124)
...

The method in question is annotated with jakarta.annotation.Nullable, so this should not happen imho.
It's easily reproducible with this test code

package org.springframework.data.util;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.core.ParameterNameDiscoverer;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MethodNullnessTest {

    private final ParameterNameDiscoverer discoverer = new ParameterNameDiscoverer() {
        @Override
        public String[] getParameterNames(Method method) {
            return null;
        }

        @Override
        public String[] getParameterNames(Constructor<?> ctor) {
            return null;
        }
    };

    @Test
    void isNullableReturn() throws NoSuchMethodException {

        assertFalse(NullnessMethodInvocationValidator.MethodNullness
                .of(Dummy.class.getDeclaredMethod("getNonnullData"), discoverer)
                .isNullableReturn());

        assertTrue(NullnessMethodInvocationValidator.MethodNullness
                .of(Dummy.class.getDeclaredMethod("getNullableData"), discoverer)
                .isNullableReturn());

    }

    static class Dummy {

        @Nonnull
        String getNonnullData() {
            return "";
        }

        @Nullable
        String getNullableData() {
            return "";
        }
    }
}

where the assertion for nullable = true always fails.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions