Search in sources :

Example 11 with ASTRewrite

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

the class AbstractPrimitiveRatherThanWrapperCleanUp method refactorWrapper.

private void refactorWrapper(final VariableDeclarationStatement visited, final Expression initializer, final List<MethodInvocation> toStringMethods, final List<MethodInvocation> compareToMethods, final List<MethodInvocation> primitiveValueMethods) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    // $NON-NLS-1$
    TextEditGroup group = new TextEditGroup("");
    String parsingMethodName = getParsingMethodName(getWrapperFullyQualifiedName());
    if (initializer instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) initializer;
        if (ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", getPrimitiveTypeName())) {
            // $NON-NLS-1$
            rewrite.replace(methodInvocation, ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0)), group);
        }
        if (// $NON-NLS-1$
        ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName()) || ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName(), int.class.getSimpleName())) {
            // $NON-NLS-1$
            rewrite.set(methodInvocation, MethodInvocation.NAME_PROPERTY, ast.newSimpleName(parsingMethodName), group);
        }
    } else if (initializer instanceof ClassInstanceCreation) {
        ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) initializer;
        List<Expression> classInstanceCreationArguments = classInstanceCreation.arguments();
        if (classInstanceCreationArguments.size() == 1 && parsingMethodName != null && !Character.class.getCanonicalName().equals(getWrapperFullyQualifiedName()) && ASTNodes.hasType(classInstanceCreation, getWrapperFullyQualifiedName())) {
            Expression arg0 = classInstanceCreationArguments.get(0);
            if (ASTNodes.hasType(arg0, String.class.getCanonicalName())) {
                MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                newMethodInvocation.setExpression(rewrite.createCopyTarget(((SimpleType) visited.getType()).getName()));
                newMethodInvocation.setName(ast.newSimpleName(parsingMethodName));
                newMethodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arg0)));
                ASTNodes.replaceButKeepComment(rewrite, initializer, newMethodInvocation, group);
            }
        }
    }
    for (MethodInvocation primitiveValueMethod : primitiveValueMethods) {
        rewrite.replace(primitiveValueMethod, ASTNodes.createMoveTarget(rewrite, primitiveValueMethod.getExpression()), group);
    }
    for (MethodInvocation toStringMethod : toStringMethods) {
        Type wrapperType = rewrite.createCopyTarget(visited.getType());
        rewrite.insertFirst(toStringMethod, MethodInvocation.ARGUMENTS_PROPERTY, ASTNodes.createMoveTarget(rewrite, toStringMethod.getExpression()), group);
        rewrite.set(toStringMethod, MethodInvocation.EXPRESSION_PROPERTY, wrapperType, group);
    }
    for (MethodInvocation compareToMethod : compareToMethods) {
        Type wrapperType = rewrite.createCopyTarget(visited.getType());
        rewrite.insertFirst(compareToMethod, MethodInvocation.ARGUMENTS_PROPERTY, ASTNodes.createMoveTarget(rewrite, compareToMethod.getExpression()), group);
        rewrite.set(compareToMethod, MethodInvocation.EXPRESSION_PROPERTY, wrapperType, group);
        // $NON-NLS-1$
        rewrite.replace(compareToMethod.getName(), ast.newSimpleName("compare"), group);
    }
    Type primitiveType = ast.type(getPrimitiveTypeName());
    ASTNodes.replaceButKeepComment(rewrite, visited.getType(), primitiveType, group);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ArrayList(java.util.ArrayList) List(java.util.List) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 12 with ASTRewrite

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

the class AndroidWakeLockCleanUp method visit.

