Java 类org.eclipse.jdt.core.dom.ForStatement 实例源码

项目:eclipse.jdt.ls    文件:NecessaryParenthesesChecker.java   
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
    if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
        // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ...
        return false;
    }
    if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
            || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
            || locationInParent == ReturnStatement.EXPRESSION_PROPERTY
            || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
            || locationInParent == ForStatement.EXPRESSION_PROPERTY
            || locationInParent == WhileStatement.EXPRESSION_PROPERTY
            || locationInParent == DoStatement.EXPRESSION_PROPERTY
            || locationInParent == AssertStatement.EXPRESSION_PROPERTY
            || locationInParent == AssertStatement.MESSAGE_PROPERTY
            || locationInParent == IfStatement.EXPRESSION_PROPERTY
            || locationInParent == SwitchStatement.EXPRESSION_PROPERTY
            || locationInParent == SwitchCase.EXPRESSION_PROPERTY
            || locationInParent == ArrayAccess.INDEX_PROPERTY
            || locationInParent == ThrowStatement.EXPRESSION_PROPERTY
            || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY
            || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        return false;
    }
    return true;
}
项目:pandionj    文件:VarParser.java   
private BlockInfo.Type getBlockType(ASTNode node) {
    if(node instanceof TypeDeclaration)
        return BlockInfo.Type.TYPE;

    else if(node instanceof MethodDeclaration)
        return BlockInfo.Type.METHOD;

    else if(node instanceof WhileStatement)
        return BlockInfo.Type.WHILE;

    else if(node instanceof ForStatement)
        return BlockInfo.Type.FOR;

    else if(node instanceof IfStatement)
        return BlockInfo.Type.IF;
    else
        return BlockInfo.Type.OTHER;
}
项目:o3smeasures-tool    文件:WeightMethodsPerClassVisitor.java   
/**
 * Method that check statement type.
 * @author Mariana Azevedo
 * @since 13/07/2014
 * @param itStatement
 */
