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

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

Introduction

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

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

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(/*  ww  w  .  j  a va  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);//  w w w . ja v a  2  s  .  c  om
    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.j a  v  a  2s . c  o m
    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 {/*  ww  w. jav  a2 s  .  c  om*/
        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:com.google.devtools.j2objc.ast.CompilationUnit.java

License:Apache License

public CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit jdtNode, InputFile inputFile,
        String mainTypeName, String source, NameTable.Factory nameTableFactory) {
    super(jdtNode);
    typeEnv = new Types(jdtNode.getAST());
    nameTable = nameTableFactory == null ? null : nameTableFactory.newNameTable(typeEnv);
    this.inputFile = Preconditions.checkNotNull(inputFile);
    Preconditions.checkNotNull(mainTypeName);
    if (mainTypeName.endsWith(NameTable.PACKAGE_INFO_FILE_NAME)) {
        mainTypeName = mainTypeName.replace(NameTable.PACKAGE_INFO_FILE_NAME, NameTable.PACKAGE_INFO_MAIN_TYPE);
    }/* www .j  a v a  2 s.  c om*/
    this.mainTypeName = mainTypeName;
    this.source = Preconditions.checkNotNull(source);
    newlines = findNewlines(source);
    if (jdtNode.getPackage() == null) {
        packageDeclaration.set(new PackageDeclaration());
    } else {
        packageDeclaration.set((PackageDeclaration) TreeConverter.convert(jdtNode.getPackage()));
    }
    for (Object comment : jdtNode.getCommentList()) {
        // Comments are not normally parented in the JDT AST. Javadoc nodes are
        // normally parented by the BodyDeclaration they apply to, so here we only
        // keep the unparented comments to avoid duplicate comment nodes.
        ASTNode commentParent = ((ASTNode) comment).getParent();
        if (commentParent == null || commentParent == jdtNode) {
            Comment newComment = (Comment) TreeConverter.convert(comment);
            // Since the comment is unparented, it's constructor is unable to get
            // the root CompilationUnit to determine the line number.
            newComment.setLineNumber(jdtNode.getLineNumber(newComment.getStartPosition()));
            comments.add(newComment);
        }
    }
    for (Object type : jdtNode.types()) {
        types.add((AbstractTypeDeclaration) TreeConverter.convert(type));
    }
}

From source file:com.j2swift.ast.CompilationUnit.java

License:Apache License

public CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit jdtNode, String sourceFilePath,
        String mainTypeName, String source, NameTable.Factory nameTableFactory) {
    super(jdtNode);
    typeEnv = new Types(jdtNode.getAST());
    nameTable = nameTableFactory == null ? null : nameTableFactory.newNameTable(typeEnv);
    this.sourceFilePath = Preconditions.checkNotNull(sourceFilePath);
    Preconditions.checkNotNull(mainTypeName);
    if (mainTypeName.endsWith(NameTable.PACKAGE_INFO_FILE_NAME)) {
        mainTypeName = mainTypeName.replace(NameTable.PACKAGE_INFO_FILE_NAME, NameTable.PACKAGE_INFO_MAIN_TYPE);
    }//from  w w w  . j  a v  a  2 s  .  c  o  m
    this.mainTypeName = mainTypeName;
    this.source = Preconditions.checkNotNull(source);
    newlines = findNewlines(source);
    if (jdtNode.getPackage() == null) {
        packageDeclaration.set(new PackageDeclaration());
    } else {
        packageDeclaration.set((PackageDeclaration) TreeConverter.convert(jdtNode.getPackage()));
    }
    for (Object comment : jdtNode.getCommentList()) {
        // Comments are not normally parented in the JDT AST. Javadoc nodes are
        // normally parented by the BodyDeclaration they apply to, so here we only
        // keep the unparented comments to avoid duplicate comment nodes.
        ASTNode commentParent = ((ASTNode) comment).getParent();
        if (commentParent == null || commentParent == jdtNode) {
            Comment newComment = (Comment) TreeConverter.convert(comment);
            // Since the comment is unparented, it's constructor is unable to get
            // the root CompilationUnit to determine the line number.
            newComment.setLineNumber(jdtNode.getLineNumber(newComment.getStartPosition()));
            comments.add(newComment);
        }
    }
    for (Object type : jdtNode.types()) {
        types.add((AbstractTypeDeclaration) TreeConverter.convert(type));
    }
}

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  ww  .  j  a  v a2s.c om*/
        _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;/*w  w w  .  jav a2  s.c o m*/
    }
    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;/*from   w  w w  .  j  a  v a  2  s.co m*/
    }
    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();
    }/*from   w  ww  . ja v a2s .  c  o m*/
    return finder.getCoveredNode();
}