Program: 12285
Project: 12285
Buggy source:
public void extractMethod(){
  AST ast=sourceTypeDeclaration.getAST();
  ASTRewrite rewriter=ASTRewrite.create(ast);
  MethodDeclaration newMethodDeclaration=ast.newMethodDeclaration();
  SimpleName returnVariableSimpleName=variableDeclarationFragment.getName();
  rewriter.set(newMethodDeclaration,MethodDeclaration.NAME_PROPERTY,returnVariableSimpleName,null);
  rewriter.set(newMethodDeclaration,MethodDeclaration.RETURN_TYPE2_PROPERTY,variableDeclarationStatement.getType(),null);
  ListRewrite modifierRewrite=rewriter.getListRewrite(newMethodDeclaration,MethodDeclaration.MODIFIERS2_PROPERTY);
  Modifier modifier=newMethodDeclaration.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
  modifierRewrite.insertLast(modifier,null);
  ExpressionExtractor extractor=new ExpressionExtractor();
  List<SimpleName> discreteLocalVariableInstructions=new ArrayList<SimpleName>();
  List<String> discreteLocalVariableInstructionIdentifiers=new ArrayList<String>();
  for (  Statement statement : extractStatementList) {
    List<Expression> list=extractor.getVariableInstructions(statement);
    for (    Expression expression : list) {
      SimpleName simpleName=(SimpleName)expression;
      IBinding binding=simpleName.resolveBinding();
      if (binding.getKind() == IBinding.VARIABLE) {
        IVariableBinding variableBinding=(IVariableBinding)binding;
        if (!variableBinding.isField() && !simpleName.isDeclaration()) {
          if (!discreteLocalVariableInstructionIdentifiers.contains(simpleName.getIdentifier())) {
            discreteLocalVariableInstructionIdentifiers.add(simpleName.getIdentifier());
            discreteLocalVariableInstructions.add(simpleName);
          }
        }
      }
    }
  }
  ListRewrite paramRewrite=rewriter.getListRewrite(newMethodDeclaration,MethodDeclaration.PARAMETERS_PROPERTY);
  for (  SimpleName instruction : discreteLocalVariableInstructions) {
    if (!returnVariableSimpleName.getIdentifier().equals(instruction.getIdentifier())) {
      Type paramVariableDeclarationType=null;
      for (      VariableDeclarationStatement statement : variableDeclarationStatementList) {
        List<VariableDeclarationFragment> fragmentList=statement.fragments();
        for (        VariableDeclarationFragment fragment : fragmentList) {
          if (fragment.getName().getIdentifier().equals(instruction.getIdentifier()))           paramVariableDeclarationType=statement.getType();
        }
      }
      if (paramVariableDeclarationType != null) {
        SingleVariableDeclaration newParam=newMethodDeclaration.getAST().newSingleVariableDeclaration();
        rewriter.set(newParam,SingleVariableDeclaration.NAME_PROPERTY,instruction,null);
        rewriter.set(newParam,SingleVariableDeclaration.TYPE_PROPERTY,paramVariableDeclarationType,null);
        paramRewrite.insertLast(newParam,null);
      }
    }
  }
  Block newMethodBody=newMethodDeclaration.getAST().newBlock();
  ListRewrite bodyRewrite=rewriter.getListRewrite(newMethodBody,Block.STATEMENTS_PROPERTY);
  VariableDeclarationFragment localVariableDeclarationFragment=newMethodBody.getAST().newVariableDeclarationFragment();
  rewriter.set(localVariableDeclarationFragment,VariableDeclarationFragment.NAME_PROPERTY,variableDeclarationFragment.getName(),null);
  rewriter.set(localVariableDeclarationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,variableDeclarationFragment.getInitializer(),null);
  VariableDeclarationStatement localVariableDeclarationStatement=newMethodBody.getAST().newVariableDeclarationStatement(localVariableDeclarationFragment);
  rewriter.set(localVariableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,variableDeclarationStatement.getType(),null);
  bodyRewrite.insertLast(localVariableDeclarationStatement,null);
  for (  Statement statement : extractStatementList) {
    bodyRewrite.insertLast(statement,null);
  }
  ReturnStatement returnStatement=newMethodBody.getAST().newReturnStatement();
  rewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,returnVariableSimpleName,null);
  bodyRewrite.insertLast(returnStatement,null);
  rewriter.set(newMethodDeclaration,MethodDeclaration.BODY_PROPERTY,newMethodBody,null);
  ListRewrite methodDeclarationRewrite=rewriter.getListRewrite(sourceTypeDeclaration,TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
  methodDeclarationRewrite.insertLast(newMethodDeclaration,null);
  replaceExtractedCodeWithMethodInvocation(rewriter);
  ITextFileBufferManager bufferManager=FileBuffers.getTextFileBufferManager();
  ITextFileBuffer textFileBuffer=bufferManager.getTextFileBuffer(sourceFile.getFullPath(),LocationKind.IFILE);
  IDocument document=textFileBuffer.getDocument();
  TextEdit edit=rewriter.rewriteAST(document,null);
  try {
    edit.apply(document);
  }
 catch (  MalformedTreeException e) {
    e.printStackTrace();
  }
catch (  BadLocationException e) {
    e.printStackTrace();
  }
}