private void getStatementType(Object itStatement) {
    if (itStatement instanceof CatchClause){
        this.visitor.visit((CatchClause)itStatement);
    }else if (itStatement instanceof ForStatement){
        this.visitor.visit((ForStatement)itStatement);
    }else if (itStatement instanceof IfStatement){
        this.visitor.visit((IfStatement)itStatement);
    }else if (itStatement instanceof WhileStatement){
        this.visitor.visit((WhileStatement)itStatement);
    }else if (itStatement instanceof TryStatement){
        this.visitor.visit((TryStatement)itStatement);
    }else if (itStatement instanceof ConditionalExpression){
        this.visitor.visit((ConditionalExpression)itStatement);
    }else if (itStatement instanceof SwitchCase){
        this.visitor.visit((SwitchCase)itStatement);
    }else if (itStatement instanceof DoStatement){
        this.visitor.visit((DoStatement)itStatement);
    }else if (itStatement instanceof ExpressionStatement){
        this.visitor.visit((ExpressionStatement)itStatement);
    }
}
项目:eclipse.jdt.ls    文件:ExtractMethodAnalyzer.java   
private Statement getParentLoopBody(ASTNode node) {
    Statement stmt = null;
    ASTNode start = node;
    while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
        start = start.getParent();
    }
    if (start instanceof ForStatement) {
        stmt = ((ForStatement) start).getBody();
    } else if (start instanceof DoStatement) {
        stmt = ((DoStatement) start).getBody();
    } else if (start instanceof WhileStatement) {
        stmt = ((WhileStatement) start).getBody();
    } else if (start instanceof EnhancedForStatement) {
        stmt = ((EnhancedForStatement) start).getBody();
    }
    if (start != null && start.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
        fEnclosingLoopLabel = labeledStatement.getLabel();
    }
    return stmt;
}
项目:eclipse.jdt.ls    文件:StatementAnalyzer.java   
@Override
public void endVisit(ForStatement node) {
    ASTNode[] selectedNodes = getSelectedNodes();
    if (doAfterValidation(node, selectedNodes)) {
        boolean containsExpression = contains(selectedNodes, node.getExpression());
        boolean containsUpdaters = contains(selectedNodes, node.updaters());
        if (contains(selectedNodes, node.initializers()) && containsExpression) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_initializer_expression);
        } else if (containsExpression && containsUpdaters) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_expression_updater);
        } else if (containsUpdaters && contains(selectedNodes, node.getBody())) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_updater_body);
        }
    }
    super.endVisit(node);
}
项目:che    文件:ConvertForLoopQuickFixTest.java   
private static ForStatement getForStatement(ICompilationUnit cu) {
  CompilationUnit ast =
      SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, new NullProgressMonitor());

  final ForStatement[] statement = new ForStatement[1];
  ast.accept(
      new GenericVisitor() {
        protected boolean visitNode(ASTNode node) {
          if (node instanceof ForStatement) {
            statement[0] = (ForStatement) node;
            return false;
          } else {
            return super.visitNode(node);
          }
        }
      });

  return statement[0];
}
项目:che    文件:NecessaryParenthesesChecker.java   
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
  if (locationInParent instanceof ChildListPropertyDescriptor
      && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
    // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation
    // ...
    return false;
  }
  if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
      || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
      || locationInParent == ReturnStatement.EXPRESSION_PROPERTY
      || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
      || locationInParent == ForStatement.EXPRESSION_PROPERTY
      || locationInParent == WhileStatement.EXPRESSION_PROPERTY
      || locationInParent == DoStatement.EXPRESSION_PROPERTY
      || locationInParent == AssertStatement.EXPRESSION_PROPERTY
      || locationInParent == AssertStatement.MESSAGE_PROPERTY
      || locationInParent == IfStatement.EXPRESSION_PROPERTY
      || locationInParent == SwitchStatement.EXPRESSION_PROPERTY
      || locationInParent == SwitchCase.EXPRESSION_PROPERTY
      || locationInParent == ArrayAccess.INDEX_PROPERTY
      || locationInParent == ThrowStatement.EXPRESSION_PROPERTY
      || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY
      || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
    return false;
  }
  return true;
}
项目:che    文件:ConvertLoopFix.java   
private ConvertLoopOperation getConvertOperation(ForStatement node) {

      Collection<String> usedNamesCollection = fUsedNames.values();
      String[] usedNames = usedNamesCollection.toArray(new String[usedNamesCollection.size()]);
      ConvertLoopOperation convertForLoopOperation =
          new ConvertForLoopOperation(node, usedNames, fMakeFinal);
      if (convertForLoopOperation.satisfiesPreconditions().isOK()) {
        if (fFindForLoopsToConvert) {
          fUsedNames.put(node, convertForLoopOperation.getIntroducedVariableName());
          return convertForLoopOperation;
        }
      } else if (fConvertIterableForLoops) {
        ConvertLoopOperation iterableConverter =
            new ConvertIterableLoopOperation(node, usedNames, fMakeFinal);
        if (iterableConverter.satisfiesPreconditions().isOK()) {
          fUsedNames.put(node, iterableConverter.getIntroducedVariableName());
          return iterableConverter;
        }
      }

      return null;
    }
项目:che    文件:SourceProvider.java   
private boolean isSingleControlStatementWithoutBlock() {
  List<Statement> statements = fDeclaration.getBody().statements();
  int size = statements.size();
  if (size != 1) return false;
  Statement statement = statements.get(size - 1);
  int nodeType = statement.getNodeType();
  if (nodeType == ASTNode.IF_STATEMENT) {
    IfStatement ifStatement = (IfStatement) statement;
    return !(ifStatement.getThenStatement() instanceof Block)
        && !(ifStatement.getElseStatement() instanceof Block);
  } else if (nodeType == ASTNode.FOR_STATEMENT) {
    return !(((ForStatement) statement).getBody() instanceof Block);
  } else if (nodeType == ASTNode.WHILE_STATEMENT) {
    return !(((WhileStatement) statement).getBody() instanceof Block);
  }
  return false;
}
项目:che    文件:ExtractMethodAnalyzer.java   
private Statement getParentLoopBody(ASTNode node) {
  Statement stmt = null;
  ASTNode start = node;
  while (start != null
      && !(start instanceof ForStatement)
      && !(start instanceof DoStatement)
      && !(start instanceof WhileStatement)
      && !(start instanceof EnhancedForStatement)
      && !(start instanceof SwitchStatement)) {
    start = start.getParent();
  }
  if (start instanceof ForStatement) {
    stmt = ((ForStatement) start).getBody();
  } else if (start instanceof DoStatement) {
    stmt = ((DoStatement) start).getBody();
  } else if (start instanceof WhileStatement) {
    stmt = ((WhileStatement) start).getBody();
  } else if (start instanceof EnhancedForStatement) {
    stmt = ((EnhancedForStatement) start).getBody();
  }
  if (start != null && start.getParent() instanceof LabeledStatement) {
    LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
    fEnclosingLoopLabel = labeledStatement.getLabel();
  }
  return stmt;
}
项目:che    文件:StatementAnalyzer.java   
@Override
public void endVisit(ForStatement node) {
  ASTNode[] selectedNodes = getSelectedNodes();
  if (doAfterValidation(node, selectedNodes)) {
    boolean containsExpression = contains(selectedNodes, node.getExpression());
    boolean containsUpdaters = contains(selectedNodes, node.updaters());
    if (contains(selectedNodes, node.initializers()) && containsExpression) {
      invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_initializer_expression);
    } else if (containsExpression && containsUpdaters) {
      invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_expression_updater);
    } else if (containsUpdaters && contains(selectedNodes, node.getBody())) {
      invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_updater_body);
    }
  }
  super.endVisit(node);
}
项目:che    文件:GenerateForLoopAssistProposal.java   
/**
 * Helper to generate an iterator based <code>for</code> loop to iterate over an {@link Iterable}.
 *
 * @param ast the {@link AST} instance to rewrite the loop to
 * @return the complete {@link ASTRewrite} object
 */
