private static CaseLabelTree getDefaultLabel(SwitchStatementTree switchStatementTree) { for (CaseGroupTree caseGroupTree : switchStatementTree.cases()) { for (CaseLabelTree caseLabelTree : caseGroupTree.labels()) { if (JavaKeyword.DEFAULT.getValue().equals(caseLabelTree.caseOrDefaultKeyword().text())) { return caseLabelTree; } } } return null; }
@Override public void visitNode(Tree tree) { SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree; CaseLabelTree defaultLabel = getDefaultLabel(switchStatementTree); CaseLabelTree lastLabel = getLastLabel(switchStatementTree); if (defaultLabel == null) { reportIssue(switchStatementTree.switchKeyword(), "Add a default case to this switch."); } else if (!defaultLabel.equals(lastLabel)) { reportIssue(defaultLabel, "Move this default to the end of the switch."); } }