Program: 12073
Project: 12073
Buggy source:
private void modifySourceMethod(CompilationUnit compilationUnit,MethodDeclaration methodDeclaration,TreeSet<PDGNode> removableNodes,TreeSet<PDGNode> remainingNodesMovableBefore,TreeSet<PDGNode> remainingNodesMovableAfter,List<VariableDeclaration> returnedVariables,int index){
  AST ast=methodDeclaration.getAST();
  ASTRewrite methodBodyRewriter=ASTRewrite.create(ast);
  MethodInvocation methodInvocation=ast.newMethodInvocation();
  methodBodyRewriter.set(methodInvocation,MethodInvocation.NAME_PROPERTY,ast.newSimpleName(extractedMethodName),null);
  ListRewrite argumentsRewrite=methodBodyRewriter.getListRewrite(methodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
  for (  String parameterName : originalPassedParameters.keySet()) {
    List<VariableDeclaration> variableDeclarations=originalPassedParameters.get(parameterName);
    argumentsRewrite.insertLast(variableDeclarations.get(index).getName(),null);
  }
  for (  ASTNodeDifference difference : parameterizedDifferenceMap.values()) {
    List<Expression> expressions=new ArrayList<Expression>();
    if (difference.getExpression1() != null) {
      Expression expression1=difference.getExpression1().getExpression();
      expression1=ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(expression1);
      expressions.add(expression1);
    }
 else {
      expressions.add(null);
    }
    if (difference.getExpression2() != null) {
      Expression expression2=difference.getExpression2().getExpression();
      expression2=ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(expression2);
      expressions.add(expression2);
    }
 else {
      expressions.add(null);
    }
    Expression expression=expressions.get(index);
    boolean isReturnedVariable=false;
    if (expression != null)     isReturnedVariable=isReturnedVariable(expression,returnedVariables);
    if (!isReturnedVariable) {
      if (expression != null)       argumentsRewrite.insertLast(expression,null);
 else       argumentsRewrite.insertLast(ast.newThisExpression(),null);
    }
  }
  Statement firstStatement=removableNodes.first().getASTStatement();
  Block parentBlock=(Block)firstStatement.getParent();
  ListRewrite blockRewrite=methodBodyRewriter.getListRewrite(parentBlock,Block.STATEMENTS_PROPERTY);
  CloneStructureNode root=mapper.getCloneStructureRoot();
  List<CloneStructureNode> processedCloneStructureGapNodes=new ArrayList<CloneStructureNode>();
  Set<PDGNode> remainingNodes=new TreeSet<PDGNode>();
  remainingNodes.addAll(remainingNodesMovableBefore);
  remainingNodes.addAll(remainingNodesMovableAfter);
  List<Statement> statementsToBeMovedBefore=new ArrayList<Statement>();
  List<Statement> statementsToBeMovedAfter=new ArrayList<Statement>();
  for (  PDGNode remainingNode : remainingNodes) {
    CloneStructureNode remainingCloneStructureNode=null;
    if (index == 0)     remainingCloneStructureNode=root.findNodeG1(remainingNode);
 else     remainingCloneStructureNode=root.findNodeG2(remainingNode);
    if (!processedCloneStructureGapNodes.contains(remainingCloneStructureNode.getParent())) {
      Statement statement=processCloneStructureGapNode(remainingCloneStructureNode,ast,methodBodyRewriter,index);
      if (remainingNodesMovableBefore.contains(remainingNode)) {
        statementsToBeMovedBefore.add(statement);
      }
 else       if (remainingNodesMovableAfter.contains(remainingNode)) {
        statementsToBeMovedAfter.add(statement);
      }
      methodBodyRewriter.remove(remainingNode.getASTStatement(),null);
    }
    processedCloneStructureGapNodes.add(remainingCloneStructureNode);
    for (    CloneStructureNode child : remainingCloneStructureNode.getChildren()) {
      if (child.getMapping() instanceof PDGElseGap)       processedCloneStructureGapNodes.add(child);
    }
  }
  Statement extractedMethodInvocationStatement=null;
  if (returnedVariables.size() == 1) {
    VariableDeclaration variableDeclaration=returnedVariables.get(0);
    Type variableType=extractType(variableDeclaration);
    VariableDeclarationFragment newFragment=ast.newVariableDeclarationFragment();
    methodBodyRewriter.set(newFragment,VariableDeclarationFragment.NAME_PROPERTY,variableDeclaration.getName(),null);
    ITypeBinding returnTypeBinding=findReturnTypeBinding();
    if (returnTypeBinding != null && !returnTypeBinding.isEqualTo(variableType.resolveBinding())) {
      CastExpression castExpression=ast.newCastExpression();
      methodBodyRewriter.set(castExpression,CastExpression.EXPRESSION_PROPERTY,methodInvocation,null);
      methodBodyRewriter.set(castExpression,CastExpression.TYPE_PROPERTY,variableType,null);
      methodBodyRewriter.set(newFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,castExpression,null);
    }
 else {
      methodBodyRewriter.set(newFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,methodInvocation,null);
    }
    VariableDeclarationStatement newVariableDeclarationStatement=ast.newVariableDeclarationStatement(newFragment);
    methodBodyRewriter.set(newVariableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,variableType,null);
    blockRewrite.insertBefore(newVariableDeclarationStatement,firstStatement,null);
    extractedMethodInvocationStatement=newVariableDeclarationStatement;
  }
 else {
    ITypeBinding returnTypeBinding=findReturnTypeBinding();
    Statement methodInvocationStatement=null;
    if (returnTypeBinding != null) {
      ReturnStatement returnStatement=ast.newReturnStatement();
      if (returnTypeBinding.isEqualTo(methodDeclaration.getReturnType2().resolveBinding())) {
        methodBodyRewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,methodInvocation,null);
      }
 else {
        CastExpression castExpression=ast.newCastExpression();
        methodBodyRewriter.set(castExpression,CastExpression.EXPRESSION_PROPERTY,methodInvocation,null);
        methodBodyRewriter.set(castExpression,CastExpression.TYPE_PROPERTY,methodDeclaration.getReturnType2(),null);
        methodBodyRewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,castExpression,null);
      }
      methodInvocationStatement=returnStatement;
    }
 else {
      methodInvocationStatement=ast.newExpressionStatement(methodInvocation);
    }
    blockRewrite.insertBefore(methodInvocationStatement,firstStatement,null);
    extractedMethodInvocationStatement=methodInvocationStatement;
  }
  for (  Statement movedBefore : statementsToBeMovedBefore) {
    blockRewrite.insertBefore(movedBefore,extractedMethodInvocationStatement,null);
  }
  for (int i=statementsToBeMovedAfter.size() - 1; i >= 0; i--) {
    Statement movedAfter=statementsToBeMovedAfter.get(i);
    blockRewrite.insertAfter(movedAfter,extractedMethodInvocationStatement,null);
  }
  for (  PDGNode pdgNode : removableNodes) {
    Statement statement=pdgNode.getASTStatement();
    methodBodyRewriter.remove(statement,null);
  }
  Set<LabeledStatement> labeledStatements=labeledStatementsToBeRemoved.get(index);
  for (  LabeledStatement labeled : labeledStatements) {
    methodBodyRewriter.remove(labeled,null);
  }
  try {
    TextEdit sourceEdit=methodBodyRewriter.rewriteAST();
    ICompilationUnit sourceICompilationUnit=(ICompilationUnit)compilationUnit.getJavaElement();
    CompilationUnitChange change=compilationUnitChanges.get(sourceICompilationUnit);
    change.getEdit().addChild(sourceEdit);
    change.addTextEditGroup(new TextEditGroup("String_Node_Str",new TextEdit[]{sourceEdit}));
  }
 catch (  JavaModelException e) {
    e.printStackTrace();
  }
}

