Suppose I have below Person class with one getAccountNumber() method having @ShouldNotBeLogged custom annotation.
public class Person {
private String name;
private String accountNumber;
@ShouldNotBeLogged
public String getAccountNumber() {
return accountNumber;
}
}
Considering the above Person class I want to find the all occurrences where getAccountNumber() method is logged using the Logger class - error|warn|debug|info methods like in the below HelloWorld class logMessage method. Please note that there are many methods having ShouldNotBeLogged annotation in the actual code, so we cannot create search with name of the method in it.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HelloWorld {
private static final Logger logger = LogManager.getLogger(HelloWorld.class);
public void logMessage(Person person) {
logger.debug("logging Acc No. - {}", person.getAccountNumber()); // Occurence Type 1
LogManager.getLogger(HelloWorld.class).info("Account Number - {}", person.getAccountNumber()); // Occurence Type 2
}
}
I have tried using the Method call existing template and given the Text filter for $MethodCall$ with - error|warn|debug|info and it finds the Logger method with the error,warn,debug or, info names. Not able to create the filter for $Instance$ & $Parameter$ where Instance will be of Logger type (can be instantiated as a constant of a class or, directly with the Logger class method) and Parameter will be the call to @ShouldNotBeLogged annotated method.
$Instance$.$MethodCall$($Parameter$)
I'm using the IntelliJ IDEA 2022.2 (Ultimate Edition)
This is a difficult case. Hopefully this will help you.
First, create a search that will find @ShouldNotBeLogged
annotated methods:
@ShouldNotBeLogged
$ReturnType$ $Method$($ParameterType$ $Parameter$);
With a count modifier [0,∞]
on $Parameter$
(this template is based on the existing template "Deprecated methods"). Save this template under a name, for example "Methods that should not be logged".
$logger$.$call$($arg1$, $method$())
Modifiers:
$logger$
: type=Logger
$method$
: reference=Methods that should not be logged
This will find all logger calls with a call to a @ShouldNotBeLogged
annotated method as the second argument.
Unfortunately, if you want to find logger calls with calls to a @ShouldNotBeLogged
annotated method on a different position, you will have to construct a separate query template. For example:
$logger$.$call$($arg1$, $arg2$, $method$())