public static void rewriteModifiers(
      final FieldDeclaration declarationNode,
      final VariableDeclarationFragment[] toChange,
      final int includedModifiers,
      final int excludedModifiers,
      final ASTRewrite rewrite,
      final TextEditGroup group) {
    final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
    AST ast = declarationNode.getAST();
    /*
     * Problem: Same declarationNode can be the subject of multiple calls to this method.
     * For the 2nd++ calls, the original declarationNode has already been rewritten, and this has to be taken into account.
     *
     * Assumption:
     * - Modifiers for each VariableDeclarationFragment are modified at most once.
     *
     * Solution:
     * - Maintain a map from original VariableDeclarationFragments to their new FieldDeclaration.
     * - Original modifiers in declarationNode belong to the first fragment.
     * - When a later fragment needs different modifiers, we create a new FieldDeclaration and move all successive fragments into that
     * declaration
     * - When a fragment has been moved to a new declaration, make sure we don't create a new move target again,
     * but instead use the already created one
     */
    List<VariableDeclarationFragment> fragments = declarationNode.fragments();
    Iterator<VariableDeclarationFragment> iter = fragments.iterator();

    ListRewrite blockRewrite;
    if (declarationNode.getParent() instanceof AbstractTypeDeclaration) {
      blockRewrite =
          rewrite.getListRewrite(
              declarationNode.getParent(),
              ((AbstractTypeDeclaration) declarationNode.getParent())
                  .getBodyDeclarationsProperty());
    } else {
      blockRewrite =
          rewrite.getListRewrite(
              declarationNode.getParent(), AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
    }

    VariableDeclarationFragment lastFragment = iter.next();
    ASTNode lastStatement = declarationNode;

    if (fragmentsToChange.contains(lastFragment)) {
      ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
      modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
    }

    ListRewrite fragmentsRewrite = null;
    while (iter.hasNext()) {
      VariableDeclarationFragment currentFragment = iter.next();

      Map<VariableDeclarationFragment, MovedFragment> lookup =
          (Map<VariableDeclarationFragment, MovedFragment>)
              rewrite.getProperty(MovedFragment.class.getName());
      if (lookup == null) {
        lookup = new HashMap<VariableDeclarationFragment, MovedFragment>();
        rewrite.setProperty(MovedFragment.class.getName(), lookup);
      }
      MovedFragment currentMovedFragment = lookup.get(currentFragment);

      boolean changeLast = fragmentsToChange.contains(lastFragment);
      boolean changeCurrent = fragmentsToChange.contains(currentFragment);
      if (changeLast != changeCurrent || lookup.containsKey(lastFragment)) {
        ModifierRewrite modifierRewrite = null;
        if (currentMovedFragment != null) {
          // Current fragment has already been moved.

          if (currentMovedFragment.fUsesOriginalModifiers) {
            // Need to put in the right modifiers (removing any existing ones).
            modifierRewrite = ModifierRewrite.create(rewrite, currentMovedFragment.fDeclaration);
            ListRewrite listRewrite =
                rewrite.getListRewrite(
                    currentMovedFragment.fDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
            List<ASTNode> extendedList = listRewrite.getRewrittenList();
            for (int i = 0; i < extendedList.size(); i++) {
              ASTNode curr = extendedList.get(i);
              if (curr instanceof Modifier) rewrite.remove(curr, group);
            }
          }
          // otherwise, don't need to touch the modifiers, so leave modifierRewrite null

        } else { // need to split an existing field declaration
          VariableDeclarationFragment moveTarget;
          moveTarget = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);

          FieldDeclaration newStatement =
              (FieldDeclaration) ast.createInstance(FieldDeclaration.FIELD_DECLARATION);
          rewrite
              .getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY)
              .insertLast(moveTarget, group);
          lookup.put(currentFragment, new MovedFragment(moveTarget, newStatement, !changeCurrent));
          rewrite.set(
              newStatement,
              FieldDeclaration.TYPE_PROPERTY,
              rewrite.createCopyTarget(declarationNode.getType()),
              group);

          modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
          modifierRewrite.copyAllAnnotations(declarationNode, group);
          blockRewrite.insertAfter(newStatement, lastStatement, group);

          fragmentsRewrite =
              rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY);
          lastStatement = newStatement;
        }

        if (modifierRewrite != null) {
          if (changeCurrent) {
            int newModifiers =
                (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
            modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
          } else {
            int newModifiers = declarationNode.getModifiers();
            modifierRewrite.setModifiers(newModifiers, Modifier.NONE, group);
          }
        }

      } else if (fragmentsRewrite != null) {
        VariableDeclarationFragment fragment0;
        boolean usesOriginalModifiers = true;
        if (currentMovedFragment != null) {
          fragment0 = currentMovedFragment.fMoveTarget;
          usesOriginalModifiers = currentMovedFragment.fUsesOriginalModifiers;
          rewrite
              .getListRewrite(
                  currentMovedFragment.fDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY)
              .remove(fragment0, group);
        } else {
          fragment0 = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
        }
        lookup.put(
            currentFragment, new MovedFragment(fragment0, lastStatement, usesOriginalModifiers));
        fragmentsRewrite.insertLast(fragment0, group);
      }
      lastFragment = currentFragment;
    }
  }