use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class FormattedNumberRatherThanPackedNumberCleanUp method refactorNumber.
private void refactorNumber(final NumberLiteral visited, final String token) {
// $NON-NLS-1$ //$NON-NLS-2$
StringBuilder integers = new StringBuilder(token.replaceFirst("^(\\d{4,})[lLdDfF]?$", "$1"));
// $NON-NLS-1$ //$NON-NLS-2$
String suffix = token.replaceFirst("^\\d{4,}([lLdDfF]?)$", "$1");
int position = integers.length() - 3;
while (position > 0) {
integers.insert(position, '_');
position = position - 3;
}
ASTNodeFactory ast = cuRewrite.getASTBuilder();
NumberLiteral replacement = ast.newNumberLiteral(integers + suffix);
TextEditGroup group = new TextEditGroup(MultiFixMessages.FormattedNumberRatherThanPackedNumberCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.replace(visited, replacement, group);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class InlineCodeRatherThanPeremptoryConditionCleanUp method remove.
private void remove(final Statement visited) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.InlineCodeRatherThanPeremptoryConditionCleanUp_description);
if (ASTNodes.canHaveSiblings(visited) || visited.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
rewrite.remove(visited, group);
} else {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
ASTNodes.replaceButKeepComment(rewrite, visited, ast.newBlock(), group);
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class LogParametersRatherThanLogMessageCleanUp method replaceConcatenation.
private void replaceConcatenation(final MethodInvocation visited, final String methodName, final StringBuilder messageBuilder, final List<Expression> params) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.LogParametersRatherThanLogMessageCleanUp_description);
params.add(0, ast.newStringLiteral(messageBuilder.toString()));
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
newMethodInvocation.setExpression(ASTNodes.createMoveTarget(rewrite, visited.getExpression()));
newMethodInvocation.setName(ast.newSimpleName(methodName));
newMethodInvocation.arguments().addAll(params);
rewrite.replace(visited, newMethodInvocation, group);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class NewClassImportCleanUp method visit.
@Override
public boolean visit(final CompilationUnit node) {
if (!super.visit(node)) {
return false;
}
Set<String> classesToUse = getClassesToImport();
if (classesToUse.isEmpty()) {
return true;
}
Map<String, String> importsByClassname = new HashMap<>();
Map<String, String> importsByPackage = new HashMap<>();
for (String klass : classesToUse) {
importsByClassname.put(getSimpleName(klass), klass);
importsByPackage.put(getPackageName(klass), klass);
}
Set<String> alreadyImportedClasses = new HashSet<>();
Set<String> classesToImport = new HashSet<>(classesToUse);
for (Object anObject : node.imports()) {
ImportDeclaration anImport = (ImportDeclaration) anObject;
if (!anImport.isStatic()) {
if (anImport.isOnDemand()) {
String fullName = importsByPackage.get(anImport.getName().getFullyQualifiedName());
if (fullName != null) {
alreadyImportedClasses.add(fullName);
}
} else if (classesToUse.contains(anImport.getName().getFullyQualifiedName())) {
alreadyImportedClasses.add(anImport.getName().getFullyQualifiedName());
classesToImport.remove(anImport.getName().getFullyQualifiedName());
} else if (importsByClassname.containsKey(getSimpleName(anImport.getName().getFullyQualifiedName()))) {
classesToImport.remove(importsByClassname.get(getSimpleName(anImport.getName().getFullyQualifiedName())));
}
}
}
filterLocallyUsedNames(node, importsByClassname, classesToImport);
if (alreadyImportedClasses.size() < classesToUse.size() && !classesToImport.isEmpty()) {
CleanUpWithNewClassImport refactoringClass = getRefactoringClassInstance();
refactoringClass.getClassesToUseWithImport().addAll(alreadyImportedClasses);
refactoringClass.getClassesToUseWithImport().addAll(classesToImport);
node.accept(refactoringClass);
if (!refactoringClass.getImportsToAdd().isEmpty()) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
for (String importToAdd : refactoringClass.getImportsToAdd()) {
rewrite.getImportRewrite().addImport(importToAdd);
}
return false;
}
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteIfElseIfCleanUp method removeBlock.
private void removeBlock(final IfStatement node, final IfStatement ifStatement) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteIfElseIfCleanUp_description);
rewrite.replace(node.getElseStatement(), ASTNodes.createMoveTarget(rewrite, ifStatement), group);
}
Aggregations