简体   繁体   English

使用@NonNullByDefault和数组时,如何删除“空注是多余的”警告? (Eclipse,NullAnnotation)

[英]How to remove 'nullness annotation is redundant' warning when using @NonNullByDefault and arrays? (Eclipse, NullAnnotation)

I get the error 我得到错误

Null type safety (type annotations): The expression of type 'String' needs unchecked conversion to conform to '@NonNull String'

at statement A of this class: 在此类的陈述A:

package org.abego.util;

public class MyClass {
    private String[] names = new String[]{"Alice", "Bob", "Charlie"};

    public String getName(int index) {
        String name = names[index];
        return name; /* statement A */
    }
}

The package org.abego.util defines the default nullness to be "@NonNull": 软件包org.abego.util将默认的空org.abego.util定义为“ @NonNull”:

@org.eclipse.jdt.annotation.NonNullByDefault
package org.abego.util;

When adding @NonNull to the array definition: @NonNull添加到数组定义时:

package org.abego.util;

import org.eclipse.jdt.annotation.NonNull;

public class MyClass {
    private @NonNull String[] names = new @NonNull String[]{"Alice", "Bob", "Charlie"};

    public String getName(int index) {
        String name = names[index];
        return name; /* statement A */
    }
}

the warning at statement A goes away, however I get a new warning 语句A的警告消失了,但是我收到了新的警告

The nullness annotation is redundant with a default that applies to this location

for the type @NonNull String[] in the array definition. 数组定义中的@NonNull String[]类型。

I found no way to make this code warning-free. 我发现没有办法使此代码免于警告。

Could the 'redundant' warning be wrong? “冗余”警告可能是错误的吗? It is my understanding the NonNullByDefault declaration will make sure a type definition String[] will be interpreted as String @NonNull[] , but not as @NonNull String[] or as @NonNull String @NonNull[] . 据我了解, NonNullByDefault声明将确保类型定义String[]将解释为String @NonNull[] ,而不是@NonNull String[]@NonNull String @NonNull[] So the explicit nullness annotation in @NonNull String[] is not redundant, but necessary to get the effective type @NonNull String @NonNull[] . 因此,@ @NonNull String[]的显式无效注释不是多余的,而是获得有效类型@NonNull String @NonNull[]

(I am using Eclipse 4.5 (Mars) and jdk1.8.0_60.) (我正在使用Eclipse 4.5(火星)和jdk1.8.0_60。)

Your expectations are correct. 您的期望是正确的。 @NonNullBeDefault doesn't affect any details of an array type (unless you include DefaultLocation.ARRAY_CONTENTS in the annotation's value ). @NonNullBeDefault不会影响数组类型的任何详细信息(除非您在注释的值中包含DefaultLocation.ARRAY_CONTENTS )。

I believe this to be a variant of https://bugs.eclipse.org/440398 我相信这是https://bugs.eclipse.org/440398的变体

Thanks for the concise example, btw. 感谢您的简洁示例,顺便说一句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM