Example usage for org.eclipse.jdt.core.dom ASTNode getLength

List of usage examples for org.eclipse.jdt.core.dom ASTNode getLength

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ASTNode getLength.

Prototype

public final int getLength() 

Source Link

Document

Returns the length in characters of the original source file indicating where the source fragment corresponding to this node ends.

Usage

From source file:ast.AstNodeLocationMapper.java

License:Apache License

/**
 * wm?[hvf?Awu??o?B//from   w w w .  ja  v  a  2  s  . co  m
 * li[?B
 * <ul>
 * <li>
 *   {@link AstNodeLocationMapper.FindResult#enclosing} -
 *     wLbg?m?[h?A?m?[h?B
 *     lK{@code null}?B
 * </li>
 * <li>
 *   {@link AstNodeLocationMapper.FindResult#previous} -
 *     wLbg?AwLbgO?m?[h?A
 *     {@link AstNodeLocationMapper.FindResult#enclosing enclosing}
 *     ?qvf?B?Avf?B
 * ?@?@?@?@?AL{@code // CARET}uLbg????A
 *     l{@code System.out.println("prev");}?B
 * <pre><code>
 * public void hoge() {
 *   System.out.println("prev");
 *   // CARET
 *   System.out.println("next");
 * }
 * </code></pre>
 *     ?Lm?[h????Al{@code null}?B
 * </li>
 * </ul>
 * @param root ???[gm?[h
 * @param position u
 * @return
 *      ?o\{@link AstNodeLocationMapper.FindResult}IuWFNg?B
 *      {@code root}{@code position}??{@code null}
 * @throws NullPointerException ?{@code null}w??
 * @throws IllegalArgumentException
 *          {@code position}{@code 0}lw??
 */
public static FindResult findNode(ASTNode root, int position) {
    if (root == null) {
        throw new NullPointerException("root"); //$NON-NLS-1$
    }
    if (position < 0) {
        throw new IllegalArgumentException(MessageFormat.format("position = {0} (< 0)", //$NON-NLS-1$
                position));
    }
    ASTNode current = root;
    if (!isEnclosing(root.getStartPosition(), root.getLength(), position)) {
        return null;
    }

    while (true) {
        ASTNode child = findNearestPreviousChild(current, position);

        // qvfuw???A?[?
        if (child != null && isEnclosing(child.getStartPosition(), child.getLength(), position)) {
            current = child;
        }

        // wu?qvf???A?B
        else {
            return new FindResult(current, child);
        }
    }
}

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