private ASTRewrite generateIteratorBasedForRewrite(AST ast) {
  ASTRewrite rewrite = ASTRewrite.create(ast);
  ForStatement loopStatement = ast.newForStatement();

  ITypeBinding loopOverType = extractElementType(ast);

  SimpleName loopVariableName =
      resolveLinkedVariableNameWithProposals(rewrite, "iterator", null, true); // $NON-NLS-1$
  loopStatement.initializers().add(getIteratorBasedForInitializer(rewrite, loopVariableName));

  MethodInvocation loopExpression = ast.newMethodInvocation();
  loopExpression.setName(ast.newSimpleName("hasNext")); // $NON-NLS-1$
  SimpleName expressionName = ast.newSimpleName(loopVariableName.getIdentifier());
  addLinkedPosition(
      rewrite.track(expressionName), LinkedPositionGroup.NO_STOP, expressionName.getIdentifier());
  loopExpression.setExpression(expressionName);

  loopStatement.setExpression(loopExpression);

  Block forLoopBody = ast.newBlock();
  Assignment assignResolvedVariable =
      getIteratorBasedForBodyAssignment(rewrite, loopOverType, loopVariableName);
  forLoopBody.statements().add(ast.newExpressionStatement(assignResolvedVariable));
  forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));

  loopStatement.setBody(forLoopBody);

  rewrite.replace(fCurrentNode, loopStatement, null);

  return rewrite;
}
项目:che    文件:GenerateForLoopAssistProposal.java   
/**
 * Helper to generate an index based <code>for</code> loop to iterate over a {@link List}
 * implementation.
 *
 * @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
 * @return an applicable {@link ASTRewrite} instance
 */
