【问题标题】:Replacing Type with var for all 'Class class = new Class()' usages in Java project用 var 替换 Java 项目中所有 \'Class class = new Class()\' 用法的 Type
【发布时间】:2022-09-30 23:58:30
【问题描述】:

我最近为一个相当大的项目切换到 Java 11,并想切换到使用 var class = new Class() 而不是 Class class = new CLass()

我为此尝试使用 Intellij 结构搜索(和替换),但结果比预期的要复杂。

我的第一次尝试是$Type$ $Inst$ = new $Constructor$($Argument$);,它也匹配全局变量(不允许 var)。

我的第二次尝试是:

class $Class$ {
  $ReturnType$ $Method$ ($ParameterType$ $Parameter$) throws $ExceptionType$ {
      $Statements$;
      final $Type$ $Inst$ = new $Constructor$($Argument$);
      $Statements2$;
  }
}

错过了内部的所有电话,例如尝试块(因为它们被表达式匹配)

任何帮助将不胜感激!

    标签: intellij-idea structural-search


    【解决方案1】:

    使用您的第一个模板

    $Type$ $Inst$ = new $Constructor$($Argument$);
    

    但是使用以下文本添加 Script 修饰符:
    Inst instanceof com.intellij.psi.PsiLocalVariable

    或者,您可能想尝试局部变量类型可以省略IntelliJ IDEA 中可用的检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 2012-09-16
      • 1970-01-01
      • 2021-09-23
      • 2015-04-07
      相关资源
      最近更新 更多