85

Is there something similar to the Eclipse cleanup rules (Preferences > Java > Code Style > Clean Up) in NetBeans?

The cleanup rules in eclipse will allow you to clean things up like organizing imports, removing unnecessary casts, adding missing override annotations etc.

Also can you do that on a whole set of classes/packages instead of individual classes?

Guido
  • 46,642
  • 28
  • 120
  • 174
Kapsh
  • 20,751
  • 13
  • 36
  • 44
  • 3
    I've added a bounty, and I'm hoping someone will know of a plugin or some other manner of doing this. I really miss my Eclipse clean up options. – Ben Hocking May 17 '11 at 14:25
  • As answered below, there are ways to clean up certain aspects of a single source code file. I doubt that NetBeans would introduce a "fix all of my files" feature because changes to each file could be dependent upon changes to the previous file(s), so NetBeans would need to prioritize which files to fix first and compile files as it fixes them. – Daniel May 21 '11 at 12:05
  • @Daniel: The nature of these "fixes" are usually minor: stripping whitespace, adding @Override tags, organizing imports, removing unnecessary casts, removing unused variables, etc. I.e., they're fixes that should have no functional impact, and hence it shouldn't matter what order they're applied in. – Ben Hocking May 22 '11 at 15:57

10 Answers10

91

Refactor > Inspect and Transform