Fixed version (ground truth):
private void modifySourceMethod(CompilationUnit compilationUnit,MethodDeclaration methodDeclaration,TreeSet<PDGNode> removableNodes,TreeSet<PDGNode> remainingNodesMovableBefore,TreeSet<PDGNode> remainingNodesMovableAfter,List<VariableDeclaration> returnedVariables,int index){
  AST ast=methodDeclaration.getAST();
  ASTRewrite methodBodyRewriter=ASTRewrite.create(ast);
  MethodInvocation methodInvocation=ast.newMethodInvocation();
  methodBodyRewriter.set(methodInvocation,MethodInvocation.NAME_PROPERTY,ast.newSimpleName(extractedMethodName),null);
  ListRewrite argumentsRewrite=methodBodyRewriter.getListRewrite(methodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
  for (  VariableBindingKeyPair parameterName : originalPassedParameters.keySet()) {
    List<VariableDeclaration> variableDeclarations=originalPassedParameters.get(parameterName);
    argumentsRewrite.insertLast(variableDeclarations.get(index).getName(),null);
  }
  for (  ASTNodeDifference difference : parameterizedDifferenceMap.values()) {
    List<Expression> expressions=new ArrayList<Expression>();
    if (difference.getExpression1() != null) {
      Expression expression1=difference.getExpression1().getExpression();
      expression1=ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(expression1);
      expressions.add(expression1);
    }
 else {
      expressions.add(null);
    }
    if (difference.getExpression2() != null) {
      Expression expression2=difference.getExpression2().getExpression();
      expression2=ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(expression2);
      expressions.add(expression2);
    }
 else {
      expressions.add(null);
    }
    Expression expression=expressions.get(index);
    boolean isReturnedVariable=false;
    if (expression != null)     isReturnedVariable=isReturnedVariable(expression,returnedVariables);
    if (!isReturnedVariable) {
      if (expression != null)       argumentsRewrite.insertLast(expression,null);
 else       argumentsRewrite.insertLast(ast.newThisExpression(),null);
    }
  }
  Statement firstStatement=removableNodes.first().getASTStatement();
  Block parentBlock=(Block)firstStatement.getParent();
  ListRewrite blockRewrite=methodBodyRewriter.getListRewrite(parentBlock,Block.STATEMENTS_PROPERTY);
  CloneStructureNode root=mapper.getCloneStructureRoot();
  List<CloneStructureNode> processedCloneStructureGapNodes=new ArrayList<CloneStructureNode>();
  Set<PDGNode> remainingNodes=new TreeSet<PDGNode>();
  remainingNodes.addAll(remainingNodesMovableBefore);
  remainingNodes.addAll(remainingNodesMovableAfter);
  List<Statement> statementsToBeMovedBefore=new ArrayList<Statement>();
  List<Statement> statementsToBeMovedAfter=new ArrayList<Statement>();
  for (  PDGNode remainingNode : remainingNodes) {
    CloneStructureNode remainingCloneStructureNode=null;
    if (index == 0)     remainingCloneStructureNode=root.findNodeG1(remainingNode);
 else     remainingCloneStructureNode=root.findNodeG2(remainingNode);
    if (!processedCloneStructureGapNodes.contains(remainingCloneStructureNode.getParent())) {
      Statement statement=processCloneStructureGapNode(remainingCloneStructureNode,ast,methodBodyRewriter,index);
      if (remainingNodesMovableBefore.contains(remainingNode)) {
        statementsToBeMovedBefore.add(statement);
      }
 else       if (remainingNodesMovableAfter.contains(remainingNode)) {
        statementsToBeMovedAfter.add(statement);
      }
      methodBodyRewriter.remove(remainingNode.getASTStatement(),null);
    }
    processedCloneStructureGapNodes.add(remainingCloneStructureNode);
    for (    CloneStructureNode child : remainingCloneStructureNode.getChildren()) {
      if (child.getMapping() instanceof PDGElseGap)       processedCloneStructureGapNodes.add(child);
    }
  }
  Statement extractedMethodInvocationStatement=null;
  if (returnedVariables.size() == 1) {
    VariableDeclaration variableDeclaration=returnedVariables.get(0);
    Type variableType=extractType(variableDeclaration);
    VariableDeclarationFragment newFragment=ast.newVariableDeclarationFragment();
    methodBodyRewriter.set(newFragment,VariableDeclarationFragment.NAME_PROPERTY,variableDeclaration.getName(),null);
    ITypeBinding returnTypeBinding=findReturnTypeBinding();
    if (returnTypeBinding != null && !returnTypeBinding.isEqualTo(variableType.resolveBinding())) {
      CastExpression castExpression=ast.newCastExpression();
      methodBodyRewriter.set(castExpression,CastExpression.EXPRESSION_PROPERTY,methodInvocation,null);
      methodBodyRewriter.set(castExpression,CastExpression.TYPE_PROPERTY,variableType,null);
      methodBodyRewriter.set(newFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,castExpression,null);
    }
 else {
      methodBodyRewriter.set(newFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,methodInvocation,null);
    }
    VariableDeclarationStatement newVariableDeclarationStatement=ast.newVariableDeclarationStatement(newFragment);
    methodBodyRewriter.set(newVariableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,variableType,null);
    blockRewrite.insertBefore(newVariableDeclarationStatement,firstStatement,null);
    extractedMethodInvocationStatement=newVariableDeclarationStatement;
  }
 else {
    ITypeBinding returnTypeBinding=findReturnTypeBinding();
    Statement methodInvocationStatement=null;
    if (returnTypeBinding != null) {
      ReturnStatement returnStatement=ast.newReturnStatement();
      if (returnTypeBinding.isEqualTo(methodDeclaration.getReturnType2().resolveBinding())) {
        methodBodyRewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,methodInvocation,null);
      }
 else {
        CastExpression castExpression=ast.newCastExpression();
        methodBodyRewriter.set(castExpression,CastExpression.EXPRESSION_PROPERTY,methodInvocation,null);
        methodBodyRewriter.set(castExpression,CastExpression.TYPE_PROPERTY,methodDeclaration.getReturnType2(),null);
        methodBodyRewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,castExpression,null);
      }
      methodInvocationStatement=returnStatement;
    }
 else {
      methodInvocationStatement=ast.newExpressionStatement(methodInvocation);
    }
    blockRewrite.insertBefore(methodInvocationStatement,firstStatement,null);
    extractedMethodInvocationStatement=methodInvocationStatement;
  }
  for (  Statement movedBefore : statementsToBeMovedBefore) {
    blockRewrite.insertBefore(movedBefore,extractedMethodInvocationStatement,null);
  }
  for (int i=statementsToBeMovedAfter.size() - 1; i >= 0; i--) {
    Statement movedAfter=statementsToBeMovedAfter.get(i);
    blockRewrite.insertAfter(movedAfter,extractedMethodInvocationStatement,null);
  }
  for (  PDGNode pdgNode : removableNodes) {
    Statement statement=pdgNode.getASTStatement();
    methodBodyRewriter.remove(statement,null);
  }
  Set<LabeledStatement> labeledStatements=labeledStatementsToBeRemoved.get(index);
  for (  LabeledStatement labeled : labeledStatements) {
    methodBodyRewriter.remove(labeled,null);
  }
  try {
    TextEdit sourceEdit=methodBodyRewriter.rewriteAST();
    ICompilationUnit sourceICompilationUnit=(ICompilationUnit)compilationUnit.getJavaElement();
    CompilationUnitChange change=compilationUnitChanges.get(sourceICompilationUnit);
    change.getEdit().addChild(sourceEdit);
    change.addTextEditGroup(new TextEditGroup("String_Node_Str",new TextEdit[]{sourceEdit}));
  }
 catch (  JavaModelException e) {
    e.printStackTrace();
  }
}