Welcome!

Java Authors: Roger Strukhoff, Pat Romanski, Elizabeth White, Mark Cravotta, Liz McMillan

Related Topics: Java

Java: Article

A Better IDE: NetBeans 6.0 - New Core Features in Depth

A better IDE

New features and improvements in the next release of NetBeans make it a better IDE for any kind of developer. From editing to browsing, versioning, building, debugging, profiling or visual design, there is great news for everybody.

It's that time again. A major, dot-zero release of NetBeans will be available soon - over a year and a half after 5.0, which introduced significant new features like the Matisse GUI builder, and extensive improvements in CVS integration, Web services and module development, to cite but a few. In contrast, version 5.5 focused outside the core IDE by supporting several new Packs that increased NetBeans' overall functionality to a level still unmatched by any other open source IDE. Now, is NetBeans 6.0 worthy of the bump in the major version number? You bet it is, and in this article we'll look at some of the most important and interesting new features in the core IDE.

A New Editor
Common sense says no product can be perfect in everything it does, but NetBeans is getting closer each day. Historically, NetBeans users have been proud of the IDE's complete coverage of Java platforms from ME to EE, its support of effective GUI building, and its intuitive UI and open architecture. On the other hand, the IDE lagged in certain areas, like in the code editor or refactoring. This could put off programmers very focused on source code…those who'll pick emacs over visual designers any day. Well, these problems are no more with NetBeans 6.0.

AST-Based Selection
Selecting words or lines is good enough for text editors, but when working with sources you often need to work with ranges of text that form coherent pieces of code. Say you want to copy all the code inside a for loop body in order to paste it in another loop with similar logic. Just place the cursor in any blank position inside the loop body, press Alt+Shift+Period, and you're done. The editor selects the innermost range of text that includes the cursor position and delimits a node of the source's Abstract Syntax Tree.

Pressing Alt+Shift+Period again expands the selection to the next outer node, in this case the complete for statement; then a new keystroke may select the entire method, and so forth. Alt+Shift+Comma will retract the selection to an inner node. Figure 1 shows this feature being used to select a multi-line statement easily and precisely. I bet you will quickly be hooked on this feature and forget about all the other selection shortcuts! There's nothing like a code editor that groks code, not text.

Semantic Highlighter
The editor's syntax highlighter was promoted to a semantics-aware highlighter. It can apply styles based not only on the types of tokens (like identifiers, operators or comments), but also based on different meanings that akin tokens may have - for instance, an identifier may be a class name or a local variable name, a parameter, a constant field, etc.

One benefit of semantic highlighting is that it helps you take extra care when assigning static fields (since many thread-safety and memory-leak bugs involve statics). Figure 1 shows this; notice that static fields (and references to these) appear in italics.

There are other powerful uses for the new highlighting engine:

  • Identifying usages: Select any identifier, and the editor highlights all its uses in the same compilation unit. Again, Figure 1 exemplifies this - click on a method name and all invocations to it are highlighted.
  • Flagging "Smelly code": The new editor highlights unused variables and imports, as well as usage of deprecated classes and methods. You don't need to perform a build or run a code lint tool to detect these simple (but frequent) problems anymore.
  • Exit and throw points: Selecting a method's return type will highlight all return statements. Selecting an exception in the method's throws list will flag all throws of that exception type. All invocations to other methods that may throw the same exception are also flagged.
Better Code Completion
The bewildering amount of APIs you have to use these days makes code completion one of the most critical features of any modern code editor. NetBeans 6.0 has learned many new tricks here:
  • Keyword completion: If you've just typed a package declaration in a new source file (for example), Ctrl+Space will bring only the keywords that are legal in that position: abstract, class, enum, final, import, interface, and public. Figure 1 shows another example: after the opening parenthesis of a method declaration, the preferred completions are all primitive types.
  • Type-based variable names: Completing at "ConfigurationFile _", the editor will offer the variable names cf, configurationFile and file. (I'm using "_" to represent the cursor position.)
  • Generics-aware completions: When assigning a variable with a generic type to a new expression, the editor will offer all compatible types, including generic arguments. For example, at "Map<String, Integer> m = new _", code completion lists all implementations of Map, each with the same <String, Integer> parameters.
  • Annotation-aware completions: When completing after "@", you'll be offered all the annotations that can be used in the given scope. If the selected annotation requires parameters, the editor will provide completions for these too.
  • Passing parameters: At "x = m(_", the top completions will be values in scope that are compatible with m()'s first parameter. If the method's parameter names are available and there are variables with similar names in scope, this is used to sort the completions further. You'll also be offered full completions with the parameter list filled with those variables.
  • Common constructors: When you invoke code completion with the cursor positioned between class members, you'll be able to create a constructor without arguments and one that receives initial values for all fields (if these constructors don't already exist).
  • Catching exceptions: Completion at "catch (_" will only offer exceptions that are thrown in the corresponding try block, but haven't been handled yet by previous catch blocks.
New Browsing Views
The editor introduces several new views for source code browsing. The members' view shows the members of a Java type together with their javadocs, making it easy to find a particular method, field or inner class. The Hierarchy view shows the inheritance tree of a Java type. Figure 1 demonstrates this view; notice the filter buttons that let you toggle between supertypes or subtypes and between simple and fully qualified class names. You can also choose whether or not to show inner classes and interfaces.

The Declaration view summarizes the declaration of the selected Java element (type, method, or field). Despite its name, this view also shows the inspected element's source code if it's available. The Declaration View is especially useful when invoking code still under development, not yet documented with javadoc. Finally, the Javadoc view shows the javadocs for the selected Java element.


More Stories By Osvaldo Pinali Doederlein

Osvaldo holds an MSc in software enginnering and works as technology architect at Visionnaire S/A, developing J2EE technology-based applications.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.