use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class StringCleanUp method replaceStringValueOfByArg0.
private void replaceStringValueOfByArg0(final Expression toReplace, final MethodInvocation methodInvocation, final ITypeBinding expectedType) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringCleanUp_description);
ITypeBinding actualType = ((Expression) methodInvocation.arguments().get(0)).resolveTypeBinding();
if (ASTNodes.hasType(actualType, String.class.getCanonicalName()) || expectedType.equals(actualType) || Bindings.getBoxedTypeBinding(expectedType, methodInvocation.getAST()).equals(actualType)) {
rewrite.replace(toReplace, ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0))), group);
} else {
rewrite.replace(toReplace, ast.newCastExpression(ast.type(expectedType.getQualifiedName()), ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0))), group);
}
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class StringCleanUp method removeToString.
private void removeToString(final MethodInvocation visited) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringCleanUp_description);
if (visited.getExpression() != null) {
rewrite.replace(visited, ASTNodes.createMoveTarget(rewrite, visited.getExpression()), group);
} else {
rewrite.replace(visited, ast.newThisExpression(), group);
}
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory 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.internal.corext.dom.ASTNodeFactory 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.internal.corext.dom.ASTNodeFactory 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);
}
}
Aggregations