Search in sources :

Example 11 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class AndroidWakeLockCleanUp method createWakelockReleaseStatement.

private Statement createWakelockReleaseStatement(final MethodInvocation methodInvocation) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    IfStatement newIfStatement = ast.newIfStatement();
    MethodInvocation isHeldMethod = ast.newMethodInvocation();
    isHeldMethod.setExpression(ast.copyExpression(methodInvocation));
    // $NON-NLS-1$
    isHeldMethod.setName(ast.newSimpleName("isHeld"));
    newIfStatement.setExpression(ast.not(isHeldMethod));
    Block newBlock = ast.newBlock();
    MethodInvocation releaseMethod = ast.newMethodInvocation();
    releaseMethod.setExpression(ast.copyExpression(methodInvocation));
    // $NON-NLS-1$
    releaseMethod.setName(ast.newSimpleName("release"));
    newBlock.statements().add(ast.newExpressionStatement(releaseMethod));
    newIfStatement.setThenStatement(newBlock);
    return newIfStatement;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) Block(org.eclipse.jdt.core.dom.Block) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 12 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class AbstractEnumCollectionReplacementCleanUp method handleAssignment.

private boolean handleAssignment(final ClassInstanceCreation node, final Assignment a, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    Expression leftHandSide = a.getLeftHandSide();
    if (isTargetType(leftHandSide.resolveTypeBinding())) {
        ITypeBinding[] typeArguments = leftHandSide.resolveTypeBinding().getTypeArguments();
        if (typeArguments.length > 0 && typeArguments[0].isEnum()) {
            TypeNameDecider typeNameDecider = new TypeNameDecider(leftHandSide);
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            Type[] types = new Type[typeArguments.length];
            for (int i = 0; i < types.length; i++) {
                types[i] = ast.toType(typeArguments[i], typeNameDecider);
            }
            return maybeReplace(node, classesToUseWithImport, importsToAdd, types);
        }
    }
    return true;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)

Example 13 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory 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)

Example 14 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class SingleDeclarationsRatherThanMultiDeclarationCleanUp method refactorMultiDeclaration.

private void refactorMultiDeclaration(final ASTNode visited, final List<IExtendedModifier> modifiers, final Type type, final List<VariableDeclarationFragment> fragments, final Javadoc docComment) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.SingleDeclarationsRatherThanMultiDeclarationCleanUp_description);
    for (int i = fragments.size() - 1; 0 <= i; i--) {
        VariableDeclarationFragment fragment = fragments.get(i);
        SimpleName copyOfFragment = ASTNodes.createMoveTarget(rewrite, fragment.getName());
        Type copyOfType = ast.createCopyTarget(type);
        Expression copyOfInitializer;
        if (fragment.getInitializer() != null) {
            copyOfInitializer = ASTNodes.createMoveTarget(rewrite, fragment.getInitializer());
        } else {
            copyOfInitializer = null;
        }
        VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(copyOfFragment, copyOfInitializer);
        newFragment.extraDimensions().addAll(ASTNodes.createMoveTarget(rewrite, fragment.extraDimensions()));
        ASTNode newNode;
        if (visited instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement(copyOfType, newFragment);
            updateModifiers(modifiers, newStatement.modifiers());
            newNode = newStatement;
        } else {
            FieldDeclaration newField = ast.newFieldDeclaration(copyOfType, newFragment);
            if (docComment != null) {
                newField.setJavadoc(ast.createCopyTarget(docComment));
            }
            updateModifiers(modifiers, newField.modifiers());
            newNode = newField;
        }
        if (i > 0) {
            rewrite.insertAfter(newNode, visited, group);
        } else {
            ASTNodes.replaceButKeepComment(rewrite, visited, newNode, group);
        }
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) TextEditGroup(org.eclipse.text.edits.TextEditGroup) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 15 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class StringBuilderCleanUp method replaceWithString.

private void replaceWithString(final Type type, final Expression initializer, final List<Pair<ITypeBinding, Expression>> allAppendedStrings, final List<Statement> statementsToRemove, final List<MethodInvocation> toStringToRefactor) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
    rewrite.replace(initializer, createStringConcats(allAppendedStrings), group);
    ASTNodes.replaceButKeepComment(rewrite, type, ast.type(String.class.getSimpleName()), group);
    for (Statement statementToRemove : statementsToRemove) {
        ASTNodes.removeButKeepComment(rewrite, statementToRemove, group);
    }
    for (MethodInvocation readToRefactor : toStringToRefactor) {
        rewrite.replace(readToRefactor, ASTNodes.createMoveTarget(rewrite, readToRefactor.getExpression()), group);
    }
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) 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

ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)152 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)133 TextEditGroup (org.eclipse.text.edits.TextEditGroup)115 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 Expression (org.eclipse.jdt.core.dom.Expression)44 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)37 ArrayList (java.util.ArrayList)19 Block (org.eclipse.jdt.core.dom.Block)17 Statement (org.eclipse.jdt.core.dom.Statement)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)16 Type (org.eclipse.jdt.core.dom.Type)14 TypeNameDecider (org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 List (java.util.List)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Name (org.eclipse.jdt.core.dom.Name)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)6