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

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

Introduction

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

Prototype

public final void accept(ASTVisitor visitor) 

Source Link

Document

Accepts the given visitor on a visit of the current node.

Usage

From source file:ASTParser.ParseJavaFile.java

private void parse(final String str, File outputFile) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(str.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    StringBuffer allComments = new StringBuffer();

    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    for (Comment comment : (List<Comment>) cu.getCommentList()) {
        CommentVisitor commentVisitor = new CommentVisitor(cu, str);
        comment.accept(commentVisitor);
        //            System.out.println(commentVisitor.getAllComments().toString());
        allComments.append(commentVisitor.getAllComments().toString());
    }//from  w w  w. j a  v  a 2 s . c  o m

    allComments = ParseWords.parseAllWords(allComments);
    writeToFile(allComments.toString(), outputFile);
}

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

public void parser() throws IOException {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    File file = new File(filePath);

    String stringFile = FileUtils.readFileToString(file);
    parser.setSource(stringFile.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    //Setting options
    Map options;/*from w ww. j  ava 2  s .c  om*/
    options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
    parser.setCompilerOptions(options);

    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    Visitor visitor = new Visitor(cu);
    cu.accept(visitor);

    List<Comment> commentList = cu.getCommentList();

    for (Comment comment : commentList) {
        comment.accept(visitor);
    }

    languageConstructs = visitor.getLanguageConstructs();

}

From source file:com.bsiag.eclipse.jdt.java.formatter.DefaultCodeFormatter.java

License:Open Source License

private void prepareComments() {
    CommentsPreparator commentsPreparator = new CommentsPreparator(this.tokenManager, this.workingOptions,
            this.sourceLevel);
    List<Comment> comments = ((CompilationUnit) this.astRoot.getRoot()).getCommentList();
    for (Comment comment : comments) {
        comment.accept(commentsPreparator);
    }//  w  w  w  .ja  v  a 2  s  .c  o  m
    commentsPreparator.finishUp();
}

From source file:edu.buffalo.cse.Sapphire.JavaModelListener.java

License:Open Source License

/**
 * This function deals with the changes of comments since comments can't 
 * be obtained via ASTNodes, it has to be done by accepting ASTVisitor.
 * /*  www .ja v a 2  s.c o m*/
 * @author Chern Yee Chua
 */

@SuppressWarnings("unchecked")
public static void parse(final String strA) {
    ASTParser parserA = ASTParser.newParser(AST.JLS8);
    parserA.setSource(strA.toCharArray());
    parserA.setKind(ASTParser.K_COMPILATION_UNIT);
    final CompilationUnit cuA = (CompilationUnit) parserA.createAST(null);

    for (Comment comment : (List<Comment>) cuA.getCommentList()) {
        comment.accept(new ASTVisitor() {

            public boolean visit(LineComment node) {
                int start = node.getStartPosition();
                int end = start + node.getLength();
                String comment = strA.substring(start, end);
                int lineNumber;

                if (isTempLarger) {
                    lineNumber = astRootTemp.getLineNumber(node.getStartPosition());
                } else {
                    lineNumber = astRoot.getLineNumber(node.getStartPosition());
                }

                if (isTempLarger) {
                    if (!commentListTemp.contains(lineNumber + " # " + "[LINE_COMMENT] " + comment)) {
                        commentListTemp.add(lineNumber + " # " + "[LINE_COMMENT] " + comment);
                        commentListTempContent.add("[LINE_COMMENT] " + comment);
                    }
                } else {
                    if (!commentList.contains(lineNumber + " # " + "[LINE_COMMENT] " + comment)) {
                        commentList.add(lineNumber + " # " + "[LINE_COMMENT] " + comment);
                        commentListContent.add("[LINE_COMMENT] " + comment);
                    }
                }

                return true;
            }

            public boolean visit(BlockComment node) {
                int start = node.getStartPosition();
                int end = start + node.getLength();
                String comment = strA.substring(start, end);
                int lineNumber;

                if (isTempLarger) {
                    lineNumber = astRootTemp.getLineNumber(node.getStartPosition());
                } else {
                    lineNumber = astRoot.getLineNumber(node.getStartPosition());
                }

                if (isTempLarger) {
                    if (!commentListTemp.contains(lineNumber + " # " + "[BLOCK_COMMENT] \n" + comment)) {
                        commentListTemp.add(lineNumber + " # " + "[BLOCK_COMMENT] \n" + comment);
                        commentListTempContent.add("[BLOCK_COMMENT] \n" + comment);
                    }
                } else {
                    if (!commentList.contains(lineNumber + " # " + "[BLOCK_COMMENT] \n" + comment)) {
                        commentList.add(lineNumber + " # " + "[BLOCK_COMMENT] \n" + comment);
                        commentListContent.add("[BLOCK_COMMENT] \n" + comment);
                    }
                }
                return true;
            }

            public boolean visit(Javadoc node) {

                int start = node.getStartPosition();
                int end = start + node.getLength();
                String comment = strA.substring(start, end);
                int lineNumber;

                if (isTempLarger) {
                    lineNumber = astRootTemp.getLineNumber(node.getStartPosition());
                } else {
                    lineNumber = astRoot.getLineNumber(node.getStartPosition());
                }

                if (isTempLarger) {
                    if (!commentListTemp.contains(lineNumber + " # " + "[JAVADOC] \n" + comment)) {
                        commentListTemp.add(lineNumber + " # " + "[JAVADOC] \n" + comment);
                        commentListTempContent.add("[JAVADOC] \n" + comment);
                    }
                } else {
                    if (!commentList.contains(lineNumber + " # " + "[JAVADOC] \n" + comment)) {
                        commentList.add(lineNumber + " # " + "[JAVADOC] \n" + comment);
                        commentListContent.add("[JAVADOC] \n" + comment);
                    }
                }
                return true;
            }
        });
    }
}

From source file:io.spring.javaformat.formatter.preparator.JavadocLineBreakPreparator.java

License:Apache License

@Override
public void apply(TokenManager tokenManager, ASTNode astRoot) {
    ASTVisitor visitor = new Vistor(tokenManager);
    for (Comment comment : getComments(astRoot)) {
        comment.accept(visitor);
    }//  w  w w  .  j  a  v a  2 s  .  c  om
}

From source file:ko2.ic.sample.ast.Visitor.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override// w ww . j a  v a  2 s  . c o  m
public boolean visit(CompilationUnit node) {
    for (Comment comment : (List<Comment>) node.getCommentList()) {
        comment.accept(this);
    }
    return super.visit(node);
}

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

License:Open Source License

/**
 * Replaces the substring starting at the given start position with the given length by the given
 * replacement text, modifying the source ranges for the nodes in the AST in the process.
 * //from   w  w w  .j  a  v a2  s  . c o  m
 * @param oldStart
 *          the index of the first character being replaced
 * @param oldLength
 *          the number of characters being replaced
 * @param replacement
 *          the text with which they are being replaced
 */
public void replaceSubstring(final int oldStart, int oldLength, String replacement) throws Exception {
    replaceSubstring_markRemovedComments(oldStart, oldLength);
    List<Comment> commentList = getCommentList();
    // replace text
    //System.out.println("|" + m_document.get(oldStart, oldLength) + "| -> |" + replacement + "|");
    m_document.replace(oldStart, oldLength, replacement);
    // prepare positions
    final int newLength = replacement.length();
    final int difference = newLength - oldLength;
    final int oldEnd = oldStart + oldLength;
    // prepare visitor
    ASTVisitor visitor = new ASTVisitor(true) {
        @Override
        public void postVisit(ASTNode node) {
            int position = node.getStartPosition();
            int length = node.getLength();
            int end = position + length;
            // sanity checks
            {
                // we can not start replacement inside of node, but end outside
                if (position < oldStart && oldStart < end && oldEnd > end) {
                    throw new DesignerException(ICoreExceptionConstants.AST_EDITOR_REPLACE);
                }
                // we can not start replacement outside of node, but end inside
                if (position < oldEnd && oldEnd < end && oldStart < position) {
                    throw new DesignerException(ICoreExceptionConstants.AST_EDITOR_REPLACE);
                }
            }
            //
            if (end <= oldStart) {
                // before changed region: no change
            } else if (position >= oldEnd && !(node instanceof CompilationUnit)) {
                // after changed region: move
                node.setSourceRange(position + difference, length);
            } else if (position <= oldStart && position + length >= oldEnd) {
                // embraces changed region: change length
                node.setSourceRange(position, length + difference);
            }
            // special handling for AnonymousTypeDeclaration
            {
                TypeDeclaration anonymous = AnonymousTypeDeclaration.get(node);
                if (anonymous != null) {
                    anonymous.setSourceRange(node.getStartPosition(), node.getLength());
                }
            }
        }
    };
    // modify position/length for nodes in AST
    m_astUnit.accept(visitor);
    // update comments
    for (Comment comment : commentList) {
        if (!(comment instanceof Javadoc)) {
            comment.accept(visitor);
        }
    }
}

From source file:org.eclipse.xtend.core.javaconverter.JavaASTFlattener.java

License:Open Source License

@Override
public boolean visit(final TypeDeclaration it) {
    boolean _isDummyType = this._aSTFlattenerUtils.isDummyType(it);
    if (_isDummyType) {
        this.visitAll(it.bodyDeclarations(), this.nl());
        return false;
    }//  w  ww.  j a  va2s . com
    boolean _isNotSupportedInnerType = this._aSTFlattenerUtils.isNotSupportedInnerType(it);
    if (_isNotSupportedInnerType) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("/* FIXME Non-static inner classes are not supported.*/");
        this.appendToBuffer(_builder.toString());
        this.addProblem(it, "Non-static inner classes are not supported.");
    }
    Javadoc _javadoc = it.getJavadoc();
    boolean _tripleNotEquals = (_javadoc != null);
    if (_tripleNotEquals) {
        it.getJavadoc().accept(this);
    }
    this.appendModifiers(it, it.modifiers());
    boolean _isInterface = it.isInterface();
    if (_isInterface) {
        this.appendToBuffer("interface ");
    } else {
        boolean _isPackageVisibility = this._aSTFlattenerUtils
                .isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
        if (_isPackageVisibility) {
            this.appendToBuffer("package ");
        }
        this.appendToBuffer("class ");
    }
    it.getName().accept(this);
    boolean _isEmpty = it.typeParameters().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
        this.appendTypeParameters(it.typeParameters());
    }
    this.appendSpaceToBuffer();
    Type _superclassType = it.getSuperclassType();
    boolean _tripleNotEquals_1 = (_superclassType != null);
    if (_tripleNotEquals_1) {
        this.appendToBuffer("extends ");
        it.getSuperclassType().accept(this);
        this.appendSpaceToBuffer();
    }
    boolean _isEmpty_1 = it.superInterfaceTypes().isEmpty();
    boolean _not_1 = (!_isEmpty_1);
    if (_not_1) {
        boolean _isInterface_1 = it.isInterface();
        if (_isInterface_1) {
            this.appendToBuffer("extends ");
        } else {
            this.appendToBuffer("implements ");
        }
        this.visitAllSeparatedByComma(it.superInterfaceTypes());
    }
    this.appendToBuffer("{");
    this.increaseIndent();
    BodyDeclaration prev = null;
    List _bodyDeclarations = it.bodyDeclarations();
    for (final BodyDeclaration body : ((Iterable<BodyDeclaration>) _bodyDeclarations)) {
        {
            if ((prev instanceof EnumConstantDeclaration)) {
                if ((body instanceof EnumConstantDeclaration)) {
                    this.appendToBuffer(", ");
                } else {
                    this.appendToBuffer("; ");
                }
            }
            this.appendLineWrapToBuffer();
            body.accept(this);
            prev = body;
        }
    }
    ASTNode _root = it.getRoot();
    if ((_root instanceof CompilationUnit)) {
        ASTNode _root_1 = it.getRoot();
        final CompilationUnit cu = ((CompilationUnit) _root_1);
        final Consumer<Comment> _function = (Comment it_1) -> {
            it_1.accept(this);
            this.assignedComments.add(it_1);
        };
        this.unAssignedComments(cu).forEach(_function);
    }
    this.decreaseIndent();
    this.appendLineWrapToBuffer();
    this.appendToBuffer("}");
    return false;
}

