Program: 12023
Project: 12023
Buggy source:
private void modifySourceMethod(MultiTextEdit root){
  ASTRewrite sourceRewriter=ASTRewrite.create(sourceTypeDeclaration.getAST());
  AST ast=sourceMethodDeclaration.getAST();
  MethodInvocation extractedMethodInvocation=ast.newMethodInvocation();
  sourceRewriter.set(extractedMethodInvocation,MethodInvocation.NAME_PROPERTY,ast.newSimpleName(slice.getExtractedMethodName()),null);
  ListRewrite argumentRewrite=sourceRewriter.getListRewrite(extractedMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
  for (  VariableDeclaration variableDeclaration : slice.getPassedParameters()) {
    if (!variableDeclaration.resolveBinding().isField())     argumentRewrite.insertLast(variableDeclaration.getName(),null);
  }
  Statement extractedMethodInvocationInsertionStatement=slice.getExtractedMethodInvocationInsertionStatement();
  ASTNode statementParent=extractedMethodInvocationInsertionStatement.getParent();
  if (statementParent != null && statementParent instanceof Block)   statementParent=statementParent.getParent();
  if (statementParent != null && statementParent instanceof TryStatement) {
    TryStatement tryStatementParent=(TryStatement)statementParent;
    if (tryStatementsToBeRemoved.contains(tryStatementParent))     extractedMethodInvocationInsertionStatement=tryStatementParent;
  }
  VariableDeclaration returnedVariableDeclaration=slice.getLocalVariableCriterion();
  if (slice.declarationOfVariableCriterionBelongsToSliceNodes() && slice.declarationOfVariableCriterionBelongsToRemovableNodes()) {
    VariableDeclarationFragment initializationFragment=ast.newVariableDeclarationFragment();
    sourceRewriter.set(initializationFragment,VariableDeclarationFragment.NAME_PROPERTY,returnedVariableDeclaration.getName(),null);
    sourceRewriter.set(initializationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,extractedMethodInvocation,null);
    VariableDeclarationStatement initializationVariableDeclarationStatement=ast.newVariableDeclarationStatement(initializationFragment);
    ITypeBinding returnedVariableTypeBinding=extractTypeBinding(returnedVariableDeclaration);
    Type returnedVariableType=RefactoringUtility.generateTypeFromTypeBinding(returnedVariableTypeBinding,ast,sourceRewriter);
    sourceRewriter.set(initializationVariableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,returnedVariableType,null);
    ListRewrite blockRewrite=getBlockRewrite(extractedMethodInvocationInsertionStatement,sourceRewriter);
    blockRewrite.insertBefore(initializationVariableDeclarationStatement,extractedMethodInvocationInsertionStatement,null);
  }
 else   if (slice.declarationOfVariableCriterionBelongsToSliceNodes() && !slice.declarationOfVariableCriterionBelongsToRemovableNodes()) {
    if (returnedVariableDeclaration instanceof VariableDeclarationFragment) {
      VariableDeclarationFragment oldInitializationFragment=(VariableDeclarationFragment)returnedVariableDeclaration;
      VariableDeclarationFragment newInitializationFragment=ast.newVariableDeclarationFragment();
      sourceRewriter.set(newInitializationFragment,VariableDeclarationFragment.NAME_PROPERTY,returnedVariableDeclaration.getName(),null);
      sourceRewriter.set(newInitializationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,extractedMethodInvocation,null);
      if (oldInitializationFragment.getParent() instanceof VariableDeclarationStatement) {
        VariableDeclarationStatement oldVariableDeclarationStatement=(VariableDeclarationStatement)oldInitializationFragment.getParent();
        List<VariableDeclarationFragment> oldFragments=oldVariableDeclarationStatement.fragments();
        ListRewrite fragmentRewrite=sourceRewriter.getListRewrite(oldVariableDeclarationStatement,VariableDeclarationStatement.FRAGMENTS_PROPERTY);
        for (int i=0; i < oldFragments.size(); i++) {
          if (oldInitializationFragment.equals(oldFragments.get(i)))           fragmentRewrite.replace(oldFragments.get(i),newInitializationFragment,null);
        }
      }
 else       if (oldInitializationFragment.getParent() instanceof VariableDeclarationExpression) {
        VariableDeclarationExpression oldVariableDeclarationExpression=(VariableDeclarationExpression)oldInitializationFragment.getParent();
        List<VariableDeclarationFragment> oldFragments=oldVariableDeclarationExpression.fragments();
        ListRewrite fragmentRewrite=sourceRewriter.getListRewrite(oldVariableDeclarationExpression,VariableDeclarationExpression.FRAGMENTS_PROPERTY);
        for (int i=0; i < oldFragments.size(); i++) {
          if (oldInitializationFragment.equals(oldFragments.get(i)))           fragmentRewrite.replace(oldFragments.get(i),newInitializationFragment,null);
        }
      }
    }
  }
 else {
    if (!slice.isObjectSlice()) {
      Assignment assignment=ast.newAssignment();
      sourceRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_PROPERTY,returnedVariableDeclaration.getName(),null);
      sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,extractedMethodInvocation,null);
      ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment);
      ListRewrite blockRewrite=getBlockRewrite(extractedMethodInvocationInsertionStatement,sourceRewriter);
      blockRewrite.insertBefore(expressionStatement,extractedMethodInvocationInsertionStatement,null);
    }
 else {
      ExpressionStatement expressionStatement=ast.newExpressionStatement(extractedMethodInvocation);
      ListRewrite blockRewrite=getBlockRewrite(extractedMethodInvocationInsertionStatement,sourceRewriter);
      blockRewrite.insertBefore(expressionStatement,extractedMethodInvocationInsertionStatement,null);
    }
  }
  for (  Statement removableStatement : slice.getRemovableStatements()) {
    sourceRewriter.remove(removableStatement,null);
  }
  for (  TryStatement tryStatement : tryStatementsToBeRemoved) {
    sourceRewriter.remove(tryStatement,null);
  }
  try {
    TextEdit sourceEdit=sourceRewriter.rewriteAST();
    root.addChild(sourceEdit);
    compilationUnitChange.addTextEditGroup(new TextEditGroup("String_Node_Str",new TextEdit[]{sourceEdit}));
  }
 catch (  JavaModelException e) {
    e.printStackTrace();
  }
}

