简体   繁体   English

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

[英]Find classes using an annotation multiple times via IDEA structured find

I'd like to search for classes, that have at least 3 fields annotated with the @Inject annotation like this: 我想搜索至少有3个字段的类,这些字段使用@Inject注释进行注释,如下所示:

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

I've managed to create a search template that finds classes extending UIComponent AND also have an @Inject annotation in it, however, if I've set the "min" occurrences of the Annotation variable to 3, Intellij still returned classes only having one @Inject. 我设法创建了一个搜索模板,该模板可以找到扩展UIComponent的类,并且还具有@Inject批注,但是,如果将Annotation变量的“ min”出现次数设置为3,则Intellij仍返回仅包含一个的类@注入。

The reason I'd like to have the above search is to find those UI components, which are having many external defendencies, therefore are overcomplicated and need refactoring. 我想要进行以上搜索的原因是要找到那些具有许多外部防御能力的UI组件,因此它们过于复杂并且需要重构。 If you have some better way of doing that, please also share. 如果您有更好的方法,也请分享。

You want to set the minimum occurrences of the field to 3, not of the annotation. 您希望将字段的最少出现次数设置为3,而不是注释的最少出现次数。 This is to find classes with at least three fields with the @Inject annotation, not classes with one field with at least three @Inject annotations. 这是为了查找具有至少三个带有@Inject注释的字段的类,而不是查找具有至少三个@Inject注释的字段的类。 To give a complete example use a pattern as follows: 要给出完整的示例,请使用以下模式:

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

Edit variable field to have minimum occurrences of 3 and maximum occurrences of unlimited . 编辑变量field使其最少出现3 ,最大出现unlimited Variable parent should have text/regexp UIComponent and the Apply constraint within type hierarchy checkbox enabled. 变量parent应该启用text / regexp UIComponent和“ Apply constraint within type hierarchy复选框。

This will find classes extending UIComponent directly or indirectly with at least 3 fields annotated with @Inject. 这将找到直接或间接扩展UIComponent的类,其中至少包含3个用@Inject注释的字段。

  • Go to Edit -> Find -> Find in path or Ctrl + Shift + F . 转到编辑->查找->在路径中查找Ctrl + Shift + F。

  • Enter @Inject[\\S\\s]*@Inject[\\S\\s]*@Inject as the search phrase. 输入@Inject[\\S\\s]*@Inject[\\S\\s]*@Inject作为搜索词组。

(Make sure you have ticked Regular Expression under General in Find in path menu. :)) ) (请确保您已勾选Regular ExpressionGeneralFind in path菜单。:)))

to filter the classes only which extends UIComponent, you can use extends UIComponent[\\S\\s]*@Inject[\\S\\s]*@Inject[\\S\\s]*@Inject as your search phrase. 要仅过滤扩展UIComponent的类,可以使用extends UIComponent[\\S\\s]*@Inject[\\S\\s]*@Inject[\\S\\s]*@Inject作为搜索短语。

On the other hand, classes which extends UIComponent are supposed to be inside a specific package right? 另一方面,应该将扩展UIComponent的类放在特定的包中,对吗? So you can enter that directory as your search path. 因此,您可以输入该目录作为搜索路径。 :)) :))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM