if (invokedMethod.getElementName().equals(fromMethod.getElementName())
&& invokedMethod.getSignature().equals(fromMethod.getSignature())){
Block newBlock = parseCode(code);
ASTNode target = node.getParent();
if (target instanceof VariableDeclarationFragment) {
target = (Statement) target.getParent();
}
Block block = getBlock(target);
int index = 0;
for (int i = 0; i < block.statements().size(); i++) {
if (block.statements().get(i).equals(target)) {
index = i;
break;
}
}
List statements = ASTNode.copySubtrees(node.getAST(), newBlock.statements());
for (int i = statements.size() - 1; i >= 0; i--) {
block.statements().add(index, statements.get(i));
}
int expressionIndex = index + statements.size() - 1;
boolean assignmentAdded = false;
if (node.getParent() instanceof VariableDeclarationFragment) {
Statement expressionStatement = (Statement) block.statements().get(expressionIndex);
if (expressionStatement instanceof ExpressionStatement) {
VariableDeclarationFragment vdf = (VariableDeclarationFragment) node.getParent();
Expression expression = (Expression) ASTNode.copySubtree(node.getAST(), ((ExpressionStatement) expressionStatement).getExpression());
vdf.setInitializer(expression);
block.statements().remove(expressionIndex);
assignmentAdded = true;
}
}
if (! assignmentAdded) {
block.statements().remove(expressionIndex + 1);
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
return true;
}
@SuppressWarnings("unchecked")
private Block getBlock(ASTNode target) {
ASTNode parent = target.getParent();
if (parent instanceof Block) {
return (Block) parent;
}
Block newBlock = target.getAST().newBlock();
newBlock.statements().add(ASTNode.copySubtree(target.getAST(), target));
Field[] declaredFields = parent.getClass().getDeclaredFields();
for (Field field : declaredFields) {
if (!(field.getType().isAssignableFrom(Statement.class))) {
continue;
}
try {
field.setAccessible(true);
if (field.get(parent).equals(target)) {
String setterName = "set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
Method setter = parent.getClass().getDeclaredMethod(setterName, field.getType());
if (setter != null) {
setter.invoke(parent, newBlock);
}
}
} catch (Exception e) {