Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

java - How can I ignore a CheckStyle rule for a line matching a pattern "if (log.isDebugEnabled()) {"

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.

question from:https://stackoverflow.com/questions/65944851/how-can-i-ignore-a-checkstyle-rule-for-a-line-matching-a-pattern-if-log-isdebu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...