javaintellij-ideastructural-search

IntelliJ Search Template /Custom Inspection: Analyse sibling annotations


in our project we have a custom annotation @DeprecationReason which should be used to document why a member/method/class is deprecated (and thus annotated with @Deprecated)

So to have an IntelliJ warning when @Deprecated is used but @DeprecationReason is missing, I want to check if @Deprecated is present but not @DeprecatedReason.

In general, I find it difficult to find documentation about structural search, but I haven't found anything about analysing "sibling annotations".

Could anyone give me a hint about this?


Solution

  • You could try a template like this:

    <searchConfiguration name="Not annotated methods" text="@Deprecated @$Annotation$&#10;$MethodType$ $Method$($ParameterType$ $Parameter$);" recursive="true" type="JAVA" pattern_context="member" search_injected="false">
      <constraint name="__context__" within="" contains="" />
      <constraint name="Annotation" regexp="DeprecatedReason" minCount="0" maxCount="0" within="" contains="" />
      <constraint name="MethodType" within="" contains="" />
      <constraint name="Method" within="" contains="" />
      <constraint name="ParameterType" within="" contains="" />
      <constraint name="Parameter" minCount="0" maxCount="2147483647" within="" contains="" />
    </searchConfiguration>
    

    Which I based on the built-in/existing template "Not annotated methods". You can import this in the Structural Search dialog using the "Import Template from Clipboard" action under the tool button in the upper right.

    Fields and classes will need a separate template.