Search in sources :

Example 46 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class AssertJCleanUp method invokeMethod.

@Override
protected MethodInvocation invokeMethod(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final MethodInvocation originalMethod, final String methodName, final Expression copyOfActual, final Expression copyOfExpected, final Expression delta, final Expression failureMessage) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    String qualifiedClassName = originalMethod.resolveMethodBinding().getDeclaringClass().getQualifiedName();
    Expression qualifiedClass;
    if (!FAIL_METHOD.equals(methodName) && FAIL_CLASS.equals(qualifiedClassName)) {
        qualifiedClassName = ASSERTIONS_CLASS;
        qualifiedClass = null;
    } else if (originalMethod.getExpression() != null) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        qualifiedClass = ASTNodes.createMoveTarget(rewrite, originalMethod.getExpression());
    } else {
        qualifiedClass = null;
    }
    if (// $NON-NLS-1$
    originalMethod.getExpression() == null && !staticImports.contains(qualifiedClassName + "." + methodName) && !staticImports.contains(qualifiedClassName + ".*")) {
        // $NON-NLS-1$
        qualifiedClass = ASTNodeFactory.newName(ast, qualifiedClassName);
    }
    if (FAIL_METHOD.equals(methodName)) {
        return invokeFail(failureMessage, qualifiedClass);
    }
    return invokeQualifiedMethod(classesToUseWithImport, importsToAdd, qualifiedClass, methodName, copyOfActual, copyOfExpected, delta, failureMessage);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 47 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class AssertJCleanUp method invokeFail.

private MethodInvocation invokeFail(final Expression failureMessage, final Expression qualifiedClass) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    if (failureMessage != null) {
        MethodInvocation failureMethod = (MethodInvocation) failureMessage;
        List<Expression> copyOfMessages = new ArrayList<>(failureMethod.arguments().size());
        for (Object message : failureMethod.arguments()) {
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            copyOfMessages.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) message)));
        }
        MethodInvocation newMethodInvocation = ast.newMethodInvocation();
        newMethodInvocation.setExpression(qualifiedClass);
        newMethodInvocation.setName(ast.newSimpleName(FAIL_METHOD));
        newMethodInvocation.arguments().addAll(copyOfMessages);
        return newMethodInvocation;
    }
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(qualifiedClass);
    methodInvocation.setName(ast.newSimpleName(FAIL_METHOD));
    methodInvocation.arguments().add(ast.newNullLiteral());
    return methodInvocation;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 48 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class BigNumberCleanUp method replaceWithQualifiedName.

private void replaceWithQualifiedName(final ASTNode visited, final ITypeBinding typeBinding, final String field) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.BigNumberCleanUp_description);
    ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodeFactory.newName(ast, typeBinding.getName(), field), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 49 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class BooleanConstantRatherThanValueOfCleanUp method replaceMethod.

private void replaceMethod(final MethodInvocation visited, final boolean literal) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.BooleanConstantRatherThanValueOfCleanUp_description);
    FieldAccess fieldAccess = ast.getAST().newFieldAccess();
    Name expression = ASTNodes.as(visited.getExpression(), Name.class);
    if (expression != null) {
        fieldAccess.setExpression(ASTNodes.createMoveTarget(rewrite, expression));
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    fieldAccess.setName(ast.newSimpleName(literal ? "TRUE" : "FALSE"));
    rewrite.replace(visited, fieldAccess, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) TextEditGroup(org.eclipse.text.edits.TextEditGroup) Name(org.eclipse.jdt.core.dom.Name)

Example 50 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class CollectionsAddAllRatherThanAsListCleanUp method refactorMethod.

private void refactorMethod(final MethodInvocation visited, final MethodInvocation asListMethod, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.CollectionsAddAllRatherThanAsListCleanUp_description);
    String collectionsName = addImport(Collections.class, classesToUseWithImport, importsToAdd);
    List<Expression> copyOfArguments = new ArrayList<>(asListMethod.arguments().size() + 1);
    copyOfArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())));
    for (Object argument : asListMethod.arguments()) {
        copyOfArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) argument)));
    }
    MethodInvocation newMethodInvocation = ast.newMethodInvocation();
    newMethodInvocation.setExpression(ASTNodeFactory.newName(ast, collectionsName));
    newMethodInvocation.setName(ast.newSimpleName(ADD_ALL_METHOD));
    newMethodInvocation.arguments().addAll(copyOfArguments);
    MethodInvocation newCollectionsAddAllMethod = newMethodInvocation;
    rewrite.replace(visited, newCollectionsAddAllMethod, group);
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)195 TextEditGroup (org.eclipse.text.edits.TextEditGroup)167 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)133 Expression (org.eclipse.jdt.core.dom.Expression)56 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)48 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)42 Statement (org.eclipse.jdt.core.dom.Statement)24 ArrayList (java.util.ArrayList)22 IfStatement (org.eclipse.jdt.core.dom.IfStatement)20 Block (org.eclipse.jdt.core.dom.Block)15 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 Modifier (org.eclipse.jdt.core.dom.Modifier)11 Type (org.eclipse.jdt.core.dom.Type)11 List (java.util.List)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7