Program: 12055
Project: 12055
Buggy source:
private void modifySourceMethod(CompilationUnit compilationUnit,MethodDeclaration methodDeclaration,TreeSet<PDGNode> removableNodes,TreeSet<PDGNode> remainingNodesMovableBefore,TreeSet<PDGNode> remainingNodesMovableAfter,List<VariableDeclaration> returnedVariables,Set<VariableDeclaration> fieldsToBeParameterized,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);
  if (cloneInfo.extractUtilityClass) {
    methodBodyRewriter.set(methodInvocation,MethodInvocation.EXPRESSION_PROPERTY,ast.newSimpleName(cloneInfo.intermediateClassName),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) {
        if (difference.containsDifferenceType(DifferenceType.IF_ELSE_SYMMETRICAL_MATCH) && index == 1) {
          ParenthesizedExpression parenthesizedExpression=ast.newParenthesizedExpression();
          methodBodyRewriter.set(parenthesizedExpression,ParenthesizedExpression.EXPRESSION_PROPERTY,expression,null);
          PrefixExpression prefixExpression=ast.newPrefixExpression();
          methodBodyRewriter.set(prefixExpression,PrefixExpression.OPERAND_PROPERTY,parenthesizedExpression,null);
          methodBodyRewriter.set(prefixExpression,PrefixExpression.OPERATOR_PROPERTY,PrefixExpression.Operator.NOT,null);
          argumentsRewrite.insertLast(prefixExpression,null);
        }
 else {
          argumentsRewrite.insertLast(expression,null);
        }
      }
 else {
        argumentsRewrite.insertLast(ast.newThisExpression(),null);
      }
    }
  }
  Set<VariableDeclaration> accessedLocalFields=null;
  if (index == 0)   accessedLocalFields=getLocallyAccessedFields(mapper.getDirectlyAccessedLocalFieldsG1(),sourceTypeDeclarations.get(0));
 else   accessedLocalFields=getLocallyAccessedFields(mapper.getDirectlyAccessedLocalFieldsG2(),sourceTypeDeclarations.get(1));
  for (  VariableDeclaration variableDeclaration : fieldsToBeParameterized) {
    if (accessedLocalFields.contains(variableDeclaration)) {
      argumentsRewrite.insertLast(variableDeclaration.getName(),null);
    }
  }
  cloneInfo.argumentRewriteList.add(index,argumentsRewrite);
  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) {
    Statement methodInvocationStatement=null;
    VariableDeclaration variableDeclaration=returnedVariables.get(0);
    if (variableDeclaration.resolveBinding().isParameter() || variableDeclaration.resolveBinding().isField() || statementsToBeMovedBefore.contains(variableDeclaration.getParent())) {
      Type variableType=extractType(variableDeclaration);
      Assignment assignment=ast.newAssignment();
      methodBodyRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_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(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,castExpression,null);
      }
 else {
        methodBodyRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,methodInvocation,null);
      }
      ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment);
      methodInvocationStatement=expressionStatement;
      if (statementsToBeMovedBefore.contains(variableDeclaration.getParent()) && variableDeclaration.getInitializer() == null) {
        methodBodyRewriter.set(variableDeclaration,VariableDeclarationFragment.INITIALIZER_PROPERTY,generateDefaultValue(methodBodyRewriter,ast,variableType),null);
      }
    }
 else {
      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);
      methodInvocationStatement=newVariableDeclarationStatement;
    }
    blockRewrite.insertBefore(methodInvocationStatement,firstStatement,null);
    extractedMethodInvocationStatement=methodInvocationStatement;
  }
 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);
  }
  cloneInfo.originalMethodBodyRewriteList.add(index,methodBodyRewriter);
}

Fixed version (ground truth):
private void modifySourceMethod(CompilationUnit compilationUnit,MethodDeclaration methodDeclaration,TreeSet<PDGNode> removableNodes,TreeSet<PDGNode> remainingNodesMovableBefore,TreeSet<PDGNode> remainingNodesMovableAfter,List<VariableDeclaration> returnedVariables,Set<VariableDeclaration> fieldsToBeParameterized,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);
  if (cloneInfo.extractUtilityClass) {
    methodBodyRewriter.set(methodInvocation,MethodInvocation.EXPRESSION_PROPERTY,ast.newSimpleName(cloneInfo.intermediateClassName),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) {
        if (difference.containsDifferenceType(DifferenceType.IF_ELSE_SYMMETRICAL_MATCH) && index == 1) {
          ParenthesizedExpression parenthesizedExpression=ast.newParenthesizedExpression();
          methodBodyRewriter.set(parenthesizedExpression,ParenthesizedExpression.EXPRESSION_PROPERTY,expression,null);
          PrefixExpression prefixExpression=ast.newPrefixExpression();
          methodBodyRewriter.set(prefixExpression,PrefixExpression.OPERAND_PROPERTY,parenthesizedExpression,null);
          methodBodyRewriter.set(prefixExpression,PrefixExpression.OPERATOR_PROPERTY,PrefixExpression.Operator.NOT,null);
          argumentsRewrite.insertLast(prefixExpression,null);
        }
 else {
          argumentsRewrite.insertLast(expression,null);
        }
      }
 else {
        argumentsRewrite.insertLast(ast.newThisExpression(),null);
      }
    }
  }
  Set<VariableDeclaration> accessedLocalFields=null;
  if (index == 0)   accessedLocalFields=getLocallyAccessedFields(mapper.getDirectlyAccessedLocalFieldsG1(),sourceTypeDeclarations.get(0));
 else   accessedLocalFields=getLocallyAccessedFields(mapper.getDirectlyAccessedLocalFieldsG2(),sourceTypeDeclarations.get(1));
  for (  VariableDeclaration variableDeclaration : fieldsToBeParameterized) {
    if (accessedLocalFields.contains(variableDeclaration)) {
      argumentsRewrite.insertLast(variableDeclaration.getName(),null);
    }
  }
  cloneInfo.argumentRewriteList.add(index,argumentsRewrite);
  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) {
    Statement methodInvocationStatement=null;
    VariableDeclaration variableDeclaration=returnedVariables.get(0);
    if (variableDeclaration.resolveBinding().isParameter() || variableDeclaration.resolveBinding().isField() || statementsToBeMovedBefore.contains(variableDeclaration.getParent())|| variableIsPassedAsCommonParameter(variableDeclaration)) {
      Type variableType=extractType(variableDeclaration);
      Assignment assignment=ast.newAssignment();
      methodBodyRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_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(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,castExpression,null);
      }
 else {
        methodBodyRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,methodInvocation,null);
      }
      ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment);
      methodInvocationStatement=expressionStatement;
      if (statementsToBeMovedBefore.contains(variableDeclaration.getParent()) && variableDeclaration.getInitializer() == null) {
        methodBodyRewriter.set(variableDeclaration,VariableDeclarationFragment.INITIALIZER_PROPERTY,generateDefaultValue(methodBodyRewriter,ast,variableType),null);
      }
    }
 else {
      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);
      methodInvocationStatement=newVariableDeclarationStatement;
    }
    blockRewrite.insertBefore(methodInvocationStatement,firstStatement,null);
    extractedMethodInvocationStatement=methodInvocationStatement;
  }
 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);
  }
  cloneInfo.originalMethodBodyRewriteList.add(index,methodBodyRewriter);
}