Program: 12239
Project: 12239
Buggy source:
private void extractMethod(){
  AST ast=sourceTypeDeclaration.getAST();
  MethodDeclaration newMethodDeclaration=ast.newMethodDeclaration();
  SimpleName returnVariableSimpleName=extractionBlock.getReturnVariableDeclarationFragment().getName();
  sourceRewriter.set(newMethodDeclaration,MethodDeclaration.NAME_PROPERTY,ast.newSimpleName(extractionBlock.getExtractedMethodName()),null);
  sourceRewriter.set(newMethodDeclaration,MethodDeclaration.RETURN_TYPE2_PROPERTY,extractionBlock.getReturnVariableDeclarationStatement().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 (  VariableDeclarationFragment fragment : extractionBlock.getAdditionalRequiredVariableDeclarationFragments())   variableDeclarationIdentifiers.add(fragment.getName().getIdentifier());
  VariableDeclarationFragment returnFragment=extractionBlock.getReturnVariableDeclarationFragment();
  Expression returnInitializer=returnFragment.getInitializer();
  List<Expression> returnList=extractor.getVariableInstructions(returnInitializer);
  processIdentifiers(returnList,returnVariableSimpleName,extractedMethodArguments,extractedMethodArgumentIdentifiers,variableDeclarationIdentifiers);
  for (  VariableDeclarationFragment fragment : extractionBlock.getAdditionalRequiredVariableDeclarationFragments()) {
    Expression initializer=fragment.getInitializer();
    List<Expression> list=extractor.getVariableInstructions(initializer);
    processIdentifiers(list,returnVariableSimpleName,extractedMethodArguments,extractedMethodArgumentIdentifiers,variableDeclarationIdentifiers);
  }
  if (extractionBlock.getParentStatementForCopy() != null) {
    Statement parentStatementForCopy=extractionBlock.getParentStatementForCopy();
    if (parentStatementForCopy.getNodeType() == Statement.IF_STATEMENT) {
      IfStatement ifStatement=(IfStatement)parentStatementForCopy;
      List<Expression> list=extractor.getVariableInstructions(ifStatement.getExpression());
      processIdentifiers(list,returnVariableSimpleName,extractedMethodArguments,extractedMethodArgumentIdentifiers,variableDeclarationIdentifiers);
    }
  }
  for (  Statement statement : extractionBlock.getStatementsForExtraction()) {
    List<Expression> list=extractor.getVariableInstructions(statement);
    processIdentifiers(list,returnVariableSimpleName,extractedMethodArguments,extractedMethodArgumentIdentifiers,variableDeclarationIdentifiers);
  }
  ListRewrite paramRewrite=sourceRewriter.getListRewrite(newMethodDeclaration,MethodDeclaration.PARAMETERS_PROPERTY);
  for (  SimpleName argument : extractedMethodArguments) {
    Type argumentVariableDeclarationType=null;
    for (    VariableDeclarationStatement statement : extractionBlock.getAllVariableDeclarationStatements()) {
      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);
    }
  }
  Set<ITypeBinding> thrownExceptions=extractionBlock.getThrownExceptions();
  ListRewrite thrownExceptionRewrite=sourceRewriter.getListRewrite(newMethodDeclaration,MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY);
  for (  ITypeBinding thrownException : thrownExceptions) {
    SimpleName simpleName=ast.newSimpleName(thrownException.getName());
    thrownExceptionRewrite.insertLast(simpleName,null);
  }
  Block newMethodBody=newMethodDeclaration.getAST().newBlock();
  ListRewrite bodyRewrite=sourceRewriter.getListRewrite(newMethodBody,Block.STATEMENTS_PROPERTY);
  VariableDeclarationFragment returnVariableDeclarationFragment=newMethodBody.getAST().newVariableDeclarationFragment();
  sourceRewriter.set(returnVariableDeclarationFragment,VariableDeclarationFragment.NAME_PROPERTY,extractionBlock.getReturnVariableDeclarationFragment().getName(),null);
  sourceRewriter.set(returnVariableDeclarationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,extractionBlock.getReturnVariableDeclarationFragment().getInitializer(),null);
  VariableDeclarationStatement returnVariableDeclarationStatement=newMethodBody.getAST().newVariableDeclarationStatement(returnVariableDeclarationFragment);
  sourceRewriter.set(returnVariableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,extractionBlock.getReturnVariableDeclarationStatement().getType(),null);
  bodyRewrite.insertLast(returnVariableDeclarationStatement,null);
  for (  VariableDeclarationFragment fragment : extractionBlock.getAdditionalRequiredVariableDeclarationFragments()) {
    VariableDeclarationFragment variableDeclarationFragment=newMethodBody.getAST().newVariableDeclarationFragment();
    sourceRewriter.set(variableDeclarationFragment,VariableDeclarationFragment.NAME_PROPERTY,fragment.getName(),null);
    sourceRewriter.set(variableDeclarationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,fragment.getInitializer(),null);
    VariableDeclarationStatement variableDeclarationStatement=newMethodBody.getAST().newVariableDeclarationStatement(variableDeclarationFragment);
    sourceRewriter.set(variableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,extractionBlock.getAdditionalRequiredVariableDeclarationStatement(fragment).getType(),null);
    bodyRewrite.insertLast(variableDeclarationStatement,null);
  }
  if (extractionBlock.getParentStatementForCopy() == null) {
    for (    Statement statement : extractionBlock.getStatementsForExtraction()) {
      bodyRewrite.insertLast(statement,null);
    }
  }
 else {
    Statement parentStatement=extractionBlock.getParentStatementForCopy();
    Statement copiedParentStatement=(Statement)ASTNode.copySubtree(ast,parentStatement);
    if (copiedParentStatement.getNodeType() == ASTNode.IF_STATEMENT) {
      IfStatement oldIfStatement=(IfStatement)parentStatement;
      IfStatement newIfStatement=(IfStatement)copiedParentStatement;
      modifyExtractionBlock(oldIfStatement,newIfStatement);
    }
    bodyRewrite.insertLast(copiedParentStatement,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);
  if (extractionBlock.getParentStatementForCopy() == null) {
    replaceExtractedCodeWithMethodInvocation(extractedMethodArguments);
  }
 else {
    insertMethodInvocationBeforeParentStatement(extractedMethodArguments);
  }
}

Fixed version (ground truth):
private void extractMethod(){
  AST ast=sourceTypeDeclaration.getAST();
  MethodDeclaration newMethodDeclaration=ast.newMethodDeclaration();
  SimpleName returnVariableSimpleName=extractionBlock.getReturnVariableDeclarationFragment().getName();
  sourceRewriter.set(newMethodDeclaration,MethodDeclaration.NAME_PROPERTY,ast.newSimpleName(extractionBlock.getExtractedMethodName()),null);
  sourceRewriter.set(newMethodDeclaration,MethodDeclaration.RETURN_TYPE2_PROPERTY,extractionBlock.getReturnVariableDeclarationStatement().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 (  VariableDeclarationFragment fragment : extractionBlock.getAdditionalRequiredVariableDeclarationFragments())   variableDeclarationIdentifiers.add(fragment.getName().getIdentifier());
  VariableDeclarationFragment returnFragment=extractionBlock.getReturnVariableDeclarationFragment();
  Expression returnInitializer=returnFragment.getInitializer();
  List<Expression> returnList=extractor.getVariableInstructions(returnInitializer);
  processIdentifiers(returnList,returnVariableSimpleName,extractedMethodArguments,extractedMethodArgumentIdentifiers,variableDeclarationIdentifiers);
  for (  VariableDeclarationFragment fragment : extractionBlock.getAdditionalRequiredVariableDeclarationFragments()) {
    Expression initializer=fragment.getInitializer();
    List<Expression> list=extractor.getVariableInstructions(initializer);
    processIdentifiers(list,returnVariableSimpleName,extractedMethodArguments,extractedMethodArgumentIdentifiers,variableDeclarationIdentifiers);
  }
  if (extractionBlock.getParentStatementForCopy() != null) {
    Statement parentStatementForCopy=extractionBlock.getParentStatementForCopy();
    if (parentStatementForCopy.getNodeType() == Statement.IF_STATEMENT) {
      IfStatement ifStatement=(IfStatement)parentStatementForCopy;
      List<Expression> list=extractor.getVariableInstructions(ifStatement.getExpression());
      processIdentifiers(list,returnVariableSimpleName,extractedMethodArguments,extractedMethodArgumentIdentifiers,variableDeclarationIdentifiers);
    }
  }
  for (  Statement statement : extractionBlock.getStatementsForExtraction()) {
    List<Expression> list=extractor.getVariableInstructions(statement);
    processIdentifiers(list,returnVariableSimpleName,extractedMethodArguments,extractedMethodArgumentIdentifiers,variableDeclarationIdentifiers);
  }
  ListRewrite paramRewrite=sourceRewriter.getListRewrite(newMethodDeclaration,MethodDeclaration.PARAMETERS_PROPERTY);
  for (  SimpleName argument : extractedMethodArguments) {
    Type argumentVariableDeclarationType=null;
    for (    VariableDeclarationStatement statement : extractionBlock.getAllVariableDeclarationStatements()) {
      List<VariableDeclarationFragment> fragmentList=statement.fragments();
      for (      VariableDeclarationFragment fragment : fragmentList) {
        if (fragment.getName().getIdentifier().equals(argument.getIdentifier())) {
          argumentVariableDeclarationType=statement.getType();
          break;
        }
      }
    }
    if (argumentVariableDeclarationType == null) {
      for (      VariableDeclarationExpression expression : extractionBlock.getAllVariableDeclarationExpressions()) {
        List<VariableDeclarationFragment> fragmentList=expression.fragments();
        for (        VariableDeclarationFragment fragment : fragmentList) {
          if (fragment.getName().getIdentifier().equals(argument.getIdentifier())) {
            argumentVariableDeclarationType=expression.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);
    }
  }
  Set<ITypeBinding> thrownExceptions=extractionBlock.getThrownExceptions();
  ListRewrite thrownExceptionRewrite=sourceRewriter.getListRewrite(newMethodDeclaration,MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY);
  for (  ITypeBinding thrownException : thrownExceptions) {
    SimpleName simpleName=ast.newSimpleName(thrownException.getName());
    thrownExceptionRewrite.insertLast(simpleName,null);
  }
  Block newMethodBody=newMethodDeclaration.getAST().newBlock();
  ListRewrite bodyRewrite=sourceRewriter.getListRewrite(newMethodBody,Block.STATEMENTS_PROPERTY);
  VariableDeclarationFragment returnVariableDeclarationFragment=newMethodBody.getAST().newVariableDeclarationFragment();
  sourceRewriter.set(returnVariableDeclarationFragment,VariableDeclarationFragment.NAME_PROPERTY,extractionBlock.getReturnVariableDeclarationFragment().getName(),null);
  sourceRewriter.set(returnVariableDeclarationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,extractionBlock.getReturnVariableDeclarationFragment().getInitializer(),null);
  VariableDeclarationStatement returnVariableDeclarationStatement=newMethodBody.getAST().newVariableDeclarationStatement(returnVariableDeclarationFragment);
  sourceRewriter.set(returnVariableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,extractionBlock.getReturnVariableDeclarationStatement().getType(),null);
  bodyRewrite.insertLast(returnVariableDeclarationStatement,null);
  for (  VariableDeclarationFragment fragment : extractionBlock.getAdditionalRequiredVariableDeclarationFragments()) {
    VariableDeclarationFragment variableDeclarationFragment=newMethodBody.getAST().newVariableDeclarationFragment();
    sourceRewriter.set(variableDeclarationFragment,VariableDeclarationFragment.NAME_PROPERTY,fragment.getName(),null);
    sourceRewriter.set(variableDeclarationFragment,VariableDeclarationFragment.INITIALIZER_PROPERTY,fragment.getInitializer(),null);
    VariableDeclarationStatement variableDeclarationStatement=newMethodBody.getAST().newVariableDeclarationStatement(variableDeclarationFragment);
    sourceRewriter.set(variableDeclarationStatement,VariableDeclarationStatement.TYPE_PROPERTY,extractionBlock.getAdditionalRequiredVariableDeclarationStatement(fragment).getType(),null);
    bodyRewrite.insertLast(variableDeclarationStatement,null);
  }
  if (extractionBlock.getParentStatementForCopy() == null) {
    for (    Statement statement : extractionBlock.getStatementsForExtraction()) {
      bodyRewrite.insertLast(statement,null);
    }
  }
 else {
    Statement parentStatement=extractionBlock.getParentStatementForCopy();
    Statement copiedParentStatement=(Statement)ASTNode.copySubtree(ast,parentStatement);
    if (copiedParentStatement.getNodeType() == ASTNode.IF_STATEMENT) {
      IfStatement oldIfStatement=(IfStatement)parentStatement;
      IfStatement newIfStatement=(IfStatement)copiedParentStatement;
      modifyExtractionBlock(oldIfStatement,newIfStatement);
    }
    bodyRewrite.insertLast(copiedParentStatement,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);
  if (extractionBlock.getParentStatementForCopy() == null) {
    replaceExtractedCodeWithMethodInvocation(extractedMethodArguments);
  }
 else {
    insertMethodInvocationBeforeParentStatement(extractedMethodArguments);
  }
}