我有以下 java 代码使 checkstyle 变紫,说“循环复杂度为 11(允许的最大值为 10)”
public boolean validate(final BindingResult bindingResult) {
boolean validate = true;
for (String channel : getConfiguredChannels()) {
switch (channel) {
case "SMS":
// do nothing
break;
case "Email":
// do nothing
break;
case "Facebook":
// do nothing
break;
case "Voice":
final SpelExpressionParser parser = new SpelExpressionParser();
if (parser
.parseExpression(
"!voiceMessageForm.audioForms.?[audioId == '' || audioId == null].isEmpty()")
.getValue(this, Boolean.class)) {
bindingResult.rejectValue("voiceMessageForm.audioForms",
"message.voice.provide.all.audios");
validate = false;
}
boolean voiceContentErrorSet = false;
boolean voiceDescriptionErrorSet = false;
for (AudioForm audioForm : (List<AudioForm>) parser
.parseExpression(
"voiceMessageForm.audioForms.?[description.length() > 8000]")
.getValue(this)) {
if (audioForm.getAddAudioBy().equals(
AudioForm.AddBy.TTS)
&& !voiceContentErrorSet) {
voiceContentErrorSet = true;
bindingResult.rejectValue(
"voiceMessageForm.audioForms",
"message.voice.content.exceed.limit");
} else {
if (!voiceDescriptionErrorSet) {
voiceDescriptionErrorSet = false;
bindingResult
.rejectValue(
"voiceMessageForm.audioForms",
"message.describe.voice.content.exceed.limit");
}
}
validate = false;
}
break;
default:
throw new IllegalStateException("Unsupported channel: "
+ channel);
}
}
return validate;
}
}
请建议一种合适的方法来避免这种检查样式问题