com.sun.source.tree.ForLoopTree Java Examples
The following examples show how to use
com.sun.source.tree.ForLoopTree.
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: CopyFinder.java From netbeans with Apache License 2.0 | 6 votes |
public Boolean visitForLoop(ForLoopTree node, TreePath p) { if (p == null) return super.visitForLoop(node, p); ForLoopTree t = (ForLoopTree) p.getLeaf(); if (!checkLists(node.getInitializer(), t.getInitializer(), p)) { return false; } if (!scan(node.getCondition(), t.getCondition(), p)) return false; if (!checkLists(node.getUpdate(), t.getUpdate(), p)) return false; return scan(node.getStatement(), t.getStatement(), p); }
Example #2
Source File: NCLOCVisitor.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Object visitVariable(VariableTree node, Object p) { TreePath path = getCurrentPath(); Tree parent = path.getParentPath().getLeaf(); if (parent instanceof StatementTree) { boolean count = true; if (parent instanceof ForLoopTree) { count = !((ForLoopTree)parent).getInitializer().contains(node); } else if (parent instanceof EnhancedForLoopTree) { count = ((EnhancedForLoopTree)parent).getVariable() != node; } if (count) { statements++; } } return super.visitVariable(node, p); }
Example #3
Source File: ExpectedTypeResolver.java From netbeans with Apache License 2.0 | 6 votes |
@Override public List<? extends TypeMirror> visitForLoop(ForLoopTree node, Object p) { if (theExpression == null) { // ambigous return null; } if (theExpression.getLeaf() == node.getCondition()) { return booleanType(); } else { if (!((node.getInitializer() != null && node.getInitializer().contains(theExpression.getLeaf())) || (node.getUpdate() != null && node.getUpdate().contains(theExpression.getLeaf())))) { return null; } // initializer and update operation can have any result type, including none TypeElement tel = info.getElements().getTypeElement("java.lang.Void"); if (tel == null) { return null; } return Collections.singletonList(tel.asType()); // NOI18N } }
Example #4
Source File: ScanStatement.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Void visitForLoop(ForLoopTree node, Void p) { super.visitForLoop(node, p); if (isMethodCode() && phase == PHASE_AFTER_SELECTION) { //#109663𛞨: //the selection was inside the for-loop, the variables inside the //condition, update and statement parts of the for loop need to be considered to be used again after the loop: if (!secondPass) { secondPass = true; scan(node.getCondition(), p); scan(node.getUpdate(), p); scan(node.getStatement(), p); secondPass = false; stopSecondPass = false; } } return null; }
Example #5
Source File: TreeDiffer.java From compile-testing with Apache License 2.0 | 5 votes |
@Override public Void visitForLoop(ForLoopTree expected, Tree actual) { Optional<ForLoopTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } parallelScan(expected.getInitializer(), other.get().getInitializer()); scan(expected.getCondition(), other.get().getCondition()); parallelScan(expected.getUpdate(), other.get().getUpdate()); scan(expected.getStatement(), other.get().getStatement()); return null; }
Example #6
Source File: ModelBuilder.java From vertx-codetrans with Apache License 2.0 | 5 votes |
@Override public CodeModel visitForLoop(ForLoopTree node, VisitContext context) { if (node.getInitializer().size() != 1) { throw new UnsupportedOperationException(); } if (node.getUpdate().size() != 1) { throw new UnsupportedOperationException(); } StatementModel body = scan(node.getStatement(), context); if (node.getInitializer().size() == 1 && node.getInitializer().get(0).getKind() == Tree.Kind.VARIABLE && node.getCondition().getKind() == Tree.Kind.LESS_THAN && node.getUpdate().size() == 1 && node.getUpdate().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT && node.getUpdate().get(0).getExpression().getKind() == Tree.Kind.POSTFIX_INCREMENT) { VariableTree init = (VariableTree) node.getInitializer().get(0); BinaryTree lessThan = (BinaryTree) node.getCondition(); UnaryTree increment = (UnaryTree) node.getUpdate().get(0).getExpression(); if (lessThan.getLeftOperand().getKind() == Tree.Kind.IDENTIFIER && increment.getExpression().getKind() == Tree.Kind.IDENTIFIER) { String id1 = init.getName().toString(); String id2 = ((IdentifierTree) lessThan.getLeftOperand()).getName().toString(); String id3 = ((IdentifierTree) increment.getExpression()).getName().toString(); if (id1.equals(id2) && id2.equals(id3)) { ExpressionModel from = scan(init.getInitializer(), context); ExpressionModel to = scan(lessThan.getRightOperand(), context); return context.builder.sequenceForLoop(id1, from, to, body); } } } StatementModel initializer = scan(node.getInitializer().get(0), context); ExpressionModel update = scan(node.getUpdate().get(0).getExpression(), context); ExpressionModel condition = scan(node.getCondition(), context); return context.builder.forLoop(initializer, condition, update, body); }
Example #7
Source File: UForLoop.java From Refaster with Apache License 2.0 | 5 votes |
@Override @Nullable public Unifier visitForLoop(ForLoopTree loop, @Nullable Unifier unifier) { unifier = Unifier.unifyList(unifier, getInitializer(), loop.getInitializer()); unifier = Unifier.unifyNullable(unifier, getCondition(), loop.getCondition()); unifier = Unifier.unifyList(unifier, getUpdate(), loop.getUpdate()); return getStatement().unify(loop.getStatement(), unifier); }
Example #8
Source File: UTemplater.java From Refaster with Apache License 2.0 | 5 votes |
@Override public UForLoop visitForLoop(ForLoopTree tree, Void v) { return UForLoop.create( templateStatements(tree.getInitializer()), (tree.getCondition() == null) ? null : template(tree.getCondition()), cast(templateStatements(tree.getUpdate()), UExpressionStatement.class), template(tree.getStatement())); }
Example #9
Source File: TreeConverter.java From j2objc with Apache License 2.0 | 5 votes |
private TreeNode convertForLoop(ForLoopTree node, TreePath parent) { TreePath path = getTreePath(parent, node); ForStatement newNode = new ForStatement() .setExpression((Expression) convert(node.getCondition(), path)) .setBody((Statement) convert(node.getStatement(), path)); VariableDeclarationExpression lastVar = null; for (StatementTree initializer : node.getInitializer()) { if (initializer.getKind() == Kind.VARIABLE) { VariableTree var = (VariableTree) initializer; VariableDeclarationExpression newVar = convertVariableExpression(var, path); if (lastVar == null) { newNode.addInitializer(newVar); lastVar = newVar; } else { lastVar.addFragment(TreeUtil.remove(newVar.getFragment(0))); } } else { assert initializer.getKind() == Kind.EXPRESSION_STATEMENT; TreePath initializerPath = getTreePath(path, initializer); TreeNode expr = convert(((ExpressionStatementTree) initializer).getExpression(), initializerPath); newNode.addInitializer((Expression) expr); } } for (ExpressionStatementTree updater : node.getUpdate()) { newNode.addUpdater((Expression) convert(updater.getExpression(), getTreePath(path, updater))); } return newNode; }
Example #10
Source File: CompletenessStressTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private boolean testBlock(StringWriter writer, SourcePositions sp, String text, CompilationUnitTree cut, BlockTree blockTree) { boolean success = true; for (StatementTree st : blockTree.getStatements()) { if (isLegal(st)) { success &= testStatement(writer, sp, text, cut, st); } if (st instanceof IfTree) { IfTree ifTree = (IfTree) st; success &= testBranch(writer, sp, text, cut, ifTree.getThenStatement()); success &= testBranch(writer, sp, text, cut, ifTree.getElseStatement()); } else if (st instanceof WhileLoopTree) { WhileLoopTree whileLoopTree = (WhileLoopTree) st; success &= testBranch(writer, sp, text, cut, whileLoopTree.getStatement()); } else if (st instanceof DoWhileLoopTree) { DoWhileLoopTree doWhileLoopTree = (DoWhileLoopTree) st; success &= testBranch(writer, sp, text, cut, doWhileLoopTree.getStatement()); } else if (st instanceof ForLoopTree) { ForLoopTree forLoopTree = (ForLoopTree) st; success &= testBranch(writer, sp, text, cut, forLoopTree.getStatement()); } else if (st instanceof LabeledStatementTree) { LabeledStatementTree labelTree = (LabeledStatementTree) st; success &= testBranch(writer, sp, text, cut, labelTree.getStatement()); } else if (st instanceof SwitchTree) { SwitchTree switchTree = (SwitchTree) st; for (CaseTree caseTree : switchTree.getCases()) { for (StatementTree statementTree : caseTree.getStatements()) { success &= testBranch(writer, sp, text, cut, statementTree); } } } } return success; }
Example #11
Source File: NullAway.java From NullAway with MIT License | 5 votes |
@Override public Description matchForLoop(ForLoopTree tree, VisitorState state) { if (!matchWithinClass) { return Description.NO_MATCH; } if (tree.getCondition() != null) { return doUnboxingCheck(state, tree.getCondition()); } return Description.NO_MATCH; }
Example #12
Source File: InfiniteRecursion.java From netbeans with Apache License 2.0 | 5 votes |
@Override public State visitForLoop(ForLoopTree node, Void p) { State s; registerBreakTarget((node)); if (returnIfRecurse(s = scan(node.getInitializer(), p))) { return s; } returnIfRecurse(s = s.append(scan(node.getCondition(), p))); // the body + update might be skipped if condition evaluates to false immediately. // PENDING: speculatively report recursions, if unconditionally reachable from cycles ? return s; }
Example #13
Source File: Braces.java From netbeans with Apache License 2.0 | 5 votes |
@Hint(displayName="#LBL_Braces_For", description="#DSC_Braces_For", category="braces", id=BRACES_ID + "FOR_LOOP", enabled=false, suppressWarnings={"", "ControlFlowStatementWithoutBraces"}) @TriggerTreeKind({Tree.Kind.FOR_LOOP, Tree.Kind.ENHANCED_FOR_LOOP}) public static ErrorDescription checkFor(HintContext ctx) { StatementTree st; switch (ctx.getPath().getLeaf().getKind()){ case FOR_LOOP: st = ((ForLoopTree) ctx.getPath().getLeaf()).getStatement(); break; case ENHANCED_FOR_LOOP: st = ((EnhancedForLoopTree) ctx.getPath().getLeaf()).getStatement(); break; default: throw new IllegalStateException(); } return checkStatement(ctx, "LBL_Braces_For", st, ctx.getPath()); }
Example #14
Source File: TreeDuplicator.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Tree visitForLoop(ForLoopTree tree, Void p) { ForLoopTree n = make.ForLoop(tree.getInitializer(), tree.getCondition(), tree.getUpdate(), tree.getStatement()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
Example #15
Source File: DepthVisitor.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Object visitForLoop(ForLoopTree node, Object p) { depth++; Object o = super.visitForLoop(node, p); depth--; return o; }
Example #16
Source File: CyclomaticComplexityVisitor.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Object visitForLoop(ForLoopTree node, Object p) { boolean saveFlag = switchCase; switchCase = false; complexity++; Object o = super.visitForLoop(node, p); this.switchCase = saveFlag; return o; }
Example #17
Source File: NCLOCVisitor.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Object visitExpressionStatement(ExpressionStatementTree node, Object p) { boolean count = true; TreePath path = getCurrentPath(); Tree parent = path.getParentPath().getLeaf(); // do not count the update statement in a for-loop if (parent instanceof ForLoopTree) { count = !((ForLoopTree)parent).getUpdate().contains(node); } if (count) { statements++; } return super.visitExpressionStatement(node, p); }
Example #18
Source File: TreeNode.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Void visitForLoop(ForLoopTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingType(below); addCorrespondingComments(below); super.visitForLoop(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
Example #19
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 4 votes |
@Override public Void visitForLoop(ForLoopTree node, Void unused) { sync(node); token("for"); builder.space(); token("("); builder.open(plusFour); builder.open( node.getInitializer().size() > 1 && node.getInitializer().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT ? plusFour : ZERO); if (!node.getInitializer().isEmpty()) { if (node.getInitializer().get(0).getKind() == VARIABLE) { PeekingIterator<StatementTree> it = Iterators.<StatementTree>peekingIterator(node.getInitializer().iterator()); visitVariables( variableFragments(it, it.next()), DeclarationKind.NONE, Direction.HORIZONTAL); } else { boolean first = true; builder.open(ZERO); for (StatementTree t : node.getInitializer()) { if (!first) { token(","); builder.breakOp(" "); } scan(((ExpressionStatementTree) t).getExpression(), null); first = false; } token(";"); builder.close(); } } else { token(";"); } builder.close(); builder.breakOp(" "); if (node.getCondition() != null) { scan(node.getCondition(), null); } token(";"); if (!node.getUpdate().isEmpty()) { builder.breakOp(" "); builder.open(node.getUpdate().size() <= 1 ? ZERO : plusFour); boolean firstUpdater = true; for (ExpressionStatementTree updater : node.getUpdate()) { if (!firstUpdater) { token(","); builder.breakToFill(" "); } scan(updater.getExpression(), null); firstUpdater = false; } builder.guessToken(";"); builder.close(); } else { builder.space(); } builder.close(); token(")"); visitStatement( node.getStatement(), CollapseEmptyOrNot.YES, AllowLeadingBlankLine.YES, AllowTrailingBlankLine.NO); return null; }
Example #20
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
@Override public Void visitForLoop(ForLoopTree node, Void unused) { sync(node); token("for"); builder.space(); token("("); builder.open(plusFour); builder.open( node.getInitializer().size() > 1 && node.getInitializer().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT ? plusFour : ZERO); if (!node.getInitializer().isEmpty()) { if (node.getInitializer().get(0).getKind() == VARIABLE) { PeekingIterator<StatementTree> it = Iterators.<StatementTree>peekingIterator(node.getInitializer().iterator()); visitVariables( variableFragments(it, it.next()), DeclarationKind.NONE, Direction.HORIZONTAL); } else { boolean first = true; builder.open(ZERO); for (StatementTree t : node.getInitializer()) { if (!first) { token(","); builder.breakOp(" "); } scan(((ExpressionStatementTree) t).getExpression(), null); first = false; } token(";"); builder.close(); } } else { token(";"); } builder.close(); builder.breakOp(" "); if (node.getCondition() != null) { scan(node.getCondition(), null); } token(";"); if (!node.getUpdate().isEmpty()) { builder.breakOp(" "); builder.open(node.getUpdate().size() <= 1 ? ZERO : plusFour); boolean firstUpdater = true; for (ExpressionStatementTree updater : node.getUpdate()) { if (!firstUpdater) { token(","); builder.breakToFill(" "); } scan(updater.getExpression(), null); firstUpdater = false; } builder.guessToken(";"); builder.close(); } else { builder.space(); } builder.close(); token(")"); visitStatement( node.getStatement(), CollapseEmptyOrNot.YES, AllowLeadingBlankLine.YES, AllowTrailingBlankLine.NO); return null; }
Example #21
Source File: Reindenter.java From netbeans with Apache License 2.0 | 4 votes |
private LinkedList<? extends Tree> getPath(final int startOffset) { final LinkedList<Tree> path = new LinkedList<Tree>(); // When right at the token end move to previous token; otherwise move to the token that "contains" the offset if (ts.move(startOffset) == 0 && startOffset > 0 || !ts.moveNext()) { ts.movePrevious(); } final int offset = (ts.token().id() == JavaTokenId.IDENTIFIER || ts.token().id().primaryCategory().startsWith("keyword") || //NOI18N ts.token().id().primaryCategory().startsWith("string") || //NOI18N ts.token().id().primaryCategory().equals("literal")) //NOI18N ? ts.offset() : startOffset; new ErrorAwareTreeScanner<Void, Void>() { @Override public Void scan(Tree node, Void p) { if (node != null) { if (getStartPosition(node) < offset && getEndPosition(node) >= offset) { super.scan(node, p); if (node.getKind() != Tree.Kind.ERRONEOUS || !path.isEmpty()) { path.add(node); } } } return null; } }.scan(parsedTree, null); if (path.isEmpty() || path.getFirst() == parsedTree || getEndPosition(path.getFirst()) > offset) { return path; } if (!path.isEmpty() && ts.move(offset) == 0) { if (ts.movePrevious()) { switch (ts.token().id()) { case RPAREN: if (!EnumSet.of(Kind.ENHANCED_FOR_LOOP, Kind.FOR_LOOP, Kind.IF, Kind.WHILE_LOOP, Kind.DO_WHILE_LOOP, Kind.TYPE_CAST, Kind.SYNCHRONIZED).contains(path.getFirst().getKind())) { path.removeFirst(); } break; case GTGTGT: case GTGT: case GT: if (EnumSet.of(Kind.MEMBER_SELECT, Kind.CLASS, Kind.GREATER_THAN).contains(path.getFirst().getKind())) { break; } case SEMICOLON: if (path.getFirst().getKind() == Kind.FOR_LOOP && ts.offset() <= getStartPosition(((ForLoopTree)path.getFirst()).getUpdate().get(0))) { break; } case RBRACE: path.removeFirst(); switch (path.getFirst().getKind()) { case CATCH: path.removeFirst(); case METHOD: case FOR_LOOP: case ENHANCED_FOR_LOOP: case IF: case SYNCHRONIZED: case WHILE_LOOP: case TRY: path.removeFirst(); } break; } } } return path; }
Example #22
Source File: AddCastTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testAddCastInForWithoutSteps() throws Exception { testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "package hierbas.del.litoral;\n\n" + "import java.util.*;\n\n" + "public class Test<E> {\n" + " public void cast() {\n" + " Object o = null;\n" + " for (int i = 0; i < 5; ) {\n" + " String s = o;\n" + " }\n" + " }\n" + "}\n" ); String golden = "package hierbas.del.litoral;\n\n" + "import java.util.*;\n\n" + "public class Test<E> {\n" + " public void cast() {\n" + " Object o = null;\n" + " for (int i = 0; i < 5; ) {\n" + " String s = (String) o;\n" + " }\n" + " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws IOException { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); TreeMaker make = workingCopy.getTreeMaker(); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); MethodTree method = (MethodTree) clazz.getMembers().get(1); ForLoopTree forLoop = (ForLoopTree) method.getBody().getStatements().get(1); BlockTree block = (BlockTree) forLoop.getStatement(); VariableTree vt = (VariableTree) block.getStatements().get(0); ExpressionTree init = vt.getInitializer(); ExpressionTree cast = make.TypeCast(make.Identifier("String"), init); workingCopy.rewrite(init, cast); } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example #23
Source File: VisitForLoopHook.java From kan-java with Eclipse Public License 1.0 | 4 votes |
void afterVisitStatement(ForLoopTree node, List<ErrMsg> errMsgs, GlobalContext globalCtx, Closure<List<Map<String, Long>>> resolveRowAndCol, Closure<Void> setError);
Example #24
Source File: VisitForLoopHook.java From kan-java with Eclipse Public License 1.0 | 4 votes |
void afterVisitUpdateAndBeforeStatement(ForLoopTree node, List<ErrMsg> errMsgs, GlobalContext globalCtx, Closure<List<Map<String, Long>>> resolveRowAndCol, Closure<Void> setError);
Example #25
Source File: VisitForLoopHook.java From kan-java with Eclipse Public License 1.0 | 4 votes |
void afterVisitConditionAndBeforeUpdate(ForLoopTree node, List<ErrMsg> errMsgs, GlobalContext globalCtx, Closure<List<Map<String, Long>>> resolveRowAndCol, Closure<Void> setError);
Example #26
Source File: VisitForLoopHook.java From kan-java with Eclipse Public License 1.0 | 4 votes |
void afterVisitInitializerAndBeforeCondition(ForLoopTree node, List<ErrMsg> errMsgs, GlobalContext globalCtx, Closure<List<Map<String, Long>>> resolveRowAndCol, Closure<Void> setError);
Example #27
Source File: VisitForLoopHook.java From kan-java with Eclipse Public License 1.0 | 4 votes |
void beforeVisitInitializer(ForLoopTree node, List<ErrMsg> errMsgs, GlobalContext globalCtx, Closure<List<Map<String, Long>>> resolveRowAndCol, Closure<Void> setError);
Example #28
Source File: LocalVarScanner.java From netbeans with Apache License 2.0 | 4 votes |
@Override public Boolean visitForLoop(ForLoopTree node, Element p) { return null; }
Example #29
Source File: JavaInputAstVisitor.java From google-java-format with Apache License 2.0 | 4 votes |
@Override public Void visitForLoop(ForLoopTree node, Void unused) { sync(node); token("for"); builder.space(); token("("); builder.open(plusFour); builder.open( node.getInitializer().size() > 1 && node.getInitializer().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT ? plusFour : ZERO); if (!node.getInitializer().isEmpty()) { if (node.getInitializer().get(0).getKind() == VARIABLE) { PeekingIterator<StatementTree> it = Iterators.peekingIterator(node.getInitializer().iterator()); visitVariables( variableFragments(it, it.next()), DeclarationKind.NONE, Direction.HORIZONTAL); } else { boolean first = true; builder.open(ZERO); for (StatementTree t : node.getInitializer()) { if (!first) { token(","); builder.breakOp(" "); } scan(((ExpressionStatementTree) t).getExpression(), null); first = false; } token(";"); builder.close(); } } else { token(";"); } builder.close(); builder.breakOp(" "); if (node.getCondition() != null) { scan(node.getCondition(), null); } token(";"); if (!node.getUpdate().isEmpty()) { builder.breakOp(" "); builder.open(node.getUpdate().size() <= 1 ? ZERO : plusFour); boolean firstUpdater = true; for (ExpressionStatementTree updater : node.getUpdate()) { if (!firstUpdater) { token(","); builder.breakToFill(" "); } scan(updater.getExpression(), null); firstUpdater = false; } builder.guessToken(";"); builder.close(); } else { builder.space(); } builder.close(); token(")"); visitStatement( node.getStatement(), CollapseEmptyOrNot.YES, AllowLeadingBlankLine.YES, AllowTrailingBlankLine.NO); return null; }
Example #30
Source File: MethodMetrics.java From netbeans with Apache License 2.0 | 4 votes |
@Override public Object visitForLoop(ForLoopTree node, Object p) { loopCount++; return super.visitForLoop(node, p); }