Search in sources :

Example 96 with ASTNode

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

the class JavadocTagsSubProcessor method insertTag.

public static void insertTag(ListRewrite rewriter, TagElement newElement, Set<String> sameKindLeadingNames, TextEditGroup groupDescription) {
    List<? extends ASTNode> tags = rewriter.getRewrittenList();
    String insertedTagName = newElement.getTagName();
    ASTNode after = null;
    int tagRanking = getTagRanking(insertedTagName);
    for (int i = tags.size() - 1; i >= 0; i--) {
        TagElement curr = (TagElement) tags.get(i);
        String tagName = curr.getTagName();
        if (tagName == null || tagRanking > getTagRanking(tagName)) {
            after = curr;
            break;
        }
        if (sameKindLeadingNames != null && isSameTag(insertedTagName, tagName)) {
            String arg = getArgument(curr);
            if (arg != null && sameKindLeadingNames.contains(arg)) {
                after = curr;
                break;
            }
        }
    }
    if (after != null) {
        rewriter.insertAfter(newElement, after, groupDescription);
    } else {
        rewriter.insertFirst(newElement, groupDescription);
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 97 with ASTNode

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

the class RenameLinkedModeRefactoringSession method getModel.

public LinkedModeModel getModel() {
    CompilationUnit root = SharedASTProvider.getAST(compilationUnit, SharedASTProvider.WAIT_YES, null);
    LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
    ASTNode selectedNode = NodeFinder.perform(root, offset, 0);
    if (!(selectedNode instanceof SimpleName)) {
        return null;
    }
    SimpleName nameNode = (SimpleName) selectedNode;
    fOriginalName = nameNode.getIdentifier();
    final int pos = nameNode.getStartPosition();
    ASTNode[] sameNodes = LinkedNodeFinder.findByNode(root, nameNode);
    //TODO: copied from LinkedNamesAssistProposal#apply(..):
    // sort for iteration order, starting with the node @ offset
    Arrays.sort(sameNodes, new Comparator<ASTNode>() {

        public int compare(ASTNode o1, ASTNode o2) {
            return rank(o1) - rank(o2);
        }

        /**
             * Returns the absolute rank of an <code>ASTNode</code>. Nodes
             * preceding <code>pos</code> are ranked last.
             *
             * @param node the node to compute the rank for
             * @return the rank of the node with respect to the invocation offset
             */
        private int rank(ASTNode node) {
            int relativeRank = node.getStartPosition() + node.getLength() - pos;
            if (relativeRank < 0)
                return Integer.MAX_VALUE + relativeRank;
            else
                return relativeRank;
        }
    });
    for (int i = 0; i < sameNodes.length; i++) {
        ASTNode elem = sameNodes[i];
        RegionImpl position = new RegionImpl();
        position.setOffset(elem.getStartPosition());
        position.setLength(elem.getLength());
        group.addPositions(position);
    }
    LinkedModeModelImpl model = new LinkedModeModelImpl();
    model.addGroups(group);
    return model;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl)

Example 98 with ASTNode

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

the class AssociativeInfixExpressionFragment method replace.

public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup) {
    ASTNode groupNode = getGroupRoot();
    List<Expression> allOperands = findGroupMembersInOrderFor(getGroupRoot());
    if (allOperands.size() == fOperands.size()) {
        if (replacement instanceof Name && groupNode.getParent() instanceof ParenthesizedExpression) {
            // replace including the parenthesized expression around it
            rewrite.replace(groupNode.getParent(), replacement, textEditGroup);
        } else {
            rewrite.replace(groupNode, replacement, textEditGroup);
        }
        return;
    }
    rewrite.replace(fOperands.get(0), replacement, textEditGroup);
    int first = allOperands.indexOf(fOperands.get(0));
    int after = first + fOperands.size();
    for (int i = first + 1; i < after; i++) {
        rewrite.remove(allOperands.get(i), textEditGroup);
    }
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Name(org.eclipse.jdt.core.dom.Name)

Example 99 with ASTNode

use of org.eclipse.jdt.core.dom.ASTNode 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 100 with ASTNode

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

the class Java50Fix method getRawReference.

private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
    SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
    for (int j = 0; j < names.length; j++) {
        if (names[j].getParent() instanceof VariableDeclarationFragment) {
            VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
            if (fragment.getParent() instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
                if (isRawTypeReference(result))
                    return (SimpleType) result;
            } else if (fragment.getParent() instanceof FieldDeclaration) {
                FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
                ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
                if (isRawTypeReference(result))
                    return (SimpleType) result;
            }
        } else if (names[j].getParent() instanceof SingleVariableDeclaration) {
            SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
            ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
            if (isRawTypeReference(result))
                return (SimpleType) result;
        } else if (names[j].getParent() instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
            ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
            if (isRawTypeReference(result))
                return (SimpleType) result;
        }
    }
    return null;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)384 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)98 SimpleName (org.eclipse.jdt.core.dom.SimpleName)96 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)82 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)80 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)74 Expression (org.eclipse.jdt.core.dom.Expression)73 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)68 AST (org.eclipse.jdt.core.dom.AST)64 ArrayList (java.util.ArrayList)59 Type (org.eclipse.jdt.core.dom.Type)56 Block (org.eclipse.jdt.core.dom.Block)53 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)47 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)42 IBinding (org.eclipse.jdt.core.dom.IBinding)41 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)40 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)39 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)37 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)36 CastExpression (org.eclipse.jdt.core.dom.CastExpression)35