空蚀问题分析类字面null类型安全警示 [英] Eclipse null analysis issues null type safety warning for class literal
问题描述
考虑已启用空分析Eclipse项目的以下部分内容:
Consider the following partial content of an Eclipse project that has null analysis enabled:
的火星的Eclipse版本(4.5.0; 20150621-1200)用于Windows 64位的结果
的Oracle JDK 1.8.0_60 的
package-info.java
@org.eclipse.jdt.annotation.NonNullByDefault
package bar;
Foo.java
package bar;
public class Foo {
public static void main(String[] args) {
}
}
class Base<T> {
private final Class<T> type;
public Base(Class<T> type) {
this.type = type;
}
public Class<T> getType() {
return this.type;
}
}
class Derived extends Base<String> {
public Derived() {
super(String.class); // <-- Null type safety warning here
}
}
空分析产生的调用构造函数超下列警告在派生
:
Null type safety (type annotations): The expression of type 'Class<String>' needs unchecked conversion to conform to '@NonNull Class<@NonNull String>'
什么是避免这种警告的正确方法?
What is the correct way to avoid this warning?
推荐答案
您所看到的冲突的类型参数之间...
The conflict you are seeing is between the type arguments ...
- ...所需类型的:
@NonNull字符串
- ...所提供的类型:
字符串
- ... of the required type:
@NonNull String
- ... of the provided type:
String
非null类型参数到图片,从延伸基地&LT;弦乐&GT;
,它的制度下 @NonNullByDefault
被扩展为扩展基地&LT; @NonNull串&GT;
。从这里渗滤一路攀升到超级构造函数,类型参数替换为后呈现其签名(@非空类&LT; @NonNull串&GT;)
。相比之下,类常量是由编译器视为具有类型 @NonNull类&LT;弦乐方式&gt;
,这是不兼容的。
The non-null type argument comes into the picture from extends Base<String>
, which under the regime of @NonNullByDefault
is expanded to extends Base<@NonNull String>
. From here it percolates all the way up to the super constructor, to render its signature after type argument substitution as (@NonNull Class<@NonNull String>)
. By contrast, the class literal is seen by the compiler as having type @NonNull Class<String>
, which is not compatible.
真正的解决方案会通过的Bug 477719 ,请参阅尤其是在评论3 F的讨论。
The real solution will come via Bug 477719, see in particular the discussion in comment 3 f.
有关暂且(与Eclipse火星),你可能不得不减少 @NonNullByDefault
的作用,使 @NonNull类&LT;弦乐&GT;
将被接受。这可以通过一个更具体的@NNBD宣言类派生的顶部实现
像这样
For the time being (with Eclipse Mars) you may have to reduce the effect of @NonNullByDefault
so that @NonNull Class<String>
will be accepted. This can be achieved by a more specific @NNBD declaration on top of class Derived
like this
import static org.eclipse.jdt.annotation.DefaultLocation.*;
...
@NonNullByDefault({PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND})
class Derived extends Base<String> {
...
在这里,我们正在利用事实上,@NNBD可以进行微调就其所适用的位置。在此声明的差异参数少 @NonNullByDefault
在于省略位置 TYPE_ARGUMENT
,这意味着字符串
在扩展基地&LT;串GT;
将不再受到影响像上面提到。 ERGO超级构造函数现在都会有这样的签名:(@非空类&LT;串GT;)
和所有编译没有警告
Here we are leveraging the fact, the @NNBD can be fine tuned regarding the locations to which it applies. In this declaration the difference to an argument-less @NonNullByDefault
lies in omitting the location TYPE_ARGUMENT
, which means that String
in extends Base<String>
will no longer be affected like mentioned above. Ergo the super constructor will now have this signature: (@NonNull Class<String>)
and all compiles without warnings.
通过将调整@NNBD优良的类导出
此变通办法的范围保持一个尽可能小。
By putting the fine tuned @NNBD on the class Derived
the scope of this workaround is kept a small as possible.
这篇关于空蚀问题分析类字面null类型安全警示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!