私は、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 注釈のバリエーションを使用していますが、2 番目のバリエーションを使用した場合にも同じ警告が表示されます)。
Windows 64 ビット
Oracle JDK 1.8.0_60用の Eclipse Mars リリース (4.5.0; 20150621-1200)
ソース/バー/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
ますか?