Fixed version (ground truth):
private void modifySourceMethod(MultiTextEdit root){
  ASTRewrite sourceRewriter=ASTRewrite.create(sourceTypeDeclaration.getAST());
  AST ast=sourceMethodDeclaration.getAST();
  MethodInvocation extractedMethodInvocation=ast.newMethodInvocation();
  sourceRewriter.set(extractedMethodInvocation,MethodInvocation.NAME_PROPERTY,ast.newSimpleName(slice.getExtractedMethodName()),null);
  ListRewrite argumentRewrite=sourceRewriter.getListRewrite(extractedMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
  for (  VariableDeclaration variableDeclaration : slice.getPassedParameters()) {
    if (!variableDeclaration.resolveBinding().isField())     argumentRewrite.insertLast(variableDeclaration.getName(),null);
  }
  Statement extractedMethodInvocationInsertionStatement=slice.getExtractedMethodInvocationInsertionStatement();
  if (extractedMethodInvocationInsertionStatement.getParent() instanceof LabeledStatement) {
    extractedMethodInvocationInsertionStatement=(LabeledStatement)extractedMethodInvocationInsertionStatement.getParent();
  }
  ASTNode statementParent=extractedMethodInvocationInsertionStatement.getParent();
  if (statementParent != null && statementParent instanceof Block)   statementParent=statementParent.getParent();
  if (statementParent != null && statementParent instanceof TryStatement) {
    TryStatement tryStatementParent=(TryStatement)statementParent;
    if (tryStatementsToBeRemoved.contains(tryStatementParent))     extractedMethodInvocationInsertionStatement=tryStatementParent;
  }
  VariableDeclaration returnedVariableDeclaration=slice.getLocalVariableCriterion();
  if (slice.declarationOfVariableCriterionBelongsToSliceNodes() && slice.declarationOfVariableCriterionBelongsToRemovableNodes()) {
    VariableDeclarationFragment initializationFragment=ast.newVariableDeclarationFragment();
    sourceRewriter.set(initializationFragment,VariableDeclarationFragment.NAME_PROPERTY,returnedVariableDeclaration.getName(),null);
    sourceRewriter.set(initializationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,extractedMethodInvocation,null);
    VariableDeclarationStatement initializationVariableDeclarationStatement=ast.newVariableDeclarationStatement(initializationFragment);
    ITypeBinding returnedVariableTypeBinding=extractTypeBinding(returnedVariableDeclaration);
    Type returnedVariableType=RefactoringUtility.generateTypeFromTypeBinding(returnedVariableTypeBinding,ast,sourceRewriter);
    sourceRewriter.set(initializationVariableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,returnedVariableType,null);
    ListRewrite blockRewrite=getBlockRewrite(extractedMethodInvocationInsertionStatement,sourceRewriter);
    blockRewrite.insertBefore(initializationVariableDeclarationStatement,extractedMethodInvocationInsertionStatement,null);
  }
 else   if (slice.declarationOfVariableCriterionBelongsToSliceNodes() && !slice.declarationOfVariableCriterionBelongsToRemovableNodes()) {
    if (returnedVariableDeclaration instanceof VariableDeclarationFragment) {
      VariableDeclarationFragment oldInitializationFragment=(VariableDeclarationFragment)returnedVariableDeclaration;
      VariableDeclarationFragment newInitializationFragment=ast.newVariableDeclarationFragment();
      sourceRewriter.set(newInitializationFragment,VariableDeclarationFragment.NAME_PROPERTY,returnedVariableDeclaration.getName(),null);
      sourceRewriter.set(newInitializationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,extractedMethodInvocation,null);
      if (oldInitializationFragment.getParent() instanceof VariableDeclarationStatement) {
        VariableDeclarationStatement oldVariableDeclarationStatement=(VariableDeclarationStatement)oldInitializationFragment.getParent();
        List<VariableDeclarationFragment> oldFragments=oldVariableDeclarationStatement.fragments();
        ListRewrite fragmentRewrite=sourceRewriter.getListRewrite(oldVariableDeclarationStatement,VariableDeclarationStatement.FRAGMENTS_PROPERTY);
        for (int i=0; i < oldFragments.size(); i++) {
          if (oldInitializationFragment.equals(oldFragments.get(i)))           fragmentRewrite.replace(oldFragments.get(i),newInitializationFragment,null);
        }
      }
 else       if (oldInitializationFragment.getParent() instanceof VariableDeclarationExpression) {
        VariableDeclarationExpression oldVariableDeclarationExpression=(VariableDeclarationExpression)oldInitializationFragment.getParent();
        List<VariableDeclarationFragment> oldFragments=oldVariableDeclarationExpression.fragments();
        ListRewrite fragmentRewrite=sourceRewriter.getListRewrite(oldVariableDeclarationExpression,VariableDeclarationExpression.FRAGMENTS_PROPERTY);
        for (int i=0; i < oldFragments.size(); i++) {
          if (oldInitializationFragment.equals(oldFragments.get(i)))           fragmentRewrite.replace(oldFragments.get(i),newInitializationFragment,null);
        }
      }
    }
  }
 else {
    if (!slice.isObjectSlice()) {
      Assignment assignment=ast.newAssignment();
      sourceRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_PROPERTY,returnedVariableDeclaration.getName(),null);
      sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,extractedMethodInvocation,null);
      ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment);
      ListRewrite blockRewrite=getBlockRewrite(extractedMethodInvocationInsertionStatement,sourceRewriter);
      blockRewrite.insertBefore(expressionStatement,extractedMethodInvocationInsertionStatement,null);
    }
 else {
      ExpressionStatement expressionStatement=ast.newExpressionStatement(extractedMethodInvocation);
      ListRewrite blockRewrite=getBlockRewrite(extractedMethodInvocationInsertionStatement,sourceRewriter);
      blockRewrite.insertBefore(expressionStatement,extractedMethodInvocationInsertionStatement,null);
    }
  }
  for (  Statement removableStatement : slice.getRemovableStatements()) {
    sourceRewriter.remove(removableStatement,null);
  }
  for (  TryStatement tryStatement : tryStatementsToBeRemoved) {
    sourceRewriter.remove(tryStatement,null);
  }
  for (  LabeledStatement labeledStatement : labeledStatementsToBeRemoved) {
    sourceRewriter.remove(labeledStatement,null);
  }
  try {
    TextEdit sourceEdit=sourceRewriter.rewriteAST();
    root.addChild(sourceEdit);
    compilationUnitChange.addTextEditGroup(new TextEditGroup("String_Node_Str",new TextEdit[]{sourceEdit}));
  }
 catch (  JavaModelException e) {
    e.printStackTrace();
  }
}