在 Eclipse 中的保存操作中删除未使用的导入和变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18480938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove unused import and variable on save action in eclipse
提问by Lakhwinder Sharma
How can I remove unused import and variable on save actionin eclipse. I have option Ctrl+Shift+O. but it remove only unused import. I want to remove unused variable also.
如何在 Eclipse 中的保存操作中删除未使用的导入和变量。我有选项 Ctrl+Shift+O。但它只删除未使用的导入。我也想删除未使用的变量。
回答by Bambara
Here is how you can do it:
您可以这样做:
In Eclipse Go to Window -> Preferences-> Java -> Editor -> Save Actions and click on Perform the selected actions on save: Organize imports.
在 Eclipse 中,转到 Window -> Preferences-> Java -> Editor -> Save Actions 并单击 Perform the selected actions on save: Organize links。
Enjoy!
享受!
回答by Jainendra
Eclipse can remove only unused imports using ctrl+shift+O. For unused varibales/functions there is no such shortcut(AFAIK). Eclipse by default will show warning message for such type of variables, if it doesn't then you can enable it from windows > preferences > java > compiler > error/warnings > Unnecessary code
Eclipse可以只取出使用未使用的导入ctrl+ shift+ O。对于未使用的变量/函数,没有这样的快捷方式(AFAIK)。默认情况下,Eclipse 将显示此类变量的警告消息,如果没有,则您可以从windows > preferences > java > compiler > error/warnings > Unnecessary code
UPDATE:
更新:
There is one way to remove unused variables:
有一种方法可以删除未使用的变量:
Go to windows > preferences > Java > code style > cleanup
去 windows > preferences > Java > code style > cleanup
Edit the built in profile.
编辑内置配置文件。
In the unnecessary code tab check remove unused private members
and press ok.
在不必要的代码选项卡中检查remove unused private members
并按确定。
Select the active profile as the one you have just created.
选择活动配置文件作为您刚刚创建的配置文件。
Now to apply profile, open the context menu in the Java Editor and select Source > Clean Up
.
现在要应用配置文件,请在 Java 编辑器中打开上下文菜单并选择Source > Clean Up
。