Are there any tools that will, for a given method or class, list all methods that can be called by that method/class?
I am aware of code coverage tools, but I'm looking more for static analysis.
Obviously some kind of filtering system would be needed to stop the generated report being too big. I would want to identify all methods in the com.mycompany.* package hierarchy, for example.
I'm basically looking for an inverse version of the call hierarchy provided by IDEs like Eclipse and Idea.
bcel and asm permit to read and analyze class files. Then you can write the code which:
look in the target method all call ('invoke')
look into constant pool components for their resolution
I don't have anything more friendly...
You can try jarchitect tool, a static analysis tool that can help you to list all methods called by another method or a class.
In Eclipse, you can view the references to a method, class, or variable.
You can do this by right-clicking on the name of the object you're interested in, and selecting whichever scope applies to you:
References -> Workspace (Ctrl + Shift + G)
Project
Hierarchy
Working Set...
or by typing: Ctrl + Shift + G to find references in your Workspace by default.
Related
When I want to find usages of a normal method in java it is fairly easy and straightforward CTRL-SHIFT-G .
However if this method has a super definition, or implementation, then eclipse will show me usages of all types in the hierarchy types.
For example, if I have a toString() method on my class, I would only be interested in finding exactly where this toString() of this class was being called, instead eclipse gives me every single ancestor toString in the project (ie Object.toString()).
How do I find only specific usages to my class? And not usages of parent classes like Object.toString() ?
Alt + ctrl + h should give you exactly, what you are looking for.
Updated with screenshot from my workspace as some people are facing issue in following it:
Thanks to #manouti answer,
To show when using the overridden method only follow the steps:
Search by Java the method as com.package.MyObject.toString()
And after in search result screen click on the right arrow to the left and check References to Overridden
After having the same problem as many others in the comments (Eclipse shows references to Object.toString()) I have found a way that seems to work correctly:
Go to outline from your custom class, right click on your toString() implementation and References -> Project. This displays only references to said implementation.
Can't find anything on this and sort of would like a report on it and not going through all of this code. Here's the question.
I am analyzing a method that calls about 45 other methods that calls other methods. I need to find out all calls that ends up with a specific package from this specific method. How could I do this?
An option I have is to to use "call hierarchy" and drill down one method at a time (but there is no filter option as I can see to just see methods from one given package).
Actually you can do filtering when using the call hierarchy (CTRL - ALT - H). On the newly opened pane, there is a drop-down list with the name "Scope:". Here you can add a new scope where you can include/exclude filter not only on packages but also on classes or to search given a certain regex.
I checked this on the latest Intellij version (14.1.5).
In the process of refactoring large portions of a Java project, I am trying to get a sense of how many methods of a specific library class' methods my project uses. In Eclipse, I can open the Call Hierarchy for a method using Ctrl+Shift+H, which does exactly what I want, but for a single method. On the other hand, I can find all references to a given class in my workspace using Ctrl+Shift+G (or in the project through the right-click context menu), but without any information on how the class or objects hereof are used.
In other words, it seems like I'm trying to find a feature that combines the two - find all references to a class, and then which methods (if any) are called on the class or objects hereof within the scope of that reference. Does such a feature exist in Eclipse (or any other (free) IDE)?
Edit: I realize the question may be a bit cryptic, so here's a pseudo code description of the basic idea:
findMethodCalls(Class c)
1. result = empty list of method names
2. for each method m in c
3. if "Open Call Hierarchy" for m is non-empty
4. result.add(m);
5. return result;
In Netbeans or in Eclipse, you can use "Find Usages" or "References" from the right click context menu. If a() calls b(), using the functionality from b() will show you a(). However, what I want is to be able to see some kind of tree or have an option to see all usages of a given/class or method, such that if z() calls a() that using the functionality will show both z() and a().
Any IDE plugins or external tools that can do this?
Eclipse gives you a way to see Call hierarchy using Ctrl+ Alt + H or choose from the menu like references.
This should show you the entire call tree for this method.
Use PMD or CheckStyle and have it construct an abstract syntax tree. You can then use that to find indirect usages/references.
If you use Eclipse, take a look at the Call Hierarchy view.
Check out nWire for Java. It shows all the code associations in one view, which can be further expanded and explored.
I'm refactoring some Java code to be more decoupled by changing some static method calls to non-static calls, for example:
// Before:
DAO.doSomething(dataSource, arg1, ..., argN)
// After:
dao.doSomething(arg1, ..., argN)
My problem is that in a large project, it can be hard to find where static method calls are being made. Is there an easy way to do this, either from the command line or in Eclipse?
Such a tool would need to let me ignore "benign" static method calls such as these (either by not finding them in the first place, or by allowing them to be easily deleted from the search results):
String.valueOf(...)
Integer.parseInt(...)
MyClass.someBenignStaticMethod(...)
Some clarifications:
I'm not interested in finding method calls made via reflection
I don't know what static methods currently exist in this project, so it's not as simple as searching for their callers using Eclipse's "Open Call Hierarchy" command (Ctrl-Alt-H), although an easy way to search for non-private static methods would let me use this approach
I'm also interested in finding calls to static methods located outside my project, e.g. javax.mail.Transport#send
I'm looking for a free (as in beer) solution
Do you really need to search? Why not comment out the static method calls one by one? When you compile it then it will flush out the references.
I'd use grep (-R on Linux) to search for initial caps-dot-camel case-open (I don't use it enough to give you the full command line). And then grep -v to get rid of some of the rubbish.
Well, really what I'd do is refactor incrementally. Changes a method, and see what breaks (if nothing breaks, delete the code).
Theoretically you could search through the class files looking for invokestatic. The FindBugs infrastructure would probably help out here (there may be better starting points).
Some IDEs provide support for refactoring. You can refactor every static method one-by-one.
In Eclipse, you can view the call hierarchy to see all the callers of such method. To view the call hierarchy you can select the method name and press Command-Alt-H, or Right-Click on symbol and choose 'Open Call Hierarchy).
We have a product called nWire for Java which might just help. nWire analyzes your code and builds a database of your code components and associations. You can see a brief demo on our web site.
We plan to have reporting capabilities added in the future. In the mean while, if you have some basic experience with databases, you can tap into the nWire repository and, with a simple SQL query, get a list of all your static methods (you can also see the invocations there). nWire uses the H2 database engine which is open-source and free.
I can assist in accessing the database. Drop me a line to support [at] nwiresoftware.com.
I've written a small Java program that uses the excellent ASM library. It lets you exclude packages like java.lang, and produces output that looks like this:
+ java
+ io
- File
# createTempFile(java.lang.String, java.lang.String)
+ javax
+ imageio
- ImageIO
# read(java.io.InputStream)
# write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
+ mail
- Transport
# send(javax.mail.Message)
+ internet
- InternetAddress
# parse(java.lang.String, boolean)
+ xml
+ parsers
- DocumentBuilderFactory
# newInstance()
I'd prefer something that's more easily built into my existing build process, which uses CheckStyle, but this is the best solution I've come up with so far.
A possible solution could be a custom CheckSyle or PMD or ... warning. Currently I have the same challenge and trying it with CheckStyle. It seems to be right easy to write such an extention.