简体   繁体   English

用于插入空检查的IntelliJ快捷方式?

[英]IntelliJ shortcut for inserting a null check?

Is there a shortcut for inserting the code: 是否有插入代码的快捷方式:

if (someParameter == null)
   throw NullPointerException("someParameter is null");

Try typing ifn followed by Tab. 尝试输入ifn后跟Tab。 You can find more of those or define your own by going to the preferences and searching for "Live Templates". 您可以通过转到首选项并搜索“实时模板”来查找更多这些内容或定义自己的内容。

IntelliJ IDEA provides another functionality, similar to shortcuts, that can help with your productivity, namely, Postfix completion . IntelliJ IDEA提供了另一种类似于快捷方式的功能,可以帮助您提高工作效率,即Postfix完成

If you want to insert a null check, type '.null' after the variable name, eg 如果要插入空检查,请在变量名后键入“.null”,例如

后缀完成

and the null-checking code will be inserted automatically, eg 并且将自动插入空检查代码,例如

null检查代码

IntelliJ IDEA provides more handy postfix completions, which you can find in Preferences at Editor > General > Postfix Completion IntelliJ IDEA提供了更多方便的后缀完成,您可以在编辑器中的首选项>常规>后缀完成中找到它

后缀完成

现在有一种简单的方法(在版本13.1中),如下所述: http//blog.jetbrains.com/idea/2014/03/postfix-completion/

There isn't a template defined for that but you can easily create one. 没有为此定义模板,但您可以轻松创建一个。 Just follow the steps described here . 请按照此处描述的步骤操作。

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 :) 它没有直接解决问题,但如果你在很多地方都有这种空检查,你可以使用这个:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM