private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) { if (expression.getStartPosition() + expression.getLength() <= operatorOffset) { // add to the left res[0]= combineOperands(rewrite, res[0], expression, removeParentheses, operator); return; } if (operatorOffset <= expression.getStartPosition()) { // add to the right res[1]= combineOperands(rewrite, res[1], expression, removeParentheses, operator); return; } if (!(expression instanceof InfixExpression)) { throw new IllegalArgumentException("Cannot break up non-infix expression"); //$NON-NLS-1$ } InfixExpression infixExpression= (InfixExpression) expression; if (infixExpression.getOperator() != operator) { throw new IllegalArgumentException("Incompatible operator"); //$NON-NLS-1$ } breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res); breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res); List<Expression> extended= infixExpression.extendedOperands(); for (int i= 0; i < extended.size(); i++) { breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res); } }
private static Statement copyStatementExceptBreak(AST ast, ASTRewrite rewrite, Statement source) { if (source instanceof Block) { Block block= (Block) source; Block newBlock= ast.newBlock(); for (Iterator<Statement> iter= block.statements().iterator(); iter.hasNext();) { Statement statement= iter.next(); if (statement instanceof BreakStatement) { continue; } newBlock.statements().add(copyStatementExceptBreak(ast, rewrite, statement)); } return newBlock; } return (Statement) rewrite.createMoveTarget(source); }
ASTNode coveringNode= context.getCoveringNode(); if (coveringNode != null) { ArrayList<ASTNode> coveredNodes= getFullyCoveredNodes(context, coveringNode); return getConvertToIfReturnProposals(context, coveringNode, null) || getInverseIfProposals(context, coveringNode, null) || getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, null) || getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, null) || getInverseIfIntoContinueInLoopsProposals(context, coveringNode, null) || getInverseConditionProposals(context, coveringNode, coveredNodes, null) || getRemoveExtraParenthesesProposals(context, coveringNode, coveredNodes, null) || getAddParanoidalParenthesesProposals(context, coveredNodes, null) || getAddParenthesesForExpressionProposals(context, coveringNode, null) || getJoinAndIfStatementsProposals(context, coveringNode, null) || getSplitAndConditionProposals(context, coveringNode, null) || getJoinOrIfStatementsProposals(context, coveringNode, coveredNodes, null) || getSplitOrConditionProposals(context, coveringNode, null) || getInverseConditionalExpressionProposals(context, coveringNode, null) || getExchangeInnerAndOuterIfConditionsProposals(context, coveringNode, null) || getExchangeOperandsProposals(context, coveringNode, null) || getCastAndAssignIfStatementProposals(context, coveringNode, null) || getCombineStringProposals(context, coveringNode, null) || getPickOutStringProposals(context, coveringNode, null) || getReplaceIfElseWithConditionalProposals(context, coveringNode, null) || getReplaceConditionalWithIfElseProposals(context, coveringNode, null) || getInverseLocalVariableProposals(context, coveringNode, null) || getPushNegationDownProposals(context, coveringNode, null) || getPullNegationUpProposals(context, coveredNodes, null) || getJoinIfListInIfElseIfProposals(context, coveringNode, coveredNodes, null) || getConvertSwitchToIfProposals(context, coveringNode, null)
public boolean hasAssists(IInvocationContext context) throws CoreException { ASTNode coveringNode = context.getCoveringNode(); if (coveringNode != null) { ArrayList coveredNodes= getFullyCoveredNodes(context, coveringNode); return getInverseIfProposals(context, coveringNode, null) || getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, null) || getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, null) || getInverseIfIntoContinueInLoopsProposals(context, coveringNode, null) || getInverseConditionProposals(context, coveringNode, coveredNodes, null) || getRemoveExtraParenthesisProposals(context, coveringNode, coveredNodes, null) || getAddParanoidalParenthesisProposals(context, coveringNode, coveredNodes, null) || getJoinAndIfStatementsProposals(context, coveringNode, null) || getSplitAndConditionProposals(context, coveringNode, null) || getJoinOrIfStatementsProposals(context, coveringNode, coveredNodes, null) || getSplitOrConditionProposals(context, coveringNode, null) || getInverseConditionalExpressionProposals(context, coveringNode, null) || getExchangeInnerAndOuterIfConditionsProposals(context, coveringNode, null) || getExchangeOperandsProposals(context, coveringNode, null) || getCastAndAssignIfStatementProposals(context, coveringNode, null) || getPickOutStringProposals(context, coveringNode, null) || getReplaceIfElseWithConditionalProposals(context, coveringNode, null) || getReplaceConditionalWithIfElseProposals(context, coveringNode, null) || getInverseLocalVariableProposals(context, coveringNode, null) || getPushNegationDownProposals(context, coveringNode, null) || getPullNegationUpProposals(context, coveringNode, coveredNodes, null) || getJoinIfListInIfElseIfProposals(context, coveringNode, coveredNodes, null) || getConvertSwitchToIfProposals(context, coveringNode, null);
InfixExpression switchCaseCondition= createSwitchCaseCondition(ast, rewrite, importRewrite, switchStatement, switchCase); if (currentCondition == null) { currentCondition= switchCaseCondition; hasStopAsLastExecutableStatement = hasStopAsLastExecutableStatement(statement); Statement copyStatement= copyStatementExceptBreak(ast, rewrite, statement);
return false; int offset= isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength()); if (offset == -1) { return false; breakInfixOperationAtOperation(rewrite, topInfixExpression, andOperator, offset, true, newOperands);
int offset= isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength()); if (offset == -1) { return false; Expression rightExpression= null; InfixExpression currentExpression= infixExpression; leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator); if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) { leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator); leftExpression= combineOperands(rewrite, leftExpression, extendedOperand, false, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, extendedOperand, false, operator); leftExpression= getParenthesizedExpression(ast, leftExpression); rightExpression= getParenthesizedExpression(ast, rightExpression);
Assignment.Operator op= assignment.getOperator(); ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression())); ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression())); ifStatement.setThenStatement(createReturnExpression(rewrite, conditional.getThenExpression())); ifStatement.setElseStatement(createReturnExpression(rewrite, conditional.getElseExpression())); ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression())); ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
for (Iterator<ASTNode> iter= coveredNodes.iterator(); iter.hasNext();) { ASTNode covered= iter.next(); Expression coveredExpression= getBooleanExpression(covered); if (coveredExpression != null) { Expression inversedExpression= getInversedExpression(rewrite, coveredExpression); rewrite.replace(coveredExpression, inversedExpression, null); hasChanges= true;
int offset= isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength()); if (offset == -1) { return false; Expression rightExpression = null; InfixExpression currentExpression = infixExpression; leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), true, operator); if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) { leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), true, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), true, operator); leftExpression= combineOperands(rewrite, leftExpression, extendedOperand, true, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, extendedOperand, true, operator);
for (Iterator iter = coveredNodes.iterator(); iter.hasNext();) { ASTNode covered = (ASTNode) iter.next(); Expression coveredExpression= getBooleanExpression(covered); if (coveredExpression != null) { Expression inversedExpression = getInversedBooleanExpression(rewrite, coveredExpression); rewrite.replace(coveredExpression, inversedExpression, null); hasChanges = true;
private static boolean getConvertSwitchToIfProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { if (!(covering instanceof SwitchStatement)) { return false; } // we could produce quick assist (if all 'case' statements end with 'break') if (resultingCollections == null) { return true; } if (!getConvertSwitchToIfProposals(context, covering, resultingCollections, false)) return false; return getConvertSwitchToIfProposals(context, covering, resultingCollections, true); }
ASTNode coveringNode = context.getCoveringNode(); if (coveringNode != null) { ArrayList coveredNodes = getFullyCoveredNodes(context, coveringNode); ArrayList resultingCollections = new ArrayList(); if (noErrorsAtLocation(locations)) { getInverseIfProposals(context, coveringNode, resultingCollections); getIfReturnIntoIfElseAtEndOfVoidMethodProposals(context, coveringNode, resultingCollections); getInverseIfContinueIntoIfThenInLoopsProposals(context, coveringNode, resultingCollections); getInverseIfIntoContinueInLoopsProposals(context, coveringNode, resultingCollections); getInverseConditionProposals(context, coveringNode, coveredNodes, resultingCollections); getRemoveExtraParenthesisProposals(context, coveringNode, coveredNodes, resultingCollections); getAddParanoidalParenthesisProposals(context, coveringNode, coveredNodes, resultingCollections); getJoinAndIfStatementsProposals(context, coveringNode, resultingCollections); getSplitAndConditionProposals(context, coveringNode, resultingCollections); getJoinOrIfStatementsProposals(context, coveringNode, coveredNodes, resultingCollections); getSplitOrConditionProposals(context, coveringNode, resultingCollections); getInverseConditionalExpressionProposals(context, coveringNode, resultingCollections); getExchangeInnerAndOuterIfConditionsProposals(context, coveringNode, resultingCollections); getExchangeOperandsProposals(context, coveringNode, resultingCollections); getCastAndAssignIfStatementProposals(context, coveringNode, resultingCollections); getPickOutStringProposals(context, coveringNode, resultingCollections); getReplaceIfElseWithConditionalProposals(context, coveringNode, resultingCollections); getReplaceConditionalWithIfElseProposals(context, coveringNode, resultingCollections); getInverseLocalVariableProposals(context, coveringNode, resultingCollections); getPushNegationDownProposals(context, coveringNode, resultingCollections); getPullNegationUpProposals(context, coveringNode, coveredNodes, resultingCollections); getJoinIfListInIfElseIfProposals(context, coveringNode, coveredNodes, resultingCollections); getConvertSwitchToIfProposals(context, coveringNode, resultingCollections);
Expression switchCaseCondition= createSwitchCaseCondition(ast, rewrite, importRewrite, importRewriteContext, varName, switchCase, isStringsInSwitch, preserveNPE); if (currentCondition == null) { currentCondition= switchCaseCondition; hasStopAsLastExecutableStatement= hasStopAsLastExecutableStatement(statement); Statement copyStatement= copyStatementExceptBreak(ast, rewrite, statement);
return false; int offset= isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength()); if (offset == -1) { return false; breakInfixOperationAtOperation(rewrite, topInfixExpression, andOperator, offset, true, newOperands);
int offset= isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength()); if (offset == -1) { return false; Expression rightExpression= null; InfixExpression currentExpression= infixExpression; leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator); if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) { leftExpression= combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator); leftExpression= combineOperands(rewrite, leftExpression, extendedOperand, false, operator); } else { rightExpression= combineOperands(rewrite, rightExpression, extendedOperand, false, operator); leftExpression= getParenthesizedExpression(ast, leftExpression); rightExpression= getParenthesizedExpression(ast, rightExpression);
Assignment.Operator op= assignment.getOperator(); ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression())); ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression())); ifStatement.setThenStatement(createReturnExpression(rewrite, conditional.getThenExpression())); ifStatement.setElseStatement(createReturnExpression(rewrite, conditional.getElseExpression())); ifStatement.setThenStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getThenExpression())); ifStatement.setElseStatement(createAssignmentStatement(rewrite, op, assignee, conditional.getElseExpression()));
for (Iterator<ASTNode> iter= coveredNodes.iterator(); iter.hasNext();) { ASTNode covered= iter.next(); Expression coveredExpression= getBooleanExpression(covered); if (coveredExpression != null) { Expression inversedExpression= getInversedExpression(rewrite, coveredExpression); rewrite.replace(coveredExpression, inversedExpression, null); hasChanges= true;
Expression expression= getBooleanExpression(fullyCoveredNode); if (expression == null) { return false; final ASTRewrite rewrite= ASTRewrite.create(ast); Expression inversedExpression= getInversedBooleanExpression(rewrite, expression);
private static boolean getConvertSwitchToIfProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { if (!(covering instanceof SwitchStatement)) { return false; } // we could produce quick assist (if all 'case' statements end with 'break') if (resultingCollections == null) { return true; } if (!getConvertSwitchToIfProposals(context, covering, resultingCollections, false)) return false; return getConvertSwitchToIfProposals(context, covering, resultingCollections, true); }