Search in sources :

Example 1 with Operator

use of org.eclipse.jdt.core.dom.Assignment.Operator in project Main by SpartanRefactoring.

the class LocalInitializedUpdateAssignment method go.

@Override
protected ASTRewrite go(final ASTRewrite $, final VariableDeclarationFragment f, final SimpleName n, final Expression initializer, final Statement nextStatement, final TextEditGroup g) {
    if (initializer == null)
        return null;
    final Assignment a = extract.assignment(nextStatement);
    if (a == null || !wizard.eq(n, to(a)) || $FragmentAndStatement.doesUseForbiddenSiblings(f, from(a)))
        return null;
    final Operator o = a.getOperator();
    if (o == ASSIGN)
        return null;
    final InfixExpression newInitializer = subject.pair(to(a), from(a)).to(op.assign2infix(o));
    final InlinerWithValue i = new Inliner(n, $, g).byValue(initializer);
    if (!i.canInlineinto(newInitializer) || i.replacedSize(newInitializer) - metrics.size(nextStatement, initializer) > 0)
        return null;
    $.replace(initializer, newInitializer, g);
    i.inlineInto(newInitializer);
    $.remove(nextStatement, g);
    return $;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) Operator(org.eclipse.jdt.core.dom.Assignment.Operator) Inliner(il.org.spartan.spartanizer.engine.Inliner)

Example 2 with Operator

use of org.eclipse.jdt.core.dom.Assignment.Operator in project eclipse.jdt.ui by eclipse-jdt.

the class AccessAnalyzer method visit.

@Override
public boolean visit(Assignment node) {
    Expression leftHandSide = node.getLeftHandSide();
    if (!considerBinding(resolveBinding(leftHandSide), leftHandSide))
        return true;
    checkParent(node);
    Expression rightHandSide = node.getRightHandSide();
    if (!fIsFieldFinal) {
        List<Expression> arguments = new ArrayList<>();
        AST ast = node.getAST();
        Expression receiver = getReceiver(leftHandSide);
        Operator operator = node.getOperator();
        if (operator != Operator.ASSIGN) {
            // setter only selected. Ex. f += 10; --> setF(f + 10);
            if (!fSetter.isEmpty() && fGetter.isEmpty()) {
                InfixExpression argument = ast.newInfixExpression();
                MethodInvocation invocation = ast.newMethodInvocation();
                invocation.setName(ast.newSimpleName(fSetter));
                if (receiver != null)
                    invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
                invocation.arguments().add(argument);
                argument.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
                argument.setLeftOperand(ast.newSimpleName(leftHandSide.toString()));
                Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
                argument.setRightOperand(rhs);
                fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
                fReferencingSetter = true;
                return false;
            }
            // getter only selected. Ex. f += 10; --> f = getF() + 10;
            if (fSetter.isEmpty() && !fGetter.isEmpty()) {
                Assignment assignment = ast.newAssignment();
                assignment.setLeftHandSide(ast.newSimpleName(leftHandSide.toString()));
                InfixExpression argument = ast.newInfixExpression();
                MethodInvocation invocation = ast.newMethodInvocation();
                invocation.setName(ast.newSimpleName(fGetter));
                if (receiver != null)
                    invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
                argument.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
                argument.setLeftOperand(invocation);
                Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
                argument.setRightOperand(rhs);
                assignment.setRightHandSide(argument);
                fRewriter.replace(node, assignment, createGroupDescription(READ_ACCESS));
                fReferencingGetter = true;
                return false;
            }
        } else if (fSetter.isEmpty() && !fGetter.isEmpty()) {
            // assignment operator with getter only. Ex f = 10;
            // the getter only will not affect the assignment expression
            rightHandSide.accept(this);
            return false;
        }
        if (!fSetter.isEmpty()) {
            // Write access.
            MethodInvocation invocation = ast.newMethodInvocation();
            invocation.setName(ast.newSimpleName(fSetter));
            fReferencingSetter = true;
            if (receiver != null) {
                invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
            }
            arguments = invocation.arguments();
            fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
        }
        if (node.getOperator() == Assignment.Operator.ASSIGN) {
            arguments.add((Expression) fRewriter.createCopyTarget(rightHandSide));
        } else if (!fGetter.isEmpty()) {
            InfixExpression exp = ast.newInfixExpression();
            exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
            MethodInvocation getter = ast.newMethodInvocation();
            getter.setName(ast.newSimpleName(fGetter));
            fReferencingGetter = true;
            if (receiver != null) {
                getter.setExpression((Expression) fRewriter.createCopyTarget(receiver));
            }
            exp.setLeftOperand(getter);
            Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
            if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, exp, leftHandSide.resolveTypeBinding())) {
                ParenthesizedExpression p = ast.newParenthesizedExpression();
                p.setExpression(rhs);
                rhs = p;
            }
            exp.setRightOperand(rhs);
            arguments.add(exp);
        }
    }
    rightHandSide.accept(this);
    return false;
}
Also used : Operator(org.eclipse.jdt.core.dom.Assignment.Operator) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayList(java.util.ArrayList) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Aggregations

Assignment (org.eclipse.jdt.core.dom.Assignment)2 Operator (org.eclipse.jdt.core.dom.Assignment.Operator)2 Inliner (il.org.spartan.spartanizer.engine.Inliner)1 ArrayList (java.util.ArrayList)1 AST (org.eclipse.jdt.core.dom.AST)1 Expression (org.eclipse.jdt.core.dom.Expression)1 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)1 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)1