@Override public Void visitDoWhileLoop(DoWhileLoopTree node, Void p) { super.visitDoWhileLoop(node, p); if (isMethodCode() && phase == PHASE_AFTER_SELECTION) { //#109663𛞨: //the selection was inside the do-while, the variables inside the //statement part of the do-while loop need to be considered to be used again after the loop: if (!secondPass) { secondPass = true; scan(node.getStatement(), p); secondPass = false; stopSecondPass = false; } } return null; }
@Override public Mirror visitDoWhileLoop(DoWhileLoopTree arg0, EvaluationContext evaluationContext) { ExpressionTree condition = arg0.getCondition(); Tree statement = arg0.getStatement(); Mirror result = null; do { try { evaluationContext.pushBlock(); Mirror res = statement.accept(this, evaluationContext); if (res instanceof Break) { break; } else if (res instanceof Continue) { continue; } if (res != null) { result = res; } } finally { evaluationContext.popBlock(); } } while (evaluateCondition(arg0, evaluationContext, condition)); return result; }
@Override public Void visitDoWhileLoop(DoWhileLoopTree node, Void unused) { sync(node); token("do"); visitStatement( node.getStatement(), CollapseEmptyOrNot.YES, AllowLeadingBlankLine.YES, AllowTrailingBlankLine.YES); if (node.getStatement().getKind() == BLOCK) { builder.space(); } else { builder.breakOp(" "); } token("while"); builder.space(); token("("); scan(skipParen(node.getCondition()), null); token(")"); token(";"); return null; }
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; }
@Test public void testVisitDoWhileLoop() throws BadLocationException, ParseException, IOException { List<ReformatOption> optionsToReformat = new ArrayList<>(); CompilationUnitTree unit = getCompilationUnitTree(INPUT_FILE); MethodTree methodTree = TreeNavigationUtils.findMethodTreeByName("doSomething", unit).get(0); DoWhileLoopTree doWhileLoopTree = (DoWhileLoopTree) getStatementTreeByClassName(DoWhileLoopTree.class, methodTree); if (doWhileLoopTree == null) { fail(ERROR_MESSAGE); } ReformatTreeVisitor reformatTreeVisitor = getReformatTreeVisitor(INPUT_FILE); reformatTreeVisitor.visitDoWhileLoop(doWhileLoopTree, optionsToReformat); assertFalse(optionsToReformat.isEmpty()); }
@Test public void testVisitDoWhileLoopNotReformat() throws BadLocationException, ParseException, IOException { List<ReformatOption> optionsToReformat = new ArrayList<>(); CompilationUnitTree unit = getCompilationUnitTree(INPUT_FILE); MethodTree methodTree = TreeNavigationUtils.findMethodTreeByName("doSomething", unit).get(0); DoWhileLoopTree doWhileLoopTree = (DoWhileLoopTree) getStatementTreeByClassName(DoWhileLoopTree.class, methodTree); if (doWhileLoopTree == null) { fail(ERROR_MESSAGE); } ReformatTreeVisitor reformatTreeVisitor = getReformatTreeVisitor(INPUT_FILE, 90, 100); reformatTreeVisitor.visitDoWhileLoop(doWhileLoopTree, optionsToReformat); assertTrue(optionsToReformat.isEmpty()); }
@Override public Boolean visitDoWhileLoop(DoWhileLoopTree that, Void unused) { boolean completes = scan(that.getStatement()); boolean conditionIsAlwaysTrue = firstNonNull(ASTHelpers.constValue(that.getCondition(), Boolean.class), false); // (1) if (completes && !conditionIsAlwaysTrue) { return true; } // (2) or (3) if (continues.contains(that) && !conditionIsAlwaysTrue) { return true; } // (4) if (breaks.contains(that)) { return true; } return false; }
@Override public Tree visitDoWhileLoop(DoWhileLoopTree tree, Void p) { DoWhileLoopTree n = make.DoWhileLoop(tree.getCondition(), tree.getStatement()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
public Boolean visitDoWhileLoop(DoWhileLoopTree node, TreePath p) { if (p == null) { super.visitDoWhileLoop(node, p); return false; } DoWhileLoopTree t = (DoWhileLoopTree) p.getLeaf(); if (!scan(node.getStatement(), t.getStatement(), p)) return false; return scan(node.getCondition(), t.getCondition(), p); }
/** * Do-while loop adds: * - no vertex, as it is a member of the linear code. * - an additional vertex for the code after the while * - one edge for unsatisfied condition that skips the cycle * - one edge for satisfied condition which at least re-evaluates the condition * - an additional vertex and an edge if the body contains a non-empty statement * = +1 to complexity */ @Override public Object visitDoWhileLoop(DoWhileLoopTree node, Object p) { boolean saveFlag = switchCase; switchCase = false; complexity++; Object o = super.visitDoWhileLoop(node, p); this.switchCase = saveFlag; return o; }
@Override public Object visitDoWhileLoop(DoWhileLoopTree node, Object p) { depth++; Object o = super.visitDoWhileLoop(node, p); depth--; return o; }
@Override public Void visitDoWhileLoop(DoWhileLoopTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingType(below); addCorrespondingComments(below); super.visitDoWhileLoop(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
@Override public State visitDoWhileLoop(DoWhileLoopTree node, Void p) { State s; registerBreakTarget((node)); if (returnIfRecurse(s = scan(node.getStatement(), p))) { return s; } returnIfRecurse(s = s.append(scan(node.getCondition(), p))); return s; }
@Override public Boolean visitDoWhileLoop(DoWhileLoopTree node, ConstructorData p) { Map< Element, State> beforeLoop = variable2State; variable2State = new HashMap< Element, Flow.State>(beforeLoop); scan(node.getStatement(), null); Boolean condValue = scan(node.getCondition(), null); if (condValue != null) { if (condValue) { //XXX: handle possibly infinite loop } else { //will not run more than once, skip: return null; } } variable2State = mergeOr(beforeLoop, variable2State); if (!doNotRecord) { boolean oldDoNotRecord = doNotRecord; doNotRecord = true; beforeLoop = new HashMap< Element, State>(variable2State); scan(node.getStatement(), null); scan(node.getCondition(), null); doNotRecord = oldDoNotRecord; variable2State = mergeOr(beforeLoop, variable2State); // variable2State = beforeLoop; } return null; }
@Override public List<? extends TypeMirror> visitDoWhileLoop(DoWhileLoopTree node, Object p) { if (theExpression == null) { initExpression(node.getCondition()); } else if (theExpression.getLeaf() != node.getCondition()) { return null; } return booleanType(); }
@Override public List<Tree> visitDoWhileLoop(DoWhileLoopTree node, ExpressionScanner.ExpressionsInfo p) { List<Tree> statements = scan(node.getStatement(), p); List<Tree> cond = null; if (acceptsTree(node.getCondition())) { cond = scan(node.getCondition(), p); } if (cond != null && cond.size() > 0 && statements != null && statements.size() > 0) { Tree lastCond = cond.get(cond.size() - 1); p.addNextExpression(lastCond, statements.get(0)); } return reduce(statements, cond); }
@Override public Void visitDoWhileLoop(DoWhileLoopTree doWhileLoopTree, List<ReformatOption> optionsToReformat) { StatementTree statement = doWhileLoopTree.getStatement(); if (statement instanceof BlockTree) { addLeftBraceToList(optionsToReformat, ((BlockTree) statement), PreferencesFormatOptions.BRACES_IN_OTHER_DECLARATION); addRightBraceToList(optionsToReformat, ((CompoundTree) doWhileLoopTree), PreferencesFormatOptions.AFTER_OTHER_DECLARATION); } return null; }
@Override public Void visitDoWhileLoop(DoWhileLoopTree tree, VisitorState visitorState) { VisitorState state = visitorState.withPath(getCurrentPath()); for (DoWhileLoopTreeMatcher matcher : doWhileLoopMatchers) { if (!isSuppressed(matcher, state)) { try { reportMatch(matcher.matchDoWhileLoop(tree, state), tree, state); } catch (Throwable t) { handleError(matcher, t); } } } return super.visitDoWhileLoop(tree, state); }
@Override public Result visitDoWhileLoop(DoWhileLoopTree node, BreakContext cxt) { cxt.loopDepth++; try { return node.getStatement().accept(this, cxt).or(NEVER_EXITS); } finally { cxt.loopDepth--; } }
@Override public Choice<State<JCDoWhileLoop>> visitDoWhileLoop(final DoWhileLoopTree node, State<?> state) { return chooseSubtrees( state, s -> unifyStatement(node.getStatement(), s), s -> unifyExpression(node.getCondition(), s), maker()::DoLoop); }
@Override @Nullable public Choice<Unifier> visitDoWhileLoop(DoWhileLoopTree loop, @Nullable Unifier unifier) { return getStatement() .unify(loop.getStatement(), unifier) .thenChoose(unifications(getCondition(), loop.getCondition())); }
@Override public boolean matches(TryTree tryTree, VisitorState state) { return ASTHelpers.findEnclosingNode(state.getPath(), DoWhileLoopTree.class) != null || ASTHelpers.findEnclosingNode(state.getPath(), EnhancedForLoopTree.class) != null || ASTHelpers.findEnclosingNode(state.getPath(), WhileLoopTree.class) != null || ASTHelpers.findEnclosingNode(state.getPath(), ForLoopTree.class) != null; }
@Override public PurityResult visitDoWhileLoop(DoWhileLoopTree node, PurityResult p) { PurityResult r = scan(node.getStatement(), p); r = scan(node.getCondition(), r); return r; }
@Override public Void visitDoWhileLoop(DoWhileLoopTree expected, Tree actual) { Optional<DoWhileLoopTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } scan(expected.getCondition(), other.get().getCondition()); scan(expected.getStatement(), other.get().getStatement()); return null; }
@Override public Void visitDoWhileLoop(DoWhileLoopTree node, EnumSet<UseTypes> p) { scan(node.getStatement(), p); scan(node.getCondition(), EnumSet.of(UseTypes.READ)); return null; }
private List<int[]> detectBreakOrContinueTarget(CompilationInfo info, Document document, TreePath breakOrContinue, int caretPosition) { List<int[]> result = new ArrayList<int[]>(); StatementTree target = info.getTreeUtilities().getBreakContinueTarget(breakOrContinue); if (target == null) return null; TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language()); ts.move((int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), target)); if (ts.moveNext()) { result.add(new int[] {ts.offset(), ts.offset() + ts.token().length()}); } StatementTree statement = target.getKind() == Kind.LABELED_STATEMENT ? ((LabeledStatementTree) target).getStatement() : target; Tree block = null; switch (statement.getKind()) { case SWITCH: block = statement; break; case WHILE_LOOP: if (((WhileLoopTree) statement).getStatement().getKind() == Kind.BLOCK) block = ((WhileLoopTree) statement).getStatement(); break; case FOR_LOOP: if (((ForLoopTree) statement).getStatement().getKind() == Kind.BLOCK) block = ((ForLoopTree) statement).getStatement(); break; case ENHANCED_FOR_LOOP: if (((EnhancedForLoopTree) statement).getStatement().getKind() == Kind.BLOCK) block = ((EnhancedForLoopTree) statement).getStatement(); break; case DO_WHILE_LOOP: if (((DoWhileLoopTree) statement).getStatement().getKind() == Kind.BLOCK) block = ((DoWhileLoopTree) statement).getStatement(); break; } if (block != null) { ts.move((int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), block)); if (ts.movePrevious() && ts.token().id() == JavaTokenId.RBRACE) { result.add(new int[] {ts.offset(), ts.offset() + ts.token().length()}); } } if (target.getKind() == Kind.LABELED_STATEMENT && isIn(caretPosition, Utilities.findIdentifierSpan(info, document, breakOrContinue))) { result.addAll(detectLabel(info, document, info.getTrees().getPath(info.getCompilationUnit(), target))); } return result; }
@Override public Boolean visitDoWhileLoop(DoWhileLoopTree node, Element p) { return null; }
@Override public Object visitDoWhileLoop(DoWhileLoopTree node, Object p) { loopCount++; return super.visitDoWhileLoop(node, p); }
@Override public Object visitDoWhileLoop(DoWhileLoopTree node, Object p) { statements++; return super.visitDoWhileLoop(node, p); }
@Hint(displayName="#LBL_Braces_DoWhile", description="#DSC_Braces_DoWhile", category="braces", id=BRACES_ID + "DO_WHILE_LOOP", enabled=false, suppressWarnings={"", "ControlFlowStatementWithoutBraces"}) @TriggerTreeKind(Tree.Kind.DO_WHILE_LOOP) public static ErrorDescription checkDoWhile(HintContext ctx) { DoWhileLoopTree dwlt = (DoWhileLoopTree) ctx.getPath().getLeaf(); return checkStatement(ctx, "LBL_Braces_DoWhile", dwlt.getStatement(), ctx.getPath()); }
@Override protected void performRewrite(TransformationContext ctx) { WorkingCopy copy = ctx.getWorkingCopy(); TreePath path = ctx.getPath(); if ( path != null ) { TreeMaker make = copy.getTreeMaker(); Tree oldTree = path.getLeaf(); oldTree = GeneratorUtilities.get(copy).importComments(oldTree, copy.getCompilationUnit()); switch( oldTree.getKind() ) { case FOR_LOOP: ForLoopTree oldFor = (ForLoopTree)oldTree; StatementTree oldBlock = oldFor.getStatement(); BlockTree newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); break; case ENHANCED_FOR_LOOP: EnhancedForLoopTree oldEnhancedFor = (EnhancedForLoopTree)oldTree; oldBlock = oldEnhancedFor.getStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); break; case WHILE_LOOP: WhileLoopTree oldWhile = (WhileLoopTree)oldTree; oldBlock = oldWhile.getStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); break; case DO_WHILE_LOOP: DoWhileLoopTree oldDoWhile = (DoWhileLoopTree)oldTree; oldBlock = oldDoWhile.getStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); break; case IF: IfTree oldIf = (IfTree)oldTree; if ( fixThen ) { oldBlock = oldIf.getThenStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); } if ( fixElse ) { oldBlock = oldIf.getElseStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); } } } }
public Boolean visitContinue(ContinueTree node, ConstructorData p) { StatementTree loop = info.getTreeUtilities().getBreakContinueTarget(getCurrentPath()); if (loop == null) { super.visitContinue(node, p); return null; } Tree resumePoint; if (loop.getKind() == Kind.LABELED_STATEMENT) { loop = ((LabeledStatementTree) loop).getStatement(); } switch (loop.getKind()) { case WHILE_LOOP: resumePoint = ((WhileLoopTree) loop).getCondition(); break; case FOR_LOOP: { ForLoopTree flt = (ForLoopTree)loop; resumePoint = null; if (flt.getUpdate() != null && !flt.getUpdate().isEmpty()) { // resume will react on the 1st Tree of the update statement (always processed left to right) resumePoint = flt.getUpdate().get(0); } if (resumePoint == null) { resumePoint = flt.getCondition(); } if (resumePoint == null) { resumePoint = flt.getStatement(); } } break; case DO_WHILE_LOOP: resumePoint = ((DoWhileLoopTree) loop).getCondition(); break; case ENHANCED_FOR_LOOP: resumePoint = ((EnhancedForLoopTree) loop).getStatement(); break; default: resumePoint = null; break; } if (resumePoint != null) { recordResume(resumeBefore, resumePoint, variable2State); } variable2State = new HashMap< Element, State>(); super.visitContinue(node, p); return null; }
@Override public Boolean visitDoWhileLoop(DoWhileLoopTree node, Void p) { return scan(node.getStatement(), p); }
@Override public Boolean visitDoWhileLoop(DoWhileLoopTree node, EvaluationContext p) { return Boolean.FALSE; }
@Override public Void visitDoWhileLoop(DoWhileLoopTree node, Map<ErrorDescription, Integer> p) { if (!isUnsafe) addError(node, ERR_NO_LOOPS, p); return super.visitDoWhileLoop(node, p); }
@Override public Integer visitDoWhileLoop(DoWhileLoopTree node, Integer p) { return super.visitDoWhileLoop(node, p) + 1; }
@Override public R visitDoWhileLoop(DoWhileLoopTree dwlt, P p) { return null; }
@Override public List<T> visitDoWhileLoop(DoWhileLoopTree node, T p) { return checkForCriteria(node); }