private ASTRewrite generateIndexBasedForRewrite(AST ast) {
  ASTRewrite rewrite = ASTRewrite.create(ast);

  ForStatement loopStatement = ast.newForStatement();
  SimpleName loopVariableName =
      resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); // $NON-NLS-1$
  loopStatement.initializers().add(getForInitializer(ast, loopVariableName));

  MethodInvocation listSizeExpression = ast.newMethodInvocation();
  listSizeExpression.setName(ast.newSimpleName("size")); // $NON-NLS-1$
  Expression listExpression = (Expression) rewrite.createCopyTarget(fCurrentExpression);
  listSizeExpression.setExpression(listExpression);

  loopStatement.setExpression(
      getLinkedInfixExpression(
          rewrite,
          loopVariableName.getIdentifier(),
          listSizeExpression,
          InfixExpression.Operator.LESS));
  loopStatement
      .updaters()
      .add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));

  Block forLoopBody = ast.newBlock();
  forLoopBody
      .statements()
      .add(ast.newExpressionStatement(getIndexBasedForBodyAssignment(rewrite, loopVariableName)));
  forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
  loopStatement.setBody(forLoopBody);
  rewrite.replace(fCurrentNode, loopStatement, null);

  return rewrite;
}
项目:txtUML    文件:LoopFragment.java   
@Override
public boolean preNext(Statement curElement) {
    switch (curElement.getNodeType()) {
    case ASTNode.WHILE_STATEMENT:
        exportWhile((WhileStatement) curElement);
        break;
    case ASTNode.FOR_STATEMENT:
        exportFor((ForStatement) curElement);
        break;
    case ASTNode.ENHANCED_FOR_STATEMENT:
        exportForEach((EnhancedForStatement) curElement);
        break;
    case ASTNode.DO_STATEMENT:
        exportDoWhileStatement((DoStatement) curElement);
        break;
    }

    return true;
}
项目:Eclipse-Postfix-Code-Completion    文件:SourceProvider.java   
private boolean isSingleControlStatementWithoutBlock() {
    List<Statement> statements= fDeclaration.getBody().statements();
    int size= statements.size();
    if (size != 1)
        return false;
    Statement statement= statements.get(size - 1);
    int nodeType= statement.getNodeType();
    if (nodeType == ASTNode.IF_STATEMENT) {
        IfStatement ifStatement= (IfStatement) statement;
        return !(ifStatement.getThenStatement() instanceof Block)
            && !(ifStatement.getElseStatement() instanceof Block);
    } else if (nodeType == ASTNode.FOR_STATEMENT) {
        return !(((ForStatement)statement).getBody() instanceof Block);
    } else if (nodeType == ASTNode.WHILE_STATEMENT) {
        return !(((WhileStatement)statement).getBody() instanceof Block);
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion    文件:ExtractMethodAnalyzer.java   
private Statement getParentLoopBody(ASTNode node) {
    Statement stmt= null;
    ASTNode start= node;
    while (start != null
            && !(start instanceof ForStatement)
            && !(start instanceof DoStatement)
            && !(start instanceof WhileStatement)
            && !(start instanceof EnhancedForStatement)
            && !(start instanceof SwitchStatement)) {
        start= start.getParent();
    }
    if (start instanceof ForStatement) {
        stmt= ((ForStatement)start).getBody();
    } else if (start instanceof DoStatement) {
        stmt= ((DoStatement)start).getBody();
    } else if (start instanceof WhileStatement) {
        stmt= ((WhileStatement)start).getBody();
    } else if (start instanceof EnhancedForStatement) {
        stmt= ((EnhancedForStatement)start).getBody();
    }
    if (start != null && start.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement= (LabeledStatement)start.getParent();
        fEnclosingLoopLabel= labeledStatement.getLabel();
    }
    return stmt;
}
项目:Eclipse-Postfix-Code-Completion    文件:StatementAnalyzer.java   
@Override
public void endVisit(ForStatement node) {
    ASTNode[] selectedNodes= getSelectedNodes();
    if (doAfterValidation(node, selectedNodes)) {
        boolean containsExpression= contains(selectedNodes, node.getExpression());
        boolean containsUpdaters= contains(selectedNodes, node.updaters());
        if (contains(selectedNodes, node.initializers()) && containsExpression) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_initializer_expression);
        } else if (containsExpression && containsUpdaters) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_expression_updater);
        } else if (containsUpdaters && contains(selectedNodes, node.getBody())) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_updater_body);
        }
    }
    super.endVisit(node);
}
项目:Eclipse-Postfix-Code-Completion    文件:NecessaryParenthesesChecker.java   
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
    if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
        // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ...
        return false;
    }
    if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
            || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
            || locationInParent == ReturnStatement.EXPRESSION_PROPERTY
            || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
            || locationInParent == ForStatement.EXPRESSION_PROPERTY
            || locationInParent == WhileStatement.EXPRESSION_PROPERTY
            || locationInParent == DoStatement.EXPRESSION_PROPERTY
            || locationInParent == AssertStatement.EXPRESSION_PROPERTY
            || locationInParent == AssertStatement.MESSAGE_PROPERTY
            || locationInParent == IfStatement.EXPRESSION_PROPERTY
            || locationInParent == SwitchStatement.EXPRESSION_PROPERTY
            || locationInParent == SwitchCase.EXPRESSION_PROPERTY
            || locationInParent == ArrayAccess.INDEX_PROPERTY
            || locationInParent == ThrowStatement.EXPRESSION_PROPERTY
            || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY
            || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        return false;
    }
    return true;
}
项目:Eclipse-Postfix-Code-Completion    文件:ConvertLoopFix.java   
private ConvertLoopOperation getConvertOperation(ForStatement node) {

            Collection<String> usedNamesCollection= fUsedNames.values();
            String[] usedNames= usedNamesCollection.toArray(new String[usedNamesCollection.size()]);
            ConvertLoopOperation convertForLoopOperation= new ConvertForLoopOperation(node, usedNames, fMakeFinal);
            if (convertForLoopOperation.satisfiesPreconditions().isOK()) {
                if (fFindForLoopsToConvert) {
                    fUsedNames.put(node, convertForLoopOperation.getIntroducedVariableName());
                    return convertForLoopOperation;
                }
            } else if (fConvertIterableForLoops) {
                ConvertLoopOperation iterableConverter= new ConvertIterableLoopOperation(node, usedNames, fMakeFinal);
                if (iterableConverter.satisfiesPreconditions().isOK()) {
                    fUsedNames.put(node, iterableConverter.getIntroducedVariableName());
                    return iterableConverter;
                }
            }

            return null;
        }
