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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations