2

I'm making an analyser that figures out the depth of inheritance tree of a java class.

I'm using javaparser to exact this info from a java file.

How would I be able to calculate the DIT?

I'm aware of .getSuperClass() but that's not useful when parsing in a java file as a part of a static analyser.

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26
srysry
  • 173
  • 1
  • 1
  • 10
  • 1
    This question is much too broad as you've provided no context or code. In general you must parse the class declaration and the source for any superclasses, recursively, until you get to `Object`. For superclasses without source you'll either have to get the source (the correct version) or extract the class declaration from the compiled `.class` file. This is a large complex task, and explaining how to do everything is beyond the scope of StackOverflow. If you had to ask this question then you are probably not ready to tackle the overall problem. – Jim Garrison Oct 22 '16 at 16:13
  • 1
    @JimGarrison i thought the idea was quite simple... take any java class, and work back until you've reached Object, counting how many classes deep the inheritance is – srysry Oct 22 '16 at 16:17
  • 1
    It is "simple" to explain but there are LOTS of complications along the way such as handling classes for which you don't have the source code. It is not clear what you're asking for. Were you hoping someone would provide you with the code? – Jim Garrison Oct 22 '16 at 16:20

1 Answers1

1

The short answer is: you cannot do that with JavaParser alone but you can very easily do that by using JavaSymbolSolver, which is a library built to complement JavaParser and mantained by the same team who mantains JavaParser.

In practice JavaParser gives you just an AST. In the AST the superclass of a class is just a name. JavaSymbolSolver finds for you the type declaration attached to that name. That declaration has all the information you need to find all the ancestors and answer this question and many more (like, what interfaces are implemented, directly and indirectly, which fields the class has inherited, which methods, etc.)

Disclaimer: I am the original author of JavaSymbolSolver

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26
  • 1
    Does this handle all the messiness of Java8? The name resolution rules are really complex. How do you test that? – Ira Baxter Oct 28 '16 at 17:00
  • The code is not as battle tested as your commercial product :) It is a relatively young project built in the free time. There are cases in which the type inference with lambdas could not work correctly but regarding just getting the superclasses it should work fine, also considering generic parameters. I tested it by verifying it resolves correctly all the method calls in a couple of projects that are using Java 8. So I will not bet anyone life on this working perfectly under all circumstances, but for the problem considered I think it should work as expected and PRs are welcome :) – Federico Tomassetti Oct 28 '16 at 17:08
  • Yeah, its probably easier to get the superclass relationships right and that's all that is needed for DIT. – Ira Baxter Oct 28 '16 at 17:10