Java Editor Hint: Change variable type fix
By sandipchitale on May 04, 2007
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 = ...;
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.