use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class SuperCallRatherThanUselessOverridingCleanUp method visit.
@Override
public boolean visit(final MethodDeclaration visited) {
if (visited.getBody() == null) {
return true;
}
List<Statement> bodyStatements = visited.getBody().statements();
if (bodyStatements.size() == 1) {
SuperMethodInvocation bodyMi = ASTNodes.asExpression(bodyStatements.get(0), SuperMethodInvocation.class);
if (bodyMi != null) {
IMethodBinding bodyMethodBinding = bodyMi.resolveMethodBinding();
IMethodBinding declMethodBinding = visited.resolveBinding();
if (declMethodBinding != null && bodyMethodBinding != null && declMethodBinding.overrides(bodyMethodBinding) && !hasSignificantAnnotations(declMethodBinding) && haveSameModifiers(bodyMethodBinding, declMethodBinding) && haveSameParameters(visited, bodyMi)) {
if (!Modifier.isProtected(declMethodBinding.getModifiers()) || declaredInSamePackage(bodyMethodBinding, declMethodBinding) || // protected also means package visibility, so check if it is required
!isMethodUsedInItsPackage(declMethodBinding, visited)) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.SuperCallRatherThanUselessOverridingCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.remove(visited, group);
return false;
}
}
}
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class TruncatingAppendingRatherThanSubCharactersCleanUp method replaceWithAppendSubstring.
private void replaceWithAppendSubstring(final MethodInvocation visited, final MethodInvocation truncatingMethod) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.TruncatingAppendingRatherThanSubCharactersCleanUp_description);
Expression builder = ASTNodes.createMoveTarget(rewrite, visited.getExpression());
Expression stringVar = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(truncatingMethod.getExpression()));
List<Expression> args = truncatingMethod.arguments();
Expression arg0 = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(args.get(0)));
Expression arg1 = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(args.get(1)));
MethodInvocation appendMethod = ast.newMethodInvocation();
appendMethod.setExpression(builder);
// $NON-NLS-1$
appendMethod.setName(ast.newSimpleName("append"));
appendMethod.arguments().add(stringVar);
appendMethod.arguments().add(arg0);
appendMethod.arguments().add(arg1);
rewrite.replace(visited, appendMethod, group);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class UpdateSetRatherThanTestingFirstCleanUp method maybeReplaceSetContains.
private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpression, final Statement statement, final Statement oppositeStatement, final boolean negate, final String methodName) {
List<Statement> statements = ASTNodes.asList(statement);
MethodInvocation miContains = ASTNodes.as(ifExpression, MethodInvocation.class);
if (!statements.isEmpty() && ASTNodes.usesGivenSignature(miContains, Set.class.getCanonicalName(), "contains", Object.class.getCanonicalName())) {
// $NON-NLS-1$
Statement firstStatement = statements.get(0);
MethodInvocation miAddOrRemove = ASTNodes.asExpression(firstStatement, MethodInvocation.class);
if (ASTNodes.usesGivenSignature(miAddOrRemove, Set.class.getCanonicalName(), methodName, Object.class.getCanonicalName()) && ASTNodes.match(miContains.getExpression(), miAddOrRemove.getExpression()) && ASTNodes.match(((List<Expression>) miContains.arguments()).get(0), ((List<Expression>) miAddOrRemove.arguments()).get(0))) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.UpdateSetRatherThanTestingFirstCleanUp_description);
if (statements.size() == 1 && ASTNodes.asList(oppositeStatement).isEmpty()) {
// Only one statement: replace if statement with col.add() (or col.remove())
ASTNodes.replaceButKeepComment(rewrite, ifStmtToReplace, ASTNodes.createMoveTarget(rewrite, firstStatement), group);
} else {
// There are other statements, replace the if condition with col.add() (or
// col.remove())
rewrite.replace(ifStmtToReplace.getExpression(), negate ? ast.negate(miAddOrRemove, true) : ASTNodes.createMoveTarget(rewrite, miAddOrRemove), group);
rewrite.remove(firstStatement, group);
}
return false;
}
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class UseStringContainsCleanUp method replaceWithStringContains.
private void replaceWithStringContains(final InfixExpression visited, final MethodInvocation method, final boolean isPositive) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.UseStringContainsCleanUp_description);
// $NON-NLS-1$
rewrite.set(method, MethodInvocation.NAME_PROPERTY, ast.newSimpleName("contains"), group);
if (isPositive) {
rewrite.replace(visited, ASTNodes.createMoveTarget(rewrite, method), group);
} else {
rewrite.replace(visited, ast.not(ASTNodes.createMoveTarget(rewrite, method)), group);
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class DoWhileRatherThanWhileCleanUp method visit.
@Override
public boolean visit(final WhileStatement visited) {
if (ASTNodes.isPassiveWithoutFallingThrough(visited.getExpression()) && Boolean.TRUE.equals(peremptoryValue(visited, visited.getExpression()))) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.DoWhileRatherThanWhileCleanUp_description);
ASTNodes.replaceButKeepComment(rewrite, visited, ast.newDoStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())), ASTNodes.createMoveTarget(rewrite, visited.getBody())), group);
return false;
}
return true;
}
Aggregations