How to add @Deprecated to every Class and every Constructor/Method of every class?

3.5k Views Asked by At

I have some legacy code and I want to mark all of them and all of their methods @Deprecated so that as we go and touch them we can remove these annotations so we can keep track of what has been modernized and what still is bad.

I am trying to use the Structural Search/Replace and can't seem to get the correct template going.

Search Template

class $Class$ { 
  $ReturnType$ $MethodName$($ParameterType$ $Parameter$) { $Stmt$; }
}

Replace Template

@Deprecated
class $Class$ {
  @Deprecated
  $ReturnType$ $MethodName$($ParameterType$ $Parameter$) { $Stmt$; }
}

But this removes everything else that is in the class.

@Deprecated
class OldAndCrusty {
  @Deprecated
   ( );
}

This strips off all the visibility modifiers and final modifiers of all the classes it matches.

How do I replace these things and leave the rest of the code alone?

1

There are 1 best solutions below

1
On

Unfortunately I can't test this as I don't have the proper IntelliJ version. But as I found here, you can use a variable for the class content.

Search template:

    class $Class$ {
       $MyClassContent$
    }

Replacement template:

    @Deprecated
    class $Class$ {
       $MyClassContent$
    }

The minimum and maximum occurrence counts for the MyClassContent variable should be set to 0 and Integer.MAX_VALUE respectively. This should leave you with the classes annotated.

Now, I suppose you can do the same with a method template too. What I don't know is if you can apply the method template to all methods in the source. That way you should be able do a 2-step search and replace: First for classes, next for methods.