Search in sources :

Example 21 with ASTNodeFactory

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

the class StringCleanUp method replaceStringValueOfByArg0.

private void replaceStringValueOfByArg0(final Expression toReplace, final MethodInvocation methodInvocation, final ITypeBinding expectedType) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.StringCleanUp_description);
    ITypeBinding actualType = ((Expression) methodInvocation.arguments().get(0)).resolveTypeBinding();
    if (ASTNodes.hasType(actualType, String.class.getCanonicalName()) || expectedType.equals(actualType) || Bindings.getBoxedTypeBinding(expectedType, methodInvocation.getAST()).equals(actualType)) {
        rewrite.replace(toReplace, ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0))), group);
    } else {
        rewrite.replace(toReplace, ast.newCastExpression(ast.type(expectedType.getQualifiedName()), ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0))), group);
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 22 with ASTNodeFactory

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

the class StringCleanUp method removeToString.

private void removeToString(final MethodInvocation visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.StringCleanUp_description);
    if (visited.getExpression() != null) {
        rewrite.replace(visited, ASTNodes.createMoveTarget(rewrite, visited.getExpression()), group);
    } else {
        rewrite.replace(visited, ast.newThisExpression(), 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 23 with ASTNodeFactory

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

the class TruncatingAppendingRatherThanSubCharactersCleanUp method replaceWithAppendSubstring.

private void replaceWithAppendSubstring(final MethodInvocation visited, final MethodInvocation truncatingMethod) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.TruncatingAppendingRatherThanSubCharactersCleanUp_description);
    Expression builder = ASTNodes.createMoveTarget(rewrite, visited.getExpression());
    Expression stringVar = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(truncatingMethod.getExpression()));
    List<Expression> args = truncatingMethod.arguments();
    Expression arg0 = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(args.get(0)));
    Expression arg1 = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(args.get(1)));
    MethodInvocation appendMethod = ast.newMethodInvocation();
    appendMethod.setExpression(builder);
    // $NON-NLS-1$
    appendMethod.setName(ast.newSimpleName("append"));
    appendMethod.arguments().add(stringVar);
    appendMethod.arguments().add(arg0);
    appendMethod.arguments().add(arg1);
    rewrite.replace(visited, appendMethod, group);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) 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 24 with ASTNodeFactory

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

the class UpdateSetRatherThanTestingFirstCleanUp method maybeReplaceSetContains.

private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpression, final Statement statement, final Statement oppositeStatement, final boolean negate, final String methodName) {
    List<Statement> statements = ASTNodes.asList(statement);
    MethodInvocation miContains = ASTNodes.as(ifExpression, MethodInvocation.class);
    if (!statements.isEmpty() && ASTNodes.usesGivenSignature(miContains, Set.class.getCanonicalName(), "contains", Object.class.getCanonicalName())) {
        // $NON-NLS-1$
        Statement firstStatement = statements.get(0);
        MethodInvocation miAddOrRemove = ASTNodes.asExpression(firstStatement, MethodInvocation.class);
        if (ASTNodes.usesGivenSignature(miAddOrRemove, Set.class.getCanonicalName(), methodName, Object.class.getCanonicalName()) && ASTNodes.match(miContains.getExpression(), miAddOrRemove.getExpression()) && ASTNodes.match(((List<Expression>) miContains.arguments()).get(0), ((List<Expression>) miAddOrRemove.arguments()).get(0))) {
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            TextEditGroup group = new TextEditGroup(MultiFixMessages.UpdateSetRatherThanTestingFirstCleanUp_description);
            if (statements.size() == 1 && ASTNodes.asList(oppositeStatement).isEmpty()) {
                // Only one statement: replace if statement with col.add() (or col.remove())
                ASTNodes.replaceButKeepComment(rewrite, ifStmtToReplace, ASTNodes.createMoveTarget(rewrite, firstStatement), group);
            } else {
                // There are other statements, replace the if condition with col.add() (or
                // col.remove())
                rewrite.replace(ifStmtToReplace.getExpression(), negate ? ast.negate(miAddOrRemove, true) : ASTNodes.createMoveTarget(rewrite, miAddOrRemove), group);
                rewrite.remove(firstStatement, group);
            }
            return false;
        }
    }
    return true;
}
Also used : Set(java.util.Set) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) List(java.util.List) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 25 with ASTNodeFactory

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

the class UseStringContainsCleanUp method replaceWithStringContains.

private void replaceWithStringContains(final InfixExpression visited, final MethodInvocation method, final boolean isPositive) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.UseStringContainsCleanUp_description);
    // $NON-NLS-1$
    rewrite.set(method, MethodInvocation.NAME_PROPERTY, ast.newSimpleName("contains"), group);
    if (isPositive) {
        rewrite.replace(visited, ASTNodes.createMoveTarget(rewrite, method), group);
    } else {
        rewrite.replace(visited, ast.not(ASTNodes.createMoveTarget(rewrite, method)), group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) 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