Fixed version (ground truth):
public void extractMethod(){
  AST ast=sourceTypeDeclaration.getAST();
  MethodDeclaration newMethodDeclaration=ast.newMethodDeclaration();
  SimpleName returnVariableSimpleName=variableDeclarationFragment.getName();
  sourceRewriter.set(newMethodDeclaration,MethodDeclaration.NAME_PROPERTY,returnVariableSimpleName,null);
  sourceRewriter.set(newMethodDeclaration,MethodDeclaration.RETURN_TYPE2_PROPERTY,variableDeclarationStatement.getType(),null);
  ListRewrite modifierRewrite=sourceRewriter.getListRewrite(newMethodDeclaration,MethodDeclaration.MODIFIERS2_PROPERTY);
  Modifier modifier=newMethodDeclaration.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
  modifierRewrite.insertLast(modifier,null);
  ExpressionExtractor extractor=new ExpressionExtractor();
  List<SimpleName> extractedMethodArguments=new ArrayList<SimpleName>();
  List<String> extractedMethodArgumentIdentifiers=new ArrayList<String>();
  List<String> variableDeclarationIdentifiers=new ArrayList<String>();
  for (  Statement statement : extractStatementList) {
    List<Expression> list=extractor.getVariableInstructions(statement);
    for (    Expression expression : list) {
      SimpleName simpleName=(SimpleName)expression;
      IBinding binding=simpleName.resolveBinding();
      if (binding.getKind() == IBinding.VARIABLE) {
        IVariableBinding variableBinding=(IVariableBinding)binding;
        String simpleNameIdentifier=simpleName.getIdentifier();
        if (!variableBinding.isField() && !simpleName.isDeclaration() && !returnVariableSimpleName.getIdentifier().equals(simpleNameIdentifier)) {
          if (!extractedMethodArgumentIdentifiers.contains(simpleNameIdentifier) && !variableDeclarationIdentifiers.contains(simpleNameIdentifier)) {
            extractedMethodArgumentIdentifiers.add(simpleNameIdentifier);
            extractedMethodArguments.add(simpleName);
          }
        }
 else {
          if (!variableDeclarationIdentifiers.contains(simpleNameIdentifier))           variableDeclarationIdentifiers.add(simpleNameIdentifier);
        }
      }
    }
  }
  ListRewrite paramRewrite=sourceRewriter.getListRewrite(newMethodDeclaration,MethodDeclaration.PARAMETERS_PROPERTY);
  for (  SimpleName argument : extractedMethodArguments) {
    Type argumentVariableDeclarationType=null;
    for (    VariableDeclarationStatement statement : variableDeclarationStatementList) {
      List<VariableDeclarationFragment> fragmentList=statement.fragments();
      for (      VariableDeclarationFragment fragment : fragmentList) {
        if (fragment.getName().getIdentifier().equals(argument.getIdentifier())) {
          argumentVariableDeclarationType=statement.getType();
          break;
        }
      }
    }
    if (argumentVariableDeclarationType == null) {
      List<SingleVariableDeclaration> sourceMethodParameters=sourceMethodDeclaration.parameters();
      for (      SingleVariableDeclaration variableDeclaration : sourceMethodParameters) {
        if (variableDeclaration.getName().getIdentifier().equals(argument.getIdentifier())) {
          argumentVariableDeclarationType=variableDeclaration.getType();
          break;
        }
      }
    }
    if (argumentVariableDeclarationType != null) {
      SingleVariableDeclaration newParam=newMethodDeclaration.getAST().newSingleVariableDeclaration();
      sourceRewriter.set(newParam,SingleVariableDeclaration.NAME_PROPERTY,argument,null);
      sourceRewriter.set(newParam,SingleVariableDeclaration.TYPE_PROPERTY,argumentVariableDeclarationType,null);
      paramRewrite.insertLast(newParam,null);
    }
  }
  Block newMethodBody=newMethodDeclaration.getAST().newBlock();
  ListRewrite bodyRewrite=sourceRewriter.getListRewrite(newMethodBody,Block.STATEMENTS_PROPERTY);
  VariableDeclarationFragment localVariableDeclarationFragment=newMethodBody.getAST().newVariableDeclarationFragment();
  sourceRewriter.set(localVariableDeclarationFragment,VariableDeclarationFragment.NAME_PROPERTY,variableDeclarationFragment.getName(),null);
  sourceRewriter.set(localVariableDeclarationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,variableDeclarationFragment.getInitializer(),null);
  VariableDeclarationStatement localVariableDeclarationStatement=newMethodBody.getAST().newVariableDeclarationStatement(localVariableDeclarationFragment);
  sourceRewriter.set(localVariableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,variableDeclarationStatement.getType(),null);
  bodyRewrite.insertLast(localVariableDeclarationStatement,null);
  for (  Statement statement : extractStatementList) {
    bodyRewrite.insertLast(statement,null);
  }
  ReturnStatement returnStatement=newMethodBody.getAST().newReturnStatement();
  sourceRewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,returnVariableSimpleName,null);
  bodyRewrite.insertLast(returnStatement,null);
  sourceRewriter.set(newMethodDeclaration,MethodDeclaration.BODY_PROPERTY,newMethodBody,null);
  ListRewrite methodDeclarationRewrite=sourceRewriter.getListRewrite(sourceTypeDeclaration,TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
  methodDeclarationRewrite.insertLast(newMethodDeclaration,null);
  replaceExtractedCodeWithMethodInvocation(extractedMethodArguments);
  ITextFileBufferManager bufferManager=FileBuffers.getTextFileBufferManager();
  ITextFileBuffer textFileBuffer=bufferManager.getTextFileBuffer(sourceFile.getFullPath(),LocationKind.IFILE);
  IDocument document=textFileBuffer.getDocument();
  TextEdit edit=sourceRewriter.rewriteAST(document,null);
  try {
    edit.apply(document);
  }
 catch (  MalformedTreeException e) {
    e.printStackTrace();
  }
catch (  BadLocationException e) {
    e.printStackTrace();
  }
}