Search in sources :

Example 96 with VariableDeclarationFragment

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

the class ASTBuilder method declareFragment.

/**
 * Builds a new {@link VariableDeclarationFragment} instance.
 *
 * @param varName
 *            the declared variable name
 * @param initializer
 *            the variable initializer
 * @return a new variable declaration fragment
 */
public VariableDeclarationFragment declareFragment(SimpleName varName, Expression initializer) {
    final VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(varName);
    vdf.setInitializer(initializer);
    return vdf;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment)

Example 97 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment 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)

Example 98 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment 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 99 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment 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 100 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project bayou by capergroup.

the class DOMVariableDeclarationStatement method handle.

@Override
public DSubTree handle() {
    DSubTree tree = new DSubTree();
    for (Object o : statement.fragments()) {
        VariableDeclarationFragment fragment = (VariableDeclarationFragment) o;
        DSubTree t = new DOMVariableDeclarationFragment(fragment, visitor).handle();
        tree.addNodes(t.getNodes());
    }
    return tree;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment)

Aggregations

VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)101 ASTNode (org.eclipse.jdt.core.dom.ASTNode)44 AST (org.eclipse.jdt.core.dom.AST)38 Expression (org.eclipse.jdt.core.dom.Expression)33 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)33 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)30 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)28 Assignment (org.eclipse.jdt.core.dom.Assignment)25 SimpleName (org.eclipse.jdt.core.dom.SimpleName)25 Type (org.eclipse.jdt.core.dom.Type)25 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)22 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)21 Block (org.eclipse.jdt.core.dom.Block)17 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ArrayList (java.util.ArrayList)15 CastExpression (org.eclipse.jdt.core.dom.CastExpression)15 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)15 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)15 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)15 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)14