Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java scoping construct cannot be annotated with type-use

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?

like image 486
Kasper van den Berg Avatar asked Jan 27 '14 16:01

Kasper van den Berg


People also ask

What Cannot be annotated in Java?

Java scoping construct cannot be annotated with type-use.

What is @interface annotation in Java?

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.

What is @target annotation in Java?

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.


1 Answers

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*/.

like image 147
Kasper van den Berg Avatar answered Sep 21 '22 06:09

Kasper van den Berg