java.lang.Object#getClass() 的 Eclipse 外部空注释 [英] Eclipse external null annotation for java.lang.Object#getClass()

查看:31
本文介绍了java.lang.Object#getClass() 的 Eclipse 外部空注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Eclipse Mars 中可用的外部空注释工具.我正在尝试为 java.lang.Object#getClass() 添加外部注释,但似乎无法获得正确的签名.我尝试了以下变体:

I'm using the external null annotation facility available in Eclipse Mars. I'm trying to add an external annotation for java.lang.Object#getClass() but can't seem to get the signature right. I've tried the following variations:

@NonNull Class<?> getClass() [()L1java/lang/Class<*>;]
@NonNull Class<@NonNull ?> getClass() [()L1java/lang/Class<*1>;]

但是在将调用 getClass() 的结果传递给接受 Class 实例的方法时继续收到警告,该参数是用 @NonNull 注释.

but continue to get a warning when passing the result of invoking getClass() to a method that accepts an instance of Class<?>, where that parameter is annotated with @NonNull.

以下是重现该问题的最小 Eclipse Mars 项目的相关文件(此示例使用上面的第一个空注释变体,但在使用第二个变体时我也收到相同的警告):

Below are the relevant files from a minimal Eclipse Mars project that reproduces the issue (this example uses the first null annotation variation above, but I also get the same warning when using the second variation):

适用于 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;

annotations/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)收到以下警告:

For the above project, I receive the following warning on line 10 of Foo.java (where printType is called):

Null type safety (type annotations): The expression of type 'Class<capture#of ? extends Foo>' needs unchecked conversion to conform to '@NonNull Class<?>'

这与我在没有外部空注释的情况下得到的警告相同.

which is the same warning I get without the external null annotation being present.

如何为 java.lang.Object#getClass() 正确创建外部空注释以删除此警告?还是我的printType声明有问题?

How do I correctly create an external null annotation for java.lang.Object#getClass() to remove this warning? Or is my problem in the declaration of printType?

推荐答案

Louis Wasserman 是对的,在类型检查方面没有one getClass() 方法,但是每个类都有自己的带有专门签名的方法.因此,.eea 文件中的任何签名都不会与实际的 getClass() 方法匹配.

Louis Wasserman is correct, in terms of type checking there is not one getClass() method, but each class has it's own method with specialized signature. Hence no signature in a .eea file will ever match the actual getClass() methods.

我提交了 RFE 以提供有关 getClass() 进入编译器,这样这个野兽就不需要外部注释了.

I filed an RFE to put more special knowledge about getClass() into the compiler, so that no external annotations are needed for this beast.

此功能已实现并将随 Eclipse 4.6 (Neon) 一起发布.

This feature has been implemented and will be released with Eclipse 4.6 (Neon).

这篇关于java.lang.Object#getClass() 的 Eclipse 外部空注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