【问题标题】:Is there a way to code complete a null check in java within netbeans IDE?有没有办法在 netbeans IDE 中对 java 中的空值检查进行编码?
【发布时间】:2013-08-18 01:45:31
【问题描述】:

在 Netbeans 中,您可以执行诸如编写“psvm”然后按 Tab 并生成

 public static void main(String[] args) {
     //your cursor is placed here.
 }

类似地,还有编写循环、尝试 catch 块、instanceof 检查和类似的东西的方法。有没有类似这种方法的方法来生成变量的空检查?

我想要这样的东西:

ResultSet rs;
rs //pressing some magic button like ctrl+space or "rs null<TAB>" 
   //and a code like this would be generated:

if (rs != null) //your cursor will be placed here.

if (rs != null)
{
    //your cursor here
}

【问题讨论】:

  • 是的,查看首选项,有一个地方可以让您编写自己的自动完成功能。
  • 是的,我开始深入研究,应该早点完成。还不知道怎么用语言写,等我弄明白了就写,也写在答案里。

标签: java netbeans autocomplete isnull


【解决方案1】:

所以我决定创建这个模板:(已接受答案的修改版本。)

if(${EXP instanceof="Object"} != null) {
    ${EXP}.${cursor}
} 

分配给代表“非空”的“nn”。

这也是为了在方法的开头验证属性/参数。

Validate.notNull(${var}, "${var} can't be null");

我通过写“valp”并按tab(验证参数的缩写)来扩展它。 它比以下更短更简洁:

if(someVar == null){
    throw new Exception("someVar can't be null");
}

当有许多经过验证的参数时,这很有用。

【讨论】:

    【解决方案2】:

    您可以为此创建自己的模板:

    进入工具->选项->编辑器->代码模板->新建->缩写:ifnn

    扩展文本:

    if (${EXP instanceof=”Object”} != null) {
        ${selection}${cursor}
    }
    

    然后在编辑器中按“ifnn”+TAB。

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 2021-09-26
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多