use of org.eclipse.jdt.core.dom.PrefixExpression in project che 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 che 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());
}
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 che by eclipse.
the class AccessAnalyzer method visit.
@Override
public boolean visit(PrefixExpression node) {
Expression operand = node.getOperand();
if (!considerBinding(resolveBinding(operand), operand))
return true;
PrefixExpression.Operator operator = node.getOperator();
if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT)
return true;
checkParent(node);
fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS));
return false;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class BooleanEqualsRatherThanNullCheckRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
if (hasOperator(node, CONDITIONAL_AND) || hasOperator(node, CONDITIONAL_OR)) {
final Expression leftOperand = node.getLeftOperand();
final Expression rightOperand = node.getRightOperand();
final InfixExpression condition = as(leftOperand, InfixExpression.class);
final boolean isNullCheck = hasOperator(condition, EQUALS);
final boolean isAndExpr = hasOperator(node, CONDITIONAL_AND);
if (!node.hasExtendedOperands() && (isNullCheck ^ isAndExpr) && condition != null && (hasOperator(condition, EQUALS) || hasOperator(condition, NOT_EQUALS))) {
Expression firstExpr = null;
if (isNullLiteral(condition.getLeftOperand())) {
firstExpr = condition.getRightOperand();
} else if (isNullLiteral(condition.getRightOperand())) {
firstExpr = condition.getLeftOperand();
}
Expression secondExpr = null;
final PrefixExpression negateSecondExpr = as(rightOperand, PrefixExpression.class);
final boolean isPositiveExpr;
if (negateSecondExpr != null && hasOperator(negateSecondExpr, NOT)) {
secondExpr = negateSecondExpr.getOperand();
isPositiveExpr = false;
} else {
secondExpr = rightOperand;
isPositiveExpr = true;
}
if (firstExpr != null && hasType(firstExpr, "java.lang.Boolean") && isPassive(firstExpr) && match(new ASTMatcher(), firstExpr, secondExpr)) {
replaceNullCheck(node, firstExpr, isNullCheck, isAndExpr, isPositiveExpr);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class BigDecimalRefactoring method visit.
@Override
public boolean visit(MethodInvocation node) {
if (node.getExpression() == null) {
return VISIT_SUBTREE;
}
if (getJavaMinorVersion() >= 5 && (isMethod(node, "java.math.BigDecimal", "valueOf", "long") || isMethod(node, "java.math.BigDecimal", "valueOf", "double"))) {
final ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
final Expression arg0 = arg0(node);
if (arg0 instanceof NumberLiteral) {
final NumberLiteral nb = (NumberLiteral) arg0;
if (nb.getToken().contains(".")) {
this.ctx.getRefactorings().replace(node, getClassInstanceCreatorNode((Name) node.getExpression(), nb.getToken()));
} else if (ZERO_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
replaceWithQualifiedName(node, typeBinding, "ZERO");
} else if (ONE_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
replaceWithQualifiedName(node, typeBinding, "ONE");
} else if (TEN_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
replaceWithQualifiedName(node, typeBinding, "TEN");
} else {
return VISIT_SUBTREE;
}
return DO_NOT_VISIT_SUBTREE;
}
} else if (!(node.getParent() instanceof PrefixExpression) || !hasOperator((PrefixExpression) node.getParent(), NOT)) {
return maybeReplaceEquals(true, node, node);
}
return VISIT_SUBTREE;
}
Aggregations