Search in sources :

Example 51 with Assignment

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

the class AssignmentOperatorBloater method tip.

@Override
public Tip tip(final Assignment ¢) {
    return new Tip(description(¢), getClass(), ¢) {

        @Override
        public void go(final ASTRewrite r, final TextEditGroup g) {
            // TODO Ori Roth: use class subject
            final InfixExpression e = ¢.getAST().newInfixExpression();
            e.setLeftOperand(copy.of(left(¢)));
            e.setRightOperand(make.plant(copy.of(right(¢))).into(e));
            e.setOperator(op.assign2infix(¢.getOperator()));
            final Assignment a = ¢.getAST().newAssignment();
            a.setLeftHandSide(copy.of(left(¢)));
            a.setRightHandSide(fix(e, left(¢).resolveTypeBinding()));
            a.setOperator(Operator.ASSIGN);
            r.replace(¢, a, g);
        }
    };
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment)

Example 52 with Assignment

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

the class TippersTest method inlineExpressionWithSideEffect.

@Test
public void inlineExpressionWithSideEffect() {
    assert !sideEffects.free(e("f()"));
    final VariableDeclarationFragment f = findFirst.variableDeclarationFragment(WrapIntoComilationUnit.Statement.intoCompilationUnit("int a = f(); return a += 2 * a;"));
    azzert.that(f, iz("a=f()"));
    final SimpleName n = f.getName();
    azzert.that(n, iz("a"));
    final Expression initializer = f.getInitializer();
    azzert.that(initializer, iz("f()"));
    assert !sideEffects.free(initializer);
    final ASTNode parent = f.getParent();
    azzert.that(parent, iz("int a = f();"));
    final ASTNode block = parent.getParent();
    azzert.that(block, iz("{int a = f(); return a += 2*a;}"));
    final ReturnStatement returnStatement = (ReturnStatement) statements((Block) block).get(1);
    azzert.that(returnStatement, iz("return a += 2 *a;"));
    final Assignment a = (Assignment) returnStatement.getExpression();
    final Operator o = a.getOperator();
    azzert.that(o, iz("+="));
    final InfixExpression alternateInitializer = subject.pair(to(a), from(a)).to(op.assign2infix(o));
    azzert.that(alternateInitializer, iz("a + 2 * a"));
    assert !sideEffects.free(initializer);
    azzert.that(collect.usesOf(n).in(alternateInitializer).size(), is(2));
    assert !new Inliner(n).byValue(initializer).canInlineinto(alternateInitializer);
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment)

Example 53 with Assignment

