use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class StringBuilderMethodRatherThanReassignationRefactoring method visit.
@Override
public boolean visit(Assignment node) {
final Expression targetVar = node.getLeftHandSide();
Expression var = node.getRightHandSide();
if (ASSIGN.equals(node.getOperator()) && hasType(targetVar, "java.lang.StringBuffer", "java.lang.StringBuilder") && var instanceof MethodInvocation) {
var = getVar(var);
if (isSameVariable(targetVar, var)) {
final ASTBuilder b = this.ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.copy(node.getRightHandSide()));
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class StringBuilderRefactoring method replaceWithAppendSubstring.
private void replaceWithAppendSubstring(final MethodInvocation node, final MethodInvocation embeddedMI) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Expression stringVar = b.copy(embeddedMI.getExpression());
final List<Expression> args = arguments(embeddedMI);
final Expression arg0 = b.copy(args.get(0));
final Expression arg1 = b.copy(args.get(1));
final Expression lastExpr = b.copy(node.getExpression());
MethodInvocation newAppendSubstring = null;
if (arg1 == null) {
newAppendSubstring = b.invoke(lastExpr, "append", stringVar, arg0);
} else {
newAppendSubstring = b.invoke(lastExpr, "append", stringVar, arg0, arg1);
}
this.ctx.getRefactorings().replace(node, newAppendSubstring);
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project eclipse-pmd by acanda.
the class UnnecessaryCaseChangeQuickFix method apply.
/**
* Removes the <code>.toString()</code> from <code>"foo".toString()</code> if the expression is only a part of an
* statement. Removes the expression completely if it is the whole statement.
*/
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final MethodInvocation node) {
final AST ast = node.getAST();
final MethodInvocation invocation = ast.newMethodInvocation();
if (node.getExpression().getNodeType() == ASTNode.METHOD_INVOCATION) {
invocation.setExpression(removeCaseChange((MethodInvocation) node.getExpression()));
invocation.setName(ast.newSimpleName("equalsIgnoreCase"));
invocation.arguments().add(copy((Expression) node.arguments().get(0)));
return replace(node, invocation);
}
return false;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project eclipse-pmd by acanda.
the class AddEmptyStringQuickFix method apply.
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final InfixExpression node) {
final Expression rightOperand = node.getRightOperand();
// "" + x.toString() -> x.toString()
if (isString(rightOperand)) {
return replace(node, copy(rightOperand));
}
// "" + 'a' -> "a"
if (isCharacterLiteral(rightOperand)) {
final AST ast = node.getAST();
final StringLiteral stringLiteral = ast.newStringLiteral();
final String escapedCharacter = ((CharacterLiteral) rightOperand).getEscapedValue();
stringLiteral.setEscapedValue(convertToEscapedString(escapedCharacter));
return replace(node, stringLiteral);
}
// "" + x -> String.valueOf(x)
final AST ast = node.getAST();
final MethodInvocation toString = ast.newMethodInvocation();
toString.setExpression(ast.newSimpleName("String"));
toString.setName(ast.newSimpleName("valueOf"));
toString.arguments().add(copy(rightOperand));
return replace(node, toString);
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project eclipse-pmd by acanda.
the class IntegerInstantiationValueOfQuickFix method apply.
/**
* Replaces the Integer instantiation with its argument, e.g. {@code new Integer(123 + x)} with
* {@code Integer.valueOf(123 + x)}.
*/
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final ClassInstanceCreation node) {
final AST ast = node.getAST();
final MethodInvocation invocation = ast.newMethodInvocation();
invocation.setExpression(ast.newSimpleName("Integer"));
invocation.setName(ast.newSimpleName("valueOf"));
invocation.arguments().add(copy((Expression) node.arguments().get(0)));
replace(node, invocation);
return true;
}
Aggregations