java SonarQube:仅有条件地调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44324597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SonarQube: Invoke method(s) only conditionally
提问by Olezt
The following part of code raises a major bug at SonarQube :
"Invoke method(s) only conditionally."
How am I supposed to fix this?
以下代码部分在 SonarQube 中引发了一个主要错误:“仅有条件地调用方法。”
我该如何解决这个问题?
if(us != null){
logger.info("Log this: {}", us.toString());
}
采纳答案by Tibor Blenessy
The call to us.toString()is redundant, toString()method will be called regardless the configured log level. You should pass only usas an argument to infowithout an ifstatement.
调用us.toString()是多余的,toString()无论配置的日志级别如何,都会调用方法。你应该只us作为参数传递给info没有if声明。
logger.info("Log this: {}", us);
回答by Olezt
As stated at the comments of the question, another working answer is:
正如问题的评论中所述,另一个有效的答案是:
if(logger.isInfoEnabled() && us != null){
logger.info("Log this: {}", us.toString());
}