use of org.eclipse.jdt.core.dom.Assignment in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final PostfixExpression node) {
    final AST dummyAST = AST.newAST(node.getAST().apiLevel());
    final PostfixExpression.Operator pfOperator = node.getOperator();
    Expression _operand = node.getOperand();
    if ((_operand instanceof ArrayAccess)) {
        Expression _operand_1 = node.getOperand();
        final ArrayAccess pfOperand = ((ArrayAccess) _operand_1);
        if ((Objects.equal(pfOperator, PostfixExpression.Operator.INCREMENT) || Objects.equal(pfOperator, PostfixExpression.Operator.DECREMENT))) {
            final String arrayName = this.computeArrayName(pfOperand);
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("_postIndx_");
            _builder.append(arrayName);
            final String idxName = _builder.toString();
            StringConcatenation _builder_1 = new StringConcatenation();
            _builder_1.append("_postVal_");
            _builder_1.append(arrayName);
            final String tempVarName = _builder_1.toString();
            StringConcatenation _builder_2 = new StringConcatenation();
            _builder_2.append("{ var ");
            _builder_2.append(idxName);
            _builder_2.append("=");
            this.appendToBuffer(_builder_2.toString());
            pfOperand.getIndex().accept(this);
            StringConcatenation _builder_3 = new StringConcatenation();
            _builder_3.append(" ");
            _builder_3.append("var  ");
            this.appendToBuffer(_builder_3.toString());
            final VariableDeclarationFragment varDeclaration = dummyAST.newVariableDeclarationFragment();
            varDeclaration.setName(dummyAST.newSimpleName(tempVarName));
            ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, pfOperand);
            final ArrayAccess arrayAccess = ((ArrayAccess) _copySubtree);
            arrayAccess.setIndex(dummyAST.newSimpleName(idxName));
            varDeclaration.setInitializer(arrayAccess);
            varDeclaration.accept(this);
            final InfixExpression infixOp = dummyAST.newInfixExpression();
            infixOp.setLeftOperand(dummyAST.newSimpleName(tempVarName));
            PostfixExpression.Operator _operator = node.getOperator();
            boolean _equals = Objects.equal(_operator, PostfixExpression.Operator.DECREMENT);
            if (_equals) {
                infixOp.setOperator(InfixExpression.Operator.MINUS);
            } else {
                infixOp.setOperator(InfixExpression.Operator.PLUS);
            }
            infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
            final Assignment assigment = dummyAST.newAssignment();
            ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, pfOperand);
            final ArrayAccess writeArray = ((ArrayAccess) _copySubtree_1);
            writeArray.setIndex(dummyAST.newSimpleName(idxName));
            assigment.setLeftHandSide(writeArray);
            ASTNode _copySubtree_2 = ASTNode.copySubtree(dummyAST, infixOp);
            assigment.setRightHandSide(((Expression) _copySubtree_2));
            assigment.accept(this);
            StringConcatenation _builder_4 = new StringConcatenation();
            String _xifexpression = null;
            boolean _needsReturnValue = this._aSTFlattenerUtils.needsReturnValue(node);
            if (_needsReturnValue) {
                _xifexpression = tempVarName;
            }
            _builder_4.append(_xifexpression);
            _builder_4.append(" }");
            this.appendToBuffer(_builder_4.toString());
            return false;
        }
    }
    node.getOperand().accept(this);
    this.appendToBuffer(pfOperator.toString());
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) AST(org.eclipse.jdt.core.dom.AST) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Example 54 with Assignment

use of org.eclipse.jdt.core.dom.Assignment in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final CastExpression node) {
    ASTNode _parent = node.getParent();
    final boolean parantesis = (!(_parent instanceof Assignment));
    if (parantesis) {
        this.appendToBuffer("(");
    }
    node.getExpression().accept(this);
    this.appendToBuffer(" as ");
    node.getType().accept(this);
    if (parantesis) {
        this.appendToBuffer(")");
    }
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 55 with Assignment

use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.

the class HotSpotIntrinsicedAPIsRefactoring method collectUniqueIndex.

private void collectUniqueIndex(ForStatement node, SystemArrayCopyParams params) {
    if (initializers(node).size() != 1) {
        return;
    }
    final Expression initializer0 = initializers(node).get(0);
    if (initializer0 instanceof VariableDeclarationExpression) {
        final VariableDeclarationExpression vde = (VariableDeclarationExpression) initializer0;
        if (isPrimitive(vde, "int") && fragments(vde).size() == 1) {
            // this must be the array index
            VariableDeclarationFragment vdf = fragments(vde).get(0);
            if (vdf.getExtraDimensions() == 0) {
                params.indexStartPos = vdf.getInitializer();
                params.indexVarBinding = vdf.resolveBinding();
                return;
            }
        }
    } else if (initializer0 instanceof Assignment) {
        final Assignment as = (Assignment) initializer0;
        if (hasOperator(as, ASSIGN) && isPrimitive(as.resolveTypeBinding(), "int")) {
            // this must be the array index
            params.indexStartPos = as.getRightHandSide();
            final Expression lhs = as.getLeftHandSide();
            if (lhs instanceof SimpleName) {
                final IBinding binding = ((SimpleName) lhs).resolveBinding();
                if (binding instanceof IVariableBinding) {
                    params.indexVarBinding = (IVariableBinding) binding;
                    return;
                }
            }
        }
    }
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

Assignment (org.eclipse.jdt.core.dom.Assignment)58 Expression (org.eclipse.jdt.core.dom.Expression)30 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)25 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)24 AST (org.eclipse.jdt.core.dom.AST)23 ASTNode (org.eclipse.jdt.core.dom.ASTNode)23 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)19 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)18 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)17 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)14 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)14 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)13 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)12 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 Statement (org.eclipse.jdt.core.dom.Statement)11