0

So, the expected behavior is, that the program outputs a tree of all the classes and objects inside a .java file. Like this:

class One >
    method one()
    method two()
class Two extends One>
    method three()

And so on. I came up with 2 regex patterns, one for a class and the other for a method.

this.classPattern = Pattern.compile("class\\s([a-zA-Z]+)(?:\\sextends\\s([a-zA-Z<>]+))?\\s?\\{");
this.methodPattern = Pattern.compile("([^ new=/\\(\\)][a-zA-Z\\[\\]]*)\\s([a-zA-Z]+)\\((.*)\\)\\s?\\{?(\\;?)");

And I'm stuck, can't figure out how to parse the file successfully. Here is what I came up with, trying to do it:

Matcher matcher = classPattern.matcher(string);

while (matcher.find()) {
    System.out.println("class > " + matcher.group(1));
    matcher.usePattern(methodPattern);

         while (matcher.find()) {
              System.out.println("\t method > " + matcher.group(2) + " (" + matcher.group(1) + ")");
         }
}

And this is where I got so far.

class > RegExpLanguageHosts
     method > getInstance (RegExpLanguageHosts)
     method > RegExpLanguageHosts (private)
     method > setRegExpHost (void)
     method > findRegExpHost (RegExpLanguageHost)
     method > isRedundantEscape (boolean)
     method > supportsInlineOptionFlag (boolean)
     method > supportsExtendedHexCharacter (boolean)
     method > supportsLiteralBackspace (boolean)
     method > supportsNamedGroupSyntax (boolean)
     method > supportsNamedGroupRefSyntax (boolean)
     method > supportsPythonConditionalRefs (boolean)
     method > supportsPossessiveQuantifiers (boolean)
     method > supportsSimpleClass (boolean)
     method > isValidCategory (boolean)
     method > getAllKnownProperties (String[][])
     method > getPropertyDescription (String)
     method > main (void)
     method > MyExitAction (public)
     method > actionPerformed (void)
     method > MyPackAction (public)
     method > actionPerformed (void)
     method > MySetLafAction (public)
     method > actionPerformed (void)
     method > getKnownCharacterClasses (String[][])
     method > getPosixCharacterClasses (String[][])
     method > something (Logger)
     method > main (void)
     method > MyExitAction (public)
     method > actionPerformed (void)
     method > MyPackAction (public)
     method > actionPerformed (void)
     method > MySetLafAction (public)
     method > actionPerformed (void)

How can I alternate patterns and detect class ends, and group methods into specific classes?

There is more than one class inside the file, but the methods get piled up to the first class and ignores the others.

The closest I got to was:

RegExpLanguageHosts
     > getInstance
FormPreviewFrame
     > RegExpLanguageHosts
MyExitAction
     > setRegExpHost
MyPackAction
     > findRegExpHost
MySetLafAction
     > isRedundantEscape
FormPreviewFrame
     > supportsInlineOptionFlag
MyExitAction
     > supportsExtendedHexCharacter
MyPackAction
     > supportsLiteralBackspace
MySetLafAction
     > supportsNamedGroupSyntax

with

        while (classMatcher.find()) {
            System.out.println(classMatcher.group(1));
            if (methodMatcher.find()) {
                System.out.println("\t > " + methodMatcher.group(2));
            }   
        }
Gala
  • 2,592
  • 3
  • 25
  • 33
  • 1
    Please do not try using a regular expression to parse an irregular language. – OldCurmudgeon Jun 09 '16 at 12:40
  • That it is **NOT** a task for a RegExp, cause it cannot count opening and closing brackets. Try to compile your java files and use reflection. Here is a similar question about HTML and [the best answer ever](http://stackoverflow.com/a/1732454/3913440) – TEXHIK Jun 09 '16 at 12:40
  • Well that was a hell of a read. How can I tackle this problem then? Compiling is not an option. – Gala Jun 09 '16 at 12:59

0 Answers0