将现有泛型转换为菱形语法
我更喜欢 Java 7 引入的泛型菱形语法 - 与其说是从节省时间的角度来看(毕竟大多数 IDE 无论如何都会为您填充这一点),而只是因为它使代码看起来更干净一些。由于这个原因和其他原因(主要是我正在开发一个新软件,并且 Java 7 中的一些新 API 将会很有用),我很可能会切换现有的代码库以使用/需要 Java 7。
然而,有相当多的块已经编写成钻石前语法,我想在整个过程中始终使用钻石语法。是否有 IDE 快捷方式(我正在使用 Netbeans,但显然可以在任何免费 IDE 中打开项目来执行任务)或其他可以自动切换现有通用代码以使用钻石语法的东西?
I rather like the diamond syntax for generics that Java 7 introduces - not so much from a time saving perspective (after all most IDEs fill that bit in for you anyway) but just because it makes the code look a bit cleaner. Because of this and other reasons (mainly the fact I'm developing a new piece of software and some of the new APIs in Java 7 will be useful) I'm most likely going to switch the existing codebase to use / require Java 7.
However there's a fair chunk already written pre-diamond syntax, and I'd like to consistently use the diamond syntax throughout. Is there an IDE shortcut (I'm using Netbeans but obviously can open the project in any free IDE to do the task) or something else that can automatically switch the existing generic code to use diamond syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
哦,是的,我已经在 IntelliJ(免费社区版)上成功完成了此操作。
菜单>分析>检查代码...
在结果中,选择“Java 语言级别迁移辅助工具 > 显式类型可以替换为 <>”
右键单击,运行“应用修复‘替换为<>’”,你就得到了钻石。
匿名类上存在有关 Diamond 的错误,因此修复后某些代码可能无法编译。那时你必须恢复它们。
Oh yes, I have successfully done this on IntelliJ (free Community Edition).
Menu > Analyze > Inspect Code...
In the result, select "Java language level migration aids > Explicity type can be replaced with <>"
Right click, run "Apply Fix 'Replace with <>'" And you got diamonds.
There was a bug about diamond on anomymous classes, so some code may not compile after the fix. You'll have to revert them back then.
如果您只想分析钻石而不是所有其他检查,请使用 IntelliJ IDEA 12 并转到:
分析 >按名称运行检查...> type “显式类型可以替换为 <>”进入打开的提示符 >选择下拉条目并按 Enter
检查运行后,您可以选择在屏幕底部的“检查”选项卡中应用修复。
这比使用常规“Analyze >”运行每个代码检查要快得多。检查代码...'
If you just want to analyze diamonds and not all the other inspections, use IntelliJ IDEA 12 and go to:
Analyze > Run Inspection by Name... > type "explicit type can be replaced with <>" into the prompt that opens > select the drop down entry and hit Enter
After the inspection runs, you can choose to apply the fix in the Inspection tab at the bottom of the screen.
This is way faster than running every code inspection by using regular 'Analyze > Inspect Code...'
使用 Eclipse,您可以使用正则表达式进行查找/替换。
搜索:
并替换为:
这也将替换任何匿名内部类,因此可能会出现编译错误。
Using Eclipse, you can use a find/replace using regular expressions.
Search for:
And replace for:
This will also replace any anonymous inner classes, so compilation errors might occur.
Eclipse 检测冗余类型参数并提供快速修复来删除它们并创建菱形,请参阅 http://thecoderlounge.blogspot.com/2011/07/java-7-support-in-eclipse-jdt-beta-part_22.html
Eclipse detects redundant type arguments and offers a quick fix to remove them and create a diamond, see http://thecoderlounge.blogspot.com/2011/07/java-7-support-in-eclipse-jdt-beta-part_22.html
这可以使用 Netbeans 内置功能“检查和转换”来完成。
重构
->检查和转换
单一检查
:可以使用Diamond
检查
进行重构
This can be done using Netbeans build-in feature "Inspect and Transform".
Refactor
->Inspect and Transform
Single Inspection
:Can Use Diamond
Inspect
Do Refactoring
您还可以使用 eclipse 的“清理”工具。 (来源 -> 清理,选项卡:不必要的代码,“删除冗余类型参数”)
You can also use the eclipse "Clean Up" tool. (Source -> Clean Up, tab: Unnecessary Code, "Remove redundant type arguments")