项目:Eclipse-Postfix-Code-Completion    文件:QuickAssistProcessor.java   
private static boolean getConvertForLoopProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ForStatement forStatement= getEnclosingForStatementHeader(node);
    if (forStatement == null)
        return false;

    if (resultingCollections == null)
        return true;

    IProposableFix fix= ConvertLoopFix.createConvertForLoopToEnhancedFix(context.getASTRoot(), forStatement);
    if (fix == null)
        return false;

    Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    Map<String, String> options= new HashMap<String, String>();
    options.put(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED, CleanUpOptions.TRUE);
    ICleanUp cleanUp= new ConvertLoopCleanUp(options);
    FixCorrectionProposal proposal= new FixCorrectionProposal(fix, cleanUp, IProposalRelevance.CONVERT_FOR_LOOP_TO_ENHANCED, image, context);
    proposal.setCommandId(CONVERT_FOR_LOOP_ID);

    resultingCollections.add(proposal);
    return true;
}
项目:Eclipse-Postfix-Code-Completion    文件:QuickAssistProcessor.java   
private static boolean getConvertIterableLoopProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ForStatement forStatement= getEnclosingForStatementHeader(node);
    if (forStatement == null)
        return false;

    if (resultingCollections == null)
        return true;

    IProposableFix fix= ConvertLoopFix.createConvertIterableLoopToEnhancedFix(context.getASTRoot(), forStatement);
    if (fix == null)
        return false;

    Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    Map<String, String> options= new HashMap<String, String>();
    options.put(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED, CleanUpOptions.TRUE);
    ICleanUp cleanUp= new ConvertLoopCleanUp(options);
    FixCorrectionProposal proposal= new FixCorrectionProposal(fix, cleanUp, IProposalRelevance.CONVERT_ITERABLE_LOOP_TO_ENHANCED, image, context);
    proposal.setCommandId(CONVERT_FOR_LOOP_ID);

    resultingCollections.add(proposal);
    return true;
}
项目:Eclipse-Postfix-Code-Completion    文件:AdvancedQuickAssistProcessor.java   
private static boolean isLastStatementInEnclosingMethodOrLambda(Statement statement) {
    ASTNode currentStructure= statement;
    ASTNode currentParent= statement.getParent();
    while (!(currentParent instanceof MethodDeclaration || currentParent instanceof LambdaExpression)) {
        // should not be in a loop
        if (currentParent instanceof ForStatement || currentParent instanceof EnhancedForStatement
                || currentParent instanceof WhileStatement || currentParent instanceof DoStatement) {
            return false;
        }
        if (currentParent instanceof Block) {
            Block parentBlock= (Block) currentParent;
            if (parentBlock.statements().indexOf(currentStructure) != parentBlock.statements().size() - 1) { // not last statement in the block
                return false;
            }
        }
        currentStructure= currentParent;
        currentParent= currentParent.getParent();
    }
    return true;
}
项目:Eclipse-Postfix-Code-Completion    文件:GenerateForLoopAssistProposal.java   
/**
 * Helper to generate an index based <code>for</code> loop to iterate over an array.
 * 
 * @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
 * @return an applicable {@link ASTRewrite} instance
 */
private ASTRewrite generateForRewrite(AST ast) {
    ASTRewrite rewrite= ASTRewrite.create(ast);

    ForStatement loopStatement= ast.newForStatement();
    SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); //$NON-NLS-1$
    loopStatement.initializers().add(getForInitializer(ast, loopVariableName));

    FieldAccess getArrayLengthExpression= ast.newFieldAccess();
    getArrayLengthExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
    getArrayLengthExpression.setName(ast.newSimpleName("length")); //$NON-NLS-1$

    loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), getArrayLengthExpression, InfixExpression.Operator.LESS));
    loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));

    Block forLoopBody= ast.newBlock();
    forLoopBody.statements().add(ast.newExpressionStatement(getForBodyAssignment(rewrite, loopVariableName)));
    forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
    loopStatement.setBody(forLoopBody);
    rewrite.replace(fCurrentNode, loopStatement, null);

    return rewrite;
}
项目:Eclipse-Postfix-Code-Completion    文件:GenerateForLoopAssistProposal.java   
/**
 * Helper to generate an index based <code>for</code> loop to iterate over a {@link List}
 * implementation.
 * 
 * @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
 * @return an applicable {@link ASTRewrite} instance
 */
