Search in sources :

Example 46 with VariableDeclarationStatement

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

the class AbstractClassSubstituteRefactoring method replaceClass.

private void replaceClass(final ClassInstanceCreation originalInstanceCreation, final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactor) {
    final ASTBuilder b = ctx.getASTBuilder();
    final Type substituteType = substituteType(b, originalInstanceCreation.getType(), originalInstanceCreation);
    if (substituteType != null) {
        ctx.getRefactorings().replace(originalInstanceCreation.getType(), substituteType);
        originalInstanceCreation.setType(substituteType);
    }
    for (final MethodInvocation methodCall : methodCallsToRefactor) {
        final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
        refactorMethod(b, methodCall, copyOfMethodCall);
        ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
    }
    for (final VariableDeclaration variableDecl : variableDecls) {
        final VariableDeclarationStatement oldDeclareStmt = (VariableDeclarationStatement) variableDecl.getParent();
        final Type substituteVarType = substituteType(b, oldDeclareStmt.getType(), (ASTNode) oldDeclareStmt.fragments().get(0));
        if (substituteVarType != null) {
            ctx.getRefactorings().replace(oldDeclareStmt.getType(), substituteVarType);
        }
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ASTHelper.hasType(org.autorefactor.refactoring.ASTHelper.hasType) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 47 with VariableDeclarationStatement

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

the class ASTBuilder method declareStmt.

/**
 * Builds a new {@link VariableDeclarationStatement} instance.
 *
 * @param type
 *            the type of the variable being declared
 * @param varName
 *            the name of the variable being declared
 * @param initializer
 *            the variable initializer, can be null
 * @return a new variable declaration statement
 */
public VariableDeclarationStatement declareStmt(Type type, SimpleName varName, Expression initializer) {
    final VariableDeclarationFragment fragment = declareFragment(varName, initializer);
    final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(fragment);
    vds.setType(type);
    return vds;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 48 with VariableDeclarationStatement

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

Aggregations

VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)48 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)33 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 AST (org.eclipse.jdt.core.dom.AST)20 Expression (org.eclipse.jdt.core.dom.Expression)20 Type (org.eclipse.jdt.core.dom.Type)20 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 Block (org.eclipse.jdt.core.dom.Block)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)13 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)12 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)12 Statement (org.eclipse.jdt.core.dom.Statement)12 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)11 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10 Assignment (org.eclipse.jdt.core.dom.Assignment)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)9 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)9 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)9