3

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();
Eran Medan
  • 44,555
  • 61
  • 184
  • 276

4 Answers4

3

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

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • Thanks, now I just need to know how to use it ;) see my follow up question here: http://stackoverflow.com/questions/5373199/examples-tutorials-for-usage-of-javax-lang-model-or-antlr-javaparser-to-get-inf – Eran Medan Mar 21 '11 at 03:00
  • I'll take a look at my Doclet to see if it is in a state to be published ... but not now, I should have been sleeping already some hours ago. – Paŭlo Ebermann Mar 21 '11 at 03:06
2

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.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
0

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

Eran Medan
  • 44,555
  • 61
  • 184
  • 276
  • ANTLR can parse Java source code. It looks like the poster wants to traverse compiled classes. – Andy Thomas Mar 21 '11 at 01:07
  • @Andy - Thanks, I'm the author (answered my own question) but I do want traversing source not compiled classes – Eran Medan Mar 21 '11 at 01:37
  • Oh! I actually have used ANTLR. I ran across an existing Java grammar, but don't recall what version of the Java language was supported, and there are changes afoot for Java 7. You might be able to use it as a starting point, and add your own actions. I recommend the ANTLR author's book, "The Definitive Antlr Reference: Building Domain-Specific Languages." – Andy Thomas Mar 21 '11 at 02:53
0

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

Community
  • 1
  • 1
Eran Medan
  • 44,555
  • 61
  • 184
  • 276