Search in sources :

Example 1 with PostfixExpression

use of org.eclipse.jdt.core.dom.PostfixExpression 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;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Example 2 with PostfixExpression

use of org.eclipse.jdt.core.dom.PostfixExpression in project che by eclipse.

the class AccessAnalyzer method visit.

@Override
public boolean visit(PostfixExpression node) {
    Expression operand = node.getOperand();
    if (!considerBinding(resolveBinding(operand), operand))
        return true;
    ASTNode parent = node.getParent();
    if (!(parent instanceof ExpressionStatement)) {
        fStatus.addError(RefactoringCoreMessages.SelfEncapsulateField_AccessAnalyzer_cannot_convert_postfix_expression, JavaStatusContext.create(fCUnit, SourceRangeFactory.create(node)));
        return false;
    }
    fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(POSTFIX_ACCESS));
    return false;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 3 with PostfixExpression

use of org.eclipse.jdt.core.dom.PostfixExpression in project che by eclipse.

the class GenerateForLoopAssistProposal method getLinkedIncrementExpression.

/**
	 * Creates a {@link PostfixExpression} used to increment the loop variable of a <code>for</code>
	 * loop to iterate over an array.
	 * 
	 * @param rewrite the current {@link ASTRewrite} instance
	 * @param variableToIncrement the name of the variable to increment
	 * @return a filled {@link PostfixExpression} realizing an incrementation of the specified
	 *         variable
	 */
private Expression getLinkedIncrementExpression(ASTRewrite rewrite, String variableToIncrement) {
    AST ast = rewrite.getAST();
    PostfixExpression incrementLoopVariable = ast.newPostfixExpression();
    SimpleName name = ast.newSimpleName(variableToIncrement);
    addLinkedPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP, name.getIdentifier());
    incrementLoopVariable.setOperand(name);
    incrementLoopVariable.setOperator(PostfixExpression.Operator.INCREMENT);
    return incrementLoopVariable;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Example 4 with PostfixExpression

use of org.eclipse.jdt.core.dom.PostfixExpression in project che by eclipse.

the class ConvertForLoopOperation method validateUpdaters.

/*
	 * Must be one of:
	 * <ul>
	 * <li>[indexBinding]++</li>
	 * <li>++[indexBinding]</li>
	 * <li>[indexBinding]+= 1</li>
	 * <li>[indexBinding]= [indexBinding] + 1</li>
	 * <li>[indexBinding]= 1 + [indexBinding]</li>
	 * <ul>
	 */
private boolean validateUpdaters(ForStatement statement) {
    List<Expression> updaters = statement.updaters();
    if (updaters.size() != 1)
        return false;
    Expression updater = updaters.get(0);
    if (updater instanceof PostfixExpression) {
        PostfixExpression postfix = (PostfixExpression) updater;
        if (!PostfixExpression.Operator.INCREMENT.equals(postfix.getOperator()))
            return false;
        IBinding binding = getBinding(postfix.getOperand());
        if (!fIndexBinding.equals(binding))
            return false;
        return true;
    } else if (updater instanceof PrefixExpression) {
        PrefixExpression prefix = (PrefixExpression) updater;
        if (!PrefixExpression.Operator.INCREMENT.equals(prefix.getOperator()))
            return false;
        IBinding binding = getBinding(prefix.getOperand());
        if (!fIndexBinding.equals(binding))
            return false;
        return true;
    } else if (updater instanceof Assignment) {
        Assignment assignment = (Assignment) updater;
        Expression left = assignment.getLeftHandSide();
        IBinding binding = getBinding(left);
        if (!fIndexBinding.equals(binding))
            return false;
        if (Assignment.Operator.PLUS_ASSIGN.equals(assignment.getOperator())) {
            return isOneLiteral(assignment.getRightHandSide());
        } else if (Assignment.Operator.ASSIGN.equals(assignment.getOperator())) {
            Expression right = assignment.getRightHandSide();
            if (!(right instanceof InfixExpression))
                return false;
            InfixExpression infixExpression = (InfixExpression) right;
            Expression leftOperand = infixExpression.getLeftOperand();
            IBinding leftBinding = getBinding(leftOperand);
            Expression rightOperand = infixExpression.getRightOperand();
            IBinding rightBinding = getBinding(rightOperand);
            if (fIndexBinding.equals(leftBinding)) {
                return isOneLiteral(rightOperand);
            } else if (fIndexBinding.equals(rightBinding)) {
                return isOneLiteral(leftOperand);
            }
        }
    }
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) IBinding(org.eclipse.jdt.core.dom.IBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Aggregations

PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)4 Expression (org.eclipse.jdt.core.dom.Expression)3 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)3 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)3 AST (org.eclipse.jdt.core.dom.AST)2 Assignment (org.eclipse.jdt.core.dom.Assignment)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)1