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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Statement 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:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(DoStatement node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    Statement body = node.getBody();

    if (body != null) {

        int beginLineBody = cu.getLineNumber(body.getStartPosition());
        int endLineBody = cu.getLineNumber(body.getStartPosition() + body.getLength());
        int beginColumnBody = cu.getColumnNumber(body.getStartPosition());
        int endColumnBody = cu.getColumnNumber(body.getStartPosition() + body.getLength());

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, beginLineBody, endLineBody, beginColumnBody, endColumnBody, null));
    } else {//from  ww w.  j  a v  a  2 s.c  om
        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn));
    }
    return true;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(EnhancedForStatement node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    Statement body = node.getBody();
    if (body != null) {

        int beginLineBody = cu.getLineNumber(body.getStartPosition());
        int endLineBody = cu.getLineNumber(body.getStartPosition() + body.getLength());
        int beginColumnBody = cu.getColumnNumber(body.getStartPosition());
        int endColumnBody = cu.getColumnNumber(body.getStartPosition() + body.getLength());

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, beginLineBody, endLineBody, beginColumnBody, endColumnBody, null));
    } else {//w w w  . j  a va  2s  . co m
        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn));
    }

    return true;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(ForStatement node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    Statement body = node.getBody();
    if (body != null) {

        int beginLineBody = cu.getLineNumber(body.getStartPosition());
        int endLineBody = cu.getLineNumber(body.getStartPosition() + body.getLength());
        int beginColumnBody = cu.getColumnNumber(body.getStartPosition());
        int endColumnBody = cu.getColumnNumber(body.getStartPosition() + body.getLength());

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, beginLineBody, endLineBody, beginColumnBody, endColumnBody, null));
    } else {//from   w  ww .  j a  va2  s.co  m
        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn));
    }
    return true;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(WhileStatement node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    Statement body = node.getBody();
    if (body != null) {

        int beginLineBody = cu.getLineNumber(body.getStartPosition());
        int endLineBody = cu.getLineNumber(body.getStartPosition() + body.getLength());
        int beginColumnBody = cu.getColumnNumber(body.getStartPosition());
        int endColumnBody = cu.getColumnNumber(body.getStartPosition() + body.getLength());

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, beginLineBody, endLineBody, beginColumnBody, endColumnBody, null));
    } else {/*from  w w  w .  j  a va  2  s .  c  om*/
        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn));
    }

    return true;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public boolean visit(Statement node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    languageConstructs.add(//w ww .  ja  v  a2  s  .c  o m
            new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine, beginColumn, endColumn));

    return true;
}

From source file:com.architexa.diagrams.relo.jdt.ParseUtilities.java

License:Open Source License

/**
 * @param method//w  w w  .  j  a va  2  s .  co  m
 * @return SourceRange
 * @throws JavaModelException
 */
public static ISourceRange getBodyRange(IMethod method) throws JavaModelException {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(method.getCompilationUnit());
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    TypeDeclaration td = findTypeDeclaration(cu, method);
    MethodDeclaration md = findMethodDeclaration(td, method);
    Block body = md.getBody();
    final int startPos;
    final int length;
    List<?> bodyStatements = body.statements();
    int stmnts = bodyStatements.size();
    if (stmnts > 0) {
        startPos = ((Statement) bodyStatements.get(0)).getStartPosition();
        Statement lastStmnt = (Statement) bodyStatements.get(stmnts - 1);
        length = lastStmnt.getStartPosition() + lastStmnt.getLength() - startPos;
    } else {
        startPos = body.getStartPosition();
        length = body.getLength();
    }
    return new ISourceRange() {
        public int getLength() {
            return length;
        }

        public int getOffset() {
            return startPos;
        }
    };
}

From source file:com.chookapp.org.bracketeer.jdt.ClosingBracketHintVisitor.java

License:Open Source License

private boolean visitBreak(Statement node) {
    try {/*  w  w w. j av a  2 s.c o m*/
        if (_scopeStack.isEmpty())
            throw new ScopeTraceException("break without scope: " + node); //$NON-NLS-1$

        ScopeInfo scope = _scopeStack.peek();

        if (node instanceof BreakStatement) {
            // ignoring break with labels on them
            if (((BreakStatement) node).getLabel() != null)
                return shouldContinue();
        }

        String hintType;
        if (scope._statement instanceof ForStatement)
            hintType = "break-for"; //$NON-NLS-1$
        else if (scope._statement instanceof EnhancedForStatement)
            hintType = "break-foreach"; //$NON-NLS-1$
        else if (scope._statement instanceof WhileStatement)
            hintType = "break-while"; //$NON-NLS-1$
        else if (scope._statement instanceof DoStatement)
            hintType = "break-do"; //$NON-NLS-1$
        else if (scope._statement instanceof SwitchCase)
            hintType = "break-case"; //$NON-NLS-1$
        else
            throw new ScopeTraceException(
                    "Unexpect scope (" + scope._statement + ") on break/continue:" + node); //$NON-NLS-1$ //$NON-NLS-2$

        int endLoc = node.getStartPosition() + node.getLength() - 1;
        _container.add(new Hint(hintType, scope._offset, endLoc, scope._str));
    } catch (ScopeTraceException e) {
        if (Activator.DEBUG)
            Activator.log(e);
    } catch (BadLocationException e) {
        _cancelProcessing.set(true);
    }
    return shouldContinue();
}

