use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project Main by SpartanRefactoring.
the class InfixConditionalCommon method replacement.
@Override
public Expression replacement(final InfixExpression x) {
final Operator $ = x.getOperator();
if (!in($, CONDITIONAL_AND, CONDITIONAL_OR))
return null;
final Operator conjugate = conjugate($);
final InfixExpression left = az.infixExpression(core(left(x)));
if (left == null || left.getOperator() != conjugate)
return null;
final InfixExpression right = az.infixExpression(core(right(x)));
if (right == null || right.getOperator() != conjugate)
return null;
final Expression leftLeft = left(left);
return !sideEffects.free(leftLeft) || !wizard.eq(leftLeft, left(right)) ? null : subject.pair(leftLeft, subject.pair(chopHead(left), chopHead(right)).to($)).to(conjugate);
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project che 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 che 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, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project che by eclipse.
the class AdvancedQuickAssistProcessor method getConvertIfElseToSwitchProposals.
private static boolean getConvertIfElseToSwitchProposals(IInvocationContext context, ASTNode coveringNode, ArrayList<ICommandAccess> resultingCollections, boolean handleNullArg) {
final AST ast = coveringNode.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
final ImportRewrite importRewrite = StubUtility.createImportRewrite(context.getASTRoot(), true);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(coveringNode), importRewrite);
IfStatement ifStatement = (IfStatement) coveringNode;
IfStatement currentIf = ifStatement;
Statement currentStatement = ifStatement;
Expression currentExpression = currentIf.getExpression();
SwitchStatement switchStatement = ast.newSwitchStatement();
Expression switchExpression = null;
boolean executeDefaultOnNullExpression = false;
Statement defaultStatement = null;
while (currentStatement != null) {
Expression expression = null;
List<Expression> caseExpressions = new ArrayList<Expression>();
if (currentIf != null) {
while (currentExpression != null) {
// loop for fall through cases - multiple expressions with || operator
Expression leftOperand;
Expression rightOperand;
boolean isMethodInvocationCase = false;
if (currentExpression instanceof MethodInvocation) {
isMethodInvocationCase = true;
if (//$NON-NLS-1$
!(((MethodInvocation) currentExpression).getName().getIdentifier()).equals("equals"))
return false;
MethodInvocation invocation = (MethodInvocation) currentExpression;
leftOperand = invocation.getExpression();
if (leftOperand == null)
return false;
ITypeBinding leftBinding = leftOperand.resolveTypeBinding();
if (leftBinding != null) {
if (leftBinding.getQualifiedName().equals("java.lang.String")) {
//$NON-NLS-1$
if (!JavaModelUtil.is17OrHigher(context.getCompilationUnit().getJavaProject()))
return false;
} else if (!leftBinding.isEnum()) {
return false;
}
}
List<Expression> arguments = invocation.arguments();
if (arguments.size() != 1)
return false;
rightOperand = arguments.get(0);
ITypeBinding rightBinding = leftOperand.resolveTypeBinding();
if (rightBinding != null) {
if (rightBinding.getQualifiedName().equals("java.lang.String")) {
//$NON-NLS-1$
if (!JavaModelUtil.is17OrHigher(context.getCompilationUnit().getJavaProject()))
return false;
} else if (!rightBinding.isEnum()) {
return false;
}
}
} else if (currentExpression instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) currentExpression;
Operator operator = infixExpression.getOperator();
if (!(operator.equals(InfixExpression.Operator.CONDITIONAL_OR) || operator.equals(InfixExpression.Operator.EQUALS)))
return false;
leftOperand = infixExpression.getLeftOperand();
rightOperand = infixExpression.getRightOperand();
if (operator.equals(InfixExpression.Operator.EQUALS)) {
ITypeBinding typeBinding = leftOperand.resolveTypeBinding();
if (typeBinding != null && typeBinding.getQualifiedName().equals("java.lang.String")) {
// don't propose quick assist when == is used to compare strings, since switch will use equals()
return false;
}
} else if (operator.equals(InfixExpression.Operator.CONDITIONAL_OR)) {
currentExpression = leftOperand;
continue;
}
} else {
return false;
}
if (leftOperand.resolveConstantExpressionValue() != null) {
caseExpressions.add(leftOperand);
expression = rightOperand;
executeDefaultOnNullExpression |= isMethodInvocationCase;
} else if (rightOperand.resolveConstantExpressionValue() != null) {
caseExpressions.add(rightOperand);
expression = leftOperand;
} else if (leftOperand instanceof QualifiedName) {
QualifiedName qualifiedName = (QualifiedName) leftOperand;
IVariableBinding binding = (IVariableBinding) qualifiedName.resolveBinding();
if (binding == null || !binding.isEnumConstant())
return false;
importRewrite.addImport(binding.getDeclaringClass(), importRewriteContext);
caseExpressions.add(qualifiedName.getName());
expression = rightOperand;
executeDefaultOnNullExpression |= isMethodInvocationCase;
} else if (rightOperand instanceof QualifiedName) {
QualifiedName qualifiedName = (QualifiedName) rightOperand;
IVariableBinding binding = (IVariableBinding) qualifiedName.resolveBinding();
if (binding == null || !binding.isEnumConstant())
return false;
importRewrite.addImport(binding.getDeclaringClass(), importRewriteContext);
caseExpressions.add(qualifiedName.getName());
expression = leftOperand;
} else {
return false;
}
if (expression == null) {
// paranoidal check: this condition should never be true
return false;
}
if (currentExpression.getParent() instanceof InfixExpression) {
currentExpression = getNextSiblingExpression(currentExpression);
} else {
currentExpression = null;
}
if (switchExpression == null) {
switchExpression = expression;
}
if (!switchExpression.subtreeMatch(new ASTMatcher(), expression)) {
return false;
}
}
}
Statement thenStatement;
if (currentIf == null) {
//currentStatement has the default else block
thenStatement = currentStatement;
defaultStatement = currentStatement;
} else {
thenStatement = currentIf.getThenStatement();
}
SwitchCase[] switchCaseStatements = createSwitchCaseStatements(ast, rewrite, caseExpressions);
for (int i = 0; i < switchCaseStatements.length; i++) {
switchStatement.statements().add(switchCaseStatements[i]);
}
boolean isBreakRequired = true;
if (thenStatement instanceof Block) {
Statement statement = null;
for (Iterator<Statement> iter = ((Block) thenStatement).statements().iterator(); iter.hasNext(); ) {
statement = iter.next();
switchStatement.statements().add(rewrite.createCopyTarget(statement));
}
if (statement instanceof ReturnStatement || statement instanceof ThrowStatement)
isBreakRequired = false;
} else {
if (thenStatement instanceof ReturnStatement || thenStatement instanceof ThrowStatement)
isBreakRequired = false;
switchStatement.statements().add(rewrite.createCopyTarget(thenStatement));
}
if (isBreakRequired)
switchStatement.statements().add(ast.newBreakStatement());
// advance currentStatement to the next "else if" or "else":
if (currentIf != null && currentIf.getElseStatement() != null) {
Statement elseStatement = currentIf.getElseStatement();
if (elseStatement instanceof IfStatement) {
currentIf = (IfStatement) elseStatement;
currentStatement = currentIf;
currentExpression = currentIf.getExpression();
} else {
currentIf = null;
currentStatement = elseStatement;
currentExpression = null;
}
} else {
currentStatement = null;
}
}
if (switchExpression == null)
return false;
switchStatement.setExpression((Expression) rewrite.createCopyTarget(switchExpression));
if (handleNullArg) {
if (executeDefaultOnNullExpression) {
IfStatement newIfStatement = ast.newIfStatement();
InfixExpression infixExpression = ast.newInfixExpression();
infixExpression.setLeftOperand((Expression) rewrite.createCopyTarget(switchExpression));
infixExpression.setRightOperand(ast.newNullLiteral());
infixExpression.setOperator(InfixExpression.Operator.EQUALS);
newIfStatement.setExpression(infixExpression);
if (defaultStatement == null) {
Block block = ast.newBlock();
newIfStatement.setThenStatement(block);
} else if (defaultStatement instanceof Block) {
Block block = ast.newBlock();
for (Iterator<Statement> iter = ((Block) defaultStatement).statements().iterator(); iter.hasNext(); ) {
block.statements().add(rewrite.createCopyTarget(iter.next()));
}
newIfStatement.setThenStatement(block);
} else {
newIfStatement.setThenStatement((Statement) rewrite.createCopyTarget(defaultStatement));
}
Block block = ast.newBlock();
block.statements().add(switchStatement);
newIfStatement.setElseStatement(block);
rewrite.replace(ifStatement, newIfStatement, null);
//$NON-NLS-1$ //$NON-NLS-2$
String source = ASTNodes.asString(switchExpression).replaceAll("\r\n?|\n", " ");
String label = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_convertIfElseToSwitch_handleNullArg, source);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_IF_ELSE_TO_SWITCH);
proposal.setImportRewrite(importRewrite);
resultingCollections.add(proposal);
}
} else {
rewrite.replace(ifStatement, switchStatement, null);
String label = CorrectionMessages.AdvancedQuickAssistProcessor_convertIfElseToSwitch;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_IF_ELSE_TO_SWITCH);
proposal.setImportRewrite(importRewrite);
resultingCollections.add(proposal);
}
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getSplitOrConditionProposals.
public static boolean getSplitOrConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
// check that user invokes quick assist on infix expression
if (!(node instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression = (InfixExpression) node;
if (infixExpression.getOperator() != orOperator) {
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() == orOperator) {
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, orOperator, offset, true, newOperands);
Expression leftCondition = newOperands[0];
Expression rightCondition = newOperands[1];
// prepare first statement
rewrite.replace(ifStatement.getExpression(), leftCondition, null);
IfStatement secondIf = ast.newIfStatement();
secondIf.setExpression(rightCondition);
secondIf.setThenStatement((Statement) rewrite.createCopyTarget(ifStatement.getThenStatement()));
Statement elseStatement = ifStatement.getElseStatement();
if (elseStatement == null) {
rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, secondIf, null);
} else {
rewrite.replace(elseStatement, secondIf, null);
secondIf.setElseStatement((Statement) rewrite.createMoveTarget(elseStatement));
}
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitOrCondition_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_OR_CONDITION);
resultingCollections.add(proposal);
return true;
}
Aggregations