gpt4 book ai didi

spring - 未注释的参数覆盖@???范围

转载 作者:行者123 更新时间:2023-12-04 11:07:15 31 4
gpt4 key购买 nike

覆盖代码时:

    @Override
public void open(ExecutionContext executionContext) {
super.open(executionContext);

来自:org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader

我收到 IntelliJ 警告:

未注释的参数覆盖@???参数少... (Strg+F1)
检查信息:报告与在常量条件和异常检查中配置的 @Nullable 和 @NotNull 注释用法相关的问题。

我能做些什么来避免这种情况?

最佳答案

解释

尽管我无法在 AbstractItemCountingItemStreamItemReader 上重现此内容从这个神器

    implementation group: 'org.springframework.batch', name: 'spring-batch-core', version: '4.1.2.RELEASE'

, this annotation means that the parent method has some @NotNull / @NonNull annotation on the parameter, while the overriding method has no.

To fix it, you need to put the same annotation on the overriding method.

As I wrote, there is no such annotation in the version I've downloaded, but check your sources and copy the parameter annotation from there.

This warning sometimes gets buggy, so it can help to restart the IDEA. Restarting helped when I tried to write a trivial example to reproduce the warning.

A trivial reproducible example

Parent class:

package com.dpopov.java.stackoverflow;

import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

public class ParentClass {
public void nonNullMethod(@NonNull String string) {
System.out.println("S is: " + string);
}

public void nullMethod(@Nullable String string) {
System.out.println("S is: " + string);
}
}

child 类:
package com.dpopov.java.stackoverflow;

public class NonNullOverride extends ParentClass {

public static void main(String[] args) {
NonNullOverride nonNullOverride = new NonNullOverride();
nonNullOverride.nonNullMethod(null);
nonNullOverride.nullMethod(null);
}

@Override
public void nonNullMethod(final String string) {
super.nonNullMethod(string);

if (string.equals("666")) {
System.out.println("yes");
}
}

@Override
public void nullMethod(final String string) {
super.nullMethod(string);

if (string == null) {
System.out.println("I am null, it can be.");
return;
}

if (string.equals("666")) {
System.out.println("yes");
}
}
}
  • 为了 100% 确定,我添加了 org.springframework.lang.NonNullorg.springframework.lang.Nullable适当的注释 Nullable/NonNull检查注释Java | Probable bugs | @NotNull/@Nullable problems (在 File | Settings | Editor | Inspections 中)。您可以通过按 Configure annotations 来执行此操作按钮(见截图)。
    Cofigure non-null and nullable annotations for IDEA inspections
  • 结果,我收到了 child 类(class)的警告#nonNullMethod :
    Warning on non-annotated non-null child method argument
  • 特别注意非注释@Nullable方法不会引起任何警告。
  • 警告仅在 IDEA 重新启动后起作用。
  • 如果您打开/关闭检查设置中的复选框并尝试重现它们,则整个检查看起来工作起来很麻烦。因此,再次启动/使缓存无效可能有助于摆脱或显示不正确的警告。
  • 关于spring - 未注释的参数覆盖@???范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56685326/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com