From source file:com.chookapp.org.bracketeer.jdt.ClosingBracketHintVisitor.java

License:Open Source License

@Override
public boolean visit(IfStatement node) {
    Statement thenStmt = node.getThenStatement();
    Statement elseStmt = node.getElseStatement();
    String hint = GetNodeText(node.getExpression());

    boolean showIfHint = (elseStmt == null);
    int endLoc = -1;

    try {/* w w  w.j  a va2 s  .  c  om*/

        if (!showIfHint) {

            if (_doc.getLineOfOffset(elseStmt.getStartPosition()) != _doc
                    .getLineOfOffset(thenStmt.getStartPosition() + thenStmt.getLength())) {
                showIfHint = true;
            }

            // if the else looks like this "} else {", then show the hint on the "{"
            if (!showIfHint && !(elseStmt instanceof IfStatement)) {
                endLoc = elseStmt.getStartPosition();
                showIfHint = true;
            }
        }

        if (showIfHint && !(thenStmt instanceof Block))
            showIfHint = false;

        if (showIfHint) {
            if (endLoc == -1)
                endLoc = thenStmt.getStartPosition() + thenStmt.getLength() - 1;
            int startLoc = node.getStartPosition();
            _container.add(new Hint("if", startLoc, endLoc, "if( " + hint + " )")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        }

        if (elseStmt != null && !(elseStmt instanceof IfStatement) && (elseStmt instanceof Block)) {
            endLoc = elseStmt.getStartPosition() + elseStmt.getLength() - 1;
            int startLoc = elseStmt.getStartPosition();
            _container.add(new Hint("if", startLoc, endLoc, "else_of_if( " + hint + " )")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        }

    } catch (BadLocationException e) {
        _cancelProcessing.set(true);
    }

    return shouldContinue();
}

From source file:net.sf.j2s.core.astvisitors.ASTJ2SDocVisitor.java

License:Open Source License

private int getPreviousStartPosition(Block node) {
    int previousStart = 0;
    ASTNode blockParent = node.getParent();
    if (blockParent != null) {
        if (blockParent instanceof Statement) {
            Statement sttmt = (Statement) blockParent;
            previousStart = sttmt.getStartPosition();
            if (sttmt instanceof Block) {
                Block parentBlock = (Block) sttmt;
                for (Iterator iter = parentBlock.statements().iterator(); iter.hasNext();) {
                    Statement element = (Statement) iter.next();
                    if (element == node) {
                        break;
                    }/*from w  ww .  jav a  2s. co  m*/
                    previousStart = element.getStartPosition() + element.getLength();
                }
            } else if (sttmt instanceof IfStatement) {
                IfStatement ifSttmt = (IfStatement) sttmt;
                if (ifSttmt.getElseStatement() == node) {
                    Statement thenSttmt = ifSttmt.getThenStatement();
                    previousStart = thenSttmt.getStartPosition() + thenSttmt.getLength();
                }
            }
        } else if (blockParent instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) blockParent;
            previousStart = method.getStartPosition();
        } else if (blockParent instanceof Initializer) {
            Initializer initializer = (Initializer) blockParent;
            previousStart = initializer.getStartPosition();
        } else if (blockParent instanceof CatchClause) {
            CatchClause catchClause = (CatchClause) blockParent;
            previousStart = catchClause.getStartPosition();
        }
    }
    return previousStart;
}

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

/**
 * @return the index of character directly after end of given {@link Statement}. This can be
 *         beginning of the next statement or EOL. This method skips any whitespace characters and
 *         end-of-line comments.//from  w  w  w.  jav  a  2 s  . c o m
 */
public int getStatementEndIndex(Statement statement) {
    int index = statement.getStartPosition() + statement.getLength();
    char c;
    // skip spaces
    while (true) {
        c = getChar(index++);
        if (Character.isWhitespace(c)) {
            if (c == '\r' || c == '\n') {
                break;
            }
        } else {
            break;
        }
    }
    // skip end-of-line comment
    if (c == '/' && getChar(index) == '/') {
        while (true) {
            c = getChar(index++);
            if (c == '\r' || c == '\n') {
                break;
            }
        }
    }
    // return result
    return index - 1;
}