Netbeans 中的代码清除

是否有类似于 NetBeans 中的 Eclipse 清理规则(Preferences > Java > Code Style > Clean Up) ?

Eclipse 中的清理规则允许您清理一些内容,比如组织导入、删除不必要的强制转换、添加缺少的覆盖注释等等。

你也可以在一整套类/包而不是单个类上这样做吗?

73853 次浏览

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

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.

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.

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

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.

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.

NetBeans 7.2 has Inspect & Transform refactoring to do this:

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

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

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!

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