Istvan Devai Istvan Devai - 1 month ago 8
Java Question

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:

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.

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. If you have some better way of doing that, please also share.

Answer

You want to set the minimum occurrences of the field to 3, not of the annotation. 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. 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. Variable parent should have text/regexp UIComponent and the Apply constraint within type hierarchy checkbox enabled.

This will find classes extending UIComponent directly or indirectly with at least 3 fields annotated with @Inject.