Search in sources :

Example 6 with CaseTree

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));
}
Also used : ArrayList(java.util.ArrayList) CaseTree(com.sun.source.tree.CaseTree) Tree(com.sun.source.tree.Tree)

Example 7 with CaseTree

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();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ElementKind(javax.lang.model.element.ElementKind) Set(java.util.Set) CaseTree(com.sun.source.tree.CaseTree) SwitchTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher) SwitchTree(com.sun.source.tree.SwitchTree) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) VisitorState(com.google.errorprone.VisitorState) Description(com.google.errorprone.matchers.Description) IdentifierTree(com.sun.source.tree.IdentifierTree) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) JDK(com.google.errorprone.BugPattern.Category.JDK) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) Type(com.sun.tools.javac.code.Type) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 8 with CaseTree

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);
}
Also used : SuggestedFixes(com.google.errorprone.fixes.SuggestedFixes) SwitchTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher) VisitorState(com.google.errorprone.VisitorState) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) IdentifierTree(com.sun.source.tree.IdentifierTree) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Fix(com.google.errorprone.fixes.Fix) Tree(com.sun.source.tree.Tree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ElementKind(javax.lang.model.element.ElementKind) Set(java.util.Set) Iterables.getLast(com.google.common.collect.Iterables.getLast) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) NO_MATCH(com.google.errorprone.matchers.Description.NO_MATCH) CaseTree(com.sun.source.tree.CaseTree) JCTree(com.sun.tools.javac.tree.JCTree) SwitchTree(com.sun.source.tree.SwitchTree) List(java.util.List) Description(com.google.errorprone.matchers.Description) StatementTree(com.sun.source.tree.StatementTree) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Reachability.canCompleteNormally(com.google.errorprone.util.Reachability.canCompleteNormally) ASTHelpers(com.google.errorprone.util.ASTHelpers) CaseTree(com.sun.source.tree.CaseTree) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) Fix(com.google.errorprone.fixes.Fix) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) IdentifierTree(com.sun.source.tree.IdentifierTree) JCTree(com.sun.tools.javac.tree.JCTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 9 with CaseTree

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;
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) StatementTree(com.sun.source.tree.StatementTree) CaseTree(com.sun.source.tree.CaseTree) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) TreePath(com.sun.source.util.TreePath)

Aggregations

CaseTree (com.sun.source.tree.CaseTree)9 BugPattern (com.google.errorprone.BugPattern)4 VisitorState (com.google.errorprone.VisitorState)4 SwitchTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher)4 Description (com.google.errorprone.matchers.Description)4 SwitchTree (com.sun.source.tree.SwitchTree)4 Iterables.getLast (com.google.common.collect.Iterables.getLast)3 JDK (com.google.errorprone.BugPattern.Category.JDK)3 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)3 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)3 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)3 NO_MATCH (com.google.errorprone.matchers.Description.NO_MATCH)3 ASTHelpers (com.google.errorprone.util.ASTHelpers)3 IdentifierTree (com.sun.source.tree.IdentifierTree)3 Tree (com.sun.source.tree.Tree)3 JCTree (com.sun.tools.javac.tree.JCTree)3 ElementKind (javax.lang.model.element.ElementKind)3 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)2 Reachability (com.google.errorprone.util.Reachability)2 StatementTree (com.sun.source.tree.StatementTree)2