Java 范围构造不能使用类型使用进行注释 [英] Java scoping construct cannot be annotated with type-use

查看:29
本文介绍了Java 范围构造不能使用类型使用进行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 @Nullable-annotation(来自 Java Checker Framework)注释一个完全限定的类名,例如:

I want to annotate a fully qualified class name with @Nullable-annotation (from the Java Checker Framework), e.g.:

class Demo {
    private transient @Nullable org.apache.lucene.search.Query cached_results;

    // ...
}

但是这会导致错误:

scoping construct cannot be annotated with type-use annotation: @checkers.nullness.quals.Nullable

如何注释完全限定的类名?

How can I annotate fully qualified class names?

推荐答案

Java 语言规范(版本 8 草案) §8.3 将UnannClassType"指定为

The Java language specification (draft for version 8) §8.3 specifies a "UnannClassType" as

UnannClassType:
标识符 [TypeArguments]
UnannClassOrInterfaceType .{Annotation} 标识符 [TypeArguments]

UnannClassType:
Identifier [TypeArguments]
UnannClassOrInterfaceType . {Annotation} Identifier [TypeArguments]

因此,您需要声明:

private transient org.apache.lucene.search.@Nullable Query cached_results;

或者在检查器框架的增强型 java 7 编译器中:

Or in the augmented java 7 compiler of the checker framework:

private transient org.apache.lucene.search./*@Nullable*/ Query cached_results;

注意:普通的 Java 编译器会忽略 /*@Nullable*/.

这篇关于Java 范围构造不能使用类型使用进行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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