Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8, Type Annotations and JSR 308

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

like image 543
xdevel2000 Avatar asked Nov 25 '13 15:11

xdevel2000


1 Answers

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

like image 92
Stephen C Avatar answered Sep 20 '22 21:09

Stephen C