Search in sources :

Example 31 with ClassInstanceCreation

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

the class AbstractClassSubstituteRefactoring method replaceClass.

private void replaceClass(final ClassInstanceCreation originalInstanceCreation, final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactorAlone, final List<MethodInvocation> methodCallsToRefactorWithVariable) {
    final ASTBuilder b = ctx.getASTBuilder();
    if (variableDecls.isEmpty() && methodCallsToRefactorAlone.isEmpty()) {
        final ClassInstanceCreation newInstanceCreation = b.copySubtree(originalInstanceCreation);
        refactorInstantiation(b, originalInstanceCreation, newInstanceCreation);
        ctx.getRefactorings().replace(originalInstanceCreation, newInstanceCreation);
    } else {
        refactorInstantiation(b, originalInstanceCreation, originalInstanceCreation);
        for (final MethodInvocation methodCall : methodCallsToRefactorAlone) {
            final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
            refactorMethod(b, methodCall, copyOfMethodCall);
            ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
        }
        for (final MethodInvocation methodCall : methodCallsToRefactorWithVariable) {
            refactorMethod(b, methodCall, methodCall);
        }
        for (final VariableDeclaration variableDecl : variableDecls) {
            final VariableDeclarationStatement parent = (VariableDeclarationStatement) variableDecl.getParent();
            final VariableDeclarationStatement newDeclareStmt = b.copySubtree(parent);
            replaceVariableType(b, parent, newDeclareStmt);
            ctx.getRefactorings().replace(parent, newDeclareStmt);
        }
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) 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 32 with ClassInstanceCreation

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

the class AbstractClassSubstituteRefactoring method visit.

@Override
public boolean visit(Block node) {
    final ObjectInstantiationVisitor classCreationVisitor = new ObjectInstantiationVisitor();
    node.accept(classCreationVisitor);
    for (final ClassInstanceCreation instanceCreation : classCreationVisitor.getObjectInstantiations()) {
        final List<VariableDeclaration> varDecls = new ArrayList<VariableDeclaration>();
        final List<MethodInvocation> methodCallsToRefactorAlone = new ArrayList<MethodInvocation>();
        final List<MethodInvocation> methodCallsToRefactorWithVariable = new ArrayList<MethodInvocation>();
        if (canInstantiationBeRefactored(instanceCreation) && canBeRefactored(node, instanceCreation, varDecls, methodCallsToRefactorAlone, methodCallsToRefactorWithVariable) && canCodeBeRefactored()) {
            replaceClass(instanceCreation, varDecls, methodCallsToRefactorAlone, methodCallsToRefactorWithVariable);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Aggregations

ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)32 ASTNode (org.eclipse.jdt.core.dom.ASTNode)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)16 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)9 Type (org.eclipse.jdt.core.dom.Type)9 Expression (org.eclipse.jdt.core.dom.Expression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)8 ArrayList (java.util.ArrayList)6 IType (org.eclipse.jdt.core.IType)6 SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 AST (org.eclipse.jdt.core.dom.AST)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 SimpleType (org.eclipse.jdt.core.dom.SimpleType)5 VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)5 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4