use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class IsEmptyRatherThanSizeRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
final MethodInvocation leftMi = as(node.getLeftOperand(), MethodInvocation.class);
final Long rightLiteral = asNumber(node.getRightOperand());
final MethodInvocation rightMi = as(node.getRightOperand(), MethodInvocation.class);
final Long leftLiteral = asNumber(node.getLeftOperand());
if ((isMethod(leftMi, "java.util.Collection", "size") || isMethod(leftMi, "java.util.Map", "size")) && rightLiteral != null) {
return replaceCollectionSize(node, leftMi, sign(node.getOperator(), true), rightLiteral);
} else if ((isMethod(rightMi, "java.util.Collection", "size") || isMethod(rightMi, "java.util.Map", "size")) && leftLiteral != null) {
return replaceCollectionSize(node, rightMi, sign(node.getOperator(), false), leftLiteral);
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class TryWithResourceRefactoring method visit.
@Override
public boolean visit(TryStatement node) {
final List<Statement> tryStmts = asList(node.getBody());
if (tryStmts.size() >= 1 && tryStmts.get(0).getNodeType() == TRY_STATEMENT) {
final TryStatement innerTryStmt = as(tryStmts.get(0), TryStatement.class);
if (innerTryStmt != null && !innerTryStmt.resources().isEmpty() && innerTryStmt.catchClauses().isEmpty()) {
return collapseTryStatements(node, innerTryStmt);
}
}
final VariableDeclarationStatement previousDeclStmt = as(getPreviousStatement(node), VariableDeclarationStatement.class);
if (previousDeclStmt == null) {
return VISIT_SUBTREE;
}
final VariableDeclarationFragment previousDeclFragment = getUniqueFragment(previousDeclStmt);
final List<Statement> finallyStmts = asList(node.getFinally());
if (previousDeclFragment != null && finallyStmts.size() >= 1) {
final List<ASTNode> nodesToRemove = new ArrayList<ASTNode>();
nodesToRemove.add(previousDeclStmt);
final Statement finallyStmt = finallyStmts.get(0);
nodesToRemove.add(finallyStmts.size() == 1 ? node.getFinally() : finallyStmt);
final ExpressionStatement finallyEs = as(finallyStmt, ExpressionStatement.class);
final IfStatement finallyIs = as(finallyStmt, IfStatement.class);
if (finallyEs != null) {
final MethodInvocation mi = as(finallyEs.getExpression(), MethodInvocation.class);
if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, mi.getExpression())) {
final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
return refactorToTryWithResources(node, newResource, nodesToRemove);
}
} else if (finallyIs != null && asList(finallyIs.getThenStatement()).size() == 1 && asList(finallyIs.getElseStatement()).isEmpty()) {
final Expression nullCheckedExpr = getNullCheckedExpression(finallyIs.getExpression());
final Statement thenStmt = asList(finallyIs.getThenStatement()).get(0);
final MethodInvocation mi = asExpression(thenStmt, MethodInvocation.class);
if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, nullCheckedExpr, mi.getExpression())) {
final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
return refactorToTryWithResources(node, newResource, nodesToRemove);
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteRefactoring method replaceClass.
private void replaceClass(final ClassInstanceCreation originalInstanceCreation, final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactorAlone, final List<MethodInvocation> methodCallsToRefactorWithVariable) {
final ASTBuilder b = ctx.getASTBuilder();
if (variableDecls.isEmpty() && methodCallsToRefactorAlone.isEmpty()) {
final ClassInstanceCreation newInstanceCreation = b.copySubtree(originalInstanceCreation);
refactorInstantiation(b, originalInstanceCreation, newInstanceCreation);
ctx.getRefactorings().replace(originalInstanceCreation, newInstanceCreation);
} else {
refactorInstantiation(b, originalInstanceCreation, originalInstanceCreation);
for (final MethodInvocation methodCall : methodCallsToRefactorAlone) {
final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
refactorMethod(b, methodCall, copyOfMethodCall);
ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
}
for (final MethodInvocation methodCall : methodCallsToRefactorWithVariable) {
refactorMethod(b, methodCall, methodCall);
}
for (final VariableDeclaration variableDecl : variableDecls) {
final VariableDeclarationStatement parent = (VariableDeclarationStatement) variableDecl.getParent();
final VariableDeclarationStatement newDeclareStmt = b.copySubtree(parent);
replaceVariableType(b, parent, newDeclareStmt);
ctx.getRefactorings().replace(parent, newDeclareStmt);
}
}
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteRefactoring method visit.
@Override
public boolean visit(Block node) {
final ObjectInstantiationVisitor classCreationVisitor = new ObjectInstantiationVisitor();
node.accept(classCreationVisitor);
for (final ClassInstanceCreation instanceCreation : classCreationVisitor.getObjectInstantiations()) {
final List<VariableDeclaration> varDecls = new ArrayList<VariableDeclaration>();
final List<MethodInvocation> methodCallsToRefactorAlone = new ArrayList<MethodInvocation>();
final List<MethodInvocation> methodCallsToRefactorWithVariable = new ArrayList<MethodInvocation>();
if (canInstantiationBeRefactored(instanceCreation) && canBeRefactored(node, instanceCreation, varDecls, methodCallsToRefactorAlone, methodCallsToRefactorWithVariable) && canCodeBeRefactored()) {
replaceClass(instanceCreation, varDecls, methodCallsToRefactorAlone, methodCallsToRefactorWithVariable);
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class BigDecimalRefactoring method getCompareToNode.
private InfixExpression getCompareToNode(final boolean isPositive, final MethodInvocation node) {
final ASTBuilder b = this.ctx.getASTBuilder();
final MethodInvocation mi = b.invoke(b.copy(node.getExpression()), "compareTo", b.copy(arg0(node)));
return b.infixExpr(mi, isPositive ? EQUALS : NOT_EQUALS, b.int0(0));
}
Aggregations