Oh No @Override! / Oh No Utilities.loadImage!
By Geertjan-Oracle on Oct 05, 2011
If you want to experiment with the amazing "Inspect & Transform" functionality that I began blogging about yesterday, my tip for you is that you shouldn't use NetBeans IDE 7.1 Beta, but a more recent development build, because some crucial fixes in this area didn't make it into the 7.1 Beta release.
Now, I want to respond to one of the questions in the comments in my blog entry from yesterday, while also showing something similar to what I showed yesterday, but even better.
So, the question I want to respond to is from Jean-Marc Borer who wanted to know whether it is now possible to inspect multiple projects, find the places where an @Override annotation could be added, and then add those annotations, without doing any kind of boring manual work. And the answer is: Yes.
Here's how. Firstly, here's the application where I want to inspect & transform for @Overrides:
And take note of this issue I filed.
Now, I go to the main menu of the IDE and I choose Refactor | Inspect & Transform. Then, I choose the inspection "Add @Override Annotation", as you can see below, on "Open Projects", i.e., this will be applied to all the modules that you see opened above:
(Tip: If you click "Manage" above, you're able to search for keywords, e.g., I went there and searched for "override" and in that way found the inspection to apply. If you want to group multiple different refactorings together, click "Configuration" and then you can define a set of refactorings that will be run simultaneously on the scope of interest. The scope can be a combination of one or more classes, packages, or projects, so you're completely free to specify to a very fine grained degree which parts of your code you're interested in transforming.)
So, then I click "Inspect" above and the "Refactoring" window shows me all the classes where an @Override can be added, i.e., throughout all my modules, and gives me the opportunity to choose which instances should be changed:
I then click the "Do Refactoring" button and, of course, now everywhere the @Override annotation is added:
(Tip: Want to undo a refactoring? Go to Refactor | Undo and note that it is a known issue and that the undo refactoring action should be part of the standard undo action.)
Cool, right? But it gets much better than that, as my blog entry yesterday indicated. Yesterday, I showed a rather obscure way in which you can add your own refactorings to the "Inspect & Transform" feature, i.e., via a file in the META-INF.upgrade folder. That, of course, is a bit hacky. Using a dev build from yesterday (and probably before also and also in 7.1 Beta, but then not as well as the build I now have), you can do it in a much better way.
Let's take for example this situation, i.e., NetBeans IDE tells me the "Utilities.loadImage" is deprecated:
That's great, thanks for the info NetBeans IDE, but since you know the code is deprecated and, in this case, it is obvious that it can only be replaced by "ImageUtilities.loadImage", why don't you go ahead and do that for me?
So, back in the "Manage Inspections" dialog, I click the "New" button, bottom left of the dialog. The very first time you do that, a node named "Custom" is created, with a subnode "HintSample". Right-click "HintSample" and rename it "Utilities2ImageUtilities". Then, click "Edit Script" and you'll see some kind of template ready for you.
Then put this in the box (i.e., this is instead of what I showed yesterday, where you needed to create your own file in a special folder in your project):
org.openide.util.Utilities.loadImage($string) => org.openide.util.ImageUtilities.loadImage($string) ;;
The above syntax, obviously, needs to be documented, otherwise all of this functionality is guess work. Now you should see this:
(Tip: Before you start writing your own refactoring, i.e., before creating a new custom refactoring as described above, make VERY sure that that refactoring doesn't already exist! There are dozens, maybe hundreds, of them already, there's a good chance that anything related to import statements, for example, already exists; so, search in the "Manage Inspections" dialog, i.e., via the handy filter, before setting out to write your own.)
Next, click Save. Then click Close.
So, now you can choose the new refactoring that you've created:
And then click Inspect. And, drum roll please, here's the result:
What you don't see is that not only are the instances found that can be refactored, but also the related import statements will automatically be added, since they'll obviously be needed by the new code.
I'm still finding all this functionality to be slightly buggy now and then, while some parts of the "Inspect & Transform" dialog don't seem to be optimally intuitive to me, but the functionality is clearly present and promises an incredible boost to productivity, more than NetBeans IDE users have probably ever imagined to be possible.