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> methodCallsToRefactor) {
final ASTBuilder b = ctx.getASTBuilder();
final Type substituteType = substituteType(b, originalInstanceCreation.getType(), originalInstanceCreation);
if (substituteType != null) {
ctx.getRefactorings().replace(originalInstanceCreation.getType(), substituteType);
originalInstanceCreation.setType(substituteType);
}
for (final MethodInvocation methodCall : methodCallsToRefactor) {
final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
refactorMethod(b, methodCall, copyOfMethodCall);
ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
}
for (final VariableDeclaration variableDecl : variableDecls) {
final VariableDeclarationStatement oldDeclareStmt = (VariableDeclarationStatement) variableDecl.getParent();
final Type substituteVarType = substituteType(b, oldDeclareStmt.getType(), (ASTNode) oldDeclareStmt.fragments().get(0));
if (substituteVarType != null) {
ctx.getRefactorings().replace(oldDeclareStmt.getType(), substituteVarType);
}
}
}
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);
node.accept(classCreationVisitor);
for (final ClassInstanceCreation instanceCreation : classCreationVisitor.getObjectInstantiations()) {
final List<VariableDeclaration> varDecls = new ArrayList<VariableDeclaration>();
final List<MethodInvocation> methodCallsToRefactor = new ArrayList<MethodInvocation>();
if (canInstantiationBeRefactored(instanceCreation) && canBeRefactored(node, instanceCreation, instanceCreation.resolveTypeBinding(), varDecls, methodCallsToRefactor) && canCodeBeRefactored()) {
replaceClass(instanceCreation, varDecls, methodCallsToRefactor);
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class AllInOneMethodRatherThanLoopRefactoring method maybeReplaceWithCollectionMethod.
private boolean maybeReplaceWithCollectionMethod(ForStatement node, ForLoopContent loopContent, String methodName, MethodInvocation colMI) {
final Expression addArg0 = arg0(colMI);
final MethodInvocation getMI = as(addArg0, MethodInvocation.class);
if (isSameVariable(loopContent, getMI)) {
replaceWithCollectionMethod(node, methodName, colMI.getExpression(), getMI.getExpression());
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class AllInOneMethodRatherThanLoopRefactoring method visit.
@Override
public boolean visit(ForStatement node) {
final ForLoopContent loopContent = iterateOverContainer(node);
final List<Statement> stmts = asList(node.getBody());
if (loopContent != null && loopContent.getLoopVariable() != null && stmts.size() == 1) {
final SimpleName loopVariable = (SimpleName) loopContent.getLoopVariable();
final IVariableBinding loopVariableName = (IVariableBinding) loopVariable.resolveBinding();
// As we replace only one, there should be no more than one occurrence
if (getVariableUseCount(loopVariableName, node.getBody()) == 1) {
final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
switch(loopContent.getContainerType()) {
case COLLECTION:
if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
return maybeReplaceWithCollectionMethod(node, loopContent, "addAll", mi);
} else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
return maybeReplaceWithCollectionMethod(node, loopContent, "removeAll", mi);
}
break;
case ARRAY:
if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), loopContent.getContainerVariable())) {
final Expression addArg0 = arg0(mi);
final ArrayAccess aa = as(addArg0, ArrayAccess.class);
if (isSameVariable(loopContent, aa)) {
replaceWithCollectionsAddAll(node, loopContent.getContainerVariable(), mi);
return DO_NOT_VISIT_SUBTREE;
}
}
break;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project AutoRefactor by JnRouvignac.
the class ASTBuilder method invoke.
/**
* Builds a new {@link MethodInvocation} instance.
*
* @param <E> the arguments type
* @param expression the method invocation expression
* @param methodName the name of the invoked method
* @param arguments the arguments for the method invocation
* @return a new method invocation
*/
public <E extends Expression> MethodInvocation invoke(Expression expression, String methodName, List<E> arguments) {
final MethodInvocation mi = ast.newMethodInvocation();
mi.setExpression(expression);
mi.setName(ast.newSimpleName(methodName));
addAll(mi, arguments);
return mi;
}
Aggregations