Java scoping construct cannot be annotated with type-use

那年仲夏 提交于 2020-02-27 05:07:12

问题


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;

    // ...
}

However this results in the error:

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

How can I annotate fully qualified class names?


回答1:


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

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

Thus, you need the declaration:

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

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

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

NOTE: normal Java compilers ignore /*@Nullable*/.



来源:https://stackoverflow.com/questions/21385938/java-scoping-construct-cannot-be-annotated-with-type-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!