Handy Tool for Code Cleanup: Automated Class Element Reordering

You're working on an application and this thought occurs to you: "Wouldn't it be cool if I could define rules specifying that all static members, initializers, and fields should always be at the top of the class? And then, whenever I wanted to, I'd start off a process that would actually do the reordering for me, moving class elements around, based on the rules I had defined, automatically, across one or more classes or packages or even complete code bases, all at the same time?"

Well, here you go:

That's where you can set rules for the ordering of your class members. A new hint (i.e., new in NetBeans IDE 7.3), which you need to enable yourself because by default it is disabled, let's the IDE show a hint in the Java Editor whenever there's code that isn't ordered according to the rules you defined:

The first element in a file that the Java Editor identifies as not matching your rules gets a lightbulb hint shown in the left sidebar:

Then, when you click the lightbulb, automatically the file is reordered according to your defined rules.

However, it's not much fun going through each file individually to fix class elements as shown above. For that reason, you can go to "Refactor | Inspect and Transform". There, in the "Inspect and Transform" dialog, you can choose the hint shown above and then specify that you'd like it to be applied to a scope of your choice, which could be a file, a package, a project, combinations of these, or all of the open projects, as shown below:

Then, when Inspect is clicked, the Refactoring window shows all the members that are ordered in ways that don't conform to your rules:

Click "Do Refactoring" above and, in one fell swoop, all the class elements within the selected scope are ordered according to your rules.


This is great! Is there a way to have all that set up exported/shared between users? It seems like it can help a team follow standards. It would also be great if you could force some refactorings on compilation (ant task or a Maven goal).

Posted by Javier Ortiz on December 11, 2012 at 02:39 PM PST #

It would be also cool as an save action. http://netbeans.org/bugzilla/show_bug.cgi?id=223702

Posted by markiewb on December 12, 2012 at 01:24 AM PST #

Absolutely yes. The "On Save" feature is new in 7.3 -- you should see a lot more functionality added to it in the coming releases. Thanks for filing the issue.

Posted by Geertjan on December 12, 2012 at 02:07 AM PST #

So... catching up with Eclipse, something like 7 or 8 years later?

Not that it isn't great and you obviously need to start somewhere and build up from there, and I'm very impressed by NetBeans' progress lately and even consider switching IDEs regularly (though I eye IntelliJ as well) but I can hardly get super excited about that.

Posted by myidecandothat on December 13, 2012 at 02:16 AM PST #

So because Eclipse already has this, I should not blog about this feature in NetBeans? Is that what you're saying? Because it does seem like that. When a new feature in NetBeans comes out, I should check whether Eclipse already has it, and then I should pretend it doesn't exist in NetBeans, and certainly not tell NetBeans users that the feature exists. OK, I'll do that. No, I won't, because that would be really really stupid.

Posted by Geertjan on December 13, 2012 at 02:37 AM PST #

That would be handly one day, now im still in the basic stuff and writing "not that big" programs. Still thanks.

Posted by magnias on December 14, 2012 at 08:24 PM PST #

Now the big question: can I integrate this into a Maven life cycle? I.e. is it available only in the UI, or also from the command-line?

Posted by Johannes Schindelin on December 15, 2012 at 01:01 PM PST #

This is an awesome feature. Can't wait to try it out :) .

Posted by SoboLAN on December 18, 2012 at 02:40 AM PST #

tis would be nice to fave with format option/shortcut as well.

Posted by Mani on December 18, 2012 at 06:04 AM PST #

Great news, that's an extremely useful feature! JBuilder used to have that, and I used it a lot to clean up my code. That was one of the features I had been missing a lot (another one is smart toggle comments http://netbeans.org/bugzilla/show_bug.cgi?id=168430). I'm really glad that it's now implemented in NetBeans.

Posted by Toni Epple on January 10, 2013 at 04:31 AM PST #

Where's the download link?

Posted by guest on January 29, 2016 at 05:43 AM PST #


Posted by Geertjan on January 29, 2016 at 06:15 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

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.


« July 2016