use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method isInnerExprHardToRead.
/**
* Returns whether the supplied expression is complex enough to read.
*
* @param innerExpr
* the inner expression to test for ease of read
* @param parent
* the parent node to test for ease of read
* @return true if the expressions is hard to read, false otherwise
*/
private boolean isInnerExprHardToRead(final Expression innerExpr, final ASTNode parent) {
if (parent instanceof InfixExpression) {
if (innerExpr instanceof InfixExpression) {
final InfixExpression innerIe = (InfixExpression) innerExpr;
final Operator innerOp = innerIe.getOperator();
final Operator parentOp = ((InfixExpression) parent).getOperator();
if (Operator.EQUALS.equals(parentOp) || shouldHaveParentheses(innerOp, parentOp)) {
return true;
}
return is(innerIe.getLeftOperand(), Assignment.class) || is(innerIe.getRightOperand(), Assignment.class);
}
} else if (parent instanceof ConditionalExpression) {
return innerExpr instanceof ConditionalExpression || innerExpr instanceof Assignment || innerExpr instanceof InstanceofExpression || innerExpr instanceof InfixExpression;
}
return false;
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method shouldHaveParentheses.
private boolean shouldHaveParentheses(Operator actualChildOp, Operator actualParentOp) {
for (Pair<Operator, Operator> pair : SHOULD_HAVE_PARENTHESES) {
final Operator childOp = pair.getFirst();
final Operator parentOp = pair.getSecond();
if (childOp.equals(actualChildOp) && parentOp.equals(actualParentOp)) {
return true;
}
}
return false;
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project eclipse.jdt.ls by eclipse.
the class AdvancedQuickAssistProcessor method getSplitOrConditionProposals.
//
// 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;
// }
//
public static boolean getSplitOrConditionProposals(IInvocationContext context, ASTNode node, Collection<CUCorrectionProposal> 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;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_OR_CONDITION);
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;
}
use of org.eclipse.jdt.core.dom.InfixExpression.Operator in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInverseLocalVariableProposals.
private static boolean getInverseLocalVariableProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
final AST ast = covering.getAST();
// cursor should be placed on variable name
if (!(covering instanceof SimpleName)) {
return false;
}
SimpleName coveringName = (SimpleName) covering;
if (!coveringName.isDeclaration()) {
return false;
}
// prepare bindings
final IBinding variableBinding = coveringName.resolveBinding();
if (!(variableBinding instanceof IVariableBinding)) {
return false;
}
IVariableBinding binding = (IVariableBinding) variableBinding;
if (binding.isField()) {
return false;
}
// we operate only on boolean variable
if (!isBoolean(coveringName)) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
// find linked nodes
final MethodDeclaration method = ASTResolving.findParentMethodDeclaration(covering);
SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(method, variableBinding);
//
final ASTRewrite rewrite = ASTRewrite.create(ast);
// create proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseBooleanVariable;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
// $NON-NLS-1$
final String KEY_NAME = "name";
final LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_BOOLEAN_VARIABLE);
// prepare new variable identifier
final String oldIdentifier = coveringName.getIdentifier();
// $NON-NLS-1$
final String notString = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_negatedVariableName, "");
final String newIdentifier;
if (oldIdentifier.startsWith(notString)) {
int notLength = notString.length();
if (oldIdentifier.length() > notLength) {
newIdentifier = Character.toLowerCase(oldIdentifier.charAt(notLength)) + oldIdentifier.substring(notLength + 1);
} else {
newIdentifier = oldIdentifier;
}
} else {
newIdentifier = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_negatedVariableName, Character.toUpperCase(oldIdentifier.charAt(0)) + oldIdentifier.substring(1));
}
//
proposal.addLinkedPositionProposal(KEY_NAME, newIdentifier, null);
proposal.addLinkedPositionProposal(KEY_NAME, oldIdentifier, null);
// iterate over linked nodes and replace variable references with negated reference
final HashSet<SimpleName> renamedNames = new HashSet<SimpleName>();
for (int i = 0; i < linkedNodes.length; i++) {
SimpleName name = linkedNodes[i];
if (renamedNames.contains(name)) {
continue;
}
// prepare new name with new identifier
SimpleName newName = ast.newSimpleName(newIdentifier);
proposal.addLinkedPosition(rewrite.track(newName), name == coveringName, KEY_NAME);
//
StructuralPropertyDescriptor location = name.getLocationInParent();
if (location == SingleVariableDeclaration.NAME_PROPERTY) {
// set new name
rewrite.replace(name, newName, null);
} else if (location == Assignment.LEFT_HAND_SIDE_PROPERTY) {
Assignment assignment = (Assignment) name.getParent();
Expression expression = assignment.getRightHandSide();
int exStart = expression.getStartPosition();
int exEnd = exStart + expression.getLength();
// collect all names that are used in assignments
HashSet<SimpleName> overlapNames = new HashSet<SimpleName>();
for (int j = 0; j < linkedNodes.length; j++) {
SimpleName name2 = linkedNodes[j];
if (name2 == null) {
continue;
}
int name2Start = name2.getStartPosition();
if (exStart <= name2Start && name2Start < exEnd) {
overlapNames.add(name2);
}
}
// prepare inverted expression
SimpleNameRenameProvider provider = new SimpleNameRenameProvider() {
public SimpleName getRenamed(SimpleName simpleName) {
if (simpleName.resolveBinding() == variableBinding) {
renamedNames.add(simpleName);
return ast.newSimpleName(newIdentifier);
}
return null;
}
};
Expression inversedExpression = getInversedExpression(rewrite, expression, provider);
// if any name was not renamed during expression inverting, we can not already rename it, so fail to create assist
for (Iterator<SimpleName> iter = overlapNames.iterator(); iter.hasNext(); ) {
Object o = iter.next();
if (!renamedNames.contains(o)) {
return false;
}
}
// check operator and replace if needed
Assignment.Operator operator = assignment.getOperator();
if (operator == Assignment.Operator.BIT_AND_ASSIGN) {
Assignment newAssignment = ast.newAssignment();
newAssignment.setLeftHandSide(newName);
newAssignment.setRightHandSide(inversedExpression);
newAssignment.setOperator(Assignment.Operator.BIT_OR_ASSIGN);
rewrite.replace(assignment, newAssignment, null);
} else if (operator == Assignment.Operator.BIT_OR_ASSIGN) {
Assignment newAssignment = ast.newAssignment();
newAssignment.setLeftHandSide(newName);
newAssignment.setRightHandSide(inversedExpression);
newAssignment.setOperator(Assignment.Operator.BIT_AND_ASSIGN);
rewrite.replace(assignment, newAssignment, null);
} else {
rewrite.replace(expression, inversedExpression, null);
// set new name
rewrite.replace(name, newName, null);
}
} else if (location == VariableDeclarationFragment.NAME_PROPERTY) {
// replace initializer for variable
VariableDeclarationFragment vdf = (VariableDeclarationFragment) name.getParent();
Expression expression = vdf.getInitializer();
if (expression != null) {
rewrite.replace(expression, getInversedExpression(rewrite, expression), null);
}
// set new name
rewrite.replace(name, newName, null);
} else if (name.getParent() instanceof PrefixExpression && ((PrefixExpression) name.getParent()).getOperator() == PrefixExpression.Operator.NOT) {
rewrite.replace(name.getParent(), newName, null);
} else {
PrefixExpression expression = ast.newPrefixExpression();
expression.setOperator(PrefixExpression.Operator.NOT);
expression.setOperand(newName);
rewrite.replace(name, expression, null);
}
}
// add correction proposal
resultingCollections.add(proposal);
return true;
}
Aggregations