package org.test; import com.google.common.base.Preconditions; import org.slf4j.*; import java.util.*; import java.io.*; import java.util.logging.Level; import static com.google.common.base.Preconditions.checkState; class LazyArgEvaluationCheck { public static final Logger slf4j = LoggerFactory.getLogger(LazyArgEvaluationCheck.class); public static final java.util.logging.Logger logger = java.util.logging.Logger.getGlobal(); public static void main(String[] args) { String csvPath = ""; String message = ""; logger.log(Level.SEVERE, message); // Compliant logger.log(Level.SEVERE, "Something went wrong: " + message); // Noncompliant {{Use the built-in formatting to construct this argument.}} logger.log(Level.SEVERE, () -> "Something went wrong: " + message); // since Java 8, we can use Supplier , which will be evaluated lazily checkState(System.currentTimeMillis() == new Date().getTime(), "Arg must be positive, but got " + System.currentTimeMillis()); // Noncompliant {{Invoke method(s) only conditionally. Use the built-in formatting to construct this argument.}} Preconditions.checkState(System.currentTimeMillis() > 0, formatMessage()); // Noncompliant {{Invoke method(s) only conditionally. }} checkState(System.currentTimeMillis() > 0, "message: %s", formatMessage()); // Noncompliant {{Invoke method(s) only conditionally. }} checkState(System.currentTimeMillis() > 0, "message: %s", LazyArgEvaluationCheck.formatMessage()); // Noncompliant {{Invoke method(s) only conditionally. }} } public static void cachingOnDisk(File path) { slf4j.info("Caching on disk @ {}", path.getAbsolutePath()); // Compliant - getters are OK slf4j.info("Caching on disk @ {}", path.isAbsolutePath()); // Compliant - getters are OK } public void exceptionalPaths() { try { } catch (Exception e) { slf4j.info("Caching on disk @ {}", path.getAbsolutePath()); // Compliant - because we don't care about small performance loss in exceptional paths myField = new MyClass() { @Overidde void doSomethingAllTheTime() { slf4j.info("logging all the time consuming resources for nothing " + computeValue() + generateStuff()); // Noncompliant } }; } } public void multiArgs() { checkState(System.currentTimeMillis() > 0, "message: %s %s", formatMessage(), "Something went wrong: " + System.currentTimeMillis()); // Noncompliant {{Invoke method(s) only conditionally. }} } private static String formatMessage() { return "Expensive computation"; } public void classTree() { logger.log(Level.SEVERE, "Something went wrong: " + new Object() { // Noncompliant {{Invoke method(s) only conditionally. Use the built-in formatting to construct this argument.}} @Override public String toString() { return "tostring"; } }); } void slf4j() { slf4j.trace("Unable to open file " + csvPath, new RuntimeException()); // Noncompliant {{Use the built-in formatting to construct this argument.}} slf4j.debug("Unable to open file " + csvPath, new RuntimeException()); // Noncompliant {{Use the built-in formatting to construct this argument.}} slf4j.info("Unable to open file " + csvPath, new RuntimeException()); // Noncompliant {{Use the built-in formatting to construct this argument.}} slf4j.warn("Unable to open file " + csvPath, new RuntimeException()); // Noncompliant {{Use the built-in formatting to construct this argument.}} slf4j.error("Unable to open file " + csvPath, new RuntimeException()); // Noncompliant {{Use the built-in formatting to construct this argument.}} slf4j.error("Unable to open file " + csvPath, new RuntimeException()); // Noncompliant slf4j.error("Unable to open file " + csvPath, new RuntimeException(), 1); // Noncompliant slf4j.error("Unable to open file " + csvPath, new RuntimeException(), 1, 2); // Noncompliant slf4j.error("Unable to open file " + csvPath, new RuntimeException(), 1, "vargs", "vargs", "vargs FTW!"); // Noncompliant Marker confidentialMarker = MarkerFactory.getMarker("CONFIDENTIAL"); slf4j.error(confidentialMarker, "Unable to open file " + csvPath, new RuntimeException()); // Noncompliant slf4j.error(confidentialMarker, "Unable to open file " + csvPath, new RuntimeException(), 1); // Noncompliant slf4j.error(confidentialMarker, "Unable to open file " + csvPath, new RuntimeException(), 1, 2); // Noncompliant slf4j.error(confidentialMarker, "Unable to open file " + csvPath, new RuntimeException(), 1, "vargs", "vargs", "vargs FTW!"); // Noncompliant if (slf4j.isTraceEnabled()) { slf4j.trace("Unable to open file " + csvPath, new RuntimeException()); // Compliant - inside if test } if (slf4j.isDebugEnabled()) { slf4j.debug("Unable to open file " + csvPath, new RuntimeException()); // Compliant - inside if test } if (slf4j.isInfoEnabled()) { slf4j.info("Unable to open file " + csvPath, new RuntimeException()); // Compliant - inside if test } if (slf4j.isWarnEnabled()) { slf4j.warn("Unable to open file " + csvPath, new RuntimeException()); // Compliant - inside if test } if (slf4j.isErrorEnabled()) { slf4j.error("Unable to open file " + csvPath, new RuntimeException()); // Compliant - inside if test } if (b) { slf4j.error("Unable to open file " + csvPath, new RuntimeException()); // Noncompliant } } void jul() { logger.finest("Unable to open file " + csvPath); // Noncompliant {{Use the built-in formatting to construct this argument.}} logger.finer("Unable to open file " + csvPath); // Noncompliant {{Use the built-in formatting to construct this argument.}} logger.fine("Unable to open file " + csvPath); // Noncompliant {{Use the built-in formatting to construct this argument.}} logger.config("Unable to open file " + csvPath); // Noncompliant {{Use the built-in formatting to construct this argument.}} logger.info("Unable to open file " + csvPath); // Noncompliant {{Use the built-in formatting to construct this argument.}} logger.warning("Unable to open file " + csvPath); // Noncompliant {{Use the built-in formatting to construct this argument.}} logger.severe("Unable to open file " + csvPath); // Noncompliant {{Use the built-in formatting to construct this argument.}} if (logger.isLoggable(Level.FINEST)) { logger.finest("Unable to open file " + csvPath); // Compliant - inside if test } if (logger.isLoggable(Level.INFO)) { logger.trace("Unable to open file " + csvPath); // Compliant - FN, we don't verify that level in "if" matches actual level used in logging logger.info("Unable to open file " + csvPath); // Compliant } } }