Monday Dec 03, 2007

Add Property module functionality in trunk

With this check in the Add Property module's functionality is available in trunk. Thanks to Jan Lahoda. The functionality is available through the Add Property popup menu in the Generate Code (Alt+Insert) popup menu in Java Editor.

Thursday Nov 29, 2007

Which Tree module on NetBeans Plugin Portal

I have uploaded the WhichTree module to the NetBeans Plugin Portal.

This module shows information about the Javac Tree Path at the caret position in current Java editor in a read-only text field in the status bar. The information is updated as the caret moves. The text field can be hidden and shown using the button on it's left. The tool tip shows the full text so that it can be seen when it is wider than 40 columns.

Which Tree


Double clicking on the text field shows a dialog which shows the Tree Path in a tree. The source for the selected Tree is shown in a text area. This is a good tool to understand how the Javac sees the Java source. The IDs shown in the tree are the TreeKind enumeration values.


Tree Path details


This is along the lines of WhichElement module I blogged about a few months ago. 


Sources (on branch release60)

DISCLAIMER: This module is experimental. So no guarantees. Use the module at your own risk.

Thursday Nov 15, 2007

Highlight Boxing Unboxing Varargs module on NetBeans Plugin Portal

I have uploaded the Highlight Boxing Unboxing Varargs module to the NetBeans Plugin Portal. It enables highlighting of occurences of boxing, unboxing and varargs in the Java editor. Obviously this only works for files belonging to projects with source level set to 1.5+.

UPDATE: The download link on the Plugin Portal has now been fixed.

The Java compiler silently applies the boxing and unboxing to the primitive types and the corresponding reference types as needed. This module reveals where that may be happening in your code. This module may help you diagnose mysterious NPEs when a reference type null value is attempted to be unboxed at runtime (well the example in the screen shot is not the best ;) ). To turn the highlighting on or off use the toggle buttons on the toolbar. Here is a screen shot:

NetBeans Java Editor showing highlighted boxing, unboxing and varargs occurences

Jan Lahoda contributed to this module.


DISCLAIMER: This module is experimental. So no guarantees. Use the module at your own risk.

Sunday Nov 11, 2007

TIP: Shift text of selected lines or current line to left or right in NetBeans 6.0 editor

In any NetBeans 6.0 editor you can shift the text of selected lines or the current line to the left or right using the following key bindings:

Alt+Shift+Left (Ctrl+Shift+Left on Mac) - shift the text of selected lines or the current line to the left by one indent unit

Alt+Shift+Right (Ctrl+Shift+Right on Mac) - shift the text of selected lines or the current line to the right by one indent unit

The number of spaces used for shifting is controlled by the Tools:Options:Editor:Indetation:Number of Spaces per Indent: option.


Sunday Nov 04, 2007

Add Property module on NetBeans Plugin Portal

I have uploaded the Add Property module to the NetBeans Plugin Portal. It installs the action Source:Add Property. Invoking the action shows the following dialog.

Add Property Dialog


You can configure the property settings. The preview window shows what code will be inserted in Java editor. Select OK to insert the code at current caret position.


You can customize the template by editing it using the Tools:Templates action.

Template Manager Dialog

The template uses the freemarker template engine.



  • validation of name and type
  • validation of duplicate property name
  • browse to select type
  • nicer positioning of the field, getters and setters
  • insertion of required import statements instead of using FQNs


DISCLAIMER: This module is experimental. So no guarantees. Use the module at your own risk.

Friday Oct 19, 2007

Java Decl Gen module

I have uploaded the Java Decl Gen module on the Plugin Portal.

This module supports several forms of parameter, variable, array parameter and array variable declarations.

These actions were formerly part of the contrib/CamelCase module. The CameCase movement and selections are now part of NetBeans 6.0 Java editor.

Assuming you have typed:


in the Java editor you can invoke the following actions:

