Java Code Examples for com.sun.tools.javac.tree.JCTree#JCCase
The following examples show how to use
com.sun.tools.javac.tree.JCTree#JCCase .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TreeAnalyzer.java From meghanada-server with GNU General Public License v3.0 | 6 votes |
private static void analyzeSwitch(SourceContext context, JCTree.JCSwitch jcSwitch) throws IOException { JCTree.JCExpression expression = jcSwitch.getExpression(); analyzeParsedTree(context, expression); List<JCTree.JCCase> cases = jcSwitch.getCases(); if (nonNull(cases)) { for (JCTree.JCCase jcCase : cases) { JCTree.JCExpression expression1 = jcCase.getExpression(); analyzeParsedTree(context, expression1); List<JCTree.JCStatement> statements = jcCase.getStatements(); analyzeSimpleStatements(context, statements); } } }
Example 2
Source File: VeryPretty.java From netbeans with Apache License 2.0 | 5 votes |
public void visitSwitchExpression(Tree tree) { print("switch"); print(cs.spaceBeforeSwitchParen() ? " (" : "("); if (cs.spaceWithinSwitchParens()) { print(' '); } printNoParenExpr((JCTree) TreeShims.getExpressions(tree).get(0)); print(cs.spaceWithinSwitchParens() ? " )" : ")"); int bcol = out.leftMargin; switch (cs.getOtherBracePlacement()) { case NEW_LINE: newline(); toColExactly(bcol); break; case NEW_LINE_HALF_INDENTED: newline(); bcol += (indentSize >> 1); toColExactly(bcol); break; case NEW_LINE_INDENTED: newline(); bcol += indentSize; toColExactly(bcol); break; } if (cs.spaceBeforeSwitchLeftBrace()) { needSpace(); } print('{'); if (!TreeShims.getCases(tree).isEmpty()) { newline(); ListBuffer<JCTree.JCCase> newTcases = new ListBuffer<JCTree.JCCase>(); for (CaseTree t : TreeShims.getCases(tree)) { newTcases.append((JCTree.JCCase) t); } printStats(newTcases.toList()); toColExactly(bcol); } print('}'); }
Example 3
Source File: TreeShims.java From netbeans with Apache License 2.0 | 5 votes |
public static Tree SwitchExpression(TreeMaker make, ExpressionTree selector, List<? extends CaseTree> caseList) throws SecurityException { ListBuffer<JCTree.JCCase> cases = new ListBuffer<JCTree.JCCase>(); for (CaseTree t : caseList) { cases.append((JCTree.JCCase) t); } try { Method getMethod = TreeMaker.class.getDeclaredMethod("SwitchExpression", JCTree.JCExpression.class, com.sun.tools.javac.util.List.class); return (Tree) getMethod.invoke(make, (JCTree.JCExpression) selector, cases.toList()); } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw TreeShims.<RuntimeException>throwAny(ex); } }
Example 4
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
private static List<? extends StatementTree> getSwitchStatement(CaseTree ct) { if (ct.getStatements() != null) { return ct.getStatements(); } else if (ct instanceof JCTree.JCCase) { return ((JCTree.JCCase) ct).stats; } else { return null; } }
Example 5
Source File: SwitchExpressionTest.java From netbeans with Apache License 2.0 | 4 votes |
/** * Rewrite Switch Expression cases. * * @throws IOException */ private void rewriteSwitchExpression() throws IOException { JavaSource js = getJavaSource(); assertNotNull(js); Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws IOException { workingCopy.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); TreeMaker make = workingCopy.getTreeMaker(); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); MethodTree method = (MethodTree) clazz.getMembers().get(1); List<CaseTree> newCases = new ArrayList<>(); VariableTree switcExpression = (VariableTree) ((BlockTree) method.getBody()).getStatements().get(0); Tree switchBlock = switcExpression.getInitializer(); List<? extends CaseTree> cases; List<ExpressionTree> patterns = new ArrayList<>(); boolean switchExpressionFlag = switchBlock.getKind().toString().equals(TreeShims.SWITCH_EXPRESSION); if (switchExpressionFlag) { cases = TreeShims.getCases(switchBlock); } else { cases = ((SwitchTree) switchBlock).getCases(); } for (Iterator<? extends CaseTree> it = cases.iterator(); it.hasNext();) { CaseTree ct = it.next(); patterns.addAll(TreeShims.getExpressions(ct)); List<StatementTree> statements; if (ct.getStatements() == null) { statements = new ArrayList<>(((JCTree.JCCase) ct).stats); } else { statements = new ArrayList<>(ct.getStatements()); } if (statements.isEmpty()) { if (it.hasNext()) { continue; } } Set<Element> seenVariables = new HashSet<>(); int idx = 0; for (StatementTree statement : new ArrayList<>(statements)) { Tree body = make.Block(statements, false); if (statements.size() == 1) { if (statements.get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT || statements.get(0).getKind() == Tree.Kind.THROW || statements.get(0).getKind() == Tree.Kind.BLOCK) { body = statements.get(0); } } newCases.add(make.Case(patterns, body)); patterns = new ArrayList<>(); } } workingCopy.rewrite(switchBlock, make.SwitchExpression(TreeShims.getExpressions(switchBlock).get(0), newCases)); } }; js.runModificationTask(task).commit(); }