private ASTRewrite generateIndexBasedForRewrite(AST ast) {
    ASTRewrite rewrite= ASTRewrite.create(ast);

    ForStatement loopStatement= ast.newForStatement();
    SimpleName loopVariableName= resolveLinkedVariableNameWithProposals(rewrite, "int", null, true); //$NON-NLS-1$
    loopStatement.initializers().add(getForInitializer(ast, loopVariableName));

    MethodInvocation listSizeExpression= ast.newMethodInvocation();
    listSizeExpression.setName(ast.newSimpleName("size")); //$NON-NLS-1$
    Expression listExpression= (Expression) rewrite.createCopyTarget(fCurrentExpression);
    listSizeExpression.setExpression(listExpression);

    loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), listSizeExpression, InfixExpression.Operator.LESS));
    loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));

    Block forLoopBody= ast.newBlock();
    forLoopBody.statements().add(ast.newExpressionStatement(getIndexBasedForBodyAssignment(rewrite, loopVariableName)));
    forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
    loopStatement.setBody(forLoopBody);
    rewrite.replace(fCurrentNode, loopStatement, null);

    return rewrite;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:InlineTempRefactoring.java   
private RefactoringStatus checkSelection(VariableDeclaration decl) {
    ASTNode parent= decl.getParent();
    if (parent instanceof MethodDeclaration) {
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_method_parameter);
    }

    if (parent instanceof CatchClause) {
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_exceptions_declared);
    }

    if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == ForStatement.INITIALIZERS_PROPERTY) {
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_for_initializers);
    }

    if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_resource_in_try_with_resources);
    }

    if (decl.getInitializer() == null) {
        String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_not_initialized, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier()));
        return RefactoringStatus.createFatalErrorStatus(message);
    }

    return checkAssignments(decl);
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:SourceProvider.java   
private boolean isSingleControlStatementWithoutBlock() {
    List<Statement> statements= fDeclaration.getBody().statements();
    int size= statements.size();
    if (size != 1)
        return false;
    Statement statement= statements.get(size - 1);
    int nodeType= statement.getNodeType();
    if (nodeType == ASTNode.IF_STATEMENT) {
        IfStatement ifStatement= (IfStatement) statement;
        return !(ifStatement.getThenStatement() instanceof Block)
            && !(ifStatement.getElseStatement() instanceof Block);
    } else if (nodeType == ASTNode.FOR_STATEMENT) {
        return !(((ForStatement)statement).getBody() instanceof Block);
    } else if (nodeType == ASTNode.WHILE_STATEMENT) {
        return !(((WhileStatement)statement).getBody() instanceof Block);
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:ExtractMethodAnalyzer.java   
private Statement getParentLoopBody(ASTNode node) {
    Statement stmt= null;
    ASTNode start= node;
    while (start != null
            && !(start instanceof ForStatement)
            && !(start instanceof DoStatement)
            && !(start instanceof WhileStatement)
            && !(start instanceof EnhancedForStatement)
            && !(start instanceof SwitchStatement)) {
        start= start.getParent();
    }
    if (start instanceof ForStatement) {
        stmt= ((ForStatement)start).getBody();
    } else if (start instanceof DoStatement) {
        stmt= ((DoStatement)start).getBody();
    } else if (start instanceof WhileStatement) {
        stmt= ((WhileStatement)start).getBody();
    } else if (start instanceof EnhancedForStatement) {
        stmt= ((EnhancedForStatement)start).getBody();
    }
    if (start != null && start.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement= (LabeledStatement)start.getParent();
        fEnclosingLoopLabel= labeledStatement.getLabel();
    }
    return stmt;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:StatementAnalyzer.java   
@Override
public void endVisit(ForStatement node) {
    ASTNode[] selectedNodes= getSelectedNodes();
    if (doAfterValidation(node, selectedNodes)) {
        boolean containsExpression= contains(selectedNodes, node.getExpression());
        boolean containsUpdaters= contains(selectedNodes, node.updaters());
        if (contains(selectedNodes, node.initializers()) && containsExpression) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_initializer_expression);
        } else if (containsExpression && containsUpdaters) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_expression_updater);
        } else if (containsUpdaters && contains(selectedNodes, node.getBody())) {
            invalidSelection(RefactoringCoreMessages.StatementAnalyzer_for_updater_body);
        }
    }
    super.endVisit(node);
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:NecessaryParenthesesChecker.java   
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
    if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
        // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ...
        return false;
    }
    if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
            || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
            || locationInParent == ReturnStatement.EXPRESSION_PROPERTY
            || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
            || locationInParent == ForStatement.EXPRESSION_PROPERTY
            || locationInParent == WhileStatement.EXPRESSION_PROPERTY
            || locationInParent == DoStatement.EXPRESSION_PROPERTY
            || locationInParent == AssertStatement.EXPRESSION_PROPERTY
            || locationInParent == AssertStatement.MESSAGE_PROPERTY
            || locationInParent == IfStatement.EXPRESSION_PROPERTY
            || locationInParent == SwitchStatement.EXPRESSION_PROPERTY
            || locationInParent == SwitchCase.EXPRESSION_PROPERTY
            || locationInParent == ArrayAccess.INDEX_PROPERTY
            || locationInParent == ThrowStatement.EXPRESSION_PROPERTY
            || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY
            || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
        return false;
    }
    return true;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:ConvertLoopFix.java   
