@Nullable / @NotNull

Just downloaded IDEA 5.0, and from the What's new document
(http://www.jetbrains.com/idea/features/newfeatures.html#nullable) there
seem to be a very clever feature for annotating methods that can return
null.

However, I can't get this to work. When I type @Nullable before a method
signature, it's not recognized by IDEA, and Nullable / NotNull doesn't
appear in the autocomplete popup.

Is this a bug, or is it some configuration preference I need to swith on?

--
Vidar S. Ramdal
Remove -nsp to email me

14 comments
Comment actions Permalink

Hello Vidar,

Just downloaded IDEA 5.0, and from the What's new document
(http://www.jetbrains.com/idea/features/newfeatures.html#nullable)
there seem to be a very clever feature for annotating methods that can
return null.

However, I can't get this to work. When I type @Nullable before a
method signature, it's not recognized by IDEA, and Nullable / NotNull
doesn't appear in the autocomplete popup.

Is this a bug, or is it some configuration preference I need to swith
on?


Make sure that Language Level is set to 5.0 in Settings | Paths.

--
Serge Baranov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

Also, annotations.jar needs to be in your classpath (and will need to be at runtime as well). It's in your IDEA directory, under redist/annotations.jar. It's under the Apache 2.0 license, if that matters to you, which among other things means it can be used freely in commercial products.

It is, indeed, a very clever feature.

--Dave Griffith

0
Comment actions Permalink

Serge Baranov wrote:

Hello Vidar,

>> Just downloaded IDEA 5.0, and from the What's new document
>> (http://www.jetbrains.com/idea/features/newfeatures.html#nullable)
>> there seem to be a very clever feature for annotating methods that can
>> return null.
>>
>> However, I can't get this to work. When I type @Nullable before a
>> method signature, it's not recognized by IDEA, and Nullable / NotNull
>> doesn't appear in the autocomplete popup.
>>
>> Is this a bug, or is it some configuration preference I need to swith
>> on?

Make sure that Language Level is set to 5.0 in Settings | Paths.


Thanks for the reply.

Language Level is already set to 5.0 ('enum' keyword, autoboxing etc.).

I even created a brand new project, but to no avail.

--
Vidar S. Ramdal

0
Comment actions Permalink

Dave Griffith wrote:

Also, annotations.jar needs to be in your classpath (and will need to
be at runtime as well). It's in your IDEA directory, under
redist/annotations.jar.


Great, that did the trick. Thanks a lot!


It is, indeed, a very clever feature.


Yep, this just might squish 50% of my bugs :)

--
Vidar S. Ramdal

0
Comment actions Permalink

You need to add

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;


Definitly something that needs to be documented....

0
Comment actions Permalink

Peter Speck wrote:

You need to add

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

Definitly something that needs to be documented....


Yep, should have been in the FAQ, me thinks.

--
Vidar S. Ramdal

0
Comment actions Permalink

Well I have the annotations recognized in the IDE, but I can't seem to get them to actually DO anything. Maybe I'm misunderstanding the point of them, but it seems to me that you shouldn't be able to pass a @Nullable variable into a @NonNull parameter or assign a @NonNull variable from a @Nullable variable or function.

At the very least these type of actions should be selectable as errors and/or warnings in the control panel. Optimally I'd like to have it cause a compilation error, but I'd settle for the former. Any ideas? Are these meant to be simply documentation?

0
Comment actions Permalink

I think it's the "Constant Conditions and Exceptions" inspection that
checks for these things.

0
Comment actions Permalink

Is it possible to use the annotations in code destined for 1.4 JVMs? I wasn't sure if annotations actually result in code in the class files or not. If not, it seems a shame since a lot of them are only really in place to help IDEs (Nullable, NotNull, SuppressWarning etc).

0
Comment actions Permalink

Colin Fleming wrote:

Is it possible to use the annotations in code destined for 1.4 JVMs? I wasn't sure if annotations actually result in code in the class files or not. If not, it seems a shame since a lot of them are only really in place to help IDEs (Nullable, NotNull, SuppressWarning etc).


They are present in the bytecode if the annotation retention for the particular
annotation is set to RUNTIME. JetBrains' Nullable and NotNull annotations are
not set to RUNTIME. However, this isn't an issue because of RetroWeaver anyway,
isn't it?

0
Comment actions Permalink

RUNTIME or CLASS retention are both visible in the bytecode, actually. The difference is that those marked RUNTIME are available for use via the reflection API, while those marked CLASS are only visible by introspecting the bytecode. @Nullable/@NotNull have CLASS retention, and so will be written to bytecode.

Actually, none of this matters anyway, I believe. You can't even compile code with annotations with a 1.4 compiler, and the 1.5 compilers all output bytecode with version numbers that will keep older JVMs from loading it.

Upgrade, or use RetroWeaver.

--Dave Griffith

0
Comment actions Permalink

Ah yes. Very intuitive. ;) Thanks.

So I like it for the most part, but it isn't as strict as it could be. It still seems to me that ANY assignment from either an unannotated variable or @Nullable variable should produce the error. For instance, the following error is not detected:


This should produce a error when trying to return ret. You should be forced to declare ret with a @NotNull in order to return it from a @NotNull function.


Also @NotNull member variables don't require assignment:


This doesn't produce an error. It should be an error unless x is assigned at declaration or during a constructor or init block.

0
Comment actions Permalink

Is there any response as to if/when these sorts of improvements might make it into IDEA?

0

Please sign in to leave a comment.