0

I'm using CheckStyle to check curly bracket placement and generally want to enforce left curly brace being at the end of line with indenting following it. I have this working. e.g.

if (something_is_true) {
  ...

However, in the very specific case of logging statements, which I want to keep compact, I want to allow

if (log.isDebugEnabled()) { log.debug("my logging statement."); }

The name of the logger may change but generally the line will contain "isDebugEnabled". I don't want to use SuppressWarnings or otherwise be adding extra lines around the debug lines - I want them to not interfere with the view and the flow of the code.

Is there a way to suppress warnings via the checkstyle config file for lines which match a given pattern?

Thanks.

Gordon Little
  • 141
  • 1
  • 2
  • 16
  • `if (log.isDebugEnabled()) { log.debug("my logging statement."); }` ← Don’t do that, ever. The *very first thing* the log.debug method does is that very check. All logging frameworks work that way. You are making your code longer than it needs to be, and you are impairing readability by making logging a significant part of the code instead of being mere decoration. – VGR Jan 29 '21 at 00:46
  • 1
    Sometimes I need to construct longer strings with variables from the code using string concatenation, and am still on log4j 1.2 (vendor platform) so don't have the luxury of format strings. I'd rather pay the one isDebugEnabled check upfront than construct unnecessary strings before checking. As per https://stackoverflow.com/questions/963492/in-log4j-does-checking-isdebugenabled-before-logging-improve-performance – Gordon Little Jan 29 '21 at 12:00

2 Answers2

1

I think this is a duplicate of How to disable a particular checkstyle rule for a particular line of code?.

In fact you have 3 options:

  • Use @SuppressWarning("checkstyle:rule")
  • Suppress rule in checkstyle-suppressions.xml
  • Use @checkstyle rule (N lines) from java comment(works for javadoc also)

The options fully depend on your version of checkstyle plugin, thus its better to start research from already raised questions:

lazylead
  • 1,453
  • 1
  • 14
  • 26
  • I very much don't want to use SuppressWarnings as I'm trying to avoid adding more lines. Your checkstyle-suppressions.xml hit the nail though. – Gordon Little Jan 28 '21 at 22:27
0

Answered my own question. I add a suppression file to my checkstyle configuration file, where the suppression file had the following contents:

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionXpathFilter Experimental Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2_xpath_experimental.dtd">

<suppressions>
  <suppress-xpath checks="LeftCurly"
    query="//LITERAL_IF[.//METHOD_CALL/DOT/IDENT[@text='isDebugEnabled']]/SLIST"/>
</suppressions>

This finds all if statements that contain a reference to a isDebugEnabled call and matches on the following "list of statements" which begins with the leading left curly bracket.

Gordon Little
  • 141
  • 1
  • 2
  • 16