简体   繁体   中英

Java source code dependency graph

Well, first of all, this could potentially be stupid and crazy.

I am trying to build (or use, if there already exists one) a framework, which takes in a number of Java source files and outputs the way these files interact; for example, file1.java could import file2.java; file2.java could call a static method from file3.java. It would ideally be great, if this could be done without compiling or running the set of files. I am aware of the Reflection API that allows me to explore the parts of a class, but is that what I am looking for?

Also, how hard is this to extend to other languages, for example, Python, or Lisp (add any other language here)?

I was not really sure how to construct the question title, so if something like this has been asked before, I'd be glad if you could link me to that question.

i am currently working on a project , the main function is very similar to what you mentioned , and i use the javaparser : http://code.google.com/p/javaparser/

javaparser is very powerful , it can help us a lot in source code analysis , but it is very hard to get the full dependencies and classes interactions even if i use javaparser.

For example: if you want to get all the dependencies of a class , the most directly approach is to get the "import" area of the source code - this is very easy by using javaparser.

But only the "import" is not enough , if the class - ClassA - you are currently analyzing called a class - ClassB - which is in the same package with ClassA , then ClassB will not appears in the import area .

So in this situation , we can not get the ClassB dependency.

And for the interactive of classes , if you can not 100% get the right dependencies of a class , then you can not 100% know the right interactive between classes.

But anyway , up to now, the javaparser is the most powerful and useful java source code analyze tool i can find .

You have asked two questions and I will try to address the first one. I believe you are doing some kind of source analysis of java files to see how they could interact with each other (at least that's what I understood) So basically to do this you have to act a bit like the Eclipse IDE. Scanning the source code in each .java file and constructing data structures of java reserved words and constructs. Once each .java file has been analyzed you can then proceed to discover the links between them.

ex.

  1. Store the package name of the class and its name and its scope
  2. Store a HashMap of all declared variables, their values and their scope
  3. Discover the methods in the source file and store their names, in + out parameters and scope

You can do a lot more too and to detect these constructs you'd have to write your own (or find something on the net) parser and use regular expressions to detect these. You store them in your program and then once all source files are analyzed, you can begin to see the interactions.

ex.

Source file 1 is in package xy and has 3 public methods and 2 package scope methods. Source file 2 is in package z and has 1 public method and 3 private methods.

So you can conclude that file 1 can interact with file 2 by invoking that 1 public method. And you can do the same analysis for all the files.

I know you said you'd rather not do the compile, but it would be far easier to look through the class files. Between the bytecode and the constants pool you can get everything you need without resorting to essentially rewriting javac. Use Apache BCEL and you're mostly there. Obviously no tool will be able to find dependencies accessed through reflection; for that you'd need to do runtime analysis with a custom classloader or something.

https://en.wikipedia.org/wiki/Java_class_file http://commons.apache.org/proper/commons-bcel/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM