Search in sources :

Example 31 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class ConvertIterableLoopOperation method convert.

@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, final TextEditGroup group, final LinkedProposalModel positionGroups) throws CoreException {
    final AST ast = cuRewrite.getAST();
    final ASTRewrite astRewrite = cuRewrite.getASTRewrite();
    final ImportRewrite importRewrite = cuRewrite.getImportRewrite();
    final ImportRemover remover = cuRewrite.getImportRemover();
    fEnhancedForLoop = ast.newEnhancedForStatement();
    String[] names = getVariableNameProposals();
    String name;
    if (fElementVariable != null) {
        name = fElementVariable.getName();
    } else {
        name = names[0];
    }
    final LinkedProposalPositionGroup pg = positionGroups.getPositionGroup(name, true);
    if (fElementVariable != null)
        pg.addProposal(name, null, 10);
    for (int i = 0; i < names.length; i++) {
        pg.addProposal(names[i], null, 10);
    }
    final Statement body = getForStatement().getBody();
    if (body != null) {
        final ListRewrite list;
        if (body instanceof Block) {
            list = astRewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
            for (final Iterator<Expression> iterator = fOccurrences.iterator(); iterator.hasNext(); ) {
                final Statement parent = (Statement) ASTNodes.getParent(iterator.next(), Statement.class);
                if (parent != null && list.getRewrittenList().contains(parent)) {
                    list.remove(parent, null);
                    remover.registerRemovedNode(parent);
                }
            }
        } else {
            list = null;
        }
        final String text = name;
        body.accept(new ASTVisitor() {

            private boolean replace(final Expression expression) {
                final SimpleName node = ast.newSimpleName(text);
                astRewrite.replace(expression, node, group);
                remover.registerRemovedNode(expression);
                pg.addPosition(astRewrite.track(node), false);
                return false;
            }

            @Override
            public final boolean visit(final MethodInvocation node) {
                final IMethodBinding binding = node.resolveMethodBinding();
                if (binding != null && (binding.getName().equals("next") || binding.getName().equals("nextElement"))) {
                    //$NON-NLS-1$ //$NON-NLS-2$
                    final Expression expression = node.getExpression();
                    if (expression instanceof Name) {
                        final IBinding result = ((Name) expression).resolveBinding();
                        if (result != null && result.equals(fIteratorVariable))
                            return replace(node);
                    } else if (expression instanceof FieldAccess) {
                        final IBinding result = ((FieldAccess) expression).resolveFieldBinding();
                        if (result != null && result.equals(fIteratorVariable))
                            return replace(node);
                    }
                }
                return super.visit(node);
            }

            @Override
            public final boolean visit(final SimpleName node) {
                if (fElementVariable != null) {
                    final IBinding binding = node.resolveBinding();
                    if (binding != null && binding.equals(fElementVariable)) {
                        final Statement parent = (Statement) ASTNodes.getParent(node, Statement.class);
                        if (parent != null && (list == null || list.getRewrittenList().contains(parent)))
                            pg.addPosition(astRewrite.track(node), false);
                    }
                }
                return false;
            }
        });
        fEnhancedForLoop.setBody(getBody(cuRewrite, group, positionGroups));
    }
    final SingleVariableDeclaration declaration = ast.newSingleVariableDeclaration();
    final SimpleName simple = ast.newSimpleName(name);
    pg.addPosition(astRewrite.track(simple), true);
    declaration.setName(simple);
    final ITypeBinding elementType = getElementType(fIteratorVariable.getType());
    declaration.setType(importType(elementType, getForStatement(), importRewrite, getRoot()));
    if (fMakeFinal) {
        ModifierRewrite.create(astRewrite, declaration).setModifiers(Modifier.FINAL, 0, group);
    }
    remover.registerAddedImport(elementType.getQualifiedName());
    fEnhancedForLoop.setParameter(declaration);
    fEnhancedForLoop.setExpression(getExpression(astRewrite));
    for (Iterator<Expression> iterator = getForStatement().initializers().iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        if (node instanceof VariableDeclarationExpression) {
            VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) node;
            remover.registerRemovedNode(variableDeclarationExpression.getType());
        } else {
            remover.registerRemovedNode(node);
        }
    }
    for (Iterator<Expression> iterator = getForStatement().updaters().iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        remover.registerRemovedNode(node);
    }
    return fEnhancedForLoop;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ImportRemover(org.eclipse.jdt.internal.corext.refactoring.structure.ImportRemover) AST(org.eclipse.jdt.core.dom.AST) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 32 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class StatementRewrite method handleOneMany.

