Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the dependent method calls  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello experts,

This is my problem.
I have a class A with a method testA() that would call testB() method in class B. How can i programatically know this dependency method call in Java ?
I have class A with me and also its source as a String. I am able to use some API and get the source code of the individual methods also into Strings.
Now should I do a String parsing with my own rules and token parsing to know the dependency method calls ? or do I have any better way of getting this info ?

Please help me out. I am breaking my head for this
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch.

There is no api for this in java which I know of. You can at runtime get the calling method by getting the stack-trace and viewing the last element of it. But there are tools that allow you to view the dependencies and usages of a class/method. Writing a parser it not the proper solution for this.
 
Sathiesh Kumar Vs
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wouter,

Thanks for your reply.

The actual need of it is, i am trying to design a plugin that can generate the JUnit test cases for any class. I have a code to generate the skeleton of the test cases to which I am trying to integrate the mocking tools like PowerMock, for which I need to know these dependencies. This ultimately would require only input and output to be given by the developer with no extra coding.

The problem here using Stack trace is, I am not executing this method so no runtime I need this plugin to independently generate the test cases which I would integrate into maven build.

Can you suggest me the best solution for this ? If there are any tools built on Java for this, please tell me. I would look into them to get any solution.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with tools like the one you are trying to create, is that the may know which dependencies there are but they have no idea if they are correct and how they should be used. I can only speak for myself but I rather a tool that generates nothing then one that generates wrong code. Because then I have to check and correct the generated code which usually takes longer than writing it yourself.

There are a lot of static analyses tools out there. You could check those out and see if you can reuse some of their code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!