Istvan Devai Istvan Devai - 1 year ago 54
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 {
MyService1 myservice1;
MyService2 myservice2;
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 Source

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$ { 
  $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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download