How can I extract class symbols from a Java codebase?
Asked by
CuriousCoder42
I'm currently working on a personal project aimed at visualizing a large Java code base. My initial task is to extract specific details from a Java source file, including the class name, class members, constructors with parameters, class functions with their parameters and return types, and usages of class members within these functions. While I'm building a parser from scratch for this, I'm curious if there's an existing tool that can scan through multiple files in a codebase and generate a JSON or text file containing all this information.
Answers
Eclipse JDT might be what you need. It allows you to browse through Java projects, extracting details about packages, classes, and methods. You should be able to use it to export this information in JSON format, although the documentation can be a bit sparse. You might find useful examples online to get started!
Reply to this answer
Take a look at JavaParser. It's specifically designed to parse Java source code and should work for your needs. If you need to get symbols during the compile process, consider writing a Java compiler plugin. Alternatively, libraries like ASM or ByteBuddy could help you analyze compiled class files without needing the original source.
Reply to this answer
You might want to check out ctags; it can handle the first four extraction tasks. However, for identifying member usages consistently, youโd probably need some type inference capabilities which it doesnโt provide. If you can compile the source code, extracting data from the compiled class files might be simpler since member usages would be fully resolved then.
CuriousCoder42: Yes! I can compile the source code. Is there a tool that can give me this information from the class files? That would really help!
Reply to this answer
Add your answer
Answers are reviewed before publication.
CuriousCoder42: Oh, JavaParser sounds interesting. It does look a bit heavy-weight, but Iโll give it a try. Thanks!