My goal is to parse java source files to find line numbers containing code without comment. Since StreamTokenizer has slashStarComments () and slashSlashComments (), I decided to use it to filter lines containing only comments and code.
The program below prints line numbers and any line tokens in that line, for each line that has something that is not a comment.
It works most of the time, but sometimes it doesn’t ... For example, line numbers are skipped each time and start with line 144 of the comment in the following source file from log4j, Category.java:
http://logging.apache.org/log4j/1.2/xref /org/apache/log4j/Category.html
StreamTokenizer sometimes just skips some lines at the end of javadoc comments.
Here is my code:
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
public class LinesWithCodeFinder {
public static void main (String [] args) throws IOException {
String filePath = args [0];
Reader reader = new FileReader (filePath);
StreamTokenizer tokenizer = new StreamTokenizer (reader);
tokenizer.slashStarComments (true);
tokenizer.slashSlashComments (true);
tokenizer.eolIsSignificant (false);
int ttype = 0;
int lastline = -1;
String s = "";
while (ttype! = StreamTokenizer.TT_EOF) {
ttype = tokenizer.nextToken ();
int lineno = tokenizer.lineno ();
String sval = ttype == StreamTokenizer.TT_WORD? tokenizer.sval: "";
if (lineno == lastline) {
s + = "" + sval;
}
else {
if (lastline! = -1)
System.out.println (lastline + "\ t" + s);
s = sval;
}
lastline = lineno;
}
}
}
source
share