Not annotated parameter overrides @NonNullApi parameter
Asked Answered
S

2

10

I've been looking around other questions and I still don't understand what's going on here. I have this class:

package com.test.service.database.converter;

import com.test.service.database.dao.DocumentType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class DocumentStringConverter implements Converter<DocumentType, String> {

    @Override
    public String convert(DocumentType documentType) {
        return Optional.ofNullable(documentType).map(DocumentType::type).orElse(null);
    }
}

Which implements this Spring interface:

package org.springframework.core.convert.converter;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

@FunctionalInterface
public interface Converter<S, T> {

    @Nullable
    T convert(S source);

    default <U> Converter<S, U> andThen(Converter<? super T, ? extends U> after) {
        Assert.notNull(after, "After Converter must not be null");
        return (S s) -> {
            T initialResult = convert(s);
            return (initialResult != null ? after.convert(initialResult) : null);
        };
    }
}

And IntelliJ gives a warning on the param in

public String convert(DocumentType documentType) {

that states

Not annotated parameter overrides @NonNullApi parameter

documentType – the source object to convert, which must be an instance of S (never null)

Why is this? What does it mean, since Converter is annotated with @Nullable? And how do I solve this warning?

Sower answered 18/5, 2022 at 8:33 Comment(0)
P
5

From Spring Official Docs:

A common Spring annotation to declare that parameters and return values are to be considered as non-nullable by default for a given package.

Here's the org/springframework/core/convert/converter/package-info.java:

/**
 * SPI to implement Converters for the type conversion system.
 */
@NonNullApi       // !!!!!!!!!!!!
@NonNullFields
package org.springframework.core.convert.converter;

@NonNullApi can be overridden at the method level with the @Nullable annotation to prevent the warning for a method that might return null, and that's what you see in org.springframework.core.convert.converter.Converter:

@FunctionalInterface
public interface Converter<S, T> {

    /**
     * Convert the source object of type {@code S} to target type {@code T}.
     * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
     * @return the converted object, which must be an instance of {@code T} (potentially {@code null})
     * @throws IllegalArgumentException if the source cannot be converted to the desired target type
     */
    @Nullable            // !!!!!!!!!!
    T convert(S source);

On your overridden method, you don't specify the @Nullable annotation and thus the warning.

Pontormo answered 18/5, 2022 at 9:38 Comment(3)
I take it the "// !!!!!!!!!!!!" is there because of how counterinvtuitive it is to have the method Nullable but not the class?Sower
No that's just my comment to mark a particular line of interest =)Pontormo
To fix this I added the package-info.java and added annotations NonNullFields NonNullApi and it works for me. thanks.Isosceles
P
6

Fixed this warning by adding a package-info.java in the same package where the class file is present:

@NonNullApi
package com.my.demo;

import org.springframework.lang.NonNullApi;

Policeman answered 28/8, 2023 at 15:11 Comment(0)
P
5

From Spring Official Docs:

A common Spring annotation to declare that parameters and return values are to be considered as non-nullable by default for a given package.

Here's the org/springframework/core/convert/converter/package-info.java:

/**
 * SPI to implement Converters for the type conversion system.
 */
@NonNullApi       // !!!!!!!!!!!!
@NonNullFields
package org.springframework.core.convert.converter;

@NonNullApi can be overridden at the method level with the @Nullable annotation to prevent the warning for a method that might return null, and that's what you see in org.springframework.core.convert.converter.Converter:

@FunctionalInterface
public interface Converter<S, T> {

    /**
     * Convert the source object of type {@code S} to target type {@code T}.
     * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
     * @return the converted object, which must be an instance of {@code T} (potentially {@code null})
     * @throws IllegalArgumentException if the source cannot be converted to the desired target type
     */
    @Nullable            // !!!!!!!!!!
    T convert(S source);

On your overridden method, you don't specify the @Nullable annotation and thus the warning.

Pontormo answered 18/5, 2022 at 9:38 Comment(3)
I take it the "// !!!!!!!!!!!!" is there because of how counterinvtuitive it is to have the method Nullable but not the class?Sower
No that's just my comment to mark a particular line of interest =)Pontormo
To fix this I added the package-info.java and added annotations NonNullFields NonNullApi and it works for me. thanks.Isosceles

© 2022 - 2024 — McMap. All rights reserved.