ActionKey binding
Declare Parameter e.g. Foo foo |Alt+U SPACE
Declare Parameter with comma e.g. Foo foo, |Alt+U ,
Declare Variable e.g. Foo foo;|Alt+U ;
Declare Variable with initializer e.g. Foo foo = new Foo()|;Alt+U =
Declare Variable with casted initializer e.g. Foo foo = (Foo)|;Alt+U (
Declare Array Parameter e.g. Foo[] foos|Ctrl+Alt+U SPACE
Declare Array Parameter with comma e.g. Foo[] foos, |Ctrl+Alt+U ,
Declare Array Variable e.g. Foo[] foos;|Ctrl+Alt+U ;
Declare Array Variable with initializer e.g. Foo[] foos = new Foo[]{};|Ctrl+Alt+U =
Declare Array Variable with casted initializer e.g. Foo[] foos = (Foo[]) ;|Ctrl+Alt+U (

The highlighted regions behave like editor code templates. Use ENTER to escape the code template parameter editing mode - then the cursor will be positioned at |.


  • Use the same algorithm as the one used by Java Editor to generate proposed parameter or variable names.


DISCLAIMER: This module is experimental. So no guarantees. Use the module at your own risk.


Friday May 11, 2007

How is it implemented? Java Editor Hint: Change variable type fix

In the previous entry I talked about the Change Variable Type fix. In this entry I will talk about how it is implemented.

Registration of the factory

Like many other features in NetBeans the hints/fixes/suggestions are implemented using a factory mechanism. The factory is registered in the module's layer file like this:

  <folder name="org-netbeans-modules-java-hints">
    <folder name="rules">\\
      <folder name="errors">
        <file name="org-netbeans-modules-javahints-ChangeTypeCreator.instance"/>

The factory is registered under the org-netbeans-modules-java-hints/rules/errors folder because it creates a fix for certain compilation errors such as type mismatch of the left and right side of a vatriable declaration statement.

Implementation of the factory

As the user edits the source code, the Java editor incrementally runs the javac in the background. As the javac emits compilation errors the editor shows the red squiggly lines. Aside from that it also invokes the factories registered to handle the particular compilation errors. In this case org.netbeans.modules.javahints.ChangeTypeCreator is one such factory. The ChangeTypeCreator implements the interface This intreface essentially has two important method:

    /\*\* Get the diagnostic codes this rule should run on
    public Set<String> getCodes();
    /\*\* Return possible fixes for a given diagnostic report.
    public List<Fix> run(CompilationInfo compilationInfo,
                         String diagnosticKey,
                         int offset,
                         TreePath treePath,
                         Data<T> data);

The getCodes() method essentially indicates the set of compilatioin error codes that are handled by the factory. The ChangeTypeCreator handles:


These codes are part of standard error codes emitted by the javac.

In the run(...) method of ChangeTypeCreator,enough information about the context in the editor is passed in. The ChangeTypeCreator uses the APIs to determine the types of expressions on the left and right side of a variable assignmenets and returns an instance of org.netbeans.modules.javahints.ChangeTypeHint. This instance is used to populate the list of fixes that appear as actions in the light bulb pop up menu in the left margin of the editor.

Implementation of the Change variable type fix

The ChangeTypeHint which implements org.netbeans.spi.editor.hints.Fix . It provides the text of menu item which is returned from:

    public String getText()

The actual fix is implemented in the method:

 public ChangeInfo implement()

using the Java Source APIs.

Friday May 04, 2007

Java Editor Hint: Change variable type fix

In NetBeans 6.0 Java editor supports writing custom, so called, fixes, hints and suggestions. These appear as a light bulb icon in the left margin of the Java editor. For example, in the code below:

100        List list = ...;
101        :
110        String s = list.get(0);

the type of the variable s does not match the type of the expression list.get(0). In such cases a light bulb icon appears in the left margin of the Java editor on the line 110 of the code. Clicking or invoking the fix action reveals a menu item saying Cast ...get(...) to String. Selecting the menu item changes the code on line 110 to:

110        String s = (String) list.get(0);

This is all well and good. However what if you wanted to adjust the type of the variable s to Object. It is very easy to write such a fix and that is what I have implemented.

With this fix, I always write my variable like this:

110        int i = .....; // some expression that return a type I don't know - assume it is RefactoringPluginFactory

Select the hint to change the type of i to the correct type.

110        RefactoringPluginFactory i = ....; 

(I know, I know some may say why I am doing this...because I am an extremely poor typist :( )
Then I quickly delete i and use the smart variable name proposal code completion to get:

110        RefactoringPluginFactory refactoringPluginFactory = ....;

I have added the fix to the Experimental Java Hints (contrib/editorhints/java) module. You can get the module from Development Update center.


DISCLAIMER: This module is experimental. So no guarantees. Use the module at your own risk.




« February 2017