From source file:org.eclipse.xtend.core.javaconverter.JavaASTFlattener.java

License:Open Source License

@Override
public boolean visit(final Block node) {
    this.appendToBuffer("{");
    this.increaseIndent();
    boolean _isEmpty = node.statements().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {//from w  ww . j  ava2s . com
        final Procedure2<ASTNode, Integer> _function = (ASTNode child, Integer counter) -> {
            this.appendLineWrapToBuffer();
            child.accept(this);
        };
        IterableExtensions.<ASTNode>forEach(node.statements(), _function);
    }
    ASTNode _root = node.getRoot();
    if ((_root instanceof CompilationUnit)) {
        ASTNode _root_1 = node.getRoot();
        final CompilationUnit cu = ((CompilationUnit) _root_1);
        final Function1<Comment, Boolean> _function_1 = (Comment it) -> {
            int _startPosition = it.getStartPosition();
            int _startPosition_1 = node.getStartPosition();
            int _length = node.getLength();
            int _plus = (_startPosition_1 + _length);
            return Boolean.valueOf((_startPosition < _plus));
        };
        final Consumer<Comment> _function_2 = (Comment it) -> {
            if ((!(it instanceof LineComment))) {
                this.appendLineWrapToBuffer();
            }
            it.accept(this);
            this.assignedComments.add(it);
        };
        IterableExtensions.<Comment>filter(this.unAssignedComments(cu), _function_1).forEach(_function_2);
    }
    this.decreaseIndent();
    this.appendLineWrapToBuffer();
    this.appendToBuffer("}");
    return false;
}

From source file:org.eclipse.xtend.core.javaconverter.JavaASTFlattener.java

License:Open Source License

@Override
public void preVisit(final ASTNode node) {
    if ((((node instanceof Comment) || (node instanceof TagElement)) || (node instanceof TextElement))) {
        return;//from ww w  .ja v  a  2 s.  c  o m
    }
    ASTNode _root = node.getRoot();
    if ((_root instanceof CompilationUnit)) {
        ASTNode _root_1 = node.getRoot();
        final CompilationUnit cu = ((CompilationUnit) _root_1);
        final Function1<Comment, Boolean> _function = (Comment it) -> {
            int _startPosition = it.getStartPosition();
            int _startPosition_1 = node.getStartPosition();
            return Boolean.valueOf((_startPosition < _startPosition_1));
        };
        final Consumer<Comment> _function_1 = (Comment it) -> {
            it.accept(this);
            this.assignedComments.add(it);
        };
        IterableExtensions.<Comment>filter(this.unAssignedComments(cu), _function).forEach(_function_1);
    }
}