Search in sources :

Example 71 with Assignment

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

the class ExtractTempRefactoring method getSelectedExpression.

private IExpressionFragment getSelectedExpression() throws JavaModelException {
    if (fSelectedExpression != null)
        return fSelectedExpression;
    IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
    if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
        fSelectedExpression = (IExpressionFragment) selectedFragment;
    } else if (selectedFragment != null) {
        if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
            ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
            Expression expression = exprStatement.getExpression();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
        } else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
            Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
        }
    }
    if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
        fSelectedExpression = null;
    }
    return fSelectedExpression;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) 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) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SourceRange(org.eclipse.jdt.core.SourceRange)

Example 72 with Assignment

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

the class TryWithResourceRefactoring method newFragment.

private VariableDeclarationFragment newFragment(List<Statement> tryStmts, VariableDeclarationFragment existingFragment, List<ASTNode> nodesToRemove) {
    final VariableDefinitionsUsesVisitor visitor = new VariableDefinitionsUsesVisitor(existingFragment).find();
    final List<SimpleName> definitions = visitor.getDefinitions();
    final ASTBuilder b = ctx.getASTBuilder();
    if (!tryStmts.isEmpty()) {
        final Statement tryStmt = tryStmts.get(0);
        final Assignment assignResource = asExpression(tryStmt, Assignment.class);
        if (assignResource != null && isSameVariable(existingFragment, assignResource.getLeftHandSide())) {
            nodesToRemove.add(tryStmt);
            if (containsOnly(definitions, assignResource.getLeftHandSide(), existingFragment.getName())) {
                return b.declareFragment(b.move(existingFragment.getName()), b.move(assignResource.getRightHandSide()));
            }
            return null;
        }
    }
    return containsOnly(definitions, existingFragment.getName()) ? b.move(existingFragment) : null;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 73 with Assignment

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

the class RemoveUnnecessaryCastRefactoring method canRemoveCast.

private boolean canRemoveCast(CastExpression node) {
    final ASTNode parent = node.getParent();
    switch(parent.getNodeType()) {
        case RETURN_STATEMENT:
            final MethodDeclaration md = getAncestor(parent, MethodDeclaration.class);
            return isAssignmentCompatible(node.getExpression(), md.getReturnType2());
        case ASSIGNMENT:
            final Assignment as = (Assignment) parent;
            return isAssignmentCompatible(node.getExpression(), as) || isConstantExpressionAssignmentConversion(node);
        case VARIABLE_DECLARATION_FRAGMENT:
            final VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
            return isAssignmentCompatible(node.getExpression(), resolveTypeBinding(vdf)) || isConstantExpressionAssignmentConversion(node);
        case INFIX_EXPRESSION:
            final InfixExpression ie = (InfixExpression) parent;
            final Expression lo = ie.getLeftOperand();
            final Expression ro = ie.getRightOperand();
            if (node.equals(lo)) {
                return (isStringConcat(ie) || isAssignmentCompatible(node.getExpression(), ro)) && !isPrimitiveTypeNarrowing(node) && !hasOperator(ie, DIVIDE) && !hasOperator(ie, PLUS) && !hasOperator(ie, MINUS);
            } else {
                final boolean integralDivision = isIntegralDivision(ie);
                return ((isNotRefactored(lo) && isStringConcat(ie)) || (!integralDivision && isAssignmentCompatibleInInfixExpression(node, ie)) || (integralDivision && canRemoveCastInIntegralDivision(node, ie))) && !isPrimitiveTypeNarrowing(node) && !isIntegralDividedByFloatingPoint(node, ie);
            }
    }
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Expression(org.eclipse.jdt.core.dom.Expression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 74 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 75 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)

Aggregations

Assignment (org.eclipse.jdt.core.dom.Assignment)229 Expression (org.eclipse.jdt.core.dom.Expression)115 SimpleName (org.eclipse.jdt.core.dom.SimpleName)94 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)91 ASTNode (org.eclipse.jdt.core.dom.ASTNode)84 AST (org.eclipse.jdt.core.dom.AST)69 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)64 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)63 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)63 CastExpression (org.eclipse.jdt.core.dom.CastExpression)62 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)61 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)60 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)59 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)58 Statement (org.eclipse.jdt.core.dom.Statement)55 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)49 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)48 Block (org.eclipse.jdt.core.dom.Block)43 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)43 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)41