1

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?

Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
Daniel Schmidt
  • 11,605
  • 5
  • 38
  • 70
  • `but it just finds a small amount`. When does it work? When does it not work? – Chetan Kinger May 31 '15 at 13:52
  • It only finds one currently: `for (final JavadocTagInfo tag : JavadocTagInfo.values()) {` and examples for one not found are `for (PropertyDescriptor> descriptor : propertyDescriptors) {` and `for (int i = 1; i < typeCodes.length; i++) {` – Daniel Schmidt May 31 '15 at 14:08
  • Please add this information to your question. – Chetan Kinger May 31 '15 at 14:19
  • I honestly don't know what I might add to specify the question. Should I share a Github link? – Daniel Schmidt May 31 '15 at 19:18
  • 1
    Daniel, I took the liberty of adding the examples from your comments to the question. That's what @Chetan was referring to when he asked you to "add this information to the question". This way, it is easier for people reading the question to get an understanding of what works and what doesn't, without having to look through the comments. – Amos M. Carpenter Jun 03 '15 at 08:47
  • @AmosM.Carpenter Thank you, I didn't get it. – Daniel Schmidt Jun 03 '15 at 10:16
  • It seems a bug to me, it would be great if you could open an issue at https://github.com/javaparser/javaparser Thanks! – Federico Tomassetti Jun 08 '15 at 12:59

0 Answers0