Пример #1
0
    private void splitUpDeclarations(
        ASTRewrite rewrite,
        TextEditGroup group,
        VariableDeclarationFragment frag,
        ListRewrite statementRewrite,
        VariableDeclarationStatement originalStatement) {
      Expression initializer = frag.getInitializer();
      // keep constructors and method invocations
      if (initializer instanceof MethodInvocation || initializer instanceof ClassInstanceCreation) {
        Expression movedInitializer = (Expression) rewrite.createMoveTarget(initializer);

        ExpressionStatement newInitializer =
            rewrite.getAST().newExpressionStatement(movedInitializer);
        statementRewrite.insertAfter(newInitializer, originalStatement, group);

        VariableDeclarationStatement newDeclaration = null;
        List<VariableDeclarationFragment> fragments = originalStatement.fragments();
        int fragIndex = fragments.indexOf(frag);
        ListIterator<VariableDeclarationFragment> fragmentIterator =
            fragments.listIterator(fragIndex + 1);
        while (fragmentIterator.hasNext()) {
          VariableDeclarationFragment currentFragment = fragmentIterator.next();
          VariableDeclarationFragment movedFragment =
              (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
          if (newDeclaration == null) {
            newDeclaration = rewrite.getAST().newVariableDeclarationStatement(movedFragment);
            Type copiedType = (Type) rewrite.createCopyTarget(originalStatement.getType());
            newDeclaration.setType(copiedType);
          } else newDeclaration.fragments().add(movedFragment);
        }
        if (newDeclaration != null) {
          statementRewrite.insertAfter(newDeclaration, newInitializer, group);
        }
        if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1) {
          rewrite.remove(originalStatement, group);
        }
      }
    }
Пример #2
0
 @Override
 public boolean visit(VariableDeclarationStatement node) {
   visitBackwards(node.fragments());
   return false;
 }
  public static void rewriteModifiers(
      final VariableDeclarationStatement declarationNode,
      final VariableDeclarationFragment[] toChange,
      final int includedModifiers,
      final int excludedModifiers,
      ASTRewrite rewrite,
      final TextEditGroup group) {
    final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
    AST ast = declarationNode.getAST();

    List<VariableDeclarationFragment> fragments = declarationNode.fragments();
    Iterator<VariableDeclarationFragment> iter = fragments.iterator();

    ListRewrite blockRewrite = null;
    ASTNode parentStatement = declarationNode.getParent();
    if (parentStatement instanceof SwitchStatement) {
      blockRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
    } else if (parentStatement instanceof Block) {
      blockRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
    } else {
      // should not happen. VariableDeclaration's can not be in a control statement body
      Assert.isTrue(false);
    }

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

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

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

      if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) {

        VariableDeclarationStatement newStatement =
            ast.newVariableDeclarationStatement(
                (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment));
        newStatement.setType((Type) rewrite.createCopyTarget(declarationNode.getType()));

        ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
        if (fragmentsToChange.contains(currentFragment)) {
          modifierRewrite.copyAllAnnotations(declarationNode, group);
          int newModifiers =
              (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
          modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
        } else {
          modifierRewrite.copyAllModifiers(declarationNode, group, modifiersModified);
        }
        blockRewrite.insertAfter(newStatement, lastStatement, group);

        fragmentsRewrite =
            rewrite.getListRewrite(newStatement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
        lastStatement = newStatement;
      } else if (fragmentsRewrite != null) {
        ASTNode fragment0 = rewrite.createMoveTarget(currentFragment);
        fragmentsRewrite.insertLast(fragment0, group);
      }
      lastFragment = currentFragment;
    }
  }