2014-01-27 56 views
8

我想詮釋一個完全合格的類名@Nullable -annotation(從Java檢查架構),例如:Java的作用域結構無法用型使用被註釋

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 

如何註釋完全限定的類名?

回答

9

Java language specification (draft for version 8)§8.3指定一個 「UnannClassType」 爲

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

因此,你需要聲明:

private transient [email protected] Query cached_results; 

或者在檢查框架的增強Java 7的編譯器:

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

注:普通Java編譯器忽略/*@Nullable*/