17

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
    It might be easier to add a `package-info.java` class to all packages and annotate the `package` declaration as `@Deprecated`. – Sotirios Delimanolis May 22 '14 at 14:47
  • 3
    He's asking how to do it in IntelliJ though that is only obvious from the tags. Learning a editor like vim for a multi module java search and replace task isn't a very wise usage of time... – RichieHH May 22 '14 at 14:52
  • 1
    Doesn't just adding `@Deprecated` to the top level `class $ClassName` statement mark everything in the class? – DirkyJerky May 22 '14 at 15:09
  • regex search and replace won't work in these cases, that is why structual search and replaces exists –  May 22 '14 at 15:09
  • marking the class deprecated only marks that class, the methods don't show up as deprecated unless they have the annotation as well. –  May 22 '14 at 15:10

1 Answers1

1

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.

MicSim
  • 26,265
  • 16
  • 90
  • 133
  • This is only a partial solution. This only does the class, I want to do all the methods as well in one shot if possible. It also it doesn't work on any non-package level classes or ones that are marked `final`. –  May 22 '14 at 16:22