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

mernst
  • 7,437
  • 30
  • 45
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
  • 2
    Check that those annotations can be applied at those positions. Check the `@Target` annotation. – Sotirios Delimanolis Nov 25 '13 at 15:59
  • 1
    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 http://docs.oracle.com/javase/tutorial/java/annotations/basics.html – Stephen C Nov 25 '13 at 16:12
  • 1
    @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)! – xdevel2000 Nov 25 '13 at 16:17
  • @StephenC, yes I'm using a Java 8 compiler. My mistake was not to use @Target(ElementType.TYPE_USE). Now it works. – xdevel2000 Nov 26 '13 at 08:57
  • You should take a minute so to Answer your own Question ... and accept it. – Stephen C Nov 26 '13 at 09:05
  • @StephenC, Ok but however I have not an answer to my second question... – xdevel2000 Nov 26 '13 at 09:22
  • As a small clarification, when you say "we must annotate the annotation", you mean to annotate the definition of NonNull or Interned. – mernst Sep 29 '14 at 16:17

2 Answers2

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
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.

mernst
  • 7,437
  • 30
  • 45