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