通过IDEA结构化查找多次使用注释查找类

时间:2016-10-18 17:59:17

标签: java intellij-idea annotations structural-search

我想搜索至少有3个用@Inject注释注释的字段的类,如下所示:

class SpecificUiComponent extends UIComponent {
      @Inject
      MyService1 myservice1;
      @Inject
      MyService2 myservice2;
      @Inject
      MyService3 myservice3;
}

我设法创建了一个搜索模板,找到了扩展UIComponent的类,并且还有一个@Inject注释,但是,如果我设置了" min"将Annotation变量发生到3时,Intellij仍返回只有一个@Inject的类。

我希望进行上述搜索的原因是找到那些具有许多外部防御的UI组件,因此过于复杂并需要重构。如果您有更好的方法,请分享。

2 个答案:

答案 0 :(得分:2)

您希望将字段的最小出现次数设置为3,而不是注释。这是使用@Inject注释查找至少包含三个字段的类,而不是具有至少三个@Inject注释的一个字段的类。要给出一个完整的示例,请使用如下模式:

class $Class$ extends $Parent$ { 
  @Inject
  $FieldType$ $Field$;
}

修改变量field,使3的出现次数最少,unlimited的出现次数最少。 变量parent应该启用text / regexp UIComponent并启用Apply constraint within type hierarchy复选框。

这将找到直接或间接扩展UIComponent的类,至少有3个用@Inject注释的字段。

答案 1 :(得分:0)

  • 转到修改 - >查找 - >在路径中查找 Ctrl + Shift + F

  • 输入@Inject[\S\s]*@Inject[\S\s]*@Inject作为搜索词组。

(确保在Regular Expression菜单中的General下方勾选了Find in path。:))

仅过滤扩展UIComponent的类,您可以使用extends UIComponent[\S\s]*@Inject[\S\s]*@Inject[\S\s]*@Inject作为搜索短语。

另一方面,扩展UIComponent的类应该在特定包中吗?因此,您可以输入该目录作为搜索路径。 :))