
japa.parser.ast.comments.CommentsCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javaparser Show documentation
Show all versions of javaparser Show documentation
This package contains a Java 1.7 Parser with AST generation and visitor support.
The AST records the source code structure, javadoc and comments. Soon will be
possible change the AST nodes or create new ones to modify source code like refactoring.
This parser is based on Sreenivasa Viswanadha Java 1.5 parser.
package japa.parser.ast.comments;
import java.util.LinkedList;
import java.util.List;
/**
* Set of comments produced by CommentsParser.
*/
public class CommentsCollection {
private List lineComments = new LinkedList();
private List blockComments = new LinkedList();
private List javadocComments = new LinkedList();
public List getLineComments(){
return lineComments;
}
public List getBlockComments(){
return blockComments;
}
public List getJavadocComments(){
return javadocComments;
}
public void addComment(LineComment lineComment){
this.lineComments.add(lineComment);
}
public void addComment(BlockComment blockComment){
this.blockComments.add(blockComment);
}
public void addComment(JavadocComment javadocComment){
this.javadocComments.add(javadocComment);
}
public boolean contains(Comment comment){
for (Comment c : getAll()){
// we tollerate a difference of one element in the end column:
// it depends how \r and \n are calculated...
if ( c.getBeginLine()==comment.getBeginLine() &&
c.getBeginColumn()==comment.getBeginColumn() &&
c.getEndLine()==comment.getEndLine() &&
Math.abs(c.getEndColumn()-comment.getEndColumn())<2 ){
return true;
}
}
return false;
}
public List getAll(){
List comments = new LinkedList();
comments.addAll(lineComments);
comments.addAll(blockComments);
comments.addAll(javadocComments);
return comments;
}
public int size(){
return lineComments.size()+blockComments.size()+javadocComments.size();
}
public CommentsCollection minus(CommentsCollection other){
CommentsCollection result = new CommentsCollection();
for (LineComment comment : lineComments){
if (!other.contains(comment)){
result.lineComments.add(comment);
}
}
for (BlockComment comment : blockComments){
if (!other.contains(comment)){
result.blockComments.add(comment);
}
}
for (JavadocComment comment : javadocComments){
if (!other.contains(comment)){
result.javadocComments.add(comment);
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy