@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
Please sign in to leave a comment.
Hello Vidar,
Make sure that Language Level is set to 5.0 in Settings | Paths.
--
Serge Baranov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
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
Serge Baranov wrote:
>> 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?
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
Dave Griffith wrote:
Great, that did the trick. Thanks a lot!
Yep, this just might squish 50% of my bugs :)
--
Vidar S. Ramdal
You need to add
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
Definitly something that needs to be documented....
Peter Speck wrote:
Yep, should have been in the FAQ, me thinks.
--
Vidar S. Ramdal
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?
I think it's the "Constant Conditions and Exceptions" inspection that
checks for these things.
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).
Colin Fleming wrote:
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?
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
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.
See also http://www.jetbrains.net/jira/browse/IDEA-2436,
http://www.jetbrains.net/jira/browse/IDEA-2087 and
http://www.jetbrains.net/jira/browse/IDEA-2002 for discussions about
this and related topics.
Is there any response as to if/when these sorts of improvements might make it into IDEA?