Oh No @Override! / Oh No Utilities.loadImage!

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.

Comments:

Very nice. I wonder if it would be possible to automatically rewrite e.g. some module code from 5.5 to 7.1 this way, so developers wouldn't need to convert their code manually - this would make development much more convenient.

Posted by epdv on October 05, 2011 at 08:07 PM PDT #

Very nice. I wonder if it would be possible to automatically rewrite e.g. some module code from 5.5 to 7.1 this way, so developers wouldn't need to convert their code manually - this would make development much more convenient.

Posted by epdv on October 05, 2011 at 08:08 PM PDT #

Hi Gj,

That is a very cool feature. However not everything is working properly with NB 7.1beta: once you create your custom script, it will not appear later in the "Single Inspection" combo. As it seems to work for you, it must have been fixed meanwhile.

Posted by jmborer on October 05, 2011 at 08:31 PM PDT #

Another thing I noticed is that the refactoring also analyzes the read only blocks (for example those of interface builder), but it reads "error". That is the case for the Override annotation, where it is(was?) generally not generated for event handlers.

Posted by jmborer on October 05, 2011 at 08:38 PM PDT #

Can i say WOW?!!

Custom refactorings at project scope!!! Is there any IDE that does this?! This is awesome. Who said that this feature is useless?! (Hey, i would also like to see some advances on the UML plugin, but that's another story...)

Posted by metator on October 05, 2011 at 11:47 PM PDT #

I added this as a standard upgrade hint in [1]. Note however that while this works as a one-off hint, the Inspect & Refactor dialog does not currently offer upgrade hints in the Refactoring Manager; not sure if this is a bug or not.

[1] http://hg.netbeans.org/core-main/rev/c496395cd126

Posted by Jesse Glick on October 11, 2011 at 04:57 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today