未注释的参数覆盖@NonNullApi 参数
Not annotated parameter overrides @NonNullApi parameter
我一直在寻找其他问题,但我仍然不明白这里发生了什么。
我有这个 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);
}
}
它实现了这个 Spring 接口:
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);
};
}
}
并且 IntelliJ 对
中的参数发出警告
public String convert(DocumentType documentType) {
表示
Not annotated parameter overrides @NonNullApi parameter
documentType – the source object to convert, which must be an instance of S (never null)
这是为什么?这是什么意思,因为 Converter 被注释为 @Nullable
?我该如何解决这个警告?
A common Spring annotation to declare that parameters and return values are to be considered as non-nullable by default for a given package.
这是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
可以在方法级别使用 @Nullable
注释覆盖,以防止对可能 return 为空的方法发出警告,这就是您在 [=15 中看到的=]:
@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);
在覆盖方法时,您没有指定 @Nullable
注释,因此没有指定警告。
我一直在寻找其他问题,但我仍然不明白这里发生了什么。 我有这个 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);
}
}
它实现了这个 Spring 接口:
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);
};
}
}
并且 IntelliJ 对
中的参数发出警告public String convert(DocumentType documentType) {
表示
Not annotated parameter overrides @NonNullApi parameter
documentType – the source object to convert, which must be an instance of S (never null)
这是为什么?这是什么意思,因为 Converter 被注释为 @Nullable
?我该如何解决这个警告?
A common Spring annotation to declare that parameters and return values are to be considered as non-nullable by default for a given package.
这是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
可以在方法级别使用 @Nullable
注释覆盖,以防止对可能 return 为空的方法发出警告,这就是您在 [=15 中看到的=]:
@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);
在覆盖方法时,您没有指定 @Nullable
注释,因此没有指定警告。