This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

get all references to a Java method in source code  RSS feed

 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need a way to be able to programatically get the number of references to a given method(number of times it is called) within a group of source code files.

Basically i need to do the same as the NetBeans "Find Usages" refactoring tool, but i need to do it programatically. I have played around with the Netbeans jar files but cannot work out how to do it!

Does anyone know how to call the netbeans "Find Usages" programatically? Or have any other ideas of how to implement this (perhaps by parsing the source files etc.)

Thanks,

Andy
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parse source files.
 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you suggest any tools which would allow me to go about that?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No tools comes to mind. I´ve never had a need for such thing. Try Googling.

Why do you want to do it by the way? Do you want to create an IDE? Do you have a specific problem for which you think that this is the solution so that you´re asking how to do the (potentially wrong) solution instead of how to solve the problem?

 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to be able to work out which methods in a particular set of java source files are the most called throughout the code. Hence, i need to be able to know how many times each method is called. any other ideas?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren´t you just looking for a profiler?
 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, because i don't actually want to have to execute the code, just analyse the content of the source
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A profiler is not only a tool to calculate the execution time or so. An average profiler also returns the information what you´re asking for (execution paths, amount calls, etc). Just execute once and analyze the results.
 
Michael Peremsky
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply parsing the files is not going to be enough as you could have the same method name in multiple classes. The best way to do it is to load the .class files and find references to the method from there. Any other way (than class files) is not going to be very specific. Unless, you parse the java files, maintain a list of all the methods in the class, and the import files in each file, then try to match methods with imports. Not a trivial solution.
 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would i go about getting references to methods from within the .class files?
 
Harjit Singh
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Young wrote:How would i go about getting references to methods from within the .class files?



you are trying to re-invent the wheel, there are profiling tools which will give you that info..... If you profile your app using netbeans or Eclipse, they give you all the information needed.

 
Tim Holloway
Bartender
Posts: 18774
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Young wrote:How would i go about getting references to methods from within the .class files?


I use grep. But then, I'm just looking for references, not number of static calls. For that, I'd probably write a JavaDoc plugin, iterate the classes by brute force or take the easy way out and use one of the above-mentioned tools.

Of course, none of the above can detect calls made via the Java reflection mechanism!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!