Java 8, Type Annotations and JSR 308
Asked Answered
F

2

13

I've installed the last JDK 8 (b116) but I noticed that I can't use type annotations. For example reading the Java tutorial if I write:

String str = null;
String myString = (@NonNull String) str;

or

TEST st = new @Interned TEST();

the compiler give me the following error:

annotation type not applicable to this kind of declaration

Now it works. Before using a type annotations we must annotate the annotation with @Target(ElementType.TYPE_USE). Look at the comments below!

I also don't understand if the annotations like: NonNull, Interned, etc. will be inserted in the JDK or if we have to download the Checker Framework

Frenetic answered 25/11, 2013 at 15:56 Comment(7)
Check that those annotations can be applied at those positions. Check the @Target annotation.Rubiaceous
Are you sure that you are using the Java 8 compiler, and that you have the source level set to Java 8? Because what you are saying is in direct contradiction to what the Oracle Java Tutorial says about Java 8; see docs.oracle.com/javase/tutorial/java/annotations/basics.htmlCasie
@Sotirios Delimanolis, ok it works with my annotations interfaces. Maybe I'm making a mess with NonNull, Interned, etc... which are not present with the JDK... It seems to understand that to use the type annotations is sufficient to use @Target(ElementType.TYPE_USE)!Frenetic
@StephenC, yes I'm using a Java 8 compiler. My mistake was not to use @Target(ElementType.TYPE_USE). Now it works.Frenetic
You should take a minute so to Answer your own Question ... and accept it.Casie
@StephenC, Ok but however I have not an answer to my second question...Frenetic
As a small clarification, when you say "we must annotate the annotation", you mean to annotate the definition of NonNull or Interned.Marginalia
C
9

You have answered the first part of your Question yourself.

For the second part:

I also don't understand if the annotations like: NonNull, Interned, etc. will be inserted in the JDK or if we have to download the Checker Framework.

Annotations are just a kind of Java class / interface. They have to be defined in source code and compiled.

Ideally you should the definitive source code and / or bytecode files, obtained from the canonical place. However, if you were to reproduce the salient parts of the annotations' source code (the package name, annotation name, field names and type) and compile it, the rest of the JVM would be none the wiser.

But when you talk about specific annotations like @NonNull and @Interned, you need to realize that there could exist multiple versions of these in different packages. This could cause issues (for annotation processing software) until standard / defacto standard versions emerge. I don't know if the Checkers Framework could be called a defacto standard ... yet.

You asked if the checkers annotations would be added to the Java 8 library. I personally doubt it, because the package name for those annotations would be unacceptable. But wait and see ...

Casie answered 26/11, 2013 at 10:32 Comment(0)
M
6

For the second part:

I also don't understand if the annotations like: NonNull, Interned, etc. will be inserted in the JDK or if we have to download the Checker Framework.

The Oracle-distributed JDK does not contain annotations like @NonNull and @Interned -- neither definitions of them, nor occurrences of them on JDK methods.

However, the Checker Framework contains annotated versions of the JDK, as explained in the Checker Framework manual. The Checker Framework lets you use the definitive version of a library at run time and even at compile time, while pluggable type-checking sees the annotations and thus the type-checking results are more precise.

Marginalia answered 30/9, 2014 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.