private void handleContentElements(List<? extends ASTNode> nodes, boolean skipLeadingWhitespace) {
    ASTNode previousNode = null;
    for (Iterator<? extends ASTNode> iter = nodes.iterator(); iter.hasNext();) {
        ASTNode child = iter.next();//ww w  .  j ava  2 s.  co  m
        if (previousNode != null) {
            int previousEnd = previousNode.getStartPosition() + previousNode.getLength();
            int childStart = child.getStartPosition();
            if (previousEnd > childStart) {
                // should never happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304826
                Exception exception = new Exception("Illegal ASTNode positions: previousEnd=" + previousEnd //$NON-NLS-1$
                        + ", childStart=" + childStart //$NON-NLS-1$
                        + ", element=" + fElement.getHandleIdentifier() //$NON-NLS-1$
                        + ", Javadoc:\n" + fSource); //$NON-NLS-1$
                //TODO
                exception.printStackTrace();
            } else if (previousEnd != childStart) {
                // Need to preserve whitespace before a node that's not
                // directly following the previous node (e.g. on a new line)
                // due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=206518 :
                String textWithStars = fSource.substring(previousEnd, childStart);
                String text = removeDocLineIntros(textWithStars);
                fBuf.append(text);
            }
        }
        previousNode = child;
        if (child instanceof TextElement) {
            String text = ((TextElement) child).getText();
            if (skipLeadingWhitespace) {
                text = text.replaceFirst("^\\s+", ""); //$NON-NLS-1$ //$NON-NLS-2$
            }
            // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=233481 :
            text = text.replaceAll("(\r\n?|\n)([ \t]*\\*)", "$1"); //$NON-NLS-1$ //$NON-NLS-2$
            text = text.replace("#__", "}");
            text = text.replace("__#", "{");

            handleText(text);

        } else if (child instanceof TagElement) {
            handleInlineTagElement((TagElement) child);
        } else {
            // This is unexpected. Fail gracefully by just copying the source.
            int start = child.getStartPosition();
            String text = fSource.substring(start, start + child.getLength());
            fBuf.append(removeDocLineIntros(text));
        }
    }
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

protected void buildPosition(final ASTNode node) {
    pos = PositionInfo.newBuilder();/*from  w  ww  .j a va2s . c o m*/
    int start = node.getStartPosition();
    int length = node.getLength();
    pos.setStartPos(start);
    pos.setLength(length);
    pos.setStartLine(root.getLineNumber(start));
    pos.setStartCol(root.getColumnNumber(start));
    pos.setEndLine(root.getLineNumber(start + length));
    pos.setEndCol(root.getColumnNumber(start + length));
}

From source file:boa.datagen.util.JavaVisitor.java

License:Apache License

private void buildPosition(final ASTNode node) {
    pos = PositionInfo.newBuilder();//from   ww w  .  j  a  va 2s . com
    int start = node.getStartPosition();
    int length = node.getLength();
    pos.setStartPos(start);
    pos.setLength(length);
    pos.setStartLine(root.getLineNumber(start));
    pos.setStartCol(root.getColumnNumber(start));
    pos.setEndLine(root.getLineNumber(start + length));
    pos.setEndCol(root.getColumnNumber(start + length));
}

From source file:br.ufal.cideei.soot.instrument.asttounit.ASTNodesAtRangeFinder.java

License:Open Source License

@Override
public void preVisit(ASTNode node) {
    int nodeStartPosition = node.getStartPosition();
    int nodeEndPosition = nodeStartPosition + node.getLength();

    if (compilationUnit.getLineNumber(nodeStartPosition) == startLine
            && compilationUnit.getColumnNumber(nodeStartPosition) + 1 >= startPos
            && compilationUnit.getLineNumber(nodeEndPosition) == endLine
            && compilationUnit.getColumnNumber(nodeEndPosition) - 1 <= endPos) {

        this.foundNodes.add(node);
    }//w ww .j  a  va 2 s .co m
}

From source file:br.ufal.cideei.visitors.GetFeatureVisitor.java

License:Open Source License

/**
 * Populates the {@link #nodes} Set with the ASTNodes. 
 * Use {@link #getNodes()} to retrive the nodes after accepting this visitor to an ASTNode
 * @see org.eclipse.jdt.core.dom.ASTVisitor#preVisit(org.eclipse.jdt.core.dom.ASTNode)
 *///from  w w w  .j a  v  a2 s . c om
public void preVisit(ASTNode node) {
    super.preVisit(node);
    if (node.getStartPosition() >= textSelection.getOffset()
            && (node.getStartPosition() + node.getLength()) <= textSelection.getOffset()
                    + textSelection.getLength()) {
        IFeatureExtracter extracter = CIDEFeatureExtracterFactory.getInstance().getExtracter();
        features = extracter.getFeaturesNames(node, file);
        return;
    }
}

From source file:br.ufal.cideei.visitors.SelectionNodesVisitor.java

License:Open Source License

/**
 * Populates the {@link #nodes} Set with the ASTNodes. 
 * Use {@link #getNodes()} to retrive the nodes after accepting this visitor to an ASTNode
 * @see org.eclipse.jdt.core.dom.ASTVisitor#preVisit(org.eclipse.jdt.core.dom.ASTNode)
 *//*from   ww w .  java 2 s  .c o m*/
public void preVisit(ASTNode node) {
    super.preVisit(node);
    //System.out.println(node);
    if (node.getStartPosition() >= textSelection.getOffset()
            && (node.getStartPosition() + node.getLength()) <= textSelection.getOffset()
                    + textSelection.getLength()) {
        nodes.add(node);
    }
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaMessageGrouper.java

License:Open Source License

public IMessageGrouping[] calculateGroups2(UMLSequenceViewer viewer, Object activationElement,
        Object[] children) {/*from  w w  w . j  a va 2s.co m*/
    HashMap<ASTNode, MappedMessageGrouping> groups = new HashMap<ASTNode, MappedMessageGrouping>();
    if (!(activationElement instanceof IAdaptable)) {
        return new IMessageGrouping[0];
    }
    ASTNode activationNode = (ASTNode) ((IAdaptable) activationElement).getAdapter(ASTNode.class);
    if (!(activationNode instanceof MethodDeclaration)) {
        return new IMessageGrouping[0];
    }
    for (int i = 0; i < children.length; i++) {
        if (children[i] instanceof IAdaptable) {
            ASTNode messageNode = (ASTNode) ((IAdaptable) children[i]).getAdapter(ASTNode.class);
            if (messageNode != null) {
                ASTNode currentParent = messageNode.getParent();
                while (currentParent != null && currentParent != activationNode) {
                    ASTNode block = null;
                    String text = null;
                    Color c = null;
                    Color bc = null;
                    String expressionString = "";
                    switch (currentParent.getNodeType()) {
                    case ASTNode.IF_STATEMENT:
                        block = checkIfSide((IfStatement) currentParent, messageNode);
                        if (block != null && block == ((IfStatement) currentParent).getElseStatement()) {
                            text = "else";
                        } else if (block == ((IfStatement) currentParent).getThenStatement()) {
                            text = "if (" + ((IfStatement) currentParent).getExpression().toString() + ")";
                        }
                        c = ISketchColorConstants.CONDITION_FG;
                        bc = ISketchColorConstants.CONDITION_FG;
                        break;
                    case ASTNode.WHILE_STATEMENT:
                        if (((WhileStatement) currentParent).getExpression() != null) {
                            expressionString = ((WhileStatement) currentParent).getExpression().toString();
                        }
                        text = "while (" + expressionString + ")";
                        block = currentParent;
                        c = ISketchColorConstants.LOOP_FG;
                        bc = ISketchColorConstants.LOOP_BG;
                        break;
                    case ASTNode.FOR_STATEMENT:
                        if (((ForStatement) currentParent).getExpression() != null) {
                            expressionString = ((ForStatement) currentParent).getExpression().toString();
                        } else {
                            expressionString = ";;";
                        }
                        text = "for (" + expressionString + ")";
                        block = currentParent;
                        c = ISketchColorConstants.LOOP_FG;
                        bc = ISketchColorConstants.LOOP_BG;
                        break;
                    case ASTNode.TRY_STATEMENT:
                        text = "try";
                        block = currentParent;
                        c = ISketchColorConstants.ERROR_FG;
                        bc = ISketchColorConstants.ERROR_BG;
                        break;
                    case ASTNode.CATCH_CLAUSE:
                        text = "catch (" + ((CatchClause) currentParent).getException().toString() + ")";
                        block = currentParent;
                        c = ISketchColorConstants.ERROR_FG;
                        bc = ISketchColorConstants.ERROR_BG;
                        break;
                    case ASTNode.DO_STATEMENT:
                        text = "do while (" + ((DoStatement) currentParent).getExpression().toString() + ")";
                        block = currentParent;
                        c = ISketchColorConstants.LOOP_FG;
                        bc = ISketchColorConstants.LOOP_BG;
                        break;
                    }
                    if (text != null) {
                        MappedMessageGrouping grouping = groups.get(block);
                        if (grouping == null) {
                            grouping = new MappedMessageGrouping(activationElement, i, 1, text, block);
                            grouping.setBackground(bc);
                            grouping.setForeground(c);
                            groups.put(block, grouping);
                        } else {
                            int length = (i - grouping.getOffset()) + 1;
                            grouping.setLength(length);
                        }
                    }
                    currentParent = currentParent.getParent();
                }
            }
        }
    }
    ArrayList<MappedMessageGrouping> groupList = new ArrayList<MappedMessageGrouping>(groups.values());
    Collections.sort(groupList, new Comparator<MappedMessageGrouping>() {
        public int compare(MappedMessageGrouping o1, MappedMessageGrouping o2) {
            ASTNode n1 = (ASTNode) o1.getKey();
            ASTNode n2 = (ASTNode) o2.getKey();
            int diff = n1.getStartPosition() - n2.getStartPosition();
            if (diff == 0) {
                diff = (n1.getStartPosition() + n1.getLength()) - (n2.getStartPosition() + n2.getLength());
            }
            return diff;
        }
    });
    return groupList.toArray(new IMessageGrouping[groupList.size()]);
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaMessageGrouper.java

License:Open Source License

public IMessageGrouping[] calculateGroups(UMLSequenceViewer viewer, Object activationElement,
        Object[] children) {//from   ww w .j a va2 s  .  c  o  m
    HashMap<ASTNode, MappedMessageGrouping> groups = new HashMap<ASTNode, MappedMessageGrouping>();
    if (!(activationElement instanceof IAdaptable)) {
        return new IMessageGrouping[0];
    }
    ASTNode activationNode = (ASTNode) ((IAdaptable) activationElement).getAdapter(ASTNode.class);
    if (!(activationNode instanceof MethodDeclaration)) {
        return new IMessageGrouping[0];
    }
    for (int i = 0; i < children.length; i++) {
        if (children[i] instanceof IAdaptable) {
            ASTNode messageNode = (ASTNode) ((IAdaptable) children[i]).getAdapter(ASTNode.class);
            if (messageNode != null) {
                ASTNode blockParent = findBlockParent(messageNode);

                List<MappedMessageGrouping> blocks = new LinkedList<MappedMessageGrouping>();
                while (blockParent != null && blockParent.getNodeType() != ASTNode.METHOD_DECLARATION) {

                    if (blockParent != null && blockParent.getNodeType() == ASTNode.IF_STATEMENT) {
                        IfStatement ifStatement = (IfStatement) blockParent;
                        ASTNode block = checkIfSide(ifStatement, messageNode);
                        if (block != null && block.equals(ifStatement.getElseStatement())) {
                            //add a block for the else statement as well
                            MappedMessageGrouping blockNode = groups.get(block);
                            if (blockNode == null) {
                                blockNode = new MappedMessageGrouping(activationElement, i, 0, "", block);
                                groups.put(block, blockNode);
                            }
                            blocks.add(blockNode);
                        }
                    }
                    MappedMessageGrouping blockNode = groups.get(blockParent);
                    if (blockNode == null) {
                        blockNode = new MappedMessageGrouping(activationElement, i, 0, "", blockParent);
                        groups.put(blockParent, blockNode);
                    }
                    blocks.add(blockNode);
                    blockParent = findBlockParent(blockParent);
                }
                for (MappedMessageGrouping blockNode : blocks) {
                    blockNode.setLength(blockNode.getLength() + 1);
                }
            }
        }
    }
    ArrayList<MappedMessageGrouping> groupList = new ArrayList<MappedMessageGrouping>(groups.values());
    Collections.sort(groupList, new Comparator<MappedMessageGrouping>() {
        public int compare(MappedMessageGrouping o1, MappedMessageGrouping o2) {
            ASTNode n1 = (ASTNode) o1.getKey();
            ASTNode n2 = (ASTNode) o2.getKey();
            int diff = n1.getStartPosition() - n2.getStartPosition();
            if (diff == 0) {
                diff = (n1.getStartPosition() + n1.getLength()) - (n2.getStartPosition() + n2.getLength());
            }
            if (diff == 0) {
                IfStatement ifStatement = null;
                //make sure that else statements are contained in if statements
                if (n1 instanceof IfStatement) {
                    ifStatement = (IfStatement) n1;
                } else if (n2 instanceof IfStatement) {
                    ifStatement = (IfStatement) n2;
                }
                if (ifStatement != null) {
                    if (n2.equals(ifStatement.getElseStatement())) {
                        return -1;
                    } else if (n1.equals(ifStatement.getElseStatement())) {
                        return 1;
                    }
                }
            }
            return diff;
        }
    });
    for (MappedMessageGrouping blockNode : groupList) {
        updateGrouping(blockNode);
    }
    return groupList.toArray(new IMessageGrouping[groupList.size()]);
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.Selection.java

License:Open Source License

/**
 * Returns the selection mode of the given AST node regarding this selection. Possible
 * values are <code>INTERSECTS</code>, <code>BEFORE</code>, <code>SELECTED</code>, and
 * <code>AFTER</code>./*from   w  ww.j a  va  2s  .co m*/
 * 
 * @param node the node to return the visit mode for
 * 
 * @return the selection mode of the given AST node regarding this selection
 * @see #INTERSECTS
 * @see #BEFORE
 * @see #SELECTED
 * @see #AFTER
 */
public int getVisitSelectionMode(ASTNode node) {
    int nodeStart = node.getStartPosition();
    int nodeEnd = nodeStart + node.getLength();
    if (nodeEnd <= fStart)
        return BEFORE;
    else if (covers(node))
        return SELECTED;
    else if (fExclusiveEnd <= nodeStart)
        return AFTER;
    return INTERSECTS;
}