3

SO here is my problem. I need to go through a pretty long java code and find comments with a particular word or statement. If it is present I need to copy part of the following code into a text file. For eg. if the word is "cat"

     /** 
      *this code is related to the cat 
      */
      public static int a = 5;
      public static String b = "abcd";

I need the output text file to have the first string after the comment with the word "cat" so here it would be "abcd". I guess I could parse the whole code as a text file and look for comments but is there a faster and more intelligent way?

edit: I tried doing a grep and it does not work since the first occurrence of String is at variable distances and increasing the number of lines to take sometimes includes the next comment too.

Ruchir Patwa
  • 171
  • 2
  • 10
  • I went through that question but then I need only the string value like in the example only "abcd". will that work for this case too? Thanks for pointing it though. Also I do not know in how many lines the string can occur. I need to find the occurrence of the first string after the comment – Ruchir Patwa Jun 18 '13 at 23:29
  • Why not grep and grab surrounding lines, then on the result of THAT do another grep? – Patashu Jun 18 '13 at 23:31
  • 1
    Tried it but doesnt solve my purpose.. in some cases the string is comes a long time after the comment and if I increase the number of lines I take, some places the next comment gets included. – Ruchir Patwa Jun 18 '13 at 23:41
  • Are you allowed to modify the comments? Perhaps doclets can do what you need: http://docs.oracle.com/javase/6/docs/technotes/guides/javadoc/doclet/overview.html – Daniel Kaplan Jun 19 '13 at 00:07
  • unfortunately I cant modify the code. I need it to work on any java code. Thanks for the link though – Ruchir Patwa Jun 19 '13 at 00:14
  • 1
    This is not a duplicate. The OP has already stated that grep as a solution doesn't work and the intent was not to necessarily fine the Nth line after the matched expression. – Tim Bender Jun 19 '13 at 19:50

1 Answers1

2

The biggest problem is that to do what you are asking 100% correctly you would need to write/use a parser for Java's grammar to be sure that you are getting what you intended.

An option though would be to loop over the file contents and switch state between scanning for the comment and scanning for a string. For example:

Pattern stringPattern = Pattern.compile("String [^=]*= \\"([^\\"]*)\\"");
boolean searchComment = true;
for (String line:fileLines) {
    if(searchComment) {
        searchComment = !line.contains(statement);
    } else {
        Matcher m = stringPattern.matcher(line);
        if (m.matches()) {
            String value = m.group(1);
        }
    }
}

Some of the above code is not exactly correct, but should give the rough idea.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • Indeed it is probably better to use a parser(antlr, javacc, etc.) that doesn't ignore comments. – rimero Jun 19 '13 at 00:17