1

I am working on a small program to compare two Java files. My goal is to compare the two files so that I can see what functions were added and deleted from one file to another (like a simple version control program). I am running into issues on how I should be handling these files. My current approach is to use a Scanner and use:

        while(scanner.hasNext()) {
            String function = scanner.next("((public|private|protected|static|final|native|synchronized|abstract|threadsafe|transient)+\\s)+[\\$_\\w\\<\\>\\[\\]]*\\s+[\\$_\\w]+\\([^\\)]*\\)?\\s*\\{?[^\\}]*\\}?");
            System.out.println(function);       
    }

However this is not getting me any results for a file that I know has functions in it. Any tips or ideas on how to approach this?

Locke
  • 534
  • 8
  • 25
  • 6
    Stop. Don't go any further. Step back. Rethink what you want to do here and how you want to do it. You'll see regex is not the right way. There are some libraries for parsing Java files. – Tunaki Sep 30 '15 at 21:42
  • 1
    What about compiling the classes? (http://www.javabeat.net/the-java-6-0-compiler-api/) After you compiled the classes you can use reflection to compare the defined functions. – Matthias Wimmer Sep 30 '15 at 21:46
  • Hmm... I thought about compiling the classes but I eventually want to also compare the contents of the functions (to compare what may have changed between versions). – Locke Sep 30 '15 at 21:48
  • 2
    Stop right there and *use* a version control program. Don't waste your time rewriting standard tools. – user207421 Sep 30 '15 at 23:44
  • [Beyond Compare 4](http://www.scootersoftware.com/) will pay for itself the first time you use it. –  Sep 30 '15 at 23:46
  • What are your overarching goals? Is this for school? For practice? To learn Java, or are you trying to find an existing tool to diff files or source control them? I provided an idea if you really need to code something, that simplifies the parsing to determine the method signatures for comparison. – clearlight Sep 30 '15 at 23:54
  • I am working on this as sort of part of a school project. I like the idea of using ANTLR grammar for this since I have worked with it before. – Locke Sep 30 '15 at 23:55

1 Answers1

1

You could use ANTLR Java grammar https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4 to get a full-blown Java parser and then use it to extract any information you need about Java files.

MK.
  • 33,605
  • 18
  • 74
  • 111
  • I don't recommend to use ANTLR for Java Parser. You can use it for other languages but not for JAVA because its java parser is very slow. I have more than 2 GBs of Java files from which I want to extract functions. ANTLR took a hell lot of time for it. I would recommend to use https://github.com/javaparser/javaparser instead. – Utsav Patel Nov 02 '20 at 12:05