问题描述
我想请您为声纳问题提供帮助。声纳在下面的代码中将案例EQUALS标记为声纳发出鱿鱼:S128。
-Switch的情况应以无条件的“ break”语句结尾。 我认为在这种情况下,我不必添加“ break”语句。 有人能帮我吗?是误报问题吗?
谢谢。
public boolean causeException(Throwable throwable) {
Throwable causeException = ExceptionUtils.getRootCause(throwable);
Map<String,MatchMode> configuration = infoMessage.get(causeException.getClass());
String message = throwable.getMessage();
for (String key : configuration.keySet()) {
MatchMode matchMode = configuration.get(key);
switch (matchMode) {
case EQUALS:
if (message.equals(key)) {
return true;
}
case CONTAINS:
if (message.contains(key)) {
return false;
}
}
}
return false;
}
解决方法
如果您输入的信息不等于密钥,您将陷入失败。您需要添加休息时间来解决此问题:
public boolean causeException(Throwable throwable) {
Throwable causeException = ExceptionUtils.getRootCause(throwable);
Map<String,MatchMode> configuration = infoMessage.get(causeException.getClass());
String message = throwable.getMessage();
for (String key : configuration.keySet()) {
MatchMode matchMode = configuration.get(key);
switch (matchMode) {
case EQUALS:
if (message.equals(key)) {
return true;
}
break; // stops fallthrough
// With no "break" it would do the CONTAINS block as well
case CONTAINS:
if (message.contains(key)) {
return true;
}
break; // stops fallthrough
default:
// just for clarity
break;
}
}
return false;
}