@Override
public boolean visit(final MethodInvocation node) {
    if (ASTNodes.usesGivenSignature(node, "android.os.PowerManager.WakeLock", "release")) {
        // $NON-NLS-1$ //$NON-NLS-2$
        // Check whether it is being called in onDestroy()
        MethodDeclaration enclosingMethod = ASTNodes.getTypedAncestor(node, MethodDeclaration.class);
        if (ASTNodes.usesGivenSignature(enclosingMethod, "android.app.Activity", "onDestroy")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            TextEditGroup group = new TextEditGroup(MultiFixMessages.AndroidWakeLockCleanUp_description);
            TypeDeclaration typeDeclaration = ASTNodes.getTypedAncestor(enclosingMethod, TypeDeclaration.class);
            // $NON-NLS-1$
            MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
            if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                rewrite.remove(node.getParent(), group);
                rewrite.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY, createWakelockReleaseStatement(node), group);
            } else {
                // Add the missing onPause() method to the class.
                rewrite.insertAfter(createOnPauseMethodDeclaration(), enclosingMethod, group);
            }
            return false;
        }
    } else if (ASTNodes.usesGivenSignature(node, "android.os.PowerManager.WakeLock", "acquire")) {
        // $NON-NLS-1$ //$NON-NLS-2$
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.AndroidWakeLockCleanUp_description);
        TypeDeclaration typeDeclaration = ASTNodes.getTypedAncestor(node, TypeDeclaration.class);
        ReleasePresenceChecker releasePresenceChecker = new ReleasePresenceChecker();
        if (!releasePresenceChecker.findOrDefault(typeDeclaration, false)) {
            // $NON-NLS-1$
            MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
            if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                rewrite.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY, createWakelockReleaseStatement(node), group);
            } else {
                rewrite.insertLast(typeDeclaration, typeDeclaration.getBodyDeclarationsProperty(), createOnPauseMethodDeclaration(), group);
            }
            return false;
        }
    }
    return true;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 13 with ASTRewrite

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

the class AbstractUnitTestCleanUp method invokeAssertNull.

private MethodInvocation invokeAssertNull(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final MethodInvocation originalMethod, final boolean isPositive, final Expression actual, final Expression failureMessage) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    // $NON-NLS-1$
    String methodName = getAssertName(isPositive, "Null");
    Expression copyOfActual = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actual));
    return invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, methodName, copyOfActual, null, null, failureMessage);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 14 with ASTRewrite

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

the class AbstractUnitTestCleanUp method refactorToAssertTrueOrFalse.

private void refactorToAssertTrueOrFalse(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final Expression failureMessage, final Expression condition, final boolean isAssertTrue) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    // $NON-NLS-1$
    TextEditGroup group = new TextEditGroup("");
    // $NON-NLS-1$ //$NON-NLS-2$
    String methodName = isAssertTrue ? "assertTrue" : "assertFalse";
    ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, methodName, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(condition)), null, null, failureMessage)), group);
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 15 with ASTRewrite

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

the class AbstractUnitTestCleanUp method maybeRefactorToEquality.

private boolean maybeRefactorToEquality(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertEquals, final Expression actualValue, final Expression expectedValue, final Expression failureMessage, final boolean isRewriteNeeded) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    // $NON-NLS-1$
    TextEditGroup group = new TextEditGroup("");
    if (ASTNodes.is(actualValue, NullLiteral.class)) {
        ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, invokeAssertNull(classesToUseWithImport, importsToAdd, originalMethod, isAssertEquals, expectedValue, failureMessage)), group);
        return false;
    }
    if (ASTNodes.is(expectedValue, NullLiteral.class)) {
        ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, invokeAssertNull(classesToUseWithImport, importsToAdd, originalMethod, isAssertEquals, actualValue, failureMessage)), group);
        return false;
    }
    Expression copyOfExpected;
    Expression copyOfActual;
    boolean localIsRewriteNeeded;
    if ((ASTNodes.isHardCoded(actualValue) || isVariableNamedExpected(actualValue)) && !ASTNodes.isHardCoded(expectedValue) && !isVariableNamedExpected(expectedValue)) {
        copyOfExpected = ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(actualValue));
        copyOfActual = ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(expectedValue));
        localIsRewriteNeeded = true;
    } else {
        copyOfExpected = ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(expectedValue));
        copyOfActual = ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(actualValue));
        localIsRewriteNeeded = isRewriteNeeded;
    }
    if (localIsRewriteNeeded) {
        Expression delta = null;
        if (ASTNodes.hasType(actualValue, double.class.getCanonicalName()) && ASTNodes.hasType(expectedValue, double.class.getCanonicalName())) {
            // $NON-NLS-1$
            delta = ast.newNumberLiteral(".0");
        } else if (ASTNodes.hasType(actualValue, float.class.getCanonicalName()) && ASTNodes.hasType(expectedValue, float.class.getCanonicalName())) {
            // $NON-NLS-1$
            delta = ast.newNumberLiteral(".0F");
        }
        MethodInvocation newAssert = invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, getAssertName(isAssertEquals, "Equals"), copyOfActual, copyOfExpected, delta, // $NON-NLS-1$
        failureMessage);
        ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, newAssert), group);
        return false;
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) 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