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?
CodePudding user response:
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 you overriding method you don't specify the @Nullable
annotation and thus the warning.