use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class PushNegationDownRefactoring method visit.
@Override
public boolean visit(PrefixExpression node) {
if (!hasOperator(node, NOT)) {
return VISIT_SUBTREE;
}
final ASTBuilder b = ctx.getASTBuilder();
final Refactorings r = ctx.getRefactorings();
final Expression operand = removeParentheses(node.getOperand());
if (operand instanceof PrefixExpression) {
final PrefixExpression pe = (PrefixExpression) operand;
if (hasOperator(pe, NOT)) {
r.replace(node, b.move(pe.getOperand()));
return DO_NOT_VISIT_SUBTREE;
}
} else if (operand instanceof InfixExpression) {
final InfixExpression ie = (InfixExpression) operand;
final Operator reverseOp = (Operator) OperatorEnum.getOperator(ie).getReverseBooleanOperator();
if (reverseOp != null) {
List<Expression> allOperands = new ArrayList<Expression>(allOperands(ie));
if (Arrays.<Operator>asList(CONDITIONAL_AND, CONDITIONAL_OR, AND, OR).contains(ie.getOperator())) {
for (ListIterator<Expression> it = allOperands.listIterator(); it.hasNext(); ) {
it.set(b.negate(it.next()));
}
} else {
allOperands = b.move(allOperands);
}
r.replace(node, b.parenthesize(b.infixExpr(reverseOp, allOperands)));
return DO_NOT_VISIT_SUBTREE;
}
} else {
final Boolean constant = getBooleanLiteral(operand);
if (constant != null) {
r.replace(node, b.boolean0(!constant));
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project eclipse.jdt.ls by eclipse.
the class GetterSetterUtil method getAssignedValue.
/**
* Converts an assignment, postfix expression or prefix expression into an
* assignable equivalent expression using the getter.
*
* @param node
* the assignment/prefix/postfix node
* @param astRewrite
* the astRewrite to use
* @param getterExpression
* the expression to insert for read accesses or
* <code>null</code> if such an expression does not exist
* @param variableType
* the type of the variable that the result will be assigned to
* @param is50OrHigher
* <code>true</code> if a 5.0 or higher environment can be used
* @return an expression that can be assigned to the type variableType with
* node being replaced by a equivalent expression using the getter
*/
public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) {
InfixExpression.Operator op = null;
AST ast = astRewrite.getAST();
if (isNotInBlock(node)) {
return null;
}
if (node.getNodeType() == ASTNode.ASSIGNMENT) {
Assignment assignment = ((Assignment) node);
Expression rightHandSide = assignment.getRightHandSide();
Expression copiedRightOp = (Expression) astRewrite.createCopyTarget(rightHandSide);
if (assignment.getOperator() == Operator.ASSIGN) {
ITypeBinding rightHandSideType = rightHandSide.resolveTypeBinding();
copiedRightOp = createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher);
return copiedRightOp;
}
if (getterExpression != null) {
InfixExpression infix = ast.newInfixExpression();
infix.setLeftOperand(getterExpression);
infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
ITypeBinding infixType = infix.resolveTypeBinding();
if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) {
ParenthesizedExpression p = ast.newParenthesizedExpression();
p.setExpression(copiedRightOp);
copiedRightOp = p;
}
infix.setRightOperand(copiedRightOp);
return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
}
} else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
PostfixExpression po = (PostfixExpression) node;
if (po.getOperator() == PostfixExpression.Operator.INCREMENT) {
op = InfixExpression.Operator.PLUS;
}
if (po.getOperator() == PostfixExpression.Operator.DECREMENT) {
op = InfixExpression.Operator.MINUS;
}
} else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
PrefixExpression pe = (PrefixExpression) node;
if (pe.getOperator() == PrefixExpression.Operator.INCREMENT) {
op = InfixExpression.Operator.PLUS;
}
if (pe.getOperator() == PrefixExpression.Operator.DECREMENT) {
op = InfixExpression.Operator.MINUS;
}
}
if (op != null && getterExpression != null) {
return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher);
}
return null;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project eclipse.jdt.ls by eclipse.
the class NecessaryParenthesesChecker method needsParentheses.
/**
* Does the <code>expression</code> need parentheses when inserted into <code>parent</code> at
* <code>locationInParent</code> ?
*
* @param expression the expression
* @param parent the parent node
* @param locationInParent location of expression in the parent
* @param leftOperandType the type of the left operand in <code>parent</code> if
* <code>parent</code> is an infix expression with no bindings and
* <code>expression</code> is the right operand in it, <code>null</code> otherwise
* @return <code>true</code> if <code>expression</code> needs parentheses, <code>false</code>
* otherwise.
*
* @since 3.9
*/
private static boolean needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
if (!expressionTypeNeedsParentheses(expression)) {
return false;
}
if (!locationNeedsParentheses(locationInParent)) {
return false;
}
if (parent instanceof Expression) {
Expression parentExpression = (Expression) parent;
if (expression instanceof PrefixExpression) {
// see bug 405096
return needsParenthesesForPrefixExpression(parentExpression, ((PrefixExpression) expression).getOperator());
}
if (expression instanceof ArrayCreation) {
// see bug 394721
return parentExpression instanceof ArrayAccess && ((ArrayCreation) expression).getInitializer() == null;
}
int expressionPrecedence = OperatorPrecedence.getExpressionPrecedence(expression);
int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(parentExpression);
if (expressionPrecedence > parentPrecedence) {
// (opEx) opParent and opEx binds more -> parentheses not needed
return false;
}
if (expressionPrecedence < parentPrecedence) {
// (opEx) opParent and opEx binds less -> parentheses needed
return true;
}
if (parentExpression instanceof InfixExpression) {
return needsParenthesesInInfixExpression(expression, (InfixExpression) parentExpression, locationInParent, leftOperandType);
}
if (parentExpression instanceof ConditionalExpression && locationInParent == ConditionalExpression.EXPRESSION_PROPERTY) {
return true;
}
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project whole by wholeplatform.
the class CompilationUnitBuilder method newPrefixExpression.
public PrefixExpression newPrefixExpression(Expression exp, String op) {
PrefixExpression prefixExp = ast.newPrefixExpression();
prefixExp.setOperand(exp);
prefixExp.setOperator(PrefixExpression.Operator.toOperator(op));
return prefixExp;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getPullNegationUpProposals.
private static boolean getPullNegationUpProposals(IInvocationContext context, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
if (coveredNodes.size() != 1) {
return false;
}
//
ASTNode fullyCoveredNode = coveredNodes.get(0);
Expression expression = getBooleanExpression(fullyCoveredNode);
if (expression == null || (!(expression instanceof InfixExpression) && !(expression instanceof ConditionalExpression))) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
AST ast = expression.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
// prepared inverted expression
Expression inversedExpression = getInversedExpression(rewrite, expression);
// prepare ParenthesizedExpression
ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
parenthesizedExpression.setExpression(inversedExpression);
// prepare NOT prefix expression
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
prefixExpression.setOperand(parenthesizedExpression);
// replace old expression
rewrite.replace(expression, prefixExpression, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_pullNegationUp;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_UP);
resultingCollections.add(proposal);
return true;
}
Aggregations