Program: 12259
Project: 12259
Buggy source:
private void modifyContext(){
  AST contextAST=sourceTypeDeclaration.getAST();
  ListRewrite contextBodyRewrite=sourceRewriter.getListRewrite(sourceTypeDeclaration,TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
  VariableDeclarationFragment typeFragment=contextAST.newVariableDeclarationFragment();
  sourceRewriter.set(typeFragment,VariableDeclarationFragment.NAME_PROPERTY,typeCheckElimination.getTypeField().getName(),null);
  FieldDeclaration typeFieldDeclaration=contextAST.newFieldDeclaration(typeFragment);
  sourceRewriter.set(typeFieldDeclaration,FieldDeclaration.TYPE_PROPERTY,contextAST.newSimpleName(typeCheckElimination.getAbstractClassName()),null);
  ListRewrite typeFieldDeclrationModifiersRewrite=sourceRewriter.getListRewrite(typeFieldDeclaration,FieldDeclaration.MODIFIERS2_PROPERTY);
  typeFieldDeclrationModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD),null);
  contextBodyRewrite.insertFirst(typeFieldDeclaration,null);
  FieldDeclaration[] fieldDeclarations=sourceTypeDeclaration.getFields();
  for (  FieldDeclaration fieldDeclaration : fieldDeclarations) {
    List<VariableDeclarationFragment> fragments=fieldDeclaration.fragments();
    for (    VariableDeclarationFragment fragment : fragments) {
      if (fragment.equals(typeCheckElimination.getTypeField())) {
        if (fragments.size() == 1) {
          contextBodyRewrite.remove(fragment.getParent(),null);
        }
 else {
          ListRewrite fragmentRewrite=sourceRewriter.getListRewrite(fragment.getParent(),FieldDeclaration.FRAGMENTS_PROPERTY);
          fragmentRewrite.remove(fragment,null);
        }
      }
    }
  }
  MethodDeclaration setterMethod=typeCheckElimination.getTypeFieldSetterMethod();
  List<SingleVariableDeclaration> setterMethodParameters=setterMethod.parameters();
  Statement setterMethodStatement=(Statement)ASTNode.copySubtree(contextAST,typeCheckElimination.getTypeCheckCodeFragment());
  if (setterMethodStatement instanceof SwitchStatement) {
    SwitchStatement switchStatement=(SwitchStatement)setterMethodStatement;
    List<Statement> switchStatementStatements=switchStatement.statements();
    if (setterMethodParameters.size() == 1) {
      sourceRewriter.set(switchStatement,SwitchStatement.EXPRESSION_PROPERTY,setterMethodParameters.get(0).getName(),null);
    }
    ListRewrite switchStatementStatementsRewrite=sourceRewriter.getListRewrite(switchStatement,SwitchStatement.STATEMENTS_PROPERTY);
    List<String> subclassNames=typeCheckElimination.getSubclassNames();
    Collection<ArrayList<Statement>> allTypeCheckStatements=typeCheckElimination.getTypeCheckStatements();
    int i=0;
    for (    ArrayList<Statement> typeCheckStatements : allTypeCheckStatements) {
      int matches=0;
      for (      Statement oldStatement : typeCheckStatements) {
        for (        Statement newStatement : switchStatementStatements) {
          if (oldStatement.toString().equals(newStatement.toString())) {
            matches++;
            if (matches == typeCheckStatements.size()) {
              Assignment assignment=contextAST.newAssignment();
              sourceRewriter.set(assignment,Assignment.OPERATOR_PROPERTY,Assignment.Operator.ASSIGN,null);
              FieldAccess typeFieldAccess=contextAST.newFieldAccess();
              sourceRewriter.set(typeFieldAccess,FieldAccess.EXPRESSION_PROPERTY,contextAST.newThisExpression(),null);
              sourceRewriter.set(typeFieldAccess,FieldAccess.NAME_PROPERTY,typeCheckElimination.getTypeField().getName(),null);
              sourceRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_PROPERTY,typeFieldAccess,null);
              ClassInstanceCreation classInstanceCreation=contextAST.newClassInstanceCreation();
              sourceRewriter.set(classInstanceCreation,ClassInstanceCreation.TYPE_PROPERTY,contextAST.newSimpleName(subclassNames.get(i)),null);
              sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,classInstanceCreation,null);
              switchStatementStatementsRewrite.replace(newStatement,contextAST.newExpressionStatement(assignment),null);
            }
 else {
              switchStatementStatementsRewrite.remove(newStatement,null);
            }
          }
        }
      }
      i++;
    }
  }
 else   if (setterMethodStatement instanceof IfStatement) {
    IfStatement typeCheckIfStatement=(IfStatement)setterMethodStatement;
    List<String> subclassNames=typeCheckElimination.getSubclassNames();
    Collection<ArrayList<Statement>> allTypeCheckStatements=typeCheckElimination.getTypeCheckStatements();
    int i=0;
    for (    ArrayList<Statement> typeCheckStatements : allTypeCheckStatements) {
      if (typeCheckStatements.size() == 1) {
        Statement oldStatement=typeCheckStatements.get(0);
        IfStatement ifStatement=typeCheckIfStatement;
        Statement newStatement=typeCheckIfStatement.getThenStatement();
        while (!oldStatement.toString().equals(newStatement.toString())) {
          Statement elseStatement=ifStatement.getElseStatement();
          if (elseStatement instanceof IfStatement) {
            ifStatement=(IfStatement)elseStatement;
            newStatement=ifStatement.getThenStatement();
          }
        }
        IfStatement parentIfStatement=(IfStatement)newStatement.getParent();
        Assignment assignment=contextAST.newAssignment();
        sourceRewriter.set(assignment,Assignment.OPERATOR_PROPERTY,Assignment.Operator.ASSIGN,null);
        FieldAccess typeFieldAccess=contextAST.newFieldAccess();
        sourceRewriter.set(typeFieldAccess,FieldAccess.EXPRESSION_PROPERTY,contextAST.newThisExpression(),null);
        sourceRewriter.set(typeFieldAccess,FieldAccess.NAME_PROPERTY,typeCheckElimination.getTypeField().getName(),null);
        sourceRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_PROPERTY,typeFieldAccess,null);
        ClassInstanceCreation classInstanceCreation=contextAST.newClassInstanceCreation();
        sourceRewriter.set(classInstanceCreation,ClassInstanceCreation.TYPE_PROPERTY,contextAST.newSimpleName(subclassNames.get(i)),null);
        sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,classInstanceCreation,null);
        sourceRewriter.set(parentIfStatement,IfStatement.THEN_STATEMENT_PROPERTY,contextAST.newExpressionStatement(assignment),null);
        InfixExpression infixExpression=(InfixExpression)parentIfStatement.getExpression();
        Expression leftOperand=infixExpression.getLeftOperand();
        if (leftOperand instanceof SimpleName) {
          SimpleName simpleName=(SimpleName)leftOperand;
          if (simpleName.getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
            sourceRewriter.set(infixExpression,InfixExpression.LEFT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
          }
        }
 else         if (leftOperand instanceof FieldAccess) {
          FieldAccess fieldAccess=(FieldAccess)leftOperand;
          if (fieldAccess.getName().getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
            sourceRewriter.set(infixExpression,InfixExpression.LEFT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
          }
        }
 else         if (leftOperand instanceof MethodInvocation) {
          MethodInvocation methodInvocation=(MethodInvocation)leftOperand;
          MethodDeclaration[] contextMethods=sourceTypeDeclaration.getMethods();
          for (          MethodDeclaration methodDeclaration : contextMethods) {
            if (methodDeclaration.getName().getIdentifier().equals(methodInvocation.getName().getIdentifier())) {
              SimpleName simpleName=MethodDeclarationUtility.isGetter(methodDeclaration);
              if (simpleName != null && simpleName.getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
                sourceRewriter.set(infixExpression,InfixExpression.LEFT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
                break;
              }
            }
          }
        }
        Expression rightOperand=infixExpression.getRightOperand();
        if (rightOperand instanceof SimpleName) {
          SimpleName simpleName=(SimpleName)rightOperand;
          if (simpleName.getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
            sourceRewriter.set(infixExpression,InfixExpression.RIGHT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
          }
        }
 else         if (rightOperand instanceof FieldAccess) {
          FieldAccess fieldAccess=(FieldAccess)rightOperand;
          if (fieldAccess.getName().getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
            sourceRewriter.set(infixExpression,InfixExpression.RIGHT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
          }
        }
 else         if (rightOperand instanceof MethodInvocation) {
          MethodInvocation methodInvocation=(MethodInvocation)rightOperand;
          MethodDeclaration[] contextMethods=sourceTypeDeclaration.getMethods();
          for (          MethodDeclaration methodDeclaration : contextMethods) {
            if (methodDeclaration.getName().getIdentifier().equals(methodInvocation.getName().getIdentifier())) {
              SimpleName simpleName=MethodDeclarationUtility.isGetter(methodDeclaration);
              if (simpleName != null && simpleName.getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
                sourceRewriter.set(infixExpression,InfixExpression.RIGHT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
                break;
              }
            }
          }
        }
      }
      i++;
    }
  }
  Block setterMethodBody=setterMethod.getBody();
  List<Statement> setterMethodBodyStatements=setterMethodBody.statements();
  ListRewrite setterMethodBodyRewrite=sourceRewriter.getListRewrite(setterMethodBody,Block.STATEMENTS_PROPERTY);
  if (setterMethodBodyStatements.size() == 1) {
    setterMethodBodyRewrite.replace(setterMethodBodyStatements.get(0),setterMethodStatement,null);
  }
  MethodDeclaration getterMethod=typeCheckElimination.getTypeFieldGetterMethod();
  Block getterMethodBody=getterMethod.getBody();
  List<Statement> getterMethodBodyStatements=getterMethodBody.statements();
  ListRewrite getterMethodBodyRewrite=sourceRewriter.getListRewrite(getterMethodBody,Block.STATEMENTS_PROPERTY);
  if (getterMethodBodyStatements.size() == 1) {
    ReturnStatement returnStatement=contextAST.newReturnStatement();
    MethodInvocation abstractGetterMethodInvocation=contextAST.newMethodInvocation();
    sourceRewriter.set(abstractGetterMethodInvocation,MethodInvocation.NAME_PROPERTY,getterMethod.getName(),null);
    sourceRewriter.set(abstractGetterMethodInvocation,MethodInvocation.EXPRESSION_PROPERTY,typeCheckElimination.getTypeField().getName(),null);
    sourceRewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,abstractGetterMethodInvocation,null);
    getterMethodBodyRewrite.replace(getterMethodBodyStatements.get(0),returnStatement,null);
  }
  MethodDeclaration typeCheckMethod=typeCheckElimination.getTypeCheckMethod();
  Block typeCheckMethodBody=typeCheckMethod.getBody();
  ListRewrite typeCheckMethodBodyStatementsRewrite=sourceRewriter.getListRewrite(typeCheckMethodBody,Block.STATEMENTS_PROPERTY);
  Type typeCheckMethodReturnType=typeCheckMethod.getReturnType2();
  if (typeCheckMethodReturnType.isPrimitiveType() && ((PrimitiveType)typeCheckMethodReturnType).getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
    MethodInvocation abstractMethodInvocation=contextAST.newMethodInvocation();
    sourceRewriter.set(abstractMethodInvocation,MethodInvocation.NAME_PROPERTY,typeCheckMethod.getName(),null);
    sourceRewriter.set(abstractMethodInvocation,MethodInvocation.EXPRESSION_PROPERTY,contextAST.newSimpleName(typeCheckElimination.getTypeField().getName().getIdentifier()),null);
    for (    SingleVariableDeclaration abstractMethodParameter : typeCheckElimination.getAccessedParameters()) {
      ListRewrite methodInvocationArgumentsRewrite=sourceRewriter.getListRewrite(abstractMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
      methodInvocationArgumentsRewrite.insertLast(abstractMethodParameter.getName(),null);
    }
    if (typeCheckElimination.getAccessedFields().size() > 0) {
      ListRewrite methodInvocationArgumentsRewrite=sourceRewriter.getListRewrite(abstractMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
      methodInvocationArgumentsRewrite.insertLast(contextAST.newThisExpression(),null);
    }
    ExpressionStatement expressionStatement=contextAST.newExpressionStatement(abstractMethodInvocation);
    typeCheckMethodBodyStatementsRewrite.replace(typeCheckElimination.getTypeCheckCodeFragment(),expressionStatement,null);
  }
 else {
    Assignment assignment=contextAST.newAssignment();
    MethodInvocation abstractMethodInvocation=contextAST.newMethodInvocation();
    sourceRewriter.set(abstractMethodInvocation,MethodInvocation.NAME_PROPERTY,typeCheckMethod.getName(),null);
    sourceRewriter.set(abstractMethodInvocation,MethodInvocation.EXPRESSION_PROPERTY,contextAST.newSimpleName(typeCheckElimination.getTypeField().getName().getIdentifier()),null);
    for (    SingleVariableDeclaration abstractMethodParameter : typeCheckElimination.getAccessedParameters()) {
      ListRewrite methodInvocationArgumentsRewrite=sourceRewriter.getListRewrite(abstractMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
      methodInvocationArgumentsRewrite.insertLast(abstractMethodParameter.getName(),null);
    }
    if (typeCheckElimination.getAccessedFields().size() > 0) {
      ListRewrite methodInvocationArgumentsRewrite=sourceRewriter.getListRewrite(abstractMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
      methodInvocationArgumentsRewrite.insertLast(contextAST.newThisExpression(),null);
    }
    VariableDeclarationFragment returnedVariable=typeCheckElimination.getTypeCheckMethodReturnedVariable();
    sourceRewriter.set(assignment,Assignment.OPERATOR_PROPERTY,Assignment.Operator.ASSIGN,null);
    sourceRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_PROPERTY,returnedVariable.getName(),null);
    sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,abstractMethodInvocation,null);
    ExpressionStatement expressionStatement=contextAST.newExpressionStatement(assignment);
    typeCheckMethodBodyStatementsRewrite.replace(typeCheckElimination.getTypeCheckCodeFragment(),expressionStatement,null);
  }
  modifyContextConstructors();
  generateGettersForAccessedFields();
  ITextFileBufferManager bufferManager=FileBuffers.getTextFileBufferManager();
  ITextFileBuffer sourceTextFileBuffer=bufferManager.getTextFileBuffer(sourceFile.getFullPath(),LocationKind.IFILE);
  IDocument sourceDocument=sourceTextFileBuffer.getDocument();
  TextEdit sourceEdit=sourceRewriter.rewriteAST(sourceDocument,null);
  try {
    UndoEdit sourceUndoEdit=sourceEdit.apply(sourceDocument,UndoEdit.CREATE_UNDO);
    undoRefactoring.put(sourceFile,sourceDocument,sourceUndoEdit);
  }
 catch (  MalformedTreeException e) {
    e.printStackTrace();
  }
catch (  BadLocationException e) {
    e.printStackTrace();
  }
}

Fixed version (ground truth):
private void modifyContext(){
  AST contextAST=sourceTypeDeclaration.getAST();
  ListRewrite contextBodyRewrite=sourceRewriter.getListRewrite(sourceTypeDeclaration,TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
  VariableDeclarationFragment typeFragment=contextAST.newVariableDeclarationFragment();
  sourceRewriter.set(typeFragment,VariableDeclarationFragment.NAME_PROPERTY,typeCheckElimination.getTypeField().getName(),null);
  FieldDeclaration typeFieldDeclaration=contextAST.newFieldDeclaration(typeFragment);
  sourceRewriter.set(typeFieldDeclaration,FieldDeclaration.TYPE_PROPERTY,contextAST.newSimpleName(typeCheckElimination.getAbstractClassName()),null);
  ListRewrite typeFieldDeclrationModifiersRewrite=sourceRewriter.getListRewrite(typeFieldDeclaration,FieldDeclaration.MODIFIERS2_PROPERTY);
  typeFieldDeclrationModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD),null);
  contextBodyRewrite.insertFirst(typeFieldDeclaration,null);
  FieldDeclaration[] fieldDeclarations=sourceTypeDeclaration.getFields();
  for (  FieldDeclaration fieldDeclaration : fieldDeclarations) {
    List<VariableDeclarationFragment> fragments=fieldDeclaration.fragments();
    for (    VariableDeclarationFragment fragment : fragments) {
      if (fragment.equals(typeCheckElimination.getTypeField())) {
        if (fragments.size() == 1) {
          contextBodyRewrite.remove(fragment.getParent(),null);
        }
 else {
          ListRewrite fragmentRewrite=sourceRewriter.getListRewrite(fragment.getParent(),FieldDeclaration.FRAGMENTS_PROPERTY);
          fragmentRewrite.remove(fragment,null);
        }
      }
    }
  }
  MethodDeclaration setterMethod=typeCheckElimination.getTypeFieldSetterMethod();
  List<SingleVariableDeclaration> setterMethodParameters=setterMethod.parameters();
  Statement setterMethodStatement=(Statement)ASTNode.copySubtree(contextAST,typeCheckElimination.getTypeCheckCodeFragment());
  if (setterMethodStatement instanceof SwitchStatement) {
    SwitchStatement switchStatement=(SwitchStatement)setterMethodStatement;
    List<Statement> switchStatementStatements=switchStatement.statements();
    if (setterMethodParameters.size() == 1) {
      sourceRewriter.set(switchStatement,SwitchStatement.EXPRESSION_PROPERTY,setterMethodParameters.get(0).getName(),null);
    }
    ListRewrite switchStatementStatementsRewrite=sourceRewriter.getListRewrite(switchStatement,SwitchStatement.STATEMENTS_PROPERTY);
    List<String> subclassNames=typeCheckElimination.getSubclassNames();
    Collection<ArrayList<Statement>> allTypeCheckStatements=typeCheckElimination.getTypeCheckStatements();
    int i=0;
    for (    ArrayList<Statement> typeCheckStatements : allTypeCheckStatements) {
      int matches=0;
      for (      Statement oldStatement : typeCheckStatements) {
        for (        Statement newStatement : switchStatementStatements) {
          if (oldStatement.toString().equals(newStatement.toString())) {
            matches++;
            if (matches == typeCheckStatements.size()) {
              Assignment assignment=contextAST.newAssignment();
              sourceRewriter.set(assignment,Assignment.OPERATOR_PROPERTY,Assignment.Operator.ASSIGN,null);
              FieldAccess typeFieldAccess=contextAST.newFieldAccess();
              sourceRewriter.set(typeFieldAccess,FieldAccess.EXPRESSION_PROPERTY,contextAST.newThisExpression(),null);
              sourceRewriter.set(typeFieldAccess,FieldAccess.NAME_PROPERTY,typeCheckElimination.getTypeField().getName(),null);
              sourceRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_PROPERTY,typeFieldAccess,null);
              ClassInstanceCreation classInstanceCreation=contextAST.newClassInstanceCreation();
              sourceRewriter.set(classInstanceCreation,ClassInstanceCreation.TYPE_PROPERTY,contextAST.newSimpleName(subclassNames.get(i)),null);
              sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,classInstanceCreation,null);
              switchStatementStatementsRewrite.replace(newStatement,contextAST.newExpressionStatement(assignment),null);
            }
 else {
              switchStatementStatementsRewrite.remove(newStatement,null);
            }
          }
        }
      }
      i++;
    }
  }
 else   if (setterMethodStatement instanceof IfStatement) {
    IfStatement typeCheckIfStatement=(IfStatement)setterMethodStatement;
    List<String> subclassNames=typeCheckElimination.getSubclassNames();
    Collection<ArrayList<Statement>> allTypeCheckStatements=typeCheckElimination.getTypeCheckStatements();
    int i=0;
    for (    ArrayList<Statement> typeCheckStatements : allTypeCheckStatements) {
      if (typeCheckStatements.size() == 1) {
        Statement oldStatement=typeCheckStatements.get(0);
        IfStatement ifStatement=typeCheckIfStatement;
        Statement newStatement=typeCheckIfStatement.getThenStatement();
        while (!oldStatement.toString().equals(newStatement.toString())) {
          Statement elseStatement=ifStatement.getElseStatement();
          if (elseStatement instanceof IfStatement) {
            ifStatement=(IfStatement)elseStatement;
            newStatement=ifStatement.getThenStatement();
          }
        }
        IfStatement parentIfStatement=(IfStatement)newStatement.getParent();
        Assignment assignment=contextAST.newAssignment();
        sourceRewriter.set(assignment,Assignment.OPERATOR_PROPERTY,Assignment.Operator.ASSIGN,null);
        FieldAccess typeFieldAccess=contextAST.newFieldAccess();
        sourceRewriter.set(typeFieldAccess,FieldAccess.EXPRESSION_PROPERTY,contextAST.newThisExpression(),null);
        sourceRewriter.set(typeFieldAccess,FieldAccess.NAME_PROPERTY,typeCheckElimination.getTypeField().getName(),null);
        sourceRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_PROPERTY,typeFieldAccess,null);
        ClassInstanceCreation classInstanceCreation=contextAST.newClassInstanceCreation();
        sourceRewriter.set(classInstanceCreation,ClassInstanceCreation.TYPE_PROPERTY,contextAST.newSimpleName(subclassNames.get(i)),null);
        sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,classInstanceCreation,null);
        sourceRewriter.set(parentIfStatement,IfStatement.THEN_STATEMENT_PROPERTY,contextAST.newExpressionStatement(assignment),null);
        InfixExpression infixExpression=(InfixExpression)parentIfStatement.getExpression();
        Expression leftOperand=infixExpression.getLeftOperand();
        if (leftOperand instanceof SimpleName) {
          SimpleName simpleName=(SimpleName)leftOperand;
          if (simpleName.getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
            sourceRewriter.set(infixExpression,InfixExpression.LEFT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
          }
        }
 else         if (leftOperand instanceof FieldAccess) {
          FieldAccess fieldAccess=(FieldAccess)leftOperand;
          if (fieldAccess.getName().getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
            sourceRewriter.set(infixExpression,InfixExpression.LEFT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
          }
        }
 else         if (leftOperand instanceof MethodInvocation) {
          MethodInvocation methodInvocation=(MethodInvocation)leftOperand;
          MethodDeclaration[] contextMethods=sourceTypeDeclaration.getMethods();
          for (          MethodDeclaration methodDeclaration : contextMethods) {
            if (methodDeclaration.getName().getIdentifier().equals(methodInvocation.getName().getIdentifier())) {
              SimpleName simpleName=MethodDeclarationUtility.isGetter(methodDeclaration);
              if (simpleName != null && simpleName.getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
                sourceRewriter.set(infixExpression,InfixExpression.LEFT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
                break;
              }
            }
          }
        }
        Expression rightOperand=infixExpression.getRightOperand();
        if (rightOperand instanceof SimpleName) {
          SimpleName simpleName=(SimpleName)rightOperand;
          if (simpleName.getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
            sourceRewriter.set(infixExpression,InfixExpression.RIGHT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
          }
        }
 else         if (rightOperand instanceof FieldAccess) {
          FieldAccess fieldAccess=(FieldAccess)rightOperand;
          if (fieldAccess.getName().getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
            sourceRewriter.set(infixExpression,InfixExpression.RIGHT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
          }
        }
 else         if (rightOperand instanceof MethodInvocation) {
          MethodInvocation methodInvocation=(MethodInvocation)rightOperand;
          MethodDeclaration[] contextMethods=sourceTypeDeclaration.getMethods();
          for (          MethodDeclaration methodDeclaration : contextMethods) {
            if (methodDeclaration.getName().getIdentifier().equals(methodInvocation.getName().getIdentifier())) {
              SimpleName simpleName=MethodDeclarationUtility.isGetter(methodDeclaration);
              if (simpleName != null && simpleName.getIdentifier().equals(typeCheckElimination.getTypeField().getName().getIdentifier())) {
                sourceRewriter.set(infixExpression,InfixExpression.RIGHT_OPERAND_PROPERTY,setterMethodParameters.get(0).getName(),null);
                break;
              }
            }
          }
        }
      }
      i++;
    }
  }
  Block setterMethodBody=setterMethod.getBody();
  List<Statement> setterMethodBodyStatements=setterMethodBody.statements();
  ListRewrite setterMethodBodyRewrite=sourceRewriter.getListRewrite(setterMethodBody,Block.STATEMENTS_PROPERTY);
  if (setterMethodBodyStatements.size() == 1) {
    setterMethodBodyRewrite.replace(setterMethodBodyStatements.get(0),setterMethodStatement,null);
  }
  MethodDeclaration getterMethod=typeCheckElimination.getTypeFieldGetterMethod();
  Block getterMethodBody=getterMethod.getBody();
  List<Statement> getterMethodBodyStatements=getterMethodBody.statements();
  ListRewrite getterMethodBodyRewrite=sourceRewriter.getListRewrite(getterMethodBody,Block.STATEMENTS_PROPERTY);
  if (getterMethodBodyStatements.size() == 1) {
    ReturnStatement returnStatement=contextAST.newReturnStatement();
    MethodInvocation abstractGetterMethodInvocation=contextAST.newMethodInvocation();
    sourceRewriter.set(abstractGetterMethodInvocation,MethodInvocation.NAME_PROPERTY,getterMethod.getName(),null);
    sourceRewriter.set(abstractGetterMethodInvocation,MethodInvocation.EXPRESSION_PROPERTY,typeCheckElimination.getTypeField().getName(),null);
    sourceRewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,abstractGetterMethodInvocation,null);
    getterMethodBodyRewrite.replace(getterMethodBodyStatements.get(0),returnStatement,null);
  }
  MethodDeclaration typeCheckMethod=typeCheckElimination.getTypeCheckMethod();
  Block typeCheckCodeFragmentParentBlock=(Block)typeCheckElimination.getTypeCheckCodeFragment().getParent();
  ListRewrite typeCheckCodeFragmentParentBlockStatementsRewrite=sourceRewriter.getListRewrite(typeCheckCodeFragmentParentBlock,Block.STATEMENTS_PROPERTY);
  Type typeCheckMethodReturnType=typeCheckMethod.getReturnType2();
  if (typeCheckMethodReturnType.isPrimitiveType() && ((PrimitiveType)typeCheckMethodReturnType).getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
    MethodInvocation abstractMethodInvocation=contextAST.newMethodInvocation();
    sourceRewriter.set(abstractMethodInvocation,MethodInvocation.NAME_PROPERTY,typeCheckMethod.getName(),null);
    sourceRewriter.set(abstractMethodInvocation,MethodInvocation.EXPRESSION_PROPERTY,contextAST.newSimpleName(typeCheckElimination.getTypeField().getName().getIdentifier()),null);
    for (    SingleVariableDeclaration abstractMethodParameter : typeCheckElimination.getAccessedParameters()) {
      ListRewrite methodInvocationArgumentsRewrite=sourceRewriter.getListRewrite(abstractMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
      methodInvocationArgumentsRewrite.insertLast(abstractMethodParameter.getName(),null);
    }
    if (typeCheckElimination.getAccessedFields().size() > 0) {
      ListRewrite methodInvocationArgumentsRewrite=sourceRewriter.getListRewrite(abstractMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
      methodInvocationArgumentsRewrite.insertLast(contextAST.newThisExpression(),null);
    }
    ExpressionStatement expressionStatement=contextAST.newExpressionStatement(abstractMethodInvocation);
    typeCheckCodeFragmentParentBlockStatementsRewrite.replace(typeCheckElimination.getTypeCheckCodeFragment(),expressionStatement,null);
  }
 else {
    MethodInvocation abstractMethodInvocation=contextAST.newMethodInvocation();
    sourceRewriter.set(abstractMethodInvocation,MethodInvocation.NAME_PROPERTY,typeCheckMethod.getName(),null);
    sourceRewriter.set(abstractMethodInvocation,MethodInvocation.EXPRESSION_PROPERTY,contextAST.newSimpleName(typeCheckElimination.getTypeField().getName().getIdentifier()),null);
    for (    SingleVariableDeclaration abstractMethodParameter : typeCheckElimination.getAccessedParameters()) {
      ListRewrite methodInvocationArgumentsRewrite=sourceRewriter.getListRewrite(abstractMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
      methodInvocationArgumentsRewrite.insertLast(abstractMethodParameter.getName(),null);
    }
    if (typeCheckElimination.getAccessedFields().size() > 0) {
      ListRewrite methodInvocationArgumentsRewrite=sourceRewriter.getListRewrite(abstractMethodInvocation,MethodInvocation.ARGUMENTS_PROPERTY);
      methodInvocationArgumentsRewrite.insertLast(contextAST.newThisExpression(),null);
    }
    VariableDeclarationFragment returnedVariable=typeCheckElimination.getTypeCheckMethodReturnedVariable();
    if (returnedVariable != null) {
      Assignment assignment=contextAST.newAssignment();
      sourceRewriter.set(assignment,Assignment.OPERATOR_PROPERTY,Assignment.Operator.ASSIGN,null);
      sourceRewriter.set(assignment,Assignment.LEFT_HAND_SIDE_PROPERTY,returnedVariable.getName(),null);
      sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,abstractMethodInvocation,null);
      ExpressionStatement expressionStatement=contextAST.newExpressionStatement(assignment);
      typeCheckCodeFragmentParentBlockStatementsRewrite.replace(typeCheckElimination.getTypeCheckCodeFragment(),expressionStatement,null);
    }
 else {
      ReturnStatement returnStatement=contextAST.newReturnStatement();
      sourceRewriter.set(returnStatement,ReturnStatement.EXPRESSION_PROPERTY,abstractMethodInvocation,null);
      typeCheckCodeFragmentParentBlockStatementsRewrite.replace(typeCheckElimination.getTypeCheckCodeFragment(),returnStatement,null);
    }
  }
  modifyContextConstructors();
  generateGettersForAccessedFields();
  ITextFileBufferManager bufferManager=FileBuffers.getTextFileBufferManager();
  ITextFileBuffer sourceTextFileBuffer=bufferManager.getTextFileBuffer(sourceFile.getFullPath(),LocationKind.IFILE);
  IDocument sourceDocument=sourceTextFileBuffer.getDocument();
  TextEdit sourceEdit=sourceRewriter.rewriteAST(sourceDocument,null);
  try {
    UndoEdit sourceUndoEdit=sourceEdit.apply(sourceDocument,UndoEdit.CREATE_UNDO);
    undoRefactoring.put(sourceFile,sourceDocument,sourceUndoEdit);
  }
 catch (  MalformedTreeException e) {
    e.printStackTrace();
  }
catch (  BadLocationException e) {
    e.printStackTrace();
  }
}