Java 类com.sun.tools.javac.tree.JCTree.JCCase 实例源码

项目:javaparser2jctree    文件:JavaParser2JCTree.java   
@Override
public JCTree visit(final SwitchStmt n, final Object arg) {
    //ARG0: JCExpression selector
    JCExpression arg0 = (JCExpression) n.getSelector().accept(this, arg);

    //ARG1: List<JCCase> cases
    List<JCCase> arg1 = List.<JCCase>nil();
    if (n.getEntries() != null) {
        for (final SwitchEntryStmt e : n.getEntries()) {
            arg1 = arg1.append((JCCase) e.accept(this, arg));
        }
    }

    return new AJCSwitch(make.Switch(arg0, arg1), ((n.getComment() != null) ? n.getComment().getContent() : null));

}
项目:EasyMPermission    文件:PrettyCommentsPrinter.java   
public void visitCase(JCCase tree) {
    try {
        if (tree.pat == null) {
            print("default");
        } else {
            print("case ");
            printExpr(tree.pat);
        }
        print(": ");
        println();
        indent();
        printStats(tree.stats);
        undent();
        align();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
项目:s4j    文件:Pretty.java   
public void visitCase(JCCase tree) {
    try {
        if (tree.pat == null) {
            print("default");
        } else {
            print("case ");
            printExpr(tree.pat);
        }
        print(": ");
        println();
        indent();
        printStats(tree.stats);
        undent();
        align();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
项目:lombok    文件:PrettyCommentsPrinter.java   
public void visitCase(JCCase tree) {
    try {
        if (tree.pat == null) {
            print("default");
        } else {
            print("case ");
            printExpr(tree.pat);
        }
        print(": ");
        println();
        indent();
        printStats(tree.stats);
        undent();
        align();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
项目:incubator-netbeans    文件:CasualDiff.java   
protected int diffCase(JCCase oldT, JCCase newT, int[] bounds) {
    int localPointer = bounds[0];
    if (oldT.pat != null) {
        int[] patBounds = getBounds(oldT.pat);
        copyTo(localPointer, patBounds[0]);
        localPointer = diffTree(oldT.pat, newT.pat, patBounds);
        tokenSequence.move(patBounds[1]);
        do { } while (tokenSequence.moveNext() && JavaTokenId.COLON != tokenSequence.token().id());
        tokenSequence.moveNext();
        copyTo(localPointer, localPointer = tokenSequence.offset());
    }
    // todo (#pf): hot-fix of #113313, think about correct matching later
    if (oldT.pat == null && newT.pat != null) {
        printer.print(newT);
        printer.newline();
        return bounds[1];
    }
    PositionEstimator est = EstimatorFactory.statements(
            oldT.getStatements(),
            newT.getStatements(),
            diffContext
    );
    localPointer = diffList(oldT.stats, newT.stats, localPointer, est, Measure.MEMBER, printer);

    copyTo(localPointer, bounds[1]);

    return bounds[1];
}
项目:lombok-ianchiu    文件:PrettyPrinter.java   
@Override public void visitCase(JCCase tree) {
    if (tree.pat == null) {
        aPrint("default");
    } else {
        aPrint("case ");
        print(tree.pat);
    }
    println(": ");
    indent++;
    print(tree.stats, "");
    indent--;
}
项目:javaparser2jctree    文件:PrintAstVisitor.java   
public void visitCase(JCCase that) {
    try {
        print("JCCase:");
    } catch (Exception e) {
    }
    super.visitCase(that);
}
项目:android-retrolambda-lombok    文件:JcTreeBuilder.java   
@Override
public boolean visitSwitch(Switch node) {
    List<JCCase> cases = List.nil();

    JCExpression currentPat = null;
    Node currentNode = null;
    List<JCStatement> stats = null;
    boolean preamble = true;

    for (Statement s : node.astBody().astContents()) {
        if (s instanceof Case || s instanceof Default) {
            JCExpression newPat = (s instanceof Default) ? null : toExpression(((Case)s).astCondition());
            if (preamble) {
                preamble = false;
            } else {
                cases = addCase(cases, currentPat, currentNode, stats);
            }
            stats = List.nil();
            currentPat = newPat;
            currentNode = s;
        } else {
            if (preamble) {
                throw new RuntimeException("switch body does not start with default/case.");
            }
            stats = stats.append(toStatement(s));
        }
    }

    if (!preamble) cases = addCase(cases, currentPat, currentNode, stats);

    JCExpression expr = reParen(node, toExpression(node.astCondition()));
    return posSet(node, treeMaker.Switch(expr, cases));
}
项目:android-retrolambda-lombok    文件:JcTreeBuilder.java   
private List<JCCase> addCase(List<JCCase> cases, JCExpression currentPat, Node currentNode, List<JCStatement> stats) {
    JCStatement last = stats.last();
    int start = currentNode.getPosition().getStart();
    int end = last == null ? currentNode.getPosition().getEnd() : endPosTable.get(last);
    cases = cases.append(setPos(start, end, treeMaker.Case(currentPat, stats)));
    return cases;
}
项目:android-retrolambda-lombok    文件:JcTreeConverter.java   
@Override public void visitSwitch(JCSwitch node) {
    Switch s = new Switch();
    JCExpression cond = node.getExpression();
    setConversionPositionInfo(s, "()", getPosition(cond));
    s.rawCondition(toTree(removeParens(cond)));
    Block b = new Block();
    s.astBody(b);
    for (JCCase c : node.getCases()) {
        JCExpression rawExpr = c.getExpression();
        if (rawExpr == null) b.rawContents().addToEnd(setPos(c, new Default()));
        else b.rawContents().addToEnd(setPos(c, new Case().rawCondition(toTree(rawExpr))));
        fillList(c.getStatements(), b.rawContents());
    }
    set(node, s);
}
项目:error-prone    文件:PlaceholderUnificationVisitor.java   
@Override
public Choice<State<JCSwitch>> visitSwitch(final SwitchTree node, State<?> state) {
  return chooseSubtrees(
      state,
      s -> unifyExpression(node.getExpression(), s),
      s -> unify(node.getCases(), s),
      (expr, cases) -> maker().Switch(expr, List.convert(JCCase.class, cases)));
}
项目:error-prone    文件:PlaceholderUnificationVisitor.java   
@Override
public Choice<State<JCCase>> visitCase(final CaseTree node, State<?> state) {
  return chooseSubtrees(
      state,
      s -> unifyStatements(node.getStatements(), s),
      stmts -> maker().Case((JCExpression) node.getExpression(), stmts));
}
项目:yield4j    文件:JavacCodeGenerator.java   
@Override
public void visit(SwitchWrapper switchWrapper) {
    TreeMaker maker = builder.getTreeMaker();

    ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
    for (CaseWrapper cw : switchWrapper.getCases()) {
        cases.append(generate(cw, JCCase.class));
    }

    result = maker.Switch(generate(switchWrapper.getExpression()),
            cases.toList());
}
项目:incubator-netbeans    文件:CasualDiff.java   
private boolean matchCase(JCCase t1, JCCase t2) {
    return treesMatch(t1.pat, t2.pat) && listsMatch(t1.stats, t2.stats);
}
项目:lombok-ianchiu    文件:JavacTreeMaker.java   
public JCSwitch Switch(JCExpression selector, List<JCCase> cases) {
    return invoke(Switch, selector, cases);
}
项目:lombok-ianchiu    文件:JavacTreeMaker.java   
public JCCase Case(JCExpression pat, List<JCStatement> stats) {
    return invoke(Case, pat, stats);
}
项目:lombok-ianchiu    文件:HandleHelper.java   
private List<JCStatement> getStatementsFromJcNode(JCTree tree) {
    if (tree instanceof JCBlock) return ((JCBlock) tree).stats;
    if (tree instanceof JCCase) return ((JCCase) tree).stats;
    return null;
}
项目:lombok-ianchiu    文件:HandleHelper.java   
private void setStatementsOfJcNode(JCTree tree, List<JCStatement> statements) {
    if (tree instanceof JCBlock) ((JCBlock) tree).stats = statements;
    else if (tree instanceof JCCase) ((JCCase) tree).stats = statements;
    else throw new IllegalArgumentException("Can't set statements on node type: " + tree.getClass());
}
项目:javaparser2jctree    文件:AJCCase.java   
public AJCCase(JCCase ltree) {
    super(ltree.pat, ltree.stats);
}
项目:javaparser2jctree    文件:AJCCase.java   
public AJCCase(JCCase ltree, String lcomment) {
    this(ltree);
    setComment(lcomment);
}
项目:EasyMPermission    文件:JavacTreeMaker.java   
public JCSwitch Switch(JCExpression selector, List<JCCase> cases) {
    return invoke(Switch, selector, cases);
}
项目:EasyMPermission    文件:JavacTreeMaker.java   
public JCCase Case(JCExpression pat, List<JCStatement> stats) {
    return invoke(Case, pat, stats);
}
项目:android-retrolambda-lombok    文件:JcTreePrinter.java   
@Override public void visitCase(JCCase tree) {
    printNode(tree);
    child("pat", tree.pat);
    children("stats", tree.stats);
    indent--;
}
项目:s4j    文件:Attr.java   
public void visitSwitch(JCSwitch tree) {
    Type seltype = attribExpr(tree.selector, env);

    Env<AttrContext> switchEnv =
        env.dup(tree, env.info.dup(env.info.scope.dup()));

    boolean enumSwitch =
        allowEnums &&
        (seltype.tsym.flags() & Flags.ENUM) != 0;
    boolean stringSwitch = false;
    if (types.isSameType(seltype, syms.stringType)) {
        if (allowStringsInSwitch) {
            stringSwitch = true;
        } else {
            log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName);
        }
    }
    if (!enumSwitch && !stringSwitch)
        seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);

    // Attribute all cases and
    // check that there are no duplicate case labels or default clauses.
    Set<Object> labels = new HashSet<Object>(); // The set of case labels.
    boolean hasDefault = false;      // Is there a default label?
    for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
        JCCase c = l.head;
        Env<AttrContext> caseEnv =
            switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
        if (c.pat != null) {
            if (enumSwitch) {
                Symbol sym = enumConstant(c.pat, seltype);
                if (sym == null) {
                    log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum");
                } else if (!labels.add(sym)) {
                    log.error(c.pos(), "duplicate.case.label");
                }
            } else {
                Type pattype = attribExpr(c.pat, switchEnv, seltype);
                if (pattype.tag != ERROR) {
                    if (pattype.constValue() == null) {
                        log.error(c.pat.pos(),
                                  (stringSwitch ? "string.const.req" : "const.expr.req"));
                    } else if (labels.contains(pattype.constValue())) {
                        log.error(c.pos(), "duplicate.case.label");
                    } else {
                        labels.add(pattype.constValue());
                    }
                }
            }
        } else if (hasDefault) {
            log.error(c.pos(), "duplicate.default.label");
        } else {
            hasDefault = true;
        }
        attribStats(c.stats, caseEnv);
        caseEnv.info.scope.leave();
        addVars(c.stats, switchEnv.info.scope);
    }

    switchEnv.info.scope.leave();
    result = null;
}
项目:lombok    文件:HandleCleanup.java   
@Override public void handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) {
    if (inNetbeansEditor(annotationNode)) return;

    deleteAnnotationIfNeccessary(annotationNode, Cleanup.class);
    String cleanupName = annotation.getInstance().value();
    if (cleanupName.length() == 0) {
        annotationNode.addError("cleanupName cannot be the empty string.");
        return;
    }

    if (annotationNode.up().getKind() != Kind.LOCAL) {
        annotationNode.addError("@Cleanup is legal only on local variable declarations.");
        return;
    }

    JCVariableDecl decl = (JCVariableDecl)annotationNode.up().get();

    if (decl.init == null) {
        annotationNode.addError("@Cleanup variable declarations need to be initialized.");
        return;
    }

    JavacNode ancestor = annotationNode.up().directUp();
    JCTree blockNode = ancestor.get();

    final List<JCStatement> statements;
    if (blockNode instanceof JCBlock) {
        statements = ((JCBlock)blockNode).stats;
    } else if (blockNode instanceof JCCase) {
        statements = ((JCCase)blockNode).stats;
    } else if (blockNode instanceof JCMethodDecl) {
        statements = ((JCMethodDecl)blockNode).body.stats;
    } else {
        annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block.");
        return;
    }

    boolean seenDeclaration = false;
    ListBuffer<JCStatement> newStatements = ListBuffer.lb();
    ListBuffer<JCStatement> tryBlock = ListBuffer.lb();
    for (JCStatement statement : statements) {
        if (!seenDeclaration) {
            if (statement == decl) seenDeclaration = true;
            newStatements.append(statement);
        } else {
            tryBlock.append(statement);
        }
    }

    if (!seenDeclaration) {
        annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
        return;
    }
    doAssignmentCheck(annotationNode, tryBlock.toList(), decl.name);

    TreeMaker maker = annotationNode.getTreeMaker();
    JCFieldAccess cleanupMethod = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName));
    List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec(
            maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil())));

    JCMethodInvocation preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name));
    JCBinary isNull = maker.Binary(CTC_NOT_EQUAL, preventNullAnalysis, maker.Literal(CTC_BOT, null));

    JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null);

    JCBlock finalizer = recursiveSetGeneratedBy(maker.Block(0, List.<JCStatement>of(ifNotNullCleanup)), ast);

    newStatements.append(setGeneratedBy(maker.Try(setGeneratedBy(maker.Block(0, tryBlock.toList()), ast), List.<JCCatch>nil(), finalizer), ast));

    if (blockNode instanceof JCBlock) {
        ((JCBlock)blockNode).stats = newStatements.toList();
    } else if (blockNode instanceof JCCase) {
        ((JCCase)blockNode).stats = newStatements.toList();
    } else if (blockNode instanceof JCMethodDecl) {
        ((JCMethodDecl)blockNode).body.stats = newStatements.toList();
    } else throw new AssertionError("Should not get here");

    ancestor.rebuild();
}