簡體   English   中英

Intellij結構搜索:查找字段和對應的getter(遷移注解)

[英]Intellij structural search: find field and corresponding getter (to migrate an annotation)

我正在嘗試使用 Intellij 的花哨結構替換將注釋從一個字段遷移到它的 getter:

從:

class MyClass {
   @SomeAnnotation
   private final double a;
   @SomeAnnotation
   private final double b;

   public double getA() {
       return a;
   }

   public double getB() {
      return b;
   }
}

到:

class MyClass {
   private final double a;
   private final double b;

   @SomeAnnotation
   public double getA() {
       return a;
   }

   @SomeAnnotation
   public double getB() {
      return b;
   }
}

我大部分時間都在那里:

搜索模板:

class $Class$ {
    @SomeAnnotation
    private final $type$ $fieldName$;

    public $type$ $getter$() {
        return $value$;
    }
}

替換模板:

class $Class$ {
    private final $type$ $fieldName$;

    @SomeAnnotation
    public $type$ $getter$() {
        return $value$;
    }
}

還有一個匹配get.* getter 過濾器,以及整個模板上的 Script 以確保我們找到匹配的 getter:

if (getter.name.toLowerCase().contains(fieldName.get(0).name.toLowerCase())) {
    return true;
}
return false;

問題是這只匹配第一個 getter,腳本沒有機會嘗試所有這些。

有什么辦法可以改變我對此的搜索嗎?

我不是 IntelliJ 結構搜索的專家,但我懷疑這對它來說太過分了(盡管你總是可以一遍又一遍地運行它,直到它停止改變事物)。

我認為這是OpenRewrite或類似工具的工作,它允許您讀取 AST 並生成新代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM