Search in sources :

Example 16 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 PrefixExpression node) {
    final Expression operand = node.getOperand();
    PrefixExpression.Operator _operator = node.getOperator();
    boolean _matched = false;
    if (Objects.equal(_operator, PrefixExpression.Operator.DECREMENT)) {
        _matched = true;
    }
    if (!_matched) {
        if (Objects.equal(_operator, PrefixExpression.Operator.INCREMENT)) {
            _matched = true;
        }
    }
    if (_matched) {
        if ((operand instanceof ArrayAccess)) {
            final String arrayName = this.computeArrayName(((ArrayAccess) operand));
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("_tPreInx_");
            _builder.append(arrayName);
            final String idxName = _builder.toString();
            String op = "-";
            PrefixExpression.Operator _operator_1 = node.getOperator();
            boolean _equals = Objects.equal(_operator_1, PrefixExpression.Operator.INCREMENT);
            if (_equals) {
                op = "+";
            }
            StringConcatenation _builder_1 = new StringConcatenation();
            _builder_1.append("{val ");
            _builder_1.append(idxName);
            _builder_1.append("=");
            this.appendToBuffer(_builder_1.toString());
            ((ArrayAccess) operand).getIndex().accept(this);
            StringConcatenation _builder_2 = new StringConcatenation();
            _builder_2.append(" ");
            _builder_2.append("val ");
            _builder_2.append(idxName, " ");
            _builder_2.append("_res=");
            _builder_2.append(arrayName, " ");
            _builder_2.append(".get(");
            _builder_2.append(idxName, " ");
            _builder_2.append(")");
            _builder_2.append(op, " ");
            _builder_2.append("1");
            this.appendToBuffer(_builder_2.toString());
            StringConcatenation _builder_3 = new StringConcatenation();
            _builder_3.append(" ");
            _builder_3.append(arrayName, " ");
            _builder_3.append(".set(");
            _builder_3.append(idxName, " ");
            _builder_3.append(", ");
            _builder_3.append(idxName, " ");
            _builder_3.append("_res)  ");
            _builder_3.append(idxName, " ");
            _builder_3.append("_res}");
            this.appendToBuffer(_builder_3.toString());
            return false;
        } else {
            final AST dummyAST = AST.newAST(node.getAST().apiLevel());
            final Assignment assigment = dummyAST.newAssignment();
            final InfixExpression infixOp = dummyAST.newInfixExpression();
            ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, operand);
            infixOp.setLeftOperand(((Expression) _copySubtree));
            PrefixExpression.Operator _operator_2 = node.getOperator();
            boolean _equals_1 = Objects.equal(_operator_2, PrefixExpression.Operator.DECREMENT);
            if (_equals_1) {
                infixOp.setOperator(InfixExpression.Operator.MINUS);
            } else {
                infixOp.setOperator(InfixExpression.Operator.PLUS);
            }
            infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
            ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, operand);
            final Expression leftSide = ((Expression) _copySubtree_1);
            assigment.setLeftHandSide(leftSide);
            assigment.setRightHandSide(infixOp);
            this.appendToBuffer("{");
            Type type = null;
            if ((operand instanceof SimpleName)) {
                type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName) operand));
            }
            this.handleAssignment(assigment, leftSide, type);
            this.appendToBuffer("}");
            return false;
        }
    }
    if (!_matched) {
        if (Objects.equal(_operator, PrefixExpression.Operator.COMPLEMENT)) {
            _matched = true;
            node.getOperand().accept(this);
            this.appendToBuffer(".bitwiseNot");
        }
    }
    if (!_matched) {
        {
            this.appendToBuffer(node.getOperator().toString());
            node.getOperand().accept(this);
        }
    }
    return false;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 17 with Assignment

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

the class HotSpotIntrinsicedAPIsRefactoring method visit.

@Override
public boolean visit(ForStatement node) {
    final SystemArrayCopyParams params = new SystemArrayCopyParams();
    collectUniqueIndex(node, params);
    final IVariableBinding incrementedIdx = getUniqueIncrementedVariable(node);
    final List<Statement> stmts = asList(node.getBody());
    if (equalNotNull(params.indexVarBinding, incrementedIdx) && stmts.size() == 1) {
        collectLength(node.getExpression(), incrementedIdx, params);
        final Assignment as = asExpression(stmts.get(0), Assignment.class);
        if (hasOperator(as, ASSIGN)) {
            final Expression lhs = as.getLeftHandSide();
            final Expression rhs = as.getRightHandSide();
            if (lhs instanceof ArrayAccess && rhs instanceof ArrayAccess) {
                final ArrayAccess aaLHS = (ArrayAccess) lhs;
                final ArrayAccess aaRHS = (ArrayAccess) rhs;
                params.destArrayExpr = aaLHS.getArray();
                params.srcArrayExpr = aaRHS.getArray();
                if (haveSameType(params.srcArrayExpr, params.destArrayExpr)) {
                    params.destPos = calcIndex(aaLHS.getIndex(), params);
                    params.srcPos = calcIndex(aaRHS.getIndex(), params);
                    return replaceWithSystemArrayCopyCloneAll(node, params);
                }
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) 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) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 18 with Assignment

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

the class ReduceVariableScopeRefactoring method copy.

private Block copy(Statement stmtToCopy, Name varName) {
    if (stmtToCopy != null && !(stmtToCopy instanceof Block)) {
        final Block b = this.ctx.getAST().newBlock();
        final Assignment a = asExpression(stmtToCopy, Assignment.class);
        if (a != null) {
            final VariableDeclarationFragment vdf = getVariableDeclarationFragment(a, varName);
            statements(b).add(this.ctx.getAST().newVariableDeclarationStatement(vdf));
        } else {
            throw new NotImplementedException(stmtToCopy);
        }
        return b;
    }
    // We should never come here if we had a Block statement, see the replace() method
    throw new NotImplementedException(stmtToCopy);
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) NotImplementedException(org.autorefactor.util.NotImplementedException) Block(org.eclipse.jdt.core.dom.Block)

Example 19 with Assignment

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

the class BooleanRefactoring method noThenReturnStmt.

private boolean noThenReturnStmt(final IfStatement node) {
    final Assignment thenA = asExpression(node.getThenStatement(), Assignment.class);
    if (hasOperator(thenA, ASSIGN) && asList(node.getElseStatement()).isEmpty() && (thenA.getLeftHandSide() instanceof Name || thenA.getLeftHandSide() instanceof FieldAccess)) {
        final Statement previousSibling = getPreviousSibling(node);
        if (previousSibling instanceof VariableDeclarationStatement) {
            final VariableDeclarationStatement vds = (VariableDeclarationStatement) previousSibling;
            VariableDeclarationFragment vdf = getVariableDeclarationFragment(vds, thenA.getLeftHandSide());
            if (vdf != null) {
                final VariableDefinitionsUsesVisitor variableUseVisitor = new VariableDefinitionsUsesVisitor(vdf.resolveBinding(), node.getExpression()).find();
                if (variableUseVisitor.getUses().isEmpty()) {
                    final ITypeBinding typeBinding = vds.getType().resolveBinding();
                    return maybeReplace(node, thenA, typeBinding, vdf.getInitializer());
                }
            }
        } else if (previousSibling instanceof ExpressionStatement) {
            final Assignment elseA = asExpression(previousSibling, Assignment.class);
            if (hasOperator(elseA, ASSIGN) && isSameVariable(thenA.getLeftHandSide(), elseA.getLeftHandSide())) {
                final ITypeBinding typeBinding = elseA.resolveTypeBinding();
                return maybeReplace(node, thenA, typeBinding, elseA.getRightHandSide());
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 20 with Assignment

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

the class ForLoopHelper method decomposeInitializer.

/**
 * Decomposes an initializer into a {@link Pair} with the name of the initialized variable
 * and the initializing expression.
 *
 * @param init
 *          the initializer to decompose
 * @return a {@link Pair} with the name of the initialized variable and the initializing
 *         expression, or {@code null} if the initializer could not be decomposed
 */
public static Pair<Name, Expression> decomposeInitializer(Expression init) {
    if (init instanceof VariableDeclarationExpression) {
        final VariableDeclarationExpression vde = (VariableDeclarationExpression) init;
        final List<VariableDeclarationFragment> fragments = fragments(vde);
        if (fragments.size() == 1) {
            final VariableDeclarationFragment fragment = fragments.get(0);
            return Pair.of((Name) fragment.getName(), fragment.getInitializer());
        }
    } else if (init instanceof Assignment) {
        final Assignment as = (Assignment) init;
        if (hasOperator(as, ASSIGN) && as.getLeftHandSide() instanceof Name) {
            return Pair.of((Name) as.getLeftHandSide(), as.getRightHandSide());
        }
    }
    return Pair.empty();
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

Assignment (org.eclipse.jdt.core.dom.Assignment)96 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)42 Expression (org.eclipse.jdt.core.dom.Expression)41 SimpleName (org.eclipse.jdt.core.dom.SimpleName)37 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 CastExpression (org.eclipse.jdt.core.dom.CastExpression)29 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)29 AST (org.eclipse.jdt.core.dom.AST)28 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)25 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)24 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)24 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)23 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 Statement (org.eclipse.jdt.core.dom.Statement)21 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)19 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)18 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)18 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)18