private ConvertLoopOperation getConvertOperation(ForStatement node) {

            Collection<String> usedNamesCollection= fUsedNames.values();
            String[] usedNames= usedNamesCollection.toArray(new String[usedNamesCollection.size()]);
            ConvertLoopOperation convertForLoopOperation= new ConvertForLoopOperation(node, usedNames, fMakeFinal);
            if (convertForLoopOperation.satisfiesPreconditions().isOK()) {
                if (fFindForLoopsToConvert) {
                    fUsedNames.put(node, convertForLoopOperation.getIntroducedVariableName());
                    return convertForLoopOperation;
                }
            } else if (fConvertIterableForLoops) {
                ConvertLoopOperation iterableConverter= new ConvertIterableLoopOperation(node, usedNames, fMakeFinal);
                if (iterableConverter.satisfiesPreconditions().isOK()) {
                    fUsedNames.put(node, iterableConverter.getIntroducedVariableName());
                    return iterableConverter;
                }
            }

            return null;
        }
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:ConvertForLoopOperation.java   
@Override
public IStatus satisfiesPreconditions() {
    ForStatement statement= getForStatement();
    CompilationUnit ast= (CompilationUnit)statement.getRoot();

    IJavaElement javaElement= ast.getJavaElement();
    if (javaElement == null)
        return ERROR_STATUS;

    if (!JavaModelUtil.is50OrHigher(javaElement.getJavaProject()))
        return ERROR_STATUS;

    if (!validateInitializers(statement))
        return ERROR_STATUS;

    if (!validateExpression(statement))
        return ERROR_STATUS;

    if (!validateUpdaters(statement))
        return ERROR_STATUS;

    if (!validateBody(statement))
        return ERROR_STATUS;

    return Status.OK_STATUS;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:QuickAssistProcessor.java   
private static boolean getConvertForLoopProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ForStatement forStatement= getEnclosingForStatementHeader(node);
    if (forStatement == null)
        return false;

    if (resultingCollections == null)
        return true;

    IProposableFix fix= ConvertLoopFix.createConvertForLoopToEnhancedFix(context.getASTRoot(), forStatement);
    if (fix == null)
        return false;

    Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    Map<String, String> options= new HashMap<String, String>();
    options.put(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED, CleanUpOptions.TRUE);
    ICleanUp cleanUp= new ConvertLoopCleanUp(options);
    FixCorrectionProposal proposal= new FixCorrectionProposal(fix, cleanUp, 1, image, context);
    proposal.setCommandId(CONVERT_FOR_LOOP_ID);

    resultingCollections.add(proposal);
    return true;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:QuickAssistProcessor.java   
private static boolean getConvertIterableLoopProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ForStatement forStatement= getEnclosingForStatementHeader(node);
    if (forStatement == null)
        return false;

    if (resultingCollections == null)
        return true;

    IProposableFix fix= ConvertLoopFix.createConvertIterableLoopToEnhancedFix(context.getASTRoot(), forStatement);
    if (fix == null)
        return false;

    Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    Map<String, String> options= new HashMap<String, String>();
    options.put(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED, CleanUpOptions.TRUE);
    ICleanUp cleanUp= new ConvertLoopCleanUp(options);
    FixCorrectionProposal proposal= new FixCorrectionProposal(fix, cleanUp, 1, image, context);
    proposal.setCommandId(CONVERT_FOR_LOOP_ID);

    resultingCollections.add(proposal);
    return true;
}
项目:pandionj    文件:VarParser.java   
private boolean isLoopStatement(Block block) {
    ASTNode parent = block.getParent();
    return
            parent instanceof WhileStatement ||
            parent instanceof ForStatement ||
            parent instanceof DoStatement ||
            parent instanceof EnhancedForStatement ||
            parent instanceof IfStatement;
}
项目:pandionj    文件:TestParser.java   
@Override
public boolean visit(ForStatement node) {
    writeDebugMessage(node);

    ForBodyVisitor bodyVisitor = new ForBodyVisitor();
    node.getBody().accept(bodyVisitor);
    ForExpressionVisitor expVisitor = new ForExpressionVisitor(bodyVisitor);
    node.getExpression().accept(expVisitor);

    if(!bodyVisitor.isArrayPrimitiveFigure()) {
        System.out.println("Este ciclo n�o itera sobre uma array.");
    }

    return super.visit(node);
}
项目:pandionj    文件:TestParser.java   
private void writeDebugMessage(ForStatement node) {
    numberOfForsFound++;
    System.out.println("\n\n--------------------For numero " + numberOfForsFound + "-------------------");
    System.out.println("for(" + node.initializers().toString() + "; "
                    + node.getExpression().toString() + ";  " + node.updaters().toString() + ")\n"
                    + node.getBody().toString());
}
项目:pandionj    文件:ArrayParser.java   
@Override
public boolean visit(ForStatement node) {
    Statement body = node.getBody();
    Expression condExp = node.getExpression();
    InfixExpression exp = (InfixExpression) condExp;
    System.out.println(node.initializers() + " :: " + condExp + " :: " + body);
    return super.visit(node);
}
项目:o3smeasures-tool    文件:CyclomaticComplexityVisitor.java   
/**
 * @see ASTVisitor#visit(ForStatement)
 */
