【问题标题】:Replace getters with property with IntelliJ Structural Search and Replace使用 IntelliJ 结构搜索和替换将 getter 替换为属性
【发布时间】:2014-07-27 05:51:13
【问题描述】:

由于我正在使用的库中的更改,getter 被替换为对属性的直接访问。例如:

public class MyLibraryClass {
  private String field;

  public String getField() { return field; }
}

变成了

public class MyLibraryClass {
  public String field;
}

因此,我想使用 IntelliJ 的 SSR 将 getter 调用替换为 Bean 属性。但是,我一直坚持如何使用正则表达式捕获组进行替换。如果有另一个 IntelliJ 快捷方式来做我想做的事,我很乐意使用它。

以下 SSR 搜索参数找到了 getter,但我似乎无法在替换中使用 $property$ 的捕获组。我试过 \1\$1$1 见 (using backreferences in IntelliJ)

我还尝试将搜索更改为$instance$.get$property$()(清除$property$ 的约束,但搜索未返回任何结果。

  1. 如何访问捕获组 ($1)
  2. 是否可以在替换之前对$1 执行任何处理(以解决问题)

我注意到接受 Groovy 代码的 Script text 选项,但是我找不到有关此功能的任何文档。

【问题讨论】:

    标签: intellij-idea intellij-13 structural-search


    【解决方案1】:

    放入替换模板$instance$.$field$。此模板引入了新变量field,其中应包含以下脚本文本:

    String name = property.methodExpression.referenceName.substring(3)
    name[0].toLowerCase() + name.substring(1) 
    

    【讨论】:

    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 2019-08-14
    • 2010-10-22
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多