Search in sources :

Example 56 with Assignment

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

the class NoSettingRatherThanUselessSettingRefactoring method visit.

@Override
public boolean visit(VariableDeclarationStatement node) {
    if (node.fragments() != null && node.fragments().size() == 1) {
        final VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.fragments().get(0);
        if (fragment.getInitializer() != null && fragment.getInitializer().resolveConstantExpressionValue() != null) {
            final IVariableBinding variable = fragment.resolveBinding();
            Statement stmtToInspect = getNextSibling(node);
            boolean isOverridden = false;
            boolean isRead = false;
            while (stmtToInspect != null && !isOverridden && !isRead) {
                if (stmtToInspect instanceof ExpressionStatement) {
                    final Assignment assignment = asExpression(stmtToInspect, Assignment.class);
                    isOverridden = hasOperator(assignment, ASSIGN) && isSameVariable(fragment.getName(), assignment.getLeftHandSide());
                }
                isRead = !new VariableDefinitionsUsesVisitor(variable, stmtToInspect).find().getUses().isEmpty();
                stmtToInspect = getNextSibling(stmtToInspect);
            }
            if (isOverridden && !isRead) {
                ctx.getRefactorings().remove(fragment.getInitializer());
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 57 with Assignment

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

the class ReduceVariableScopeRefactoring method getVariableDeclarationFragment.

private VariableDeclarationFragment getVariableDeclarationFragment(Expression exprToReplace, Name varName) {
    if (exprToReplace instanceof Assignment) {
        final Assignment a = (Assignment) exprToReplace;
        if (a.getLeftHandSide() instanceof SimpleName) {
            final SimpleName sn = (SimpleName) a.getLeftHandSide();
            if (sn.getFullyQualifiedName().equals(varName.getFullyQualifiedName())) {
                final ASTBuilder b = this.ctx.getASTBuilder();
                final VariableDeclarationFragment vdf = b.getAST().newVariableDeclarationFragment();
                vdf.setInitializer(b.copy(a.getRightHandSide()));
                vdf.setName(b.copy(sn));
                return vdf;
            }
        }
        throw new NotImplementedException(a.getLeftHandSide());
    }
    throw new NotImplementedException(exprToReplace);
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 58 with Assignment

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

the class SimplifyExpressionRefactoring method isInnerExprHardToRead.

/**
 * Returns whether the supplied expression is complex enough to read.
 *
 * @param innerExpr
 *          the inner expression to test for ease of read
 * @param parent
 *          the parent node to test for ease of read
 * @return true if the expressions is hard to read, false otherwise
 */
private boolean isInnerExprHardToRead(final Expression innerExpr, final ASTNode parent) {
    if (parent instanceof InfixExpression) {
        if (innerExpr instanceof InfixExpression) {
            final InfixExpression innerIe = (InfixExpression) innerExpr;
            final Operator innerOp = innerIe.getOperator();
            final Operator parentOp = ((InfixExpression) parent).getOperator();
            if (Operator.EQUALS.equals(parentOp) || shouldHaveParentheses(innerOp, parentOp)) {
                return true;
            }
            return is(innerIe.getLeftOperand(), Assignment.class) || is(innerIe.getRightOperand(), Assignment.class);
        }
    } else if (parent instanceof ConditionalExpression) {
        return innerExpr instanceof ConditionalExpression || innerExpr instanceof Assignment || innerExpr instanceof InstanceofExpression || innerExpr instanceof InfixExpression;
    }
    return false;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) Assignment(org.eclipse.jdt.core.dom.Assignment) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

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