Filter Java comments using StreamTokenizer

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;
  }
 }
}
  • Does anyone understand why the StreamTokenizer behaves the way it does?

  • Any alternative ideas on how to filter the comments would be appreciated.

+3
source share
4 answers

. 137...

  /**
     This constructor created a new <code>Category</code> instance and
     sets its name.

     <p>It is intended to be used by sub-classes only. You should not
     create categories directly.

     @param name The name of the category.
*/

... . , 146 144 .. , . :

 /**
     This constructor created a new <code>Category</code> instance and
     sets its name.    
     <p>It is intended to be used by sub-classes only. You should not
     create categories directly.    
     @param name The name of the category.
*/

... , .

+1

, StreamTokenizer! MyStreamTokenizer 700 :

if (c == '\n')

while (c == '\n')

!

@author  James Gosling 
@since   JDK1.0
+1

I just found that there is an error in the SDN error database related to the error, error 4517649 marked "Closed, will not be fixed." http: //localhost/hawk.html? gwt.codesvr = 127.0.0.1: 9997 & locale = en

Due to compatibility restrictions, we will not lead to the further development of this heritage class. xxxxx @xxxxx 2002-02-14

There is no workaround: - (

0
source

Source: https://habr.com/ru/post/1761205/


All Articles