I am using javaparser to parse a Java file and I would like to find all occurrences of for
loops (forEach
& normal for
loops). This is my approach so far, but it only finds some, not all occurrences.
public class AstElementTraverser extends VoidVisitorAdapter<File> {
@Override
public void visit(ForeachStmt n, File sourceFile) {
System.out.println("Found a foreach loop " + sourceFile.getAbsolutePath() + " @ " + n.getBeginLine());
}
@Override
public void visit(ForStmt n, File sourceFile) {
System.out.println("Found a for loop " + sourceFile.getAbsolutePath() + " @ " + n.getBeginLine());
}
}
For instance, it seems to work for this line:
for (final JavadocTagInfo tag : JavadocTagInfo.values()) {
... but not for this:
for (PropertyDescriptor<?> descriptor : propertyDescriptors) {
... nor this one:
for (int i = 1; i < typeCodes.length; i++) {
Is this a possible bug in javaparser, or am I just using it the wrong way?