@Override
protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) {
    AST ast = fToReplace[0].getAST();
    // need to insert a block to not change structure
    if (ASTNodes.isControlStatementBody(fDescriptor)) {
        Block block = ast.newBlock();
        ListRewrite statements = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
        for (int i = 0; i < replacements.length; i++) {
            statements.insertLast(replacements[i], description);
        }
        fRewrite.replace(fToReplace[0], block, description);
    } else {
        ListRewrite container = fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor) fDescriptor);
        container.replace(fToReplace[0], replacements[0], description);
        for (int i = 1; i < replacements.length; i++) {
            container.insertAfter(replacements[i], replacements[i - 1], description);
        }
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 33 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class VariableDeclarationRewrite method rewriteModifiers.

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;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 34 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class VariableDeclarationRewrite method rewriteModifiers.

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();
        @SuppressWarnings("unchecked") 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) {
                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<IExtendedModifier> extendedList = listRewrite.getRewrittenList();
                    for (int i = 0; i < extendedList.size(); i++) {
                        ASTNode curr = (ASTNode) 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.class);
                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;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) HashMap(java.util.HashMap) Map(java.util.Map) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 35 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class ConvertForLoopOperation method convertBody.

private void convertBody(Statement body, final IBinding indexBinding, final IBinding arrayBinding, final String parameterName, final ASTRewrite rewrite, final TextEditGroup editGroup, final LinkedProposalPositionGroup pg) {
    final AST ast = body.getAST();
    body.accept(new GenericVisitor() {

        @Override
        public boolean visit(ArrayAccess node) {
            IBinding binding = getBinding(node.getArray());
            if (arrayBinding.equals(binding)) {
                IBinding index = getBinding(node.getIndex());
                if (indexBinding.equals(index)) {
                    replaceAccess(node);
                }
            }
            return super.visit(node);
        }

        private void replaceAccess(ASTNode node) {
            if (fElementDeclaration != null && node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getParent();
                IBinding targetBinding = fragment.getName().resolveBinding();
                if (targetBinding != null) {
                    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                    if (statement.fragments().size() == 1) {
                        rewrite.remove(statement, editGroup);
                    } else {
                        ListRewrite listRewrite = rewrite.getListRewrite(statement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
                        listRewrite.remove(fragment, editGroup);
                    }
                } else {
                    SimpleName name = ast.newSimpleName(parameterName);
                    rewrite.replace(node, name, editGroup);
                    pg.addPosition(rewrite.track(name), true);
                }
            } else {
                SimpleName name = ast.newSimpleName(parameterName);
                rewrite.replace(node, name, editGroup);
                pg.addPosition(rewrite.track(name), true);
            }
        }
    });
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Aggregations

ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)79 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)45 ASTNode (org.eclipse.jdt.core.dom.ASTNode)37 AST (org.eclipse.jdt.core.dom.AST)30 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)19 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 Block (org.eclipse.jdt.core.dom.Block)18 Type (org.eclipse.jdt.core.dom.Type)18 Image (org.eclipse.swt.graphics.Image)17 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)15 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)15 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)15 ArrayList (java.util.ArrayList)13 IType (org.eclipse.jdt.core.IType)13 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)13 Statement (org.eclipse.jdt.core.dom.Statement)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 Expression (org.eclipse.jdt.core.dom.Expression)12 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)12