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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Comment 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

public boolean visit(Comment 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(//from   ww  w  .  j a  v a 2 s  . c o m
            new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine, beginColumn, endColumn));

    return true;
}

From source file:com.alex.example.fixlicense.actions.SampleAction.java

License:Open Source License

private void processHeadLicense(IDocument doc) throws Exception {
    CompilationUnit cu = getAST(doc);/*from   w  w w.ja  v a 2 s.co  m*/
    Comment comment = null;
    if (cu.getCommentList().size() == 0) {
        doc.replace(0, 0, license);
    } else {
        comment = (Comment) cu.getCommentList().get(0);
        String firstComment = doc.get().substring(comment.getStartPosition(),
                comment.getStartPosition() + comment.getLength());
        if (validateHeadLicense(firstComment)) {
            doc.replace(comment.getStartPosition(), comment.getLength(), license);
        } else {
            doc.replace(0, 0, license);
        }
    }
}

From source file:com.github.lbroudoux.dsl.eip.parser.camel.CamelJavaFileParser.java

License:Apache License

/** Extract comment content from source. */
private String getCommentContent(Comment comment) {
    int start = comment.getStartPosition();
    int end = start + comment.getLength();
    String content = routeSource.substring(start, end);
    if (content.startsWith("//")) {
        content = content.substring(2).trim();
    }/*from  w  w  w . ja  va 2 s. c om*/
    return content;
}

From source file:com.google.currysrc.processors.BaseModifyCommentScanner.java

License:Apache License

@Override
public final void process(Context context, CompilationUnit cu) {
    Document document = context.document();
    Reporter reporter = context.reporter();
    List<Comment> comments = cu.getCommentList();
    try {//from ww w  .ja va  2 s .  com
        for (Comment comment : Lists.reverse(comments)) {
            String commentText = document.get(comment.getStartPosition(), comment.getLength());
            String newCommentText = processComment(reporter, comment, commentText);
            if (newCommentText != null) {
                document.replace(comment.getStartPosition(), comment.getLength(), newCommentText);
            }
        }
    } catch (BadLocationException e) {
        throw new AssertionError(e);
    }
}

From source file:ctrus.pa.bow.java.JavaFileTokenizer.java

License:Apache License

@SuppressWarnings("unchecked")
public void tokenize(List<String> sourceTextLines) {
    // initialize if not done earlier
    if (!_init)//from   w  w  w. j  a v a  2  s .co m
        _init();

    // Processing a new file, reset tokens
    _classTokens.clear();
    _positionObserver = new IdentifiersPosition();

    // Normalize single line comments to block comments
    StringBuffer sourceText = _preProcessSourceText(sourceTextLines);

    // Extract all identifiers from java source text
    _javaParser.setSource(sourceText.toString().toCharArray());
    _cu = (CompilationUnit) _javaParser.createAST(null);
    _cu.accept(this);

    // Extract all comments from java source text
    // and merge them with their corresponding identifiers
    if (!_ignoreComments && !_stateAnalysis) {
        // Extract
        List<Comment> comments = (List<Comment>) _cu.getCommentList();
        for (Comment comment : comments) {
            int start = comment.getStartPosition();
            int end = start + comment.getLength();
            String identifierOfComment = _positionObserver.getIdentifierForPosition(start, end);
            String commentText = sourceText.substring(start, end).replaceAll("[\\t\\n\\r]", " ");

            // Do filtering of comments
            if (!_considerCopyright) {
                if (commentText.toLowerCase().contains("copyright")
                        || commentText.toLowerCase().contains("license"))
                    continue;
            }
            // More filtering rules...TBD

            IdentifierTokens it = getTokens(identifierOfComment);
            it.addCommentToken(commentText);
        }
    }

    // Add package information
    if (!_stateAnalysis) {
        for (ClassTokens c : _classTokens)
            c.addToken(_packageName);
    }
}

From source file:org.autorefactor.refactoring.ASTCommentRewriter.java

License:Open Source License

private void addRemovalEdits(List<TextEdit> commentEdits, String source) {
    if (this.removals.isEmpty()) {
        return;/*from ww  w . j  a  v  a  2s  .c om*/
    }
    for (Comment node : this.removals) {
        final int start = node.getStartPosition();
        final int length = node.getLength();

        // chomp from the end before the start variable gets modified
        final int startToRemove = chompWhitespacesBefore(source, start);
        final int endToRemove = chompWhitespacesAfter(source, start + length);
        final int lengthToRemove = endToRemove - startToRemove;

        commentEdits.add(new DeleteEdit(startToRemove, lengthToRemove));
    }
}

From source file:org.autorefactor.refactoring.ASTCommentRewriter.java

License:Open Source License

private void addReplacementEdits(List<TextEdit> commentEdits) {
    if (this.replacements.isEmpty()) {
        return;/*  w ww .  j a va  2s  . c om*/
    }
    for (Pair<Comment, String> pair : this.replacements) {
        final Comment node = pair.getFirst();
        final int start = node.getStartPosition();
        final int length = node.getLength();
        commentEdits.add(new ReplaceEdit(start, length, pair.getSecond()));
    }
}

From source file:org.autorefactor.refactoring.rules.CommentsRefactoring.java

License:Open Source License

private ASTNode getNextNode(Comment node) {
    final int nodeEndPosition = node.getStartPosition() + node.getLength();
    final ASTNode coveringNode = getCoveringNode(node);
    final int parentNodeEndPosition = coveringNode.getStartPosition() + coveringNode.getLength();
    final NodeFinder finder = new NodeFinder(coveringNode, nodeEndPosition,
            parentNodeEndPosition - nodeEndPosition);
    if (node instanceof Javadoc) {
        return finder.getCoveringNode();
    }//  w  w w.  j av  a  2  s. c o  m
    return finder.getCoveredNode();
}

From source file:org.autorefactor.refactoring.rules.CommentsRefactoring.java

License:Open Source License

private ASTNode getCoveringNode(Comment node) {
    final int start = node.getStartPosition();
    final int length = node.getLength();
    final ASTNode coveringNode = getCoveringNode(start, length);
    if (coveringNode != node) {
        return coveringNode;
    }/*from  ww w .  j ava  2  s  .  co m*/
    return getCoveringNode(start, length + 1);
}

From source file:org.autorefactor.refactoring.rules.CommentsRefactoring.java

License:Open Source License

private String getComment(Comment node) {
    final String source = this.ctx.getSource(node);
    final int start = node.getStartPosition();
    return source.substring(start, start + node.getLength());
}