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?
Java scoping construct cannot be annotated with type-use.
Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.
If an @Target meta-annotation is present, the compiler will enforce the usage restrictions indicated by ElementType enum constants, in line with JLS 9.7. 4. For example, this @Target meta-annotation indicates that the declared type is itself a meta-annotation type.
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*/
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With