Program: 12088
Project: 12088
Buggy source:
private void generateSetterMethodForStateField(){
  ASTRewrite sourceRewriter=ASTRewrite.create(sourceTypeDeclaration.getAST());
  AST contextAST=sourceTypeDeclaration.getAST();
  ListRewrite contextBodyRewrite=sourceRewriter.getListRewrite(sourceTypeDeclaration,TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
  MethodDeclaration setterMethod=typeCheckElimination.getTypeFieldSetterMethod();
  SwitchStatement switchStatement=contextAST.newSwitchStatement();
  List<SimpleName> staticFieldNames=new ArrayList<SimpleName>(staticFieldMap.keySet());
  List<String> subclassNames=new ArrayList<String>(staticFieldMap.values());
  ListRewrite switchStatementStatementsRewrite=sourceRewriter.getListRewrite(switchStatement,SwitchStatement.STATEMENTS_PROPERTY);
  int i=0;
  for (  SimpleName staticFieldName : staticFieldNames) {
    SwitchCase switchCase=contextAST.newSwitchCase();
    IBinding staticFieldNameBinding=staticFieldName.resolveBinding();
    String staticFieldNameDeclaringClass=null;
    boolean isEnumConstant=false;
    if (staticFieldNameBinding != null && staticFieldNameBinding.getKind() == IBinding.VARIABLE) {
      IVariableBinding staticFieldNameVariableBinding=(IVariableBinding)staticFieldNameBinding;
      isEnumConstant=staticFieldNameVariableBinding.isEnumConstant();
      if (!sourceTypeDeclaration.resolveBinding().isEqualTo(staticFieldNameVariableBinding.getDeclaringClass())) {
        staticFieldNameDeclaringClass=staticFieldNameVariableBinding.getDeclaringClass().getName();
      }
    }
    if (staticFieldNameDeclaringClass == null || isEnumConstant) {
      sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,staticFieldName,null);
    }
 else {
      FieldAccess fieldAccess=contextAST.newFieldAccess();
      sourceRewriter.set(fieldAccess,FieldAccess.EXPRESSION_PROPERTY,contextAST.newSimpleName(staticFieldNameDeclaringClass),null);
      sourceRewriter.set(fieldAccess,FieldAccess.NAME_PROPERTY,staticFieldName,null);
      sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,fieldAccess,null);
    }
    switchStatementStatementsRewrite.insertLast(switchCase,null);
    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.insertLast(contextAST.newExpressionStatement(assignment),null);
    switchStatementStatementsRewrite.insertLast(contextAST.newBreakStatement(),null);
    i++;
  }
  for (  SimpleName staticFieldName : additionalStaticFieldMap.keySet()) {
    SwitchCase switchCase=contextAST.newSwitchCase();
    IBinding staticFieldNameBinding=staticFieldName.resolveBinding();
    String staticFieldNameDeclaringClass=null;
    boolean isEnumConstant=false;
    if (staticFieldNameBinding != null && staticFieldNameBinding.getKind() == IBinding.VARIABLE) {
      IVariableBinding staticFieldNameVariableBinding=(IVariableBinding)staticFieldNameBinding;
      isEnumConstant=staticFieldNameVariableBinding.isEnumConstant();
      if (!sourceTypeDeclaration.resolveBinding().isEqualTo(staticFieldNameVariableBinding.getDeclaringClass())) {
        staticFieldNameDeclaringClass=staticFieldNameVariableBinding.getDeclaringClass().getName();
      }
    }
    if (staticFieldNameDeclaringClass == null || isEnumConstant) {
      sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,staticFieldName,null);
    }
 else {
      FieldAccess fieldAccess=contextAST.newFieldAccess();
      sourceRewriter.set(fieldAccess,FieldAccess.EXPRESSION_PROPERTY,contextAST.newSimpleName(staticFieldNameDeclaringClass),null);
      sourceRewriter.set(fieldAccess,FieldAccess.NAME_PROPERTY,staticFieldName,null);
      sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,fieldAccess,null);
    }
    switchStatementStatementsRewrite.insertLast(switchCase,null);
    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(additionalStaticFieldMap.get(staticFieldName)),null);
    sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,classInstanceCreation,null);
    switchStatementStatementsRewrite.insertLast(contextAST.newExpressionStatement(assignment),null);
    switchStatementStatementsRewrite.insertLast(contextAST.newBreakStatement(),null);
  }
  SwitchCase switchCase=contextAST.newSwitchCase();
  sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,null,null);
  switchStatementStatementsRewrite.insertLast(switchCase,null);
  Assignment nullAssignment=contextAST.newAssignment();
  sourceRewriter.set(nullAssignment,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(nullAssignment,Assignment.LEFT_HAND_SIDE_PROPERTY,typeFieldAccess,null);
  sourceRewriter.set(nullAssignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,contextAST.newNullLiteral(),null);
  switchStatementStatementsRewrite.insertLast(contextAST.newExpressionStatement(nullAssignment),null);
  switchStatementStatementsRewrite.insertLast(contextAST.newBreakStatement(),null);
  if (setterMethod != null) {
    List<SingleVariableDeclaration> setterMethodParameters=setterMethod.parameters();
    if (setterMethodParameters.size() == 1) {
      sourceRewriter.set(switchStatement,SwitchStatement.EXPRESSION_PROPERTY,setterMethodParameters.get(0).getName(),null);
    }
    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),switchStatement,null);
    }
    try {
      TextEdit sourceEdit=sourceRewriter.rewriteAST();
      ICompilationUnit sourceICompilationUnit=(ICompilationUnit)sourceCompilationUnit.getJavaElement();
      CompilationUnitChange change=compilationUnitChanges.get(sourceICompilationUnit);
      change.getEdit().addChild(sourceEdit);
      change.addTextEditGroup(new TextEditGroup("String_Node_Str",new TextEdit[]{sourceEdit}));
    }
 catch (    JavaModelException e) {
      e.printStackTrace();
    }
  }
 else {
    MethodDeclaration setterMethodDeclaration=contextAST.newMethodDeclaration();
    sourceRewriter.set(setterMethodDeclaration,MethodDeclaration.NAME_PROPERTY,contextAST.newSimpleName("String_Node_Str" + abstractClassName),null);
    sourceRewriter.set(setterMethodDeclaration,MethodDeclaration.RETURN_TYPE2_PROPERTY,contextAST.newPrimitiveType(PrimitiveType.VOID),null);
    ListRewrite setterMethodModifiersRewrite=sourceRewriter.getListRewrite(setterMethodDeclaration,MethodDeclaration.MODIFIERS2_PROPERTY);
    setterMethodModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD),null);
    ListRewrite setterMethodParameterRewrite=sourceRewriter.getListRewrite(setterMethodDeclaration,MethodDeclaration.PARAMETERS_PROPERTY);
    SingleVariableDeclaration parameter=contextAST.newSingleVariableDeclaration();
    VariableDeclarationFragment typeField=typeCheckElimination.getTypeField();
    Type parameterType=((FieldDeclaration)typeField.getParent()).getType();
    sourceRewriter.set(parameter,SingleVariableDeclaration.TYPE_PROPERTY,parameterType,null);
    sourceRewriter.set(parameter,SingleVariableDeclaration.NAME_PROPERTY,typeField.getName(),null);
    setterMethodParameterRewrite.insertLast(parameter,null);
    sourceRewriter.set(switchStatement,SwitchStatement.EXPRESSION_PROPERTY,typeField.getName(),null);
    Block setterMethodBody=contextAST.newBlock();
    ListRewrite setterMethodBodyRewrite=sourceRewriter.getListRewrite(setterMethodBody,Block.STATEMENTS_PROPERTY);
    setterMethodBodyRewrite.insertLast(switchStatement,null);
    sourceRewriter.set(setterMethodDeclaration,MethodDeclaration.BODY_PROPERTY,setterMethodBody,null);
    contextBodyRewrite.insertLast(setterMethodDeclaration,null);
    try {
      TextEdit sourceEdit=sourceRewriter.rewriteAST();
      ICompilationUnit sourceICompilationUnit=(ICompilationUnit)sourceCompilationUnit.getJavaElement();
      CompilationUnitChange change=compilationUnitChanges.get(sourceICompilationUnit);
      change.getEdit().addChild(sourceEdit);
      change.addTextEditGroup(new TextEditGroup("String_Node_Str",new TextEdit[]{sourceEdit}));
    }
 catch (    JavaModelException e) {
      e.printStackTrace();
    }
  }
}

