use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteSimpleNameRatherThanQualifiedNameCleanUp method visit.
@Override
public boolean visit(final MethodInvocation visited) {
Expression expression = visited.getExpression();
IMethodBinding methodBinding = visited.resolveMethodBinding();
if (methodBinding != null && expression instanceof Name && hasKind((Name) expression, IBinding.TYPE) && visited.typeArguments().isEmpty()) {
ITypeBinding declaringClass = methodBinding.getDeclaringClass();
QName qname = QName.valueOf(declaringClass.getErasure().getQualifiedName(), methodBinding.getName());
if (methods.canReplaceFqnWithSimpleName(visited, qname, FqnType.METHOD)) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteSimpleNameRatherThanQualifiedNameCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.remove(expression, group);
return false;
}
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteSimpleNameRatherThanQualifiedNameCleanUp method maybeReplaceFqnWithSimpleName.
private boolean maybeReplaceFqnWithSimpleName(final QualifiedName visited, final Set<SimpleName> localIdentifiers) {
ASTNode ancestor = ASTNodes.getFirstAncestorOrNull(visited, PackageDeclaration.class, ImportDeclaration.class);
QName qname = getFullyQualifiedNameOrNull(visited);
if (ancestor != null || qname == null) {
return true;
}
if (types.canReplaceFqnWithSimpleName(visited, qname, FqnType.TYPE) || fields.canReplaceFqnWithSimpleName(visited, qname, FqnType.FIELD) && !containsLocalName(localIdentifiers, qname)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteSimpleNameRatherThanQualifiedNameCleanUp_description);
ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, visited.getName()), group);
return false;
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteSwitchCleanUp method addCaseWithStatements.
private void addCaseWithStatements(final SwitchStatement switchStatement, final List<Expression> caseValuesOrNullForDefault, final List<Statement> innerStatements) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
List<Statement> switchStatements = switchStatement.statements();
// Add the case statement(s)
if (caseValuesOrNullForDefault != null) {
for (Expression caseValue : caseValuesOrNullForDefault) {
switchStatements.add(ast.newSwitchCase(ASTNodes.createMoveTarget(rewrite, caseValue)));
}
} else {
switchStatements.add(ast.default0());
}
// Add the statement(s) for this case(s)
boolean isBreakNeeded = true;
if (!innerStatements.isEmpty()) {
for (Statement statement : innerStatements) {
switchStatements.add(ASTNodes.createMoveTarget(rewrite, statement));
}
isBreakNeeded = !ASTNodes.fallsThrough(innerStatements.get(innerStatements.size() - 1));
}
// When required: end with a break
if (isBreakNeeded) {
switchStatements.add(ast.newBreakStatement());
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method refactorHash.
private void refactorHash(final MethodDeclaration node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final CollectedData data) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp_description);
List<Statement> statements = node.getBody().statements();
String classname = addImport(Objects.class, classesToUseWithImport, importsToAdd);
Name objectsClassName = ASTNodeFactory.newName(ast, classname);
MethodInvocation hashMethod = ast.newMethodInvocation();
hashMethod.setExpression(objectsClassName);
// $NON-NLS-1$
hashMethod.setName(ast.newSimpleName("hash"));
hashMethod.arguments().addAll(ASTNodes.createMoveTarget(rewrite, data.getFields()));
ReturnStatement newReturnStatement = ast.newReturnStatement(hashMethod);
ASTNodes.replaceButKeepComment(rewrite, statements.get(0), newReturnStatement, group);
for (int i = 1; i < statements.size(); i++) {
rewrite.remove(statements.get(i), group);
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaCleanUp method removeParamParentheses.
private void removeParamParentheses(final LambdaExpression node) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
LambdaExpression copyOfLambdaExpression = ast.newLambdaExpression();
ASTNode copyOfParameter = ASTNodes.createMoveTarget(rewrite, (ASTNode) node.parameters().get(0));
copyOfLambdaExpression.parameters().add(copyOfParameter);
copyOfLambdaExpression.setBody(ASTNodes.createMoveTarget(rewrite, node.getBody()));
copyOfLambdaExpression.setParentheses(false);
ASTNodes.replaceButKeepComment(rewrite, node, copyOfLambdaExpression, group);
}
Aggregations