How to find functional interface candidates in IntelliJ

185 views Asked by At

I'm trying to use search structurally to find interfaces that could be annotated with @FunctionalInterface since they only contain a single abstract method but aren't annotated yet.

My current structural search template looks like this:

@$TheAnnotation$
interface $Interface$ { 
  $ReturnType$ $Method$ ($ParameterType$ $Parameter$);
}

I've added the following filters

  • Parameters: count -> [0, ∞]
  • TheAnnotation: script -> TheAnnotation.getClass() != FunctionalInterface.class

but that does not seem to work as the result will contain classes annotated with @FunctionalInterface nevertheless.

1

There are 1 answers

1
Bas Leijdekkers On BEST ANSWER

IntelliJ IDEA has a "Interface may be annotated @FunctionalInterface" inspection, you may want to use instead. It has a quick fix to add the @FunctionalInteface annotation.

But if you insist on using Structural Search, here's a template that should work:

<searchConfiguration name="Unnamed" text="@$TheAnnotation$&#10;interface $Interface$ {&#10;  abstract $ReturnType$ $Method$ ($ParameterType$ $Parameter$);&#10;  abstract $ReturnType2$ $Method2$ ($ParameterType2$ $Parameter2$);&#10;}" recursive="false" caseInsensitive="false" type="JAVA" pattern_context="default">
  <constraint name="__context__" within="" contains="" />
  <constraint name="TheAnnotation" regexp="FunctionalInterface" minCount="0" maxCount="0" within="" contains="" />
  <constraint name="Method" within="" contains="" />
  <constraint name="Interface" within="" contains="" />
  <constraint name="ReturnType" within="" contains="" />
  <constraint name="ReturnType2" within="" contains="" />
  <constraint name="ParameterType" within="" contains="" />
  <constraint name="ParameterType2" within="" contains="" />
  <constraint name="Parameter" minCount="0" maxCount="2147483647" within="" contains="" />
  <constraint name="Parameter2" minCount="0" maxCount="2147483647" within="" contains="" />
  <constraint name="Method2" minCount="0" maxCount="0" within="" contains="" />
</searchConfiguration>

(copy the xml and using the "Import Template from Clipboard" action under the tool button in the Structural Search dialog)