Java Code Examples for com.sun.source.tree.ForLoopTree#getUpdate()

The following examples show how to use com.sun.source.tree.ForLoopTree#getUpdate() . 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: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@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 3
Source File: Flow.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 4
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@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 5
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@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;
}