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; }
/** * 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); } }
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; }
@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; }
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; }
@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; }
@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; }
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; }
@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; }
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; }
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; }
/** * @see ASTVisitor#visit(DoStatement) */ @Override public boolean visit(DoStatement node) { cyclomaticComplexityIndex++; sumCyclomaticComplexity++; inspectExpression(node.getExpression()); return true; }
/** * 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; }
@Override public void endVisit(DoStatement node) { if (skipNode(node)) { return; } DoWhileFlowInfo info = createDoWhile(); setFlowInfo(node, info); info.mergeAction(getFlowInfo(node.getBody()), fFlowContext); // No need to merge the condition. It was already considered by the InputFlowAnalyzer. info.removeLabel(null); }
@Override public void endVisit(DoStatement node) { if (skipNode(node)) { return; } DoWhileFlowInfo info = createDoWhile(); setFlowInfo(node, info); info.mergeAction(getFlowInfo(node.getBody()), fFlowContext); info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext); info.removeLabel(null); }
@Override public void endVisit(DoStatement node) { ASTNode[] selectedNodes = getSelectedNodes(); if (doAfterValidation(node, selectedNodes)) { if (contains(selectedNodes, node.getBody()) && contains(selectedNodes, node.getExpression())) { invalidSelection(RefactoringCoreMessages.StatementAnalyzer_do_body_expression); } } super.endVisit(node); }
private List<IfStatement> collectIfStatements(Statement st) { List<IfStatement> ifStatements = new ArrayList<IfStatement>(); if (st == null) { return ifStatements; } if (st.getNodeType() == ASTNode.IF_STATEMENT) { IfStatement ifSt = (IfStatement) st; ifStatements.add(ifSt); ifStatements.addAll(collectIfStatements(ifSt.getThenStatement())); ifStatements.addAll(collectIfStatements(ifSt.getElseStatement())); } else if (st.getNodeType() == ASTNode.BLOCK) { Block block = (Block) st; for (Object blockSt : block.statements()) { if (blockSt instanceof Statement) { ifStatements .addAll(collectIfStatements((Statement) blockSt)); } } } else if (st.getNodeType() == ASTNode.DO_STATEMENT) { DoStatement doSt = (DoStatement) st; ifStatements.addAll(collectIfStatements(doSt.getBody())); } else if (st.getNodeType() == ASTNode.WHILE_STATEMENT) { WhileStatement whileSt = (WhileStatement) st; ifStatements.addAll(collectIfStatements(whileSt.getBody())); } return ifStatements; }
public boolean isDangligIf() { List<Statement> statements = fDeclaration.getBody().statements(); if (statements.size() != 1) return false; ASTNode p = statements.get(0); while (true) { if (p instanceof IfStatement) { return ((IfStatement) p).getElseStatement() == null; } else { ChildPropertyDescriptor childD; if (p instanceof WhileStatement) { childD = WhileStatement.BODY_PROPERTY; } else if (p instanceof ForStatement) { childD = ForStatement.BODY_PROPERTY; } else if (p instanceof EnhancedForStatement) { childD = EnhancedForStatement.BODY_PROPERTY; } else if (p instanceof DoStatement) { childD = DoStatement.BODY_PROPERTY; } else if (p instanceof LabeledStatement) { childD = LabeledStatement.BODY_PROPERTY; } else { return false; } Statement body = (Statement) p.getStructuralProperty(childD); if (body instanceof Block) { return false; } else { p = body; } } } }
@Override public void endVisit(DoStatement node) { if (skipNode(node)) return; DoWhileFlowInfo info = createDoWhile(); setFlowInfo(node, info); info.mergeAction(getFlowInfo(node.getBody()), fFlowContext); // No need to merge the condition. It was already considered by the InputFlowAnalyzer. info.removeLabel(null); }
private void insertAt(ASTNode target, Statement declaration) { ASTRewrite rewrite = fCURewrite.getASTRewrite(); TextEditGroup groupDescription = fCURewrite.createGroupDescription( RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable); ASTNode parent = target.getParent(); StructuralPropertyDescriptor locationInParent = target.getLocationInParent(); while (locationInParent != Block.STATEMENTS_PROPERTY && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) { if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY || locationInParent == ForStatement.BODY_PROPERTY || locationInParent == EnhancedForStatement.BODY_PROPERTY || locationInParent == DoStatement.BODY_PROPERTY || locationInParent == WhileStatement.BODY_PROPERTY) { // create intermediate block if target was the body property of a control statement: Block replacement = rewrite.getAST().newBlock(); ListRewrite replacementRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY); replacementRewrite.insertFirst(declaration, null); replacementRewrite.insertLast(rewrite.createMoveTarget(target), null); rewrite.replace(target, replacement, groupDescription); return; } target = parent; parent = parent.getParent(); locationInParent = target.getLocationInParent(); } ListRewrite listRewrite = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) locationInParent); listRewrite.insertBefore(declaration, target, groupDescription); }
/** * Determines if this ASTNode is a conditional statement. * Conditional statements include: if,do,while,for,switch * @param node * @return */ public static boolean isConditional(ASTNode node){ if(node instanceof IfStatement) return true; if(node instanceof DoStatement) return true; if(node instanceof EnhancedForStatement) return true; if(node instanceof ForStatement) return true; if(node instanceof SwitchStatement) return true; if(node instanceof WhileStatement) return true; return false; }
/** * We don't really need anything in particular from this statement, * but since it has an expression and a body, we only want to * investigate the expression part to determine if it needs to * be in the slice. */ public boolean visit(DoStatement node){ if(this.options.contains(Slicer.Options.CONTROL_EXPRESSIONS_ONLY)){ /* Visit the expression part. */ node.getExpression().accept(this); /* Don't visit the children. */ return false; } else return true; }
/** * We want to track the variables from the expression only. * * TODO: Are we handling this statement type properly? */ public boolean visit(DoStatement node){ /* Visit the expression part. */ node.getExpression().accept(this); /* Don't visit the children. */ return false; }
public boolean isDangligIf() { List<Statement> statements= fDeclaration.getBody().statements(); if (statements.size() != 1) return false; ASTNode p= statements.get(0); while (true) { if (p instanceof IfStatement) { return ((IfStatement) p).getElseStatement() == null; } else { ChildPropertyDescriptor childD; if (p instanceof WhileStatement) { childD= WhileStatement.BODY_PROPERTY; } else if (p instanceof ForStatement) { childD= ForStatement.BODY_PROPERTY; } else if (p instanceof EnhancedForStatement) { childD= EnhancedForStatement.BODY_PROPERTY; } else if (p instanceof DoStatement) { childD= DoStatement.BODY_PROPERTY; } else if (p instanceof LabeledStatement) { childD= LabeledStatement.BODY_PROPERTY; } else { return false; } Statement body= (Statement) p.getStructuralProperty(childD); if (body instanceof Block) { return false; } else { p= body; } } } }
@Override public void endVisit(DoStatement node) { if (skipNode(node)) return; DoWhileFlowInfo info= createDoWhile(); setFlowInfo(node, info); info.mergeAction(getFlowInfo(node.getBody()), fFlowContext); // No need to merge the condition. It was already considered by the InputFlowAnalyzer. info.removeLabel(null); }
@Override public void endVisit(DoStatement node) { if (skipNode(node)) return; DoWhileFlowInfo info= createDoWhile(); setFlowInfo(node, info); info.mergeAction(getFlowInfo(node.getBody()), fFlowContext); info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext); info.removeLabel(null); }
private void insertAt(ASTNode target, Statement declaration) { ASTRewrite rewrite= fCURewrite.getASTRewrite(); TextEditGroup groupDescription= fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable); ASTNode parent= target.getParent(); StructuralPropertyDescriptor locationInParent= target.getLocationInParent(); while (locationInParent != Block.STATEMENTS_PROPERTY && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) { if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY || locationInParent == ForStatement.BODY_PROPERTY || locationInParent == EnhancedForStatement.BODY_PROPERTY || locationInParent == DoStatement.BODY_PROPERTY || locationInParent == WhileStatement.BODY_PROPERTY) { // create intermediate block if target was the body property of a control statement: Block replacement= rewrite.getAST().newBlock(); ListRewrite replacementRewrite= rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY); replacementRewrite.insertFirst(declaration, null); replacementRewrite.insertLast(rewrite.createMoveTarget(target), null); rewrite.replace(target, replacement, groupDescription); return; } target= parent; parent= parent.getParent(); locationInParent= target.getLocationInParent(); } ListRewrite listRewrite= rewrite.getListRewrite(parent, (ChildListPropertyDescriptor)locationInParent); listRewrite.insertBefore(declaration, target, groupDescription); }
@Override public void endVisit(DoStatement node) { ASTNode[] selectedNodes= getSelectedNodes(); if (doAfterValidation(node, selectedNodes)) { if (contains(selectedNodes, node.getBody()) && contains(selectedNodes, node.getExpression())) { invalidSelection(RefactoringCoreMessages.StatementAnalyzer_do_body_expression); } } super.endVisit(node); }
private static Expression getBooleanExpression(ASTNode node) { if (!(node instanceof Expression)) { return null; } // check if the node is a location where it can be negated StructuralPropertyDescriptor locationInParent= node.getLocationInParent(); if (locationInParent == QualifiedName.NAME_PROPERTY) { node= node.getParent(); locationInParent= node.getLocationInParent(); } while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { node= node.getParent(); locationInParent= node.getLocationInParent(); } Expression expression= (Expression) node; if (!isBoolean(expression)) { return null; } if (expression.getParent() instanceof InfixExpression) { return expression; } if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY || locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) { return expression; } return null; }