Fixed version (ground truth):
private void generateSetterMethodForStateField(){
  ASTRewrite sourceRewriter=ASTRewrite.create(sourceTypeDeclaration.getAST());
  AST contextAST=sourceTypeDeclaration.getAST();
  ListRewrite contextBodyRewrite=sourceRewriter.getListRewrite(sourceTypeDeclaration,TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
  MethodDeclaration setterMethod=typeCheckElimination.getTypeFieldSetterMethod();
  SwitchStatement switchStatement=contextAST.newSwitchStatement();
  List<SimpleName> staticFieldNames=new ArrayList<SimpleName>(staticFieldMap.keySet());
  List<String> subclassNames=new ArrayList<String>(staticFieldMap.values());
  ListRewrite switchStatementStatementsRewrite=sourceRewriter.getListRewrite(switchStatement,SwitchStatement.STATEMENTS_PROPERTY);
  int i=0;
  for (  SimpleName staticFieldName : staticFieldNames) {
    SwitchCase switchCase=contextAST.newSwitchCase();
    IBinding staticFieldNameBinding=staticFieldName.resolveBinding();
    String staticFieldNameDeclaringClass=null;
    boolean isEnumConstant=false;
    if (staticFieldNameBinding != null && staticFieldNameBinding.getKind() == IBinding.VARIABLE) {
      IVariableBinding staticFieldNameVariableBinding=(IVariableBinding)staticFieldNameBinding;
      isEnumConstant=staticFieldNameVariableBinding.isEnumConstant();
      if (!sourceTypeDeclaration.resolveBinding().isEqualTo(staticFieldNameVariableBinding.getDeclaringClass())) {
        staticFieldNameDeclaringClass=staticFieldNameVariableBinding.getDeclaringClass().getName();
      }
    }
    if (staticFieldNameDeclaringClass == null || isEnumConstant) {
      sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,staticFieldName,null);
    }
 else {
      FieldAccess fieldAccess=contextAST.newFieldAccess();
      sourceRewriter.set(fieldAccess,FieldAccess.EXPRESSION_PROPERTY,contextAST.newSimpleName(staticFieldNameDeclaringClass),null);
      sourceRewriter.set(fieldAccess,FieldAccess.NAME_PROPERTY,staticFieldName,null);
      sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,fieldAccess,null);
    }
    switchStatementStatementsRewrite.insertLast(switchCase,null);
    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.insertLast(contextAST.newExpressionStatement(assignment),null);
    switchStatementStatementsRewrite.insertLast(contextAST.newBreakStatement(),null);
    i++;
  }
  for (  SimpleName staticFieldName : additionalStaticFieldMap.keySet()) {
    SwitchCase switchCase=contextAST.newSwitchCase();
    IBinding staticFieldNameBinding=staticFieldName.resolveBinding();
    String staticFieldNameDeclaringClass=null;
    boolean isEnumConstant=false;
    if (staticFieldNameBinding != null && staticFieldNameBinding.getKind() == IBinding.VARIABLE) {
      IVariableBinding staticFieldNameVariableBinding=(IVariableBinding)staticFieldNameBinding;
      isEnumConstant=staticFieldNameVariableBinding.isEnumConstant();
      if (!sourceTypeDeclaration.resolveBinding().isEqualTo(staticFieldNameVariableBinding.getDeclaringClass())) {
        staticFieldNameDeclaringClass=staticFieldNameVariableBinding.getDeclaringClass().getName();
      }
    }
    if (staticFieldNameDeclaringClass == null || isEnumConstant) {
      sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,staticFieldName,null);
    }
 else {
      FieldAccess fieldAccess=contextAST.newFieldAccess();
      sourceRewriter.set(fieldAccess,FieldAccess.EXPRESSION_PROPERTY,contextAST.newSimpleName(staticFieldNameDeclaringClass),null);
      sourceRewriter.set(fieldAccess,FieldAccess.NAME_PROPERTY,staticFieldName,null);
      sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,fieldAccess,null);
    }
    switchStatementStatementsRewrite.insertLast(switchCase,null);
    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(additionalStaticFieldMap.get(staticFieldName)),null);
    sourceRewriter.set(assignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,classInstanceCreation,null);
    switchStatementStatementsRewrite.insertLast(contextAST.newExpressionStatement(assignment),null);
    switchStatementStatementsRewrite.insertLast(contextAST.newBreakStatement(),null);
  }
  SwitchCase switchCase=contextAST.newSwitchCase();
  sourceRewriter.set(switchCase,SwitchCase.EXPRESSION_PROPERTY,null,null);
  switchStatementStatementsRewrite.insertLast(switchCase,null);
  Assignment nullAssignment=contextAST.newAssignment();
  sourceRewriter.set(nullAssignment,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(nullAssignment,Assignment.LEFT_HAND_SIDE_PROPERTY,typeFieldAccess,null);
  sourceRewriter.set(nullAssignment,Assignment.RIGHT_HAND_SIDE_PROPERTY,contextAST.newNullLiteral(),null);
  switchStatementStatementsRewrite.insertLast(contextAST.newExpressionStatement(nullAssignment),null);
  switchStatementStatementsRewrite.insertLast(contextAST.newBreakStatement(),null);
  if (setterMethod != null) {
    List<SingleVariableDeclaration> setterMethodParameters=setterMethod.parameters();
    if (setterMethodParameters.size() == 1) {
      sourceRewriter.set(switchStatement,SwitchStatement.EXPRESSION_PROPERTY,setterMethodParameters.get(0).getName(),null);
    }
    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),switchStatement,null);
    }
    try {
      TextEdit sourceEdit=sourceRewriter.rewriteAST();
      ICompilationUnit sourceICompilationUnit=(ICompilationUnit)sourceCompilationUnit.getJavaElement();
      CompilationUnitChange change=compilationUnitChanges.get(sourceICompilationUnit);
      change.getEdit().addChild(sourceEdit);
      change.addTextEditGroup(new TextEditGroup("String_Node_Str",new TextEdit[]{sourceEdit}));
    }
 catch (    JavaModelException e) {
      e.printStackTrace();
    }
  }
 else {
    MethodDeclaration setterMethodDeclaration=contextAST.newMethodDeclaration();
    sourceRewriter.set(setterMethodDeclaration,MethodDeclaration.NAME_PROPERTY,contextAST.newSimpleName(setterMethodName()),null);
    sourceRewriter.set(setterMethodDeclaration,MethodDeclaration.RETURN_TYPE2_PROPERTY,contextAST.newPrimitiveType(PrimitiveType.VOID),null);
    ListRewrite setterMethodModifiersRewrite=sourceRewriter.getListRewrite(setterMethodDeclaration,MethodDeclaration.MODIFIERS2_PROPERTY);
    setterMethodModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD),null);
    ListRewrite setterMethodParameterRewrite=sourceRewriter.getListRewrite(setterMethodDeclaration,MethodDeclaration.PARAMETERS_PROPERTY);
    SingleVariableDeclaration parameter=contextAST.newSingleVariableDeclaration();
    VariableDeclarationFragment typeField=typeCheckElimination.getTypeField();
    Type parameterType=((FieldDeclaration)typeField.getParent()).getType();
    sourceRewriter.set(parameter,SingleVariableDeclaration.TYPE_PROPERTY,parameterType,null);
    sourceRewriter.set(parameter,SingleVariableDeclaration.NAME_PROPERTY,typeField.getName(),null);
    setterMethodParameterRewrite.insertLast(parameter,null);
    sourceRewriter.set(switchStatement,SwitchStatement.EXPRESSION_PROPERTY,typeField.getName(),null);
    Block setterMethodBody=contextAST.newBlock();
    ListRewrite setterMethodBodyRewrite=sourceRewriter.getListRewrite(setterMethodBody,Block.STATEMENTS_PROPERTY);
    setterMethodBodyRewrite.insertLast(switchStatement,null);
    sourceRewriter.set(setterMethodDeclaration,MethodDeclaration.BODY_PROPERTY,setterMethodBody,null);
    contextBodyRewrite.insertLast(setterMethodDeclaration,null);
    try {
      TextEdit sourceEdit=sourceRewriter.rewriteAST();
      ICompilationUnit sourceICompilationUnit=(ICompilationUnit)sourceCompilationUnit.getJavaElement();
      CompilationUnitChange change=compilationUnitChanges.get(sourceICompilationUnit);
      change.getEdit().addChild(sourceEdit);
      change.addTextEditGroup(new TextEditGroup("String_Node_Str",new TextEdit[]{sourceEdit}));
    }
 catch (    JavaModelException e) {
      e.printStackTrace();
    }
  }
}