IntelliJ 插入空检查的快捷方式?
有没有插入代码的快捷方式:
if (someParameter == null)
throw NullPointerException("someParameter is null");
Is there a shortcut for inserting the code:
if (someParameter == null)
throw NullPointerException("someParameter is null");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试输入
ifn
,然后按 Tab。您可以通过转到首选项并搜索“实时模板”来找到更多此类模板或定义您自己的模板。Try typing
ifn
followed by Tab. You can find more of those or define your own by going to the preferences and searching for "Live Templates".IntelliJ IDEA 提供了另一个类似于快捷方式的功能,可以帮助您提高工作效率,即 Postfix 完成。
如果要插入空检查,请在变量名称后键入“.null”,例如
并且会自动插入空检查代码,例如
IntelliJ IDEA 提供了更方便的后缀补全,您可以在编辑器>的首选项中找到它一般>后缀完成
IntelliJ IDEA provides another functionality, similar to shortcuts, that can help with your productivity, namely, Postfix completion.
If you want to insert a null check, type '.null' after the variable name, e.g.
and the null-checking code will be inserted automatically, e.g.
IntelliJ IDEA provides more handy postfix completions, which you can find in Preferences at Editor > General > Postfix Completion
没有为此定义模板,但您可以轻松创建一个。只需按照此处描述的步骤操作< /a>.
There isn't a template defined for that but you can easily create one. Just follow the steps described here.
现在有一种简单的方法可以做到这一点(在版本 13.1 中),如下所述:
http://blog.jetbrains.com/idea/2014/03/postfix-完成/
There is now an easy way to do it (in version 13.1), described here:
http://blog.jetbrains.com/idea/2014/03/postfix-completion/
如果您同意在代码中添加一些注释,那么您可以尝试此处提到的解决方案< /a>.它不能直接解决问题,但如果你在很多地方都有这种空检查,你可以使用它:)
If you are okay with having some annotations in your code then you could try the solution mentioned here. It does not solve the problem directly but if you have this kind of null checks at many places you can use this :)