[英]Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference despite initializing the object
[英]Eclipse external null annotation for java.lang.Object#getClass()
我正在使用Eclipse Mars中提供的外部null注释功能。 我正在尝试为java.lang.Object#getClass()
添加外部注释,但似乎无法正确获得签名。 我尝试了以下变体:
@NonNull Class<?> getClass() [()L1java/lang/Class<*>;]
@NonNull Class<@NonNull ?> getClass() [()L1java/lang/Class<*1>;]
但是在将调用getClass()
的结果传递给接受Class<?>
实例的方法时继续收到警告,该参数用@NonNull
注释。
以下是再现问题的最小Eclipse Mars项目中的相关文件(此示例使用上面的第一个null注释变体,但是使用第二个变体时,我也会得到相同的警告):
适用于Windows 64位的Eclipse Mars版本(4.5.0; 20150621-1200)
Oracle JDK 1.8.0_60
src / bar / Foo.java
package bar;
public class Foo {
private static void printType(Class<?> type) {
System.out.println(type.getName());
}
public static void main(String[] args) {
Foo foo = new Foo();
printType(foo.getClass());
}
}
src / bar / package-info.java
@org.eclipse.jdt.annotation.NonNullByDefault
package bar;
注解/java/lang/Object.eea
class java/lang/Object
getClass
()Ljava/lang/Class<*>;
()L1java/lang/Class<*>;
.settings / org.eclipse.jdt.core.prefs (部分)
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=enabled
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=warning
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled
...
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
...
org.eclipse.jdt.core.compiler.compliance=1.8
...
org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=enabled
...
org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
org.eclipse.jdt.core.compiler.problem.nullReference=error
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
...
org.eclipse.jdt.core.compiler.problem.potentialNullReference=error
...
org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=warning
...
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=enabled
...
org.eclipse.jdt.core.compiler.source=1.8
.classpath
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="annotationpath" value="/null-annotation-test/annotations"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="org.eclipse.jdt.annotation_2.0.100.v20150311-1658.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
对于上述项目,我在Foo.java的第10行(调用printType
)上收到以下警告:
Null type safety (type annotations): The expression of type 'Class<capture#of ? extends Foo>' needs unchecked conversion to conform to '@NonNull Class<?>'
这是在没有外部null注释的情况下得到的相同警告。
如何为java.lang.Object#getClass()
正确创建一个外部null注释以删除此警告? 还是我在printType
声明中printType
?
Louis Wasserman是正确的,就类型检查而言,没有一个 getClass()
方法,但是每个类都有自己的带有专用签名的方法。 因此,.eea文件中的任何签名都不会与实际的getClass()方法匹配。
我提交了RFE,以将有关getClass()
更多特殊知识放入编译器,因此该野兽不需要任何外部注释。
编辑:此功能已实现,将与Eclipse 4.6(Neon)一起发布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.