Search in sources :

Example 26 with MethodInvocation

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

the class IntroduceIndirectionRefactoring method updateReferences.

private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    //$NON-NLS-1$
    monitor.beginTask("", 90);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
    if (result.hasFatalError())
        return result;
    result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup group = references[i];
        SearchMatch[] searchResults = group.getSearchResults();
        CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
        for (int j = 0; j < searchResults.length; j++) {
            SearchMatch match = searchResults[j];
            if (match.isInsideDocComment())
                continue;
            IMember enclosingMember = (IMember) match.getElement();
            ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
            if (target instanceof SuperMethodInvocation) {
                // Cannot retarget calls to super - add a warning
                result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
                continue;
            }
            //$NON-NLS-1$
            Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
            MethodInvocation invocation = (MethodInvocation) target;
            ITypeBinding typeBinding = getExpressionType(invocation);
            if (fIntermediaryFirstParameterType == null) {
                // no highest type yet
                fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
            } else {
                // check if current type is higher
                result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
            }
            if (result.hasFatalError())
                return result;
            // create an edit for this particular call
            result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
            if (monitor.isCanceled())
                throw new OperationCanceledException();
        }
        if (!isRewriteKept(group.getCompilationUnit()))
            createChangeAndDiscardRewrite(group.getCompilationUnit());
        monitor.worked(ticksPerCU);
    }
    monitor.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IMember(org.eclipse.jdt.core.IMember) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 27 with MethodInvocation

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

the class BooleanEqualsRatherThanNullCheckRefactoring method replaceNullCheck.

private void replaceNullCheck(final InfixExpression node, final Expression firstExpr, final boolean isNullCheck, final boolean isAndExpr, final boolean isPositiveExpr) {
    final ASTBuilder b = ctx.getASTBuilder();
    final Name booleanConstant = b.name("Boolean", isAndExpr == isPositiveExpr ? "TRUE" : "FALSE");
    final MethodInvocation equalsMethod = b.invoke(booleanConstant, "equals", b.copy(firstExpr));
    Expression newExpr = null;
    if (!isNullCheck || isAndExpr) {
        newExpr = equalsMethod;
    } else {
        newExpr = b.not(equalsMethod);
    }
    ctx.getRefactorings().replace(node, newExpr);
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) Name(org.eclipse.jdt.core.dom.Name)

Example 28 with MethodInvocation

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

the class AbstractUnitTestRefactoring method maybeRefactorComparison.

private boolean maybeRefactorComparison(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final InfixExpression ie, final boolean isAssertEquals, final Expression failureMessage, final boolean isRewriteNeeded) {
    final Pair<Expression, Expression> actualAndExpected = getActualAndExpected(ie.getLeftOperand(), ie.getRightOperand());
    if (isComparingObjects(ie) && !isNullLiteral(ie.getLeftOperand()) && !isNullLiteral(ie.getRightOperand())) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        final Refactorings r = this.ctx.getRefactorings();
        final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Same"), b.copy(actualAndExpected.getFirst()), b.copy(actualAndExpected.getSecond()), failureMessage);
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
        return DO_NOT_VISIT_SUBTREE;
    } else {
        return maybeRefactorToAssertEquals(nodeToReplace, originalMethod, isAssertEquals, actualAndExpected.getFirst(), actualAndExpected.getSecond(), failureMessage, true);
    }
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 29 with MethodInvocation

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

the class JUnitAssertRefactoring method visit.

@Override
public boolean visit(IfStatement node) {
    final List<Statement> stmts = asList(node.getThenStatement());
    if (node.getElseStatement() == null && stmts.size() == 1) {
        final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
        int i = 0;
        boolean shouldVisit = VISIT_SUBTREE;
        while (shouldVisit == VISIT_SUBTREE && i < PACKAGE_PATHES.length) {
            shouldVisit = maybeRefactorIf(node, mi, PACKAGE_PATHES[i]);
            i++;
        }
        return shouldVisit;
    }
    return VISIT_SUBTREE;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 30 with MethodInvocation

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

the class UpdateSetRatherThanTestingFirstRefactoring method maybeReplaceSetContains.

private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpr, final Statement stmt, final Statement oppositeStmt, final boolean negate, final String methodName) {
    final List<Statement> stmts = asList(stmt);
    final MethodInvocation miContains = as(ifExpr, MethodInvocation.class);
    if (!stmts.isEmpty() && isMethod(miContains, "java.util.Set", "contains", "java.lang.Object")) {
        final Statement firstStmt = getFirst(stmts);
        final MethodInvocation miAddOrRemove = asExpression(firstStmt, MethodInvocation.class);
        final ASTMatcher astMatcher = new ASTMatcher();
        if (isMethod(miAddOrRemove, "java.util.Set", methodName, "java.lang.Object") && match(astMatcher, miContains.getExpression(), miAddOrRemove.getExpression()) && match(astMatcher, arg0(miContains), arg0(miAddOrRemove))) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            final Refactorings r = this.ctx.getRefactorings();
            if (stmts.size() == 1 && asList(oppositeStmt).isEmpty()) {
                // Only one statement: replace if statement with col.add() (or col.remove())
                r.replace(ifStmtToReplace, b.move(firstStmt));
                return DO_NOT_VISIT_SUBTREE;
            } else {
                // There are other statements, replace the if condition with col.add() (or col.remove())
                r.replace(ifStmtToReplace.getExpression(), negate ? b.negate(miAddOrRemove, ASTBuilder.Copy.MOVE) : b.move(miAddOrRemove));
                r.remove(firstStmt);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Aggregations

MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)265 Expression (org.eclipse.jdt.core.dom.Expression)112 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)59 ASTNode (org.eclipse.jdt.core.dom.ASTNode)53 SimpleName (org.eclipse.jdt.core.dom.SimpleName)53 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)51 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)48 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)47 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)43 ArrayList (java.util.ArrayList)40 CastExpression (org.eclipse.jdt.core.dom.CastExpression)40 TextEditGroup (org.eclipse.text.edits.TextEditGroup)37 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)34 Block (org.eclipse.jdt.core.dom.Block)33 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)33 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)31 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)31 AST (org.eclipse.jdt.core.dom.AST)30 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)29 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)28