Search in sources :

Example 16 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class MergeConditionalBlocksRefactoring method maybeMergeBlocks.

private boolean maybeMergeBlocks(final Expression firstCondition, final List<Statement> ifCode, final IfStatement subNode, final Expression secondCondition, final Statement doubleStmts, final Statement remainingStmts, final boolean isPositive) {
    if (isSameCode(ifCode, asList(doubleStmts))) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        final Refactorings r = this.ctx.getRefactorings();
        final Expression additionalCondition;
        if (isPositive) {
            additionalCondition = b.copy(secondCondition);
        } else {
            additionalCondition = b.negate(secondCondition, Copy.COPY);
        }
        r.replace(firstCondition, b.infixExpr(b.parenthesizeIfNeeded(b.copy(firstCondition)), InfixExpression.Operator.CONDITIONAL_OR, b.parenthesizeIfNeeded(additionalCondition)));
        if (remainingStmts != null) {
            r.replace(subNode, b.copy(remainingStmts));
        } else {
            r.remove(subNode);
        }
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 17 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class RemoveEmptyLinesRefactoring method visit.

@Override
public boolean visit(CompilationUnit node) {
    final String source = this.ctx.getSource(node);
    if (source.length() == 0) {
        // empty file, bail out
        return VISIT_SUBTREE;
    }
    computeLineEnds(node);
    final Refactorings r = this.ctx.getRefactorings();
    int index = getIndexOfFirstNonWhitespaceChar(source, 0);
    if (index != -1) {
        r.remove(SourceLocation.fromPositions(0, index));
        return DO_NOT_VISIT_SUBTREE;
    }
    if (node.getPackage() != null) {
        int lastIndex = node.getPackage().getStartPosition();
        int lastNonWsIndex = getLastIndexOfNonWhitespaceChar(source, lastIndex - 1);
        if (lastNonWsIndex != -1) {
            int endOfLineIndex = beforeNewlineChars(source, lastNonWsIndex);
            if (maybeRemoveEmptyLines(source, endOfLineIndex, lastIndex)) {
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    boolean result = VISIT_SUBTREE;
    final String newline = "(?:" + NEWLINE_PATTERN + ")";
    Matcher m = Pattern.compile("(" + newline + "\\s*?" + newline + "\\s*?" + ")" + "(?:" + newline + "\\s*?)+").matcher(source);
    while (m.find()) {
        final String matchedString = m.group(0);
        if (!"\r\n\r\n".equals(matchedString) && !"\n\n".equals(matchedString) && !"\r\r".equals(matchedString)) {
            r.remove(SourceLocation.fromPositions(m.end(1), m.end(0)));
            result = DO_NOT_VISIT_SUBTREE;
        }
    }
    if (result == DO_NOT_VISIT_SUBTREE) {
        return DO_NOT_VISIT_SUBTREE;
    }
    int afterLastNonWsIndex = getLastIndexOfNonWhitespaceChar(source, source.length() - 1) + 1;
    if (substringMatchesAny(source, afterLastNonWsIndex, "\r\n", "\r", "\n")) {
        return VISIT_SUBTREE;
    }
    Matcher endOfFileMatcher = Pattern.compile(newline + "(" + "\\s*?" + "(" + newline + "\\s*?)+)").matcher(source).region(afterLastNonWsIndex, source.length());
    if (endOfFileMatcher.find()) {
        r.remove(SourceLocation.fromPositions(endOfFileMatcher.start(2), endOfFileMatcher.end(2)));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : Matcher(java.util.regex.Matcher) Refactorings(org.autorefactor.refactoring.Refactorings)

Example 18 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class UseStringContainsRefactoring method replaceWithStringContains.

private boolean replaceWithStringContains(InfixExpression ie, MethodInvocation node, boolean negate) {
    final Refactorings r = this.ctx.getRefactorings();
    final ASTBuilder b = this.ctx.getASTBuilder();
    r.set(node, MethodInvocation.NAME_PROPERTY, b.simpleName("contains"));
    if (negate) {
        r.replace(ie, b.not(b.move(node)));
    } else {
        r.replace(ie, b.move(node));
    }
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 19 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class IsEmptyRatherThanSizeRefactoring method replaceCollectionSize.

private boolean replaceCollectionSize(final InfixExpression node, final MethodInvocation miToReplace, final Operator operator, final long literalSize) {
    final Refactorings r = this.ctx.getRefactorings();
    final ASTBuilder b = this.ctx.getASTBuilder();
    if (literalSize == 0) {
        if (GREATER_EQUALS.equals(operator)) {
            r.replace(node, b.boolean0(true));
            return DO_NOT_VISIT_SUBTREE;
        } else if (LESS.equals(operator)) {
            r.replace(node, b.boolean0(false));
        } else if (GREATER.equals(operator)) {
            r.replace(node, b.not(b.invoke(b.copyExpression(miToReplace), "isEmpty")));
            return DO_NOT_VISIT_SUBTREE;
        } else if (EQUALS.equals(operator)) {
            r.replace(node, b.invoke(b.copyExpression(miToReplace), "isEmpty"));
            return DO_NOT_VISIT_SUBTREE;
        } else if (NOT_EQUALS.equals(operator)) {
            r.replace(node, b.not(b.invoke(b.copyExpression(miToReplace), "isEmpty")));
            return DO_NOT_VISIT_SUBTREE;
        } else if (LESS_EQUALS.equals(operator)) {
            r.replace(node, b.invoke(b.copyExpression(miToReplace), "isEmpty"));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (literalSize == 1) {
        if (GREATER_EQUALS.equals(operator)) {
            r.replace(node, b.not(b.invoke(b.copyExpression(miToReplace), "isEmpty")));
            return DO_NOT_VISIT_SUBTREE;
        } else if (LESS.equals(operator)) {
            r.replace(node, b.invoke(b.copyExpression(miToReplace), "isEmpty"));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 20 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class TryWithResourceRefactoring method refactorToTryWithResources.

private boolean refactorToTryWithResources(TryStatement node, VariableDeclarationExpression newResource, List<ASTNode> nodesToRemove) {
    if (newResource == null) {
        return VISIT_SUBTREE;
    }
    final Refactorings r = ctx.getRefactorings();
    r.insertFirst(node, TryStatement.RESOURCES_PROPERTY, newResource);
    r.remove(nodesToRemove);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings)

Aggregations

Refactorings (org.autorefactor.refactoring.Refactorings)27 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)23 Expression (org.eclipse.jdt.core.dom.Expression)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)6 IfStatement (org.eclipse.jdt.core.dom.IfStatement)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 Statement (org.eclipse.jdt.core.dom.Statement)5 List (java.util.List)3 Block (org.eclipse.jdt.core.dom.Block)3 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)3 ArrayList (java.util.ArrayList)2 TypeNameDecider (org.autorefactor.refactoring.TypeNameDecider)2 Variable (org.autorefactor.refactoring.Variable)2 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 DoStatement (org.eclipse.jdt.core.dom.DoStatement)2 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2