【问题标题】:IntelliJ IDEA: How to create a new class field during Structural replaceIntelliJ IDEA:如何在结构替换期间创建新的类字段
【发布时间】:2020-06-08 20:09:40
【问题描述】:

我有很多对静态类方法的调用

例如

LegacyLogger.println (ERROR_LEVEL, "Simple string parameter");

我已经通过使用模板找到了所有需要的代码:

LegacyLogger.println(ERROR_LEVEL, $params$)

我怎样才能导入类并创建一个新字段

Logger logger = Logger.GetInstance(SomeClass.class);

用以下内容替换此搜索字符串后的类内:

logger.error("Simple string parameter");

【问题讨论】:

    标签: intellij-idea structural-search


    【解决方案1】:

    您可以尝试在路径中搜索“记录器”之类的常规查找。并在“查找”窗口中打开结果。这应该找到您使用新记录器的所有源文件。

    然后创建一个结构搜索和替换模式以在类中插入新的记录器声明。搜索没有记录器声明的类并使用范围“以前的搜索结果”。这应该在之前搜索中找到的文件中没有记录器的所有类中插入新记录器。

    <replaceConfiguration name="Unnamed" text="class $A$ {&#10;  Logger $logger$ = Logger.GetInstance(SomeClass.class);&#10;}" recursive="false" caseInsensitive="false" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="class $A$ {&#10;  Logger logger = Logger.GetInstance(SomeClass.class);&#10;}">
      <constraint name="__context__" within="" contains="" />
      <constraint name="A" within="" contains="" />
      <constraint name="logger" minCount="0" maxCount="0" within="" contains="" />
    </replaceConfiguration>
    

    (使用对话框右上角工具按钮下的“从剪贴板导入模板”操作)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多