将现有的泛型转换为菱形语法 [英] Convert existing generics to diamond syntax
问题描述
我更喜欢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.
菜单>
分析>检查代码...
Menu > Analyze > Inspect Code...
在结果中,选择Java语言级别迁移辅助工具> Explicit类型可以替换为<>
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.
// anonymous class, <> doesn't work.
new Factory<Pig>(){ ... }
// however IntelliJ may wrongly "fix" it to
new Factory<>(){ ... } // does not compile.
这篇关于将现有的泛型转换为菱形语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!