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));
}
}