@Override
public boolean visit(ForStatement node) {
    cyclomaticComplexityIndex++;
    sumCyclomaticComplexity++;
    inspectExpression(node.getExpression());
    return true;
}
项目:eclipse.jdt.ls    文件:ASTNodes.java   
/**
 * Returns true if a node at a given location is a body of a control statement. Such body nodes are
 * interesting as when replacing them, it has to be evaluates if a Block is needed instead.
 * E.g. <code> if (x) do(); -> if (x) { do1(); do2() } </code>
 *
 * @param locationInParent Location of the body node
 * @return Returns true if the location is a body node location of a control statement.
 */
public static boolean isControlStatementBody(StructuralPropertyDescriptor locationInParent) {
    return locationInParent == IfStatement.THEN_STATEMENT_PROPERTY
            || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY
            || locationInParent == ForStatement.BODY_PROPERTY
            || locationInParent == EnhancedForStatement.BODY_PROPERTY
            || locationInParent == WhileStatement.BODY_PROPERTY
            || locationInParent == DoStatement.BODY_PROPERTY;
}
项目:eclipse.jdt.ls    文件:InputFlowAnalyzer.java   
@Override
public void endVisit(ForStatement node) {
    if (skipNode(node)) {
        return;
    }
    FlowInfo initInfo = createSequential(node.initializers());
    FlowInfo conditionInfo = getFlowInfo(node.getExpression());
    FlowInfo incrementInfo = createSequential(node.updaters());
    FlowInfo actionInfo = getFlowInfo(node.getBody());
    ForFlowInfo forInfo = createFor();
    setFlowInfo(node, forInfo);
    // the for statement is the outermost loop. In this case we only have
    // to consider the increment, condition and action.
    if (node == fLoopNode) {
        forInfo.mergeIncrement(incrementInfo, fFlowContext);
        forInfo.mergeCondition(conditionInfo, fFlowContext);
        forInfo.mergeAction(actionInfo, fFlowContext);
    } else {
        // we have to merge two different cases. One if we reenter the for statement
        // immediatelly (that means we have to consider increments, condition and action)
        // and the other case if we reenter the for in the next loop of
        // the outer loop. Then we have to consider initializations, condtion and action.
        // For a conditional flow info that means:
        // (initializations | increments) & condition & action.
        GenericConditionalFlowInfo initIncr = new GenericConditionalFlowInfo();
        initIncr.merge(initInfo, fFlowContext);
        initIncr.merge(incrementInfo, fFlowContext);
        forInfo.mergeAccessModeSequential(initIncr, fFlowContext);
        forInfo.mergeCondition(conditionInfo, fFlowContext);
        forInfo.mergeAction(actionInfo, fFlowContext);
    }
    forInfo.removeLabel(null);
}