Search code examples
javaprogramming-languagesreverse-engineering

Java API to get metadata about Java Source Code


I would like to create some reverse egineered design docs based on Java code (not bytecode), instead of writing my own interpreter, what tools and APIs are available to traverse Java code, using Java code?

Reflection is on bytecode, and is limited to the method level, I want to "objectize" also the method code.

Java doc is ignoring the code itself and only based on comments, automatic UML sequnces are too strict

E.g. an API like this (forgive my ignorance of official Programming Languages Structure terms):

JavaCodeDom jcd = new JavaCodeDom(new File(pathToJavaSource), CompilerEnum.Java16)
List <ClassSrc> classes = jcd.getClasses();
ClassSrc cls = classes.get(0);
Map<MethodSignatureSrc,MethodSrc> methods = cls.getMethodsMap();
MethodSrc main = mothds.get(new MethodSignatureSrc(Modifiers.Public, Modifiers.Static, ReturnTypes.Void, "main", new MethodParams(String[].class))
List<StatementSrc> statements = main.getStatements();
for(StatementSrc statement : statements){
   if(statement.getType()==StatementTypes.Assignment()){
        AssignmentStatementSrc assignment = (AssignmentStatementSrc)statement;
        Identifier src = assignment.getAssigneeVariable();
        ExpressinoSrc = assignment.getAssignmentValue();
   }
}
List<AnnotationsSrc> annotations = cls.getAnnotations();

Solution

  • There are several such APIs in existence (and delivered with the JDK), some of them build in in the Java Compiler (javac).

    • The most extensive is the Compiler Tree API, which gets you access to individual expressions (and subexpressions) in the Java source.
    • The language model API models types and members of types (constructors, methods, fields) - it is used by the compiler tree API and also for annotation processing. It does not give access to the contents of the methods.
    • Of course, on runtime you have the Reflection API (java.lang.Class and java.lang.reflect.*, together with java.lang.annotation).
    • To use the compiler tree API, you have to invoke the compiler, with the compiler API.
    • Additionally, there is the Doclet API for Javadoc, which gives you a similar view like the language model API, but additionally with the documentation comments (and parsed tags).

    I once used a combination of Doclet API and Compiler Tree API to format source code beautifully (this is not online, sadly).