/*
  * @see ASTVisitor#visit(DoStatement)
  */
 public boolean visit(DoStatement node) {
   this.fBuffer.append("do "); // $NON-NLS-1$
   node.getBody().accept(this);
   this.fBuffer.append(" while ("); // $NON-NLS-1$
   node.getExpression().accept(this);
   this.fBuffer.append(");"); // $NON-NLS-1$
   return false;
 }
  @Override
  public boolean visit(DoStatement node) {
    boolean result = super.visit(node);

    try {
      int actionStart =
          getTokenScanner()
              .getTokenEndOffset(ITerminalSymbols.TokenNamedo, node.getStartPosition());
      if (getSelection().getOffset() == actionStart) {
        invalidSelection(
            RefactoringCoreMessages.ExtractMethodAnalyzer_after_do_keyword,
            JavaStatusContext.create(fCUnit, getSelection()));
        return false;
      }
    } catch (CoreException e) {
      // ignore
    }

    return result;
  }
Exemplo n.º 3
0
  private boolean isClosed(IDocument document, int offset, int length) {

    CompilationUnitInfo info = getCompilationUnitForMethod(document, offset);
    if (info == null) return false;

    CompilationUnit compilationUnit = null;
    try {
      ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
      parser.setSource(info.buffer);
      compilationUnit = (CompilationUnit) parser.createAST(null);
    } catch (ArrayIndexOutOfBoundsException x) {
      // work around for parser problem
      return false;
    }

    IProblem[] problems = compilationUnit.getProblems();
    for (int i = 0; i != problems.length; ++i) {
      if (problems[i].getID() == IProblem.UnmatchedBracket) return true;
    }

    final int relativeOffset = offset - info.delta;

    ASTNode node = NodeFinder.perform(compilationUnit, relativeOffset, length);

    if (length == 0) {
      while (node != null
          && (relativeOffset == node.getStartPosition()
              || relativeOffset == node.getStartPosition() + node.getLength()))
        node = node.getParent();
    }

    if (node == null) return false;

    switch (node.getNodeType()) {
      case ASTNode.BLOCK:
        return getBlockBalance(document, offset, fPartitioning) <= 0;

      case ASTNode.IF_STATEMENT:
        {
          IfStatement ifStatement = (IfStatement) node;
          Expression expression = ifStatement.getExpression();
          IRegion expressionRegion = createRegion(expression, info.delta);
          Statement thenStatement = ifStatement.getThenStatement();
          IRegion thenRegion = createRegion(thenStatement, info.delta);

          // between expression and then statement
          if (expressionRegion.getOffset() + expressionRegion.getLength() <= offset
              && offset + length <= thenRegion.getOffset()) return thenStatement != null;

          Statement elseStatement = ifStatement.getElseStatement();
          IRegion elseRegion = createRegion(elseStatement, info.delta);

          if (elseStatement != null) {
            int sourceOffset = thenRegion.getOffset() + thenRegion.getLength();
            int sourceLength = elseRegion.getOffset() - sourceOffset;
            IRegion elseToken =
                getToken(
                    document,
                    new Region(sourceOffset, sourceLength),
                    ITerminalSymbols.TokenNameelse);
            return elseToken != null
                && elseToken.getOffset() + elseToken.getLength() <= offset
                && offset + length < elseRegion.getOffset();
          }
        }
        break;

      case ASTNode.WHILE_STATEMENT:
      case ASTNode.FOR_STATEMENT:
        {
          Expression expression =
              node.getNodeType() == ASTNode.WHILE_STATEMENT
                  ? ((WhileStatement) node).getExpression()
                  : ((ForStatement) node).getExpression();
          IRegion expressionRegion = createRegion(expression, info.delta);
          Statement body =
              node.getNodeType() == ASTNode.WHILE_STATEMENT
                  ? ((WhileStatement) node).getBody()
                  : ((ForStatement) node).getBody();
          IRegion bodyRegion = createRegion(body, info.delta);

          // between expression and body statement
          if (expressionRegion.getOffset() + expressionRegion.getLength() <= offset
              && offset + length <= bodyRegion.getOffset()) return body != null;
        }
        break;

      case ASTNode.DO_STATEMENT:
        {
          DoStatement doStatement = (DoStatement) node;
          IRegion doRegion = createRegion(doStatement, info.delta);
          Statement body = doStatement.getBody();
          IRegion bodyRegion = createRegion(body, info.delta);

          if (doRegion.getOffset() + doRegion.getLength() <= offset
              && offset + length <= bodyRegion.getOffset()) return body != null;
        }
        break;
    }

    return true;
  }