Search in sources :

Example 66 with ASTNodeFactory

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

the class ObsoletePrimitiveComparisonRatherThanWrapperComparisonCleanUp method refactor.

private void refactor(final MethodInvocation visited, final Expression primitiveValue, final Class<?> wrapperClass) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoletePrimitiveComparisonRatherThanWrapperComparisonCleanUp_description);
    MethodInvocation compareMethod = ast.newMethodInvocation();
    compareMethod.setExpression(ast.newSimpleName(wrapperClass.getSimpleName()));
    // $NON-NLS-1$
    compareMethod.setName(ast.newSimpleName("compare"));
    compareMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(primitiveValue)));
    compareMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) visited.arguments().get(0))));
    ASTNodes.replaceButKeepComment(rewrite, visited, compareMethod, group);
}
Also used : 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 67 with ASTNodeFactory

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

the class ObsoleteReduceIndentationCleanUp method moveElseStatement.

private void moveElseStatement(final IfStatement visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteReduceIndentationCleanUp_description);
    List<Statement> statementsToMove = ASTNodes.asList(visited.getElseStatement());
    if (ASTNodes.canHaveSiblings(visited)) {
        for (int i = statementsToMove.size() - 1; i >= 0; i--) {
            rewrite.insertAfter(ASTNodes.createMoveTarget(rewrite, statementsToMove.get(i)), visited, group);
        }
        rewrite.remove(visited.getElseStatement(), group);
    } else {
        List<Statement> copyOfStatements = new ArrayList<>(statementsToMove.size() + 1);
        for (Statement statement : statementsToMove) {
            copyOfStatements.add(ASTNodes.createMoveTarget(rewrite, statement));
        }
        rewrite.remove(visited.getElseStatement(), group);
        copyOfStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
        Block newBlock = ast.newBlock();
        newBlock.statements().addAll(copyOfStatements);
        ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
    }
}
Also used : ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 68 with ASTNodeFactory

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

the class ObsoleteValueOfRatherThanInstantiationCleanUp method replaceWithValueOf.

private void replaceWithValueOf(final ClassInstanceCreation visited, final ITypeBinding typeBinding, final Expression arg0) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteValueOfRatherThanInstantiationCleanUp_description);
    MethodInvocation valueOfMethod = ast.newMethodInvocation();
    valueOfMethod.setExpression(ASTNodeFactory.newName(ast, typeBinding.getName()));
    // $NON-NLS-1$
    valueOfMethod.setName(ast.newSimpleName("valueOf"));
    valueOfMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arg0)));
    ASTNodes.replaceButKeepComment(rewrite, visited, valueOfMethod, group);
}
Also used : 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 69 with ASTNodeFactory

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

the class ObsoleteValueOfRatherThanInstantiationCleanUp method replaceFloatWithFloatValue.

private void replaceFloatWithFloatValue(final ClassInstanceCreation visited, final Expression arg0) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteValueOfRatherThanInstantiationCleanUp_description);
    MethodInvocation floatValueMethod = ast.newMethodInvocation();
    floatValueMethod.setExpression(ASTNodes.createMoveTarget(rewrite, arg0));
    // $NON-NLS-1$
    floatValueMethod.setName(ast.newSimpleName("floatValue"));
    ASTNodes.replaceButKeepComment(rewrite, visited, floatValueMethod, group);
}
Also used : 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 70 with ASTNodeFactory

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

the class ObsoleteLambdaCleanUp method replaceByTypeReference.

private void replaceByTypeReference(final LambdaExpression node, final MethodInvocation methodInvocation) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    TypeNameDecider typeNameDecider = new TypeNameDecider(methodInvocation);
    TypeMethodReference typeMethodRef = ast.newTypeMethodReference();
    typeMethodRef.setType(ast.toType(ASTNodes.getCalledType(methodInvocation).getErasure(), typeNameDecider));
    typeMethodRef.setName(ASTNodes.createMoveTarget(rewrite, methodInvocation.getName()));
    ASTNodes.replaceButKeepComment(rewrite, node, typeMethodRef, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TypeMethodReference(org.eclipse.jdt.core.dom.TypeMethodReference) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider) 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