use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project flux by eclipse.
the class NecessaryParenthesesChecker method needsParenthesesInInfixExpression.
private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
InfixExpression.Operator parentInfixOperator = parentInfix.getOperator();
ITypeBinding rightOperandType;
ITypeBinding parentInfixExprType;
if (leftOperandType == null) {
// parentInfix has bindings
leftOperandType = parentInfix.getLeftOperand().resolveTypeBinding();
rightOperandType = parentInfix.getRightOperand().resolveTypeBinding();
parentInfixExprType = parentInfix.resolveTypeBinding();
} else {
rightOperandType = expression.resolveTypeBinding();
parentInfixExprType = getInfixExpressionType(parentInfixOperator, leftOperandType, rightOperandType);
}
boolean isAllOperandsHaveSameType = isAllOperandsHaveSameType(parentInfix, leftOperandType, rightOperandType);
if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) {
//infix expressions are evaluated from left to right -> parentheses not needed
return false;
} else if (isAssociative(parentInfixOperator, parentInfixExprType, isAllOperandsHaveSameType)) {
//left op (right) == (right) op left == right op left
if (expression instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) expression;
Operator operator = infixExpression.getOperator();
if (isStringType(parentInfixExprType)) {
if (parentInfixOperator == InfixExpression.Operator.PLUS && operator == InfixExpression.Operator.PLUS && isStringType(infixExpression.resolveTypeBinding())) {
// "" + (2 + "") == "" + 2 + ""
return !isStringType(infixExpression.getLeftOperand().resolveTypeBinding()) && !isStringType(leftOperandType);
}
//"" + (1 + 2), "" + (1 - 2) etc
return true;
}
if (parentInfixOperator != InfixExpression.Operator.TIMES)
return false;
if (operator == InfixExpression.Operator.TIMES)
// x * (y * z) == x * y * z
return false;
if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE)
// x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved
return true;
return false;
}
return false;
} else {
return true;
}
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getSplitAndConditionProposals.
public static boolean getSplitAndConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
Operator andOperator = InfixExpression.Operator.CONDITIONAL_AND;
// check that user invokes quick assist on infix expression
if (!(node instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression = (InfixExpression) node;
if (infixExpression.getOperator() != andOperator) {
return false;
}
int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
if (offset == -1) {
return false;
}
// check that infix expression belongs to IfStatement
Statement statement = ASTResolving.findParentStatement(node);
if (!(statement instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) statement;
// check that infix expression is part of first level && condition of IfStatement
InfixExpression topInfixExpression = infixExpression;
while (topInfixExpression.getParent() instanceof InfixExpression && ((InfixExpression) topInfixExpression.getParent()).getOperator() == andOperator) {
topInfixExpression = (InfixExpression) topInfixExpression.getParent();
}
if (ifStatement.getExpression() != topInfixExpression) {
return false;
}
//
if (resultingCollections == null) {
return true;
}
AST ast = ifStatement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare left and right conditions
Expression[] newOperands = { null, null };
breakInfixOperationAtOperation(rewrite, topInfixExpression, andOperator, offset, true, newOperands);
Expression leftCondition = newOperands[0];
Expression rightCondition = newOperands[1];
// replace conditions in outer IfStatement
rewrite.set(ifStatement, IfStatement.EXPRESSION_PROPERTY, leftCondition, null);
// prepare inner IfStatement
IfStatement innerIf = ast.newIfStatement();
innerIf.setExpression(rightCondition);
innerIf.setThenStatement((Statement) rewrite.createMoveTarget(ifStatement.getThenStatement()));
Block innerBlock = ast.newBlock();
innerBlock.statements().add(innerIf);
Statement elseStatement = ifStatement.getElseStatement();
if (elseStatement != null) {
innerIf.setElseStatement((Statement) rewrite.createCopyTarget(elseStatement));
}
// replace outer thenStatement
rewrite.replace(ifStatement.getThenStatement(), innerBlock, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitAndCondition_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_AND_CONDITION);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getExchangeOperandsProposals.
private static boolean getExchangeOperandsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
// check that user invokes quick assist on infix expression
if (!(node instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression = (InfixExpression) node;
Operator operator = infixExpression.getOperator();
if (operator != InfixExpression.Operator.CONDITIONAL_AND && operator != InfixExpression.Operator.AND && operator != InfixExpression.Operator.CONDITIONAL_OR && operator != InfixExpression.Operator.OR && operator != InfixExpression.Operator.EQUALS && operator != InfixExpression.Operator.NOT_EQUALS && operator != InfixExpression.Operator.LESS && operator != InfixExpression.Operator.LESS_EQUALS && operator != InfixExpression.Operator.GREATER && operator != InfixExpression.Operator.GREATER_EQUALS && operator != InfixExpression.Operator.PLUS && operator != InfixExpression.Operator.TIMES && operator != InfixExpression.Operator.XOR) {
return false;
}
int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
if (offset == -1) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
AST ast = infixExpression.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare left and right expressions
Expression leftExpression = null;
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);
}
for (Iterator<Expression> iter = currentExpression.extendedOperands().iterator(); iter.hasNext(); ) {
Expression extendedOperand = iter.next();
if (extendedOperand.getStartPosition() <= context.getSelectionOffset()) {
leftExpression = combineOperands(rewrite, leftExpression, extendedOperand, false, operator);
} else {
rightExpression = combineOperands(rewrite, rightExpression, extendedOperand, false, operator);
}
}
if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
leftExpression = getParenthesizedExpression(ast, leftExpression);
}
if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) {
rightExpression = getParenthesizedExpression(ast, rightExpression);
}
if (operator == InfixExpression.Operator.LESS) {
operator = InfixExpression.Operator.GREATER;
} else if (operator == InfixExpression.Operator.LESS_EQUALS) {
operator = InfixExpression.Operator.GREATER_EQUALS;
} else if (operator == InfixExpression.Operator.GREATER) {
operator = InfixExpression.Operator.LESS;
} else if (operator == InfixExpression.Operator.GREATER_EQUALS) {
operator = InfixExpression.Operator.LESS_EQUALS;
}
// create new infix expression
InfixExpression newInfix = ast.newInfixExpression();
newInfix.setOperator(operator);
newInfix.setLeftOperand(rightExpression);
newInfix.setRightOperand(leftExpression);
rewrite.replace(infixExpression, newInfix, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeOperands_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_OPERANDS);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getJoinOrIfStatementsProposals.
private static boolean getJoinOrIfStatementsProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
if (coveredNodes.size() < 2)
return false;
// check that all covered nodes are IfStatement's with same 'then' statement and without 'else'
String commonThenSource = null;
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
ASTNode node = iter.next();
if (!(node instanceof IfStatement))
return false;
//
IfStatement ifStatement = (IfStatement) node;
if (ifStatement.getElseStatement() != null)
return false;
//
Statement thenStatement = ifStatement.getThenStatement();
try {
String thenSource = context.getCompilationUnit().getBuffer().getText(thenStatement.getStartPosition(), thenStatement.getLength());
if (commonThenSource == null) {
commonThenSource = thenSource;
} else {
if (!commonThenSource.equals(thenSource))
return false;
}
} catch (Throwable e) {
return false;
}
}
if (resultingCollections == null) {
return true;
}
//
final AST ast = covering.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare OR'ed condition
InfixExpression condition = null;
boolean hasRightOperand = false;
Statement thenStatement = null;
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
IfStatement ifStatement = (IfStatement) iter.next();
if (thenStatement == null)
thenStatement = (Statement) rewrite.createCopyTarget(ifStatement.getThenStatement());
if (condition == null) {
condition = ast.newInfixExpression();
condition.setOperator(orOperator);
condition.setLeftOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.LEFT_OPERAND_PROPERTY));
} else if (!hasRightOperand) {
condition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
hasRightOperand = true;
} else {
InfixExpression newCondition = ast.newInfixExpression();
newCondition.setOperator(orOperator);
newCondition.setLeftOperand(condition);
newCondition.setRightOperand(getParenthesizedExpressionIfNeeded(ast, rewrite, ifStatement.getExpression(), condition, InfixExpression.RIGHT_OPERAND_PROPERTY));
condition = newCondition;
}
}
// prepare new IfStatement with OR'ed condition
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(condition);
newIf.setThenStatement(thenStatement);
//
ListRewrite listRewriter = null;
for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
IfStatement ifStatement = (IfStatement) iter.next();
if (listRewriter == null) {
Block sourceBlock = (Block) ifStatement.getParent();
//int insertIndex = sourceBlock.statements().indexOf(ifStatement);
listRewriter = rewrite.getListRewrite(sourceBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
}
if (newIf != null) {
listRewriter.replace(ifStatement, newIf, null);
newIf = null;
} else {
listRewriter.remove(ifStatement, null);
}
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_joinWithOr_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_IF_STATEMENTS_WITH_OR);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project Main by SpartanRefactoring.
the class InfixComparisonSizeToZero method replacement.
@Override
public ASTNode replacement(final InfixExpression x) {
final Operator $ = x.getOperator();
if (!iz.comparison($))
return null;
final Expression right = right(x), left = left(x);
return !validTypes(right, left) ? null : iz.methodInvocation(left) ? replacement($, az.methodInvocation(left), right) : replacement(op.conjugate($), az.methodInvocation(right), left);
}
Aggregations