use of com.sun.source.tree.CaseTree in project st-js by st-js.
the class CaseWriter method visit.
@Override
public JS visit(WriterVisitor<JS> visitor, CaseTree tree, GenerationContext<JS> context) {
// TODO: check qualified enums:
// if (selectorType instanceof ClassWrapper && ((ClassWrapper) selectorType).getClazz().isEnum()) {
// printer.print(names.getTypeName(selectorType));
// printer.print(".");
JS expression = null;
List<JS> statements = new ArrayList<JS>();
if (tree.getExpression() != null) {
expression = visitor.scan(tree.getExpression(), context);
}
for (Tree c : tree.getStatements()) {
statements.add(visitor.scan(c, context));
}
return context.withPosition(tree, context.js().caseStatement(expression, statements));
}
use of com.sun.source.tree.CaseTree in project error-prone by google.
the class MissingCasesInEnumSwitch method matchSwitch.
@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
Type switchType = ASTHelpers.getType(tree.getExpression());
if (switchType.asElement().getKind() != ElementKind.ENUM) {
return Description.NO_MATCH;
}
// default case is present
if (tree.getCases().stream().anyMatch(c -> c.getExpression() == null)) {
return Description.NO_MATCH;
}
ImmutableSet<String> handled = tree.getCases().stream().map(CaseTree::getExpression).filter(IdentifierTree.class::isInstance).map(e -> ((IdentifierTree) e).getName().toString()).collect(toImmutableSet());
Set<String> unhandled = Sets.difference(ASTHelpers.enumValues(switchType.asElement()), handled);
if (unhandled.isEmpty()) {
return Description.NO_MATCH;
}
return buildDescription(tree).setMessage(buildMessage(unhandled)).build();
}
use of com.sun.source.tree.CaseTree in project error-prone by google.
the class UnnecessaryDefaultInEnumSwitch method matchSwitch.
@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
TypeSymbol switchType = ((JCSwitch) tree).getExpression().type.tsym;
if (switchType.getKind() != ElementKind.ENUM) {
return NO_MATCH;
}
CaseTree caseBeforeDefault = null;
CaseTree defaultCase = null;
for (CaseTree caseTree : tree.getCases()) {
if (caseTree.getExpression() == null) {
defaultCase = caseTree;
break;
} else {
caseBeforeDefault = caseTree;
}
}
if (defaultCase == null) {
return NO_MATCH;
}
Set<String> handledCases = tree.getCases().stream().map(CaseTree::getExpression).filter(IdentifierTree.class::isInstance).map(p -> ((IdentifierTree) p).getName().toString()).collect(toImmutableSet());
if (!ASTHelpers.enumValues(switchType).equals(handledCases)) {
return NO_MATCH;
}
Fix fix;
List<? extends StatementTree> defaultStatements = defaultCase.getStatements();
if (trivialDefault(defaultStatements)) {
// deleting `default:` or `default: break;` is a no-op
fix = SuggestedFix.delete(defaultCase);
} else {
String defaultSource = state.getSourceCode().subSequence(((JCTree) defaultStatements.get(0)).getStartPosition(), state.getEndPosition(getLast(defaultStatements))).toString();
String initialComments = comments(state, defaultCase, defaultStatements);
if (!canCompleteNormally(tree)) {
// if the switch statement cannot complete normally, then deleting the default
// and moving its statements to after the switch statement is a no-op
fix = SuggestedFix.builder().delete(defaultCase).postfixWith(tree, initialComments + defaultSource).build();
} else {
// and the code is unreachable -- so use (2) as the strategy. Otherwise, use (1).
if (!SuggestedFixes.compilesWithFix(SuggestedFix.delete(defaultCase), state)) {
// case (3)
return NO_MATCH;
}
if (!canCompleteNormally(caseBeforeDefault)) {
// case (2) -- If the case before the default can't complete normally,
// it's OK to to delete the default.
fix = SuggestedFix.delete(defaultCase);
} else {
// case (1) -- If it can complete, we need to merge the default into it.
fix = SuggestedFix.builder().delete(defaultCase).postfixWith(caseBeforeDefault, initialComments + defaultSource).build();
}
}
}
return describeMatch(defaultCase, fix);
}
use of com.sun.source.tree.CaseTree in project j2objc by google.
the class TreeConverter method convertSwitch.
private TreeNode convertSwitch(SwitchTree node, TreePath parent) {
TreePath path = getTreePath(parent, node);
SwitchStatement newNode = new SwitchStatement().setExpression(convertWithoutParens(node.getExpression(), path));
for (CaseTree switchCase : node.getCases()) {
newNode.addStatement((SwitchCase) convert(switchCase, path));
TreePath switchCasePath = getTreePath(path, switchCase);
for (StatementTree s : switchCase.getStatements()) {
newNode.addStatement((Statement) convert(s, switchCasePath));
}
}
return newNode;
}
Aggregations