Find all called methods of a class in project - java

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;

Related

Find all calls to interface implementers?

There are a large number of classes in this codebase which use a specific interface. However, picking a few at random, I've been unable to find one which is actually called anywhere; as such, I don't have a great idea of how to use it.
Is there a way in Eclipse to find every instance of any class which implements this interface?
In other words, suppose there exists an interface Interface, and classes ClassA, ClassB, ClassC, ..., ClassX, which all implement it. I want to see every point in the code where something like `ClassX obj = new ClassX(). Most of the classes I'm finding that implement this interface don't have any point where they're actually used; I assume they're for future use.
Open the interface class, hold Control and move your mouse to interface's name, select open implementation. That's the simplest and easiest way to do.
Yes, highlight the interface name and hit F4 or right click -> Open type hierarchy.
Update after OP's edit:
If you are using a framework that uses dependency injection like spring probably you don't find any reference because some of the implementations are defined in a xml file.
Also consider if some implementations are created and invoked via reflection.
Some classes might be loaded during runtime e.g. using reflection. To catch-them-all you can set a method entry breakpoint on the interface method. This is explained in this answer. That way all calls to implementation methods will suspend the JVM regardless of what is the object type.
Do note that unlike the line breakpoints the method breakpoints will really slow down the performance of the JVM.

How can i use redefineClasses() method in javaagents

I have been using premain() with addTransformer(). Since, it gives javassist.ClassNotFound exceptions for certain classes when i run the agent with a server, i thought to try the agentMain() with redefineClasses(). I went through many links, but so far i am unable to find a piece of code that gives me clear idea on how to set up a simple java agent using these two methods. Some help would be really appreciated.
Can we use redefineClasses() with premain()? (When we use redefineClasses() do we still need the transform method?)
I am trying to instrument set of methods of set of classes, where i know the fully qualified name of those classes as com.test.Foo. I wanted to instrument them without going through the entire set of classes loaded onto JVM. I have been reading those documents back and forth, but still i am unable to get a clear idea on how to use that redefineClasses method?
You can call redefineClasses from anywhere, also from a premain method which is nothing but an extension to a normal Java program run by the same JVM process previous to a main method.
A trivial example for running a redefinition is:
instrumentation.redefineClasses(new ClassDefinition(Foo.class, new byte[] {...}));
This way, Foo is set to be represented by the byte array that must contain a valid class file for Foo where all signatures of fields and methods are the same as by the loaded Foo.class. You can use a tool like ASM for instrumenting the class.
If you really only want to instrument Foo, then this might just be the way to go instead of using a ClassFileTransformer.

Intellij find usages by package from method

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).

Finding called methods - reverse call hierarchy

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.

eclipse - find references only in specific class and all subclasses of that class

I need to find the references to external method say Foo.getInstance() in specific type hierarchy, basically i want to restrict the search results to specific class and all subclasses, i am not interested in all references. Is it possible to do with eclipse or any custom plugin? Thanks
You can do this creating a working set with only the files holding your hierarchy.
Then CTRL+H and Java Search with scope "Working Set".
If the classes are all within a package you can select that in Project Explorer then perform Java Search with scope "Selected".
Working sets are awesome
I have identified a workaround. Requires viewing eclipse search results(in "show as list" mode).
For example ClassA has several subclasses (ClassB,C..Z) which inherits methodA defined on class A and calls them.
Perform method reference in eclipse and change the view to "show as list" using triangle/View menu appearing on top-right corner. This will list method signatures containing super-class/sub-class references so you can easily pick or drop from the search result.
If there are too many results to filter through you can Copy this onto file editor and use search keyword of subclass name (ClassB) and each line containing classB is a potential result which calls methodA on classB object.
Note: If method signature uses classA object reference and within body it type-casts to classB then you need to pick that entry and not drop it. I'd also recommend working set approach mentioned above. This is alternate approach to the question posted.

Categories

Resources