NetBeans IDE 7.3 Knows Null

What's the difference between these two methods, "test1" and "test2"?

public int test1(String str) {
    return str.length();
}
  

public int test2(String str) {     if (str == null) {         System.err.println("Passed null!.");         //forgotten return;     }     return str.length(); }

The difference, or at least, the difference that is relevant for this blog entry, is that whoever wrote "test2" apparently thinks that the variable "str" may be null, though did not provide a null check. In NetBeans IDE 7.3, you see this hint for "test2", but no hint for "test1", since in that case we don't know anything about the developer's intention for the variable and providing a hint in that case would flood the source code with too many false positives:

 Annotations are supported in understanding how a piece of code is intended to be used. If method return types use @Nullable, @NullAllowed, @CheckForNull, the value is considered to be "strongly possible to be null", as well as if the variable is tested to be null, as shown above. When using @NotNull, @NonNull, @Nonnull, the value is considered to be non-null. (The exact FQNs of the annotations are ignored, only simple names are checked.)

Here are examples showing where the hints are displayed for the non-null hints (the "strongly possible to be null" hints are not shown below, though you can see one of them in the screenshot above), together with a comment showing what is shown when you hover over the hint:

There isn't a "one size fits all" refactoring for these various instances relating to null checks, hence you can't do an automated refactoring across your code base via tools in NetBeans IDE, as shown yesterday for class member reordering across code bases. However, you can, instead, go to Source | Inspect and then do a scan throughout a scope (e.g., current file/package/project or combinations of these or all open projects) for class elements that the IDE identifies as potentially having a problem in this area:

Thanks to Jan Lahoda, who reports that this currently also works in NetBeans IDE 7.3 dev builds for fields but that may need to be disabled since right now too many false positives are returned, for help with the info above and any misunderstandings are my own fault!

Comments:

Great, I thing 7.3 is a big step forward.

Posted by Michael Kroll on December 12, 2012 at 09:47 PM PST #

Great !

Wonderful hint.

I'm using Netbeans since 2000 and I love it, this is absolutely the best IDE !!!

Posted by Tex on December 14, 2012 at 01:12 AM PST #

Is this related to either JSR 305 or 308?

Posted by Werner Keil on December 17, 2012 at 02:03 PM PST #

The JSR305 meta annotations are not supported at this time.

Posted by Geertjan on December 18, 2012 at 01:37 AM PST #

Nice feature, but already supported by existing code checker plug-in functionality.

These code hints often depend on cached class meta data, which is fine if you make small changes to projects; however you don't even see these hints
if a lengthy meta data rebuild is in progress.

I'd much rather know that serious work is concentrated in 7.3 on ending the ridiculous and performance crippling use of huge numbers of files in the cache directory, and stop prematurely merging meta data, because this can cause massive performance and rebuild delays for large multi-module (especially Maven) projects in Filesystems like NTFS, even on fast flash disks. Proper databases do not idiotically treat a filesystem as a database by splitting data across a huge numbers of files keyed by filename; they use optimised indexes, clobs, blobs and relational data merged into far fewer files, because it uses much less costly IO resources and is vastly faster!

I'd also like to see the ability to manually mark a whole project as dirty, to force a meta data rebuild when NetBeans does not detect inconsistencies, so that I don't have to delete the whole cache directory, which impacts multiple other projects!

Posted by Infernoz on December 20, 2012 at 02:26 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
24
25
26
27
28
29
30
   
       
Today