Search in sources :

Example 81 with ASTRewrite

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

the class ObsoleteUseDiamondOperatorCleanUp method visit.

@Override
public boolean visit(final ClassInstanceCreation node) {
    Type type = node.getType();
    if (type.isParameterizedType() && node.getAnonymousClassDeclaration() == null && ASTNodes.getTargetType(node) != null && canUseDiamondOperator(node, type)) {
        List<Type> typeArguments = ((ParameterizedType) type).typeArguments();
        if (!typeArguments.isEmpty()) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteUseDiamondOperatorCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(typeArguments, group);
            return false;
        }
    }
    return true;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 82 with ASTRewrite

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

the class ObsoleteValueOfRatherThanInstantiationCleanUp method replaceWithTheSingleArgument.

private void replaceWithTheSingleArgument(final ClassInstanceCreation visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteValueOfRatherThanInstantiationCleanUp_description);
    ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, (Expression) visited.arguments().get(0)), group);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 83 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite 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 84 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite 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 85 with ASTRewrite

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

the class RemoveEmptyLinesCleanUp method maybeRemoveEmptyLines.

private boolean maybeRemoveEmptyLines(final String source, final int endOfLineIndex, final int newLineIndex) {
    if (endOfLineIndex < newLineIndex) {
        Matcher matcher = NEWLINE_PATTERN.matcher(source).region(endOfLineIndex, newLineIndex);
        boolean isEqualToNewline = matcher.matches();
        if (!isEqualToNewline && matcher.find() && matcher.end() < newLineIndex) {
            SourceLocation toRemove = SourceLocation.fromPositions(matcher.end(), newLineIndex);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(toRemove);
            return true;
        }
    }
    return false;
}
Also used : SourceLocation(org.autorefactor.jdt.internal.corext.dom.SourceLocation) Matcher(java.util.regex.Matcher) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

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