javaintellij-ideastructural-search

How to find usage of deprecated java classes with Structural Search command in Intellij IDEA?


I'm trying to create inspection based on Structural Search And Replace feature. So, I need to create search template to find all usages of deprecated classes in java code but I can't understand how to do it.

I've tried set: Search template: $Symbol$ Filters for $Symbol$: reference=annotated classes; script=com.intellij.psi.impl.PsiImplUtil.isDeprecated(Symbol) but it doesn't work (doesn't find usage of deprecated classes)

For example we have deprecated class A:

@Deprecated
public class A {}

and usage of this class in class B:

@RunWith(A.class)
public class B {}

Solution

  • You may create two search templates for this task: first, search template for deprecated class, second, search template for deprecated class reference.

    You may import the following templates with "Import Template form Clipboard" tool enter image description here

    <searchConfiguration name="deprecated class" text="@Deprecated class $C$ {} " recursive="true" caseInsensitive="true" type="JAVA" pattern_context="default">
     <constraint name="__context__" within="" contains="" />
     <constraint name="C" within="" contains="" />
    </searchConfiguration>
    
    
    <searchConfiguration name="reference to deprecated class" text="$ref$" recursive="true" caseInsensitive="true" type="JAVA" pattern_context="default">
     <constraint name="__context__" within="" contains="" />
     <constraint name="ref" reference="deprecated class" within="" contains="" />
    </searchConfiguration>
    

    BTW there is an inspection Deprecated API usage (Java | Code maturity | Deprecated API usage) that has similar functionality.