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));
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class ForLoopHelper method buildForLoopContent.
private static ForLoopContent buildForLoopContent(final Expression loopVar, final Expression containerVar) {
if (!(loopVar instanceof Name)) {
return null;
}
if (containerVar instanceof MethodInvocation) {
final MethodInvocation mi = (MethodInvocation) containerVar;
final Name containerVarName = as(mi.getExpression(), Name.class);
if (containerVarName != null && isMethod(mi, "java.util.Collection", "size")) {
return ForLoopContent.indexedCollection(containerVarName, (Name) loopVar);
}
} else if (containerVar instanceof QualifiedName) {
final QualifiedName containerVarName = (QualifiedName) containerVar;
if (isArrayLength(containerVarName)) {
Name containerVariable = ((QualifiedName) containerVar).getQualifier();
return ForLoopContent.indexedArray(containerVariable, (Name) loopVar);
}
}
return null;
}
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 xtext-xtend by eclipse.
the class JavaASTFlattener method computeArrayName.
public String computeArrayName(final ArrayAccess node) {
String _switchResult = null;
Expression _array = node.getArray();
final Expression array = _array;
boolean _matched = false;
if (array instanceof SimpleName) {
_matched = true;
_switchResult = ((SimpleName) array).getIdentifier();
}
if (!_matched) {
if (array instanceof MethodInvocation) {
_matched = true;
_switchResult = ((MethodInvocation) array).getName().getIdentifier();
}
}
if (!_matched) {
if (array instanceof ArrayAccess) {
_matched = true;
String _computeArrayName = this.computeArrayName(((ArrayAccess) array));
_switchResult = ("_" + _computeArrayName);
}
}
if (!_matched) {
_switchResult = "tmpNode";
}
return _switchResult;
}
Aggregations