Detecting Log and Throw

Just wondering if anybody knows of an plugin inspection or structural search template (?) that could help me clean up a legacy module that's full of catch statements like

try {
...
} catch (Exception e) {
     String message="Theres an error!";
     LOGGER.error(message+" "+e.getMessage());
     throw new CustomException(message, e);
}


(theres a few varitions on this general pattern)

I want to remove the logs from these.

0
3 comments

Using Structural Replace, start with the existing template "try's"
Modify the template as follows:

try {
  $TryStatement$;
} catch($ExceptionType$ $ExceptionDcl$) {
  $CatchStatement1$;
  $LogStatement$;
  $CatchStatement2$;
  throw $Throwable$;
}


CatchStatement1 - minimum count: 0, maximum count: unlimited
LogStatement - text/regexp: LOGGER\.error(.*)
CatchStatement2 - minimum count: 0, maximum count: unlimited

Replacement template:

try {
  $TryStatement$;
} catch($ExceptionType$ $ExceptionDcl$) {
  $CatchStatement1$;
  $CatchStatement2$;
  throw $Throwable$;
}



Hope that helps,
Bas

0

Thanks I'll give those a go should give me a starting point for some of the common cut&pasted occurances.

Shame we don't have a wiki to share useful macros.

Don't use the structural search feature often enough to remember the syntax, seem to remember retriving working searches was a pain.

0

osbald wrote:

Shame we don't have a wiki to share useful macros.


Stackoverflow is pretty good for that in my opinion:
http://stackoverflow.com/questions/tagged/structural-search+intellij-idea

Bas

0

Please sign in to leave a comment.