Is there something similar to the Eclipse cleanup rules ((Preferences > Java > Code Style > Clean Up) in NetBeans?

In NetBeans 8.0, the powerful batch tool is Refactor > Inspect and Transform. See the tutorial page, Refactoring with Inspect and Transform in the NetBeans IDE Java Editor.

Individual tools are also available:

  • Fixing imports
    For a single file, namespace, or project (depending on what's selected in Project window or has the focus): Source > Organize Imports (to sort and remove unused imports) or Source > Fix Imports (to sort, remove unused imports, and add missing imports). Or during every save: Tools > Options > Editor > On Save > Organize Imports.

  • Removal of trailing spaces
    For a single file (place the carret in the code file): Source > Remove Trailing Spaces. Or during every save: Tools > Options > Editor > On Save > Remove Trailing Whitespace.

  • Code Format
    For a single file, namespace, or project: Source > Format. (Customize the rules in Tools > Editor > Formatting). There is also a plugin called Jindent you can install (I have not used it myself). Or during every save: Tools > Options > Editor > On Save > Reformat.

By default, Netbeans will display hint icons next to problematic lines of code and in the scrollbar, allowing you to perform an automatic fix if desired. These can be configured via Tools > Options > Editor > Hints. Netbeans can search all problems in the project using Source > Inspect. Or, to reiterate, many of these problems can be batch fixed with Refactor > Inspect and Transform.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • Cool. Thanks. Over time, NetBeans tends to include new tools and utilities accelerating and facilitating coding into core distributions. Good plugins are often integrated into core. New plugins are offered at each release. I believe fully automated functionalities will be made available sooner or later... – Jérôme Verstrynge May 22 '11 at 21:19
  • 4
    As an update to this answer, recursive formatting is implemented in Netbeans natively (yay!). Just select "Source Packages" in the package viewer, then go to Source > Format. The only thing it doesn't do is cleanup imports – TheLQ Nov 30 '12 at 14:24
  • Is there a shortcut for inspect and transform and how to set it – shareef Jul 23 '16 at 20:30
  • It took me a bit to realize that, after selecting `Inspect and Transform` (in my 8.2 Netbeans it appears directly as an option after right-clicking over the project; no `Refactor` previous menu) I had to choose `Use: Configuration: Organize Imports` (click on the `Manage...` button if it doesn't appear directly in the dropdown. There are dozens of "inspections" available there to apply to your code! – Pere Mar 16 '17 at 16:59
22

The equivalent of Eclipse's "Code Cleanup" in NetBeans is "Format". It's under Source > Format, and the keyboard shortcut is Alt+Shift+F (on Windows). Unfortunately, unlike Eclipse, it doesn't seem like this can be configured in NetBeans.

DR.
  • 245
  • 2
  • 3
  • 8
    Eclipse has two separate functions: Code-Format and Code-Cleanup. NetBeans Source-Format is only the equivalent from Code-Format - and even there it is not as powerful. In no way it can be compared to Eclipse Code-Cleanup. Don't know how you got to up votes for this faulty answer. – Martin Dec 13 '10 at 08:12
  • 9
    In netbeans for Code-formate `alt + shift + f` and imports `ctrl + shift + i`. and this works for individual file. For all files there is no functionality. – Ravi Parekh May 19 '11 at 11:21
  • and even with format-code u cannot set column width upto what you want your line of code extend. i mean if u have 200 column width netbean will set as a single line until you manually `Enter`. – Ravi Parekh May 19 '11 at 11:27
  • @RaviParekh The options in Tools > Options > Editor > Formatting > Line Wrap are there, but I see that it's not taking effect. Bug? – Aleksandr Dubinsky Jun 26 '14 at 18:37
11

For Netbeans use Ctrl+Shift+I to remove unused import from the file.

Anil Chahal
  • 2,544
  • 2
  • 22
  • 19
10

NetBeans 7.2 has Inspect & Transform refactoring to do this:

http://netbeans.org/kb/docs/java/editor-inspect-transform.html

DarVar
  • 16,882
  • 29
  • 97
  • 146
2

tools -> options -> Tab "Editor" -> Tab "Hints" -> select Java (talking about) in JComboBox

and then you'd see yellow ocean ..., great and quick from ver 6.9

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • That notifies you about possible problems in your code (and is very useful), but what we're looking for is an option to automatically fix issues such as these, either with a single command to fix all problems in a file (or better yet a set of files), or automatically on save. Eclipse has both of these options. I use it frequently to automatically fill in missing @Override statements, to organize imports, and to eliminate trailing whitespace. – Ben Hocking May 19 '11 at 12:56
  • @Ben Hocking :-) I don't want to something ... and automatically, because in (majories) some cases there are more than one options coming from the Hints, and you can select "better" from Items, how some automat can choose and knows better option ..., loook like that I started FlameWar ... – mKorbel May 19 '11 at 13:04
  • @mKorbel: Which items are fixed automatically are not the same as the warnings, and as you point out you wouldn't want them to be. Typically, the "automatic" things are trivial things, such as organizing imports and eliminating trailing whitespace, as well as slightly bigger things such as adding missing @Override (which could break code if other people compile with Java 1.5). The point is, it's separately configurable from your hints and you can do as much or as little as you like. The "code cleanup on save" is also more dangerous than doing it on request. – Ben Hocking May 19 '11 at 13:29
  • @Ben Hocking but I've never drawn a distinction between whether wrote better or more comfortable in Eclipse or Netbeans – mKorbel May 19 '11 at 14:13
  • 1
    There are plenty of distinctions that can be made, but users of both IDEs would benefit from learning tricks from the other IDE. For example, I really love how easy it is to use cobertura from Netbeans. Although it's possible to do it from Eclipse as well, it requires many more steps. There are also more significant differences, of course. – Ben Hocking May 19 '11 at 14:33
  • @Ben Hocking yes, that was anser to your Bounty, agreed my coworker three or more times switch between Intellij IDEA and Eclipse, for same reason/depends of plugins supports – mKorbel May 19 '11 at 15:02
  • @Ben Hocking I think that you already answered by yourself your bounty, each of IDE's is designated for different areas of users(Teams Coding in Eclipse), approach, plugins, custom Compilator, support and sponsors too, then to your Bounty (maybe) isn't possible to find out rellevant answer – mKorbel May 20 '11 at 10:30
  • It might not be, but I'm hoping that someone is aware of a plugin that implements this behavior. – Ben Hocking May 20 '11 at 16:04
1

Right click on the project (or a package if you want), then choose "Inspect and Transform". In the dialog box, choose "Organize Imports" , from browse choose imports --> then --> Unused Imports

enter image description here

Istiaque Hossain
  • 2,157
  • 1
  • 17
  • 28
1

I know that netbeans points out these things automatically, but beyond merely pointing them out I don't know.

Bobby
  • 18,217
  • 15
  • 74
  • 89
  • 2
    Version 6.5 displays a little light bulb icon inline, which if you click on will suggest fixes (such as add Override annotation, remove unused imports, etc.). – ssakl Jun 10 '09 at 02:47
0

If you just want to make your code have proper indentations on a whole project, simply click the project name, go to "Source," and then click "Format." Hope this helps!

0

There is an Organize Imports plugin Read the DZone article for more info.

EDIT: I see there is a bug report to get this as part of the standard distribution.

Tim Sparg
  • 3,294
  • 2
  • 27
  • 40
0

I don't know a way to do this en mass, but if you delete all imports from a source file and then right click in the source editor, you can select fix imports. This will import all the classes for you alphabetically, asking when it encounters package ambiguities.

As Soldier.moth pointed out, Netbeans will point out other issues, like casts and override annotations, in line, by use of a light bulb to the left of the source.

James McMahon
  • 48,506
  • 64
  • 207
  • 283