Eran Medan
Eran Medan

Reputation: 45765

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

Upvotes: 3

Views: 1658

Answers (4)

Eran Medan
Eran Medan

Reputation: 45765

This seems to answer my question: How to generate AST from Java source-code? ( Spoon )

Upvotes: 0

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74790

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).

Upvotes: 3

Andy Thomas
Andy Thomas

Reputation: 86459

BCEL supports reading an manipulating Java class files. (I have not used it myself, but saw it used successfully in a third-party product.)

    The Byte Code Engineering Library is intended to give users a convenient
    possibility to analyze, create, and manipulate (binary) Java class files
    (those ending with .class). Classes are represented by objects which 
    contain all the symbolic information of the given class: methods, 
    fields and byte code instructions, in particular. 

If you're just interested in decompiling, you might find it sufficient to decompile to source code. Here's a comparison of several options for Java.

Upvotes: 2

Eran Medan
Eran Medan

Reputation: 45765

I seems ANTLR is one option, but I haven't used it

Upvotes: 0

Related Questions