Skip to content

[java] GuardLogStatement: False positive when compile-time constant is created from external constants #3602

@krallus

Description

@krallus

Affects PMD Version:
6.40.0+
7.0.0

Rule: GuardLogStatement

Description:
The changes to resolve issue #957 work well when the constant is located in the same class (or at least the same file -- I did not try the scenario of different classes and same file). However, when the concatenated constant used to create the compile-time constant log message comes from elsewhere, PMD still issues a GuardLogStatement false positive.

Code Sample demonstrating the issue:

public class OtherClass {
  public static final String TERM_MSG = "A terminating log message.";
}
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class MyClass {
  private static final Logger logger = LogManager.getLogger();
  public void doSomething() {
     final Object arg = new Object();
     logger.info("Problem message: {}. " + OtherClass.TERM_MSG, arg);
  }
}

Expected outcome:
Because the two concatenated constants result in a compile time constant, there is no work done at log time to create the message string if the log level does not include INFO and so there is no need for a guard statement. This is the same scenario as #957 except in this case, the concatenated constant String is not coming from within the same class.

PMD reports a GuardLogStatement violation at the logger.info(...) line, but that's wrong. That's a false positive.

Running PMD through: Eclipse PMD Plugin 4.29

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions