【问题标题】:How to create structural replace in IDEA to add missing annotation to the class如何在 IDEA 中创建结构替换以向类添加缺少的注释
【发布时间】:2016-02-17 20:03:17
【问题描述】:

我正在尝试在 IntelliJ IDEA(未来检查)中找出一个 结构替换模板,以便在不删除任何其他现有注释的情况下向类/接口/枚举添加缺少的注释。

到目前为止,我尝试了以下几种方式

搜索模板:

@$OtherPossibleAnnotation$ // min: 0, max: 0, text/regexp: AnnotationName, invert condition: true
@$MissingAnnotation$ // min: 0, max: 0, text/regexp: AnnotationName
class $C$ {
    $Content$ // min: 0, max: unlimited
}

替换模板:

@$OtherPossibleAnnotation$
@AnnotationName
class $C$ {
    $Content$
}

但是我的尝试都没有奏效。 我希望它能够工作

  • 使用任何现有的类注释集,并且还必须保留它们的潜在值,
  • 如果类上根本没有注释,
  • 在想法 14 和想法 15 中,
  • 还应保持课程内容不变。

例如跟随

@ProductArea("Area A")
class A {
    public void method1() {
        System.out.println("Should remain");
    }
}

应该替换为

@ProductArea("Area A")
@AnnotationName
class A {
    public void method1() {
        System.out.println("Should remain");
    }
}

有人可以建议吗?

【问题讨论】:

  • 你找到方法了吗?
  • 到目前为止我还没有

标签: java intellij-idea replace annotations structural-search


【解决方案1】:

以下内容适用于 IntelliJ IDEA 2019.3 EAP。

搜索模板:
@$MissingAnnotation$ // count=[0,0], text=AnnotationName
class $C$ {
}
替换模板
@AnnotationName
class $C$ {
}

这将保留现有的注释和类内容,但仅在使用 IntelliJ IDEA 2019.3 EAP 时

要节省一些手动编辑,请将以下内容复制到剪贴板并使用“结构搜索”对话框中 cog 菜单下的“从剪贴板导入模板”:

<replaceConfiguration name="method calls" text="@$MissingAnnotation$ &#10;class $C$ {&#10;}" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="true" replacement="@AnnotationName&#10;class $C$ {&#10;}">
  <constraint name="__context__" within="" contains="" />
  <constraint name="MissingAnnotation" regexp="AnnotationName" minCount="0" maxCount="0" within="" contains="" />
  <constraint name="C" within="" contains="" />
</replaceConfiguration>

【讨论】:

  • 太糟糕了,它仍然会在 2020.3 中替换现有的 Groovy 课程内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 2021-04-02
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 2019-05-06
  • 2015-11-11
相关资源
最近更新 更多