Search in sources :

Example 86 with ASTRewrite

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

the class RemoveEmptyLinesCleanUp method visit.

@Override
public boolean visit(final CompilationUnit visited) {
    lineEnds.clear();
    String source = cuRewrite.getSource(visited);
    if (source.isEmpty()) {
        // Empty file, bail out
        return true;
    }
    computeLineEnds(visited);
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    int index = getIndexOfFirstNonWhitespaceChar(source, 0);
    if (index != -1) {
        rewrite.remove(SourceLocation.fromPositions(0, index));
        return false;
    }
    if (visited.getPackage() != null) {
        int lastIndex = visited.getPackage().getStartPosition();
        int lastNonWsIndex = getLastIndexOfNonWhitespaceChar(source, lastIndex - 1);
        if (lastNonWsIndex != -1) {
            int endOfLineIndex = beforeNewlineChars(source, lastNonWsIndex);
            if (maybeRemoveEmptyLines(source, endOfLineIndex, lastIndex)) {
                return false;
            }
        }
    }
    boolean result = true;
    // $NON-NLS-1$ //$NON-NLS-2$
    String newline = "(?:" + NEWLINE_PATTERN + ")";
    Matcher m = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
    Pattern.compile("(" + newline + "\\s*?" + newline + "\\s*?" + ")" + "(?:" + newline + "\\s*?)+").matcher(source);
    while (m.find()) {
        String matchedString = m.group(0);
        if (// $NON-NLS-1$
        !"\r\n\r\n".equals(matchedString) && // $NON-NLS-1$
        !"\n\n".equals(matchedString) && !"\r\r".equals(matchedString)) {
            // $NON-NLS-1$
            rewrite.remove(SourceLocation.fromPositions(m.end(1), m.end(0)));
            result = false;
        }
    }
    if (!result) {
        return false;
    }
    int afterLastNonWsIndex = getLastIndexOfNonWhitespaceChar(source, source.length() - 1) + 1;
    if (substringMatchesAny(source, afterLastNonWsIndex, "\r\n", "\r", "\n")) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        return true;
    }
    Matcher endOfFileMatcher = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    Pattern.compile(newline + "(" + "\\s*?" + "(" + newline + "\\s*?)+)").matcher(source).region(afterLastNonWsIndex, source.length());
    if (endOfFileMatcher.find()) {
        rewrite.remove(SourceLocation.fromPositions(endOfFileMatcher.start(2), endOfFileMatcher.end(2)));
        return false;
    }
    return true;
}
Also used : Matcher(java.util.regex.Matcher) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 87 with ASTRewrite

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

Example 88 with ASTRewrite

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

the class ObsoleteLambdaCleanUp method replaceByCreationReference.

private void replaceByCreationReference(final LambdaExpression node, final ClassInstanceCreation ci) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    TypeNameDecider typeNameDecider = new TypeNameDecider(ci);
    CreationReference creationRef = ast.newCreationReference();
    creationRef.setType(ast.toType(ci.resolveTypeBinding().getErasure(), typeNameDecider));
    ASTNodes.replaceButKeepComment(rewrite, node, creationRef, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) CreationReference(org.eclipse.jdt.core.dom.CreationReference) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 89 with ASTRewrite

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

the class ObsoleteLazyLogicalRatherThanEagerCleanUp method replaceWithLazyOperator.

private void replaceWithLazyOperator(final InfixExpression node, final List<Expression> allOperands) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLazyLogicalRatherThanEagerCleanUp_description);
    InfixExpression.Operator lazyOperator;
    if (ASTNodes.hasOperator(node, InfixExpression.Operator.AND)) {
        lazyOperator = InfixExpression.Operator.CONDITIONAL_AND;
    } else {
        lazyOperator = InfixExpression.Operator.CONDITIONAL_OR;
    }
    ASTNodes.replaceButKeepComment(rewrite, node, ast.newInfixExpression(lazyOperator, ASTNodes.createMoveTarget(rewrite, allOperands)), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 90 with ASTRewrite

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

the class ObsoleteSerializeRatherThanBoxingAndSerializeCleanUp 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.ObsoleteSerializeRatherThanBoxingAndSerializeCleanUp_description);
    MethodInvocation toStringMethod = ast.newMethodInvocation();
    toStringMethod.setExpression(ast.newSimpleName(wrapperClass.getSimpleName()));
    // $NON-NLS-1$
    toStringMethod.setName(ast.newSimpleName("toString"));
    toStringMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(primitiveValue)));
    ASTNodes.replaceButKeepComment(rewrite, visited, toStringMethod, 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)

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