Integrated development environments

Digging into the NetBeans Java Editor

By Geertjan Wielenga

The crown jewels of the NetBeans IDE – or any Java IDE – are the features that enable application developers to quickly and efficiently create and maintain Java code. Syntax coloring, code completion, hints, and code templates—where would we be without them? But when you move past an IDE's wizards, templates, and samples, as well as its GUI builders, debuggers, and profilers, you arrive at the Java tool's core: the Java Editor.

NetBeans Java Editor and javac: A Special Relationship

One not so well known aspect of NetBeans IDE's Java Editor is its use of javac, the standard Java compiler, internally. There, NetBeans engineers have embedded it for the parsing and modeling of Java source code. Virtually all Java features in the NetBeans Java Editor are based on the Java model provided directly by javac. That's been the case ever since development of NetBeans IDE 6 began in 2009. Ever since then, NetBeans engineers have worked closely with the javac team, first at Sun and now at Oracle, to provide NetBeans with a continually enhanced Java Editor. Over the years, many suggestions have been received from the javac team, together with encouragement and pointers for optimally leveraging javac's hooks, to provide new features and refinements in the NetBeans Java Editor.

A screenshot of the Java Editor in context, showing syntax coloring, code completion, the Javadoc window, as well as an example of a new Java 7 language feature, “Strings-in-Switch” statements:

Screenshot 1 - String-in-Switch

A huge advantage in reusing the standard Java compiler as a parsing library is that it has been possible to support new language features in the NetBeans Java Editor far sooner than would otherwise have been the case. For example, at a very early stage during the development of JDK 7, NetBeans engineers were able to add experimental tools for the newly introduced Java 7 language features. The first NetBeans release to contain partial support for Java 7 was NetBeans IDE 6.9, released about a year before Java 7 itself was officially released. Such early support was very useful for the javac team, because it let users try out new language features in the NetBeans Java Editor and provide feedback to the javac team, strengthening the synergy between NetBeans and javac.

Moreover, because the NetBeans Java Editor uses the javac parser, NetBeans engineers do not need to spend any time updating and fixing internal parsers whenever a new version of Java is released. Instead, for new language changes, NetBeans engineers focus on enhancing and extending IDE features, such as tools for automatic code conversions, instead of working on low level parsing code. Even better, some of the IDE's features, particularly the Java 7 “convert to diamond” tool in the NetBeans Java editor, are mostly implemented in javac itself, so that these are developed and maintained separately by the javac team. This greatly improves consistency, as the diamond operator is inferred solely on the javac side.

Another advantage of using the standard compiler is that NetBeans now conforms tightly to the Java specification, since it takes direct advantage of any fix made in javac. Of course, the collaboration works the other way too. For example, to support NetBeans Java Editor features based on javac, parts of javac had to be improved, such as its error recovery. These and similar NetBeans improvements have been integrated back into javac, making it a better compiler.

New Features in the NetBeans Java Editor

While most Java editors are enhanced to some extent from release to release, the range and depth of the NetBeans Java Editor's enhancements have been quite special. NetBeans IDE 7.0, released earlier this year, provided a set of tools for optimally using the new Java 7 language enhancements known as Project Coin, while the minor 7.0.1 release enabled the IDE itself to run on Java 7, benefiting from its performance enhancements.

NetBeans IDE 7.1 takes these developments to the next level. A batch refactoring tool has been added, letting you configure and group multiple refactorings from the NetBeans refactoring library. Predefined refactoring configurations are included, most significantly one for converting applications to use Java 7 language constructs, as can be seen in the image:

Screenshot 2 - Manage InspectionsOnce configured, these refactorings can be applied simultaneously on a specified scope of projects, packages, and files. Moreover, the refactoring library is extensible. Either via Java APIs or via a new declarative language that requires no knowledge of Java, new refactorings can be added to tailor the library to the specific needs of individual users and their projects.

On top of that, and based on user feedback, several loose ends have been tied up in the NetBeans Java Editor in the 7.1 release. For example, when using the Java Editor tool for renaming fields, the tool renames getters and setters that belong to the field. Other examples include new refactorings and a tool for organizing import statements based on user-defined rules. More fine-grained searches of code usages can be done in the Java editor because new scopes have been added, one for searching through the current package and another for searching through the current file. To that has been added a custom scope configuration, so that searches can be performed across subsets of projects, packages, and files.

Usability Enhancements in the NetBeans Java Editor

Mouse behavior and selection within the Java Editor has been significantly beefed up. Double-clicks and triple-clicks in the editor now, for the first time, let you move left and right to select-by-word and up and down to select-by-line. Via a new toolbar button, the selection changes so that a block of code can be selected across multiple lines. For example, a block of “private” modifiers can be selected in one go, across multiple lines, and then changed to “public” via the in-place rename feature.

All in all, the Java Editor in NetBeans IDE 7.1 is the result of careful enhancements, providing a tool consistently in tune with the latest Java language developments, with features vital to every Java developer's needs.

14 Dec 2011

Related Resources

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.