Intellij find usages by package from method - java

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

Related

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.

Find all called methods of a class in project

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;

How to find and remove all occurances of a function in a class in Eclipse?

I've been rewriting a code base and I've been trying to workout where in Eclipse I can remove all occurrence of a function and any argument/param it may have.
I'm not after clever regex solutions or anything like that, I'm sure the functionality would be in Eclipse, just not sure where ?:)
Many thanks
Just to clarify:
Here's an example...
The function's class myFoo no longer exists. Because it has been refactored incorrectly before arriving with me. Thus I want to search the code base (via an eclipse tool) and remove any line that uses the foo(String string) method from the myFoo class.
Hilight the object (method or variable), then select menu "Search/Refrences" or "Search/Declarations". Do the changes manually, one at a time, so you can review each change. I would never make widespread code changes with a script due to the high probability of unintended consequences.
I'd use replace from the edit menu with
replace='function name' to '// TODO:'
This would highlight them on the gutter. Select that line and press Ctrl + D to delete that line.
It may be an option to remove the body of the function itself and then inline it via Refactor->Inline?
If needed you can of course save the function's code and insert it again afterwards.
I don't know of such a functionality within the JDT. But here is an automated solution you could use:
With MoDisco you can create a model of your java projects. Then you can use an ATL refining transformation to remove all invocations of that method in that model. You can even replace the invocation with some other expressions, if you want to replace the functionality.
If your codebase is large enough than its probably a good idea to have a closer look at these eclipse projects.
Just type a 'MUST_BE_DELETED' or something similar at the beginning/middle/end of the method name (don't use the rename function) then eclipse will highlight all the places that call it in the problems window. When you've fixed all the problems you can go and delete the method.
If you have code in jsp pages then you'll have to manually search for those.
That's what I used to do:
put your cursor at the method name, and press Control+Shift+g (in Windows), or you can do Search -> Reference -> Workspace.
This will search all reference to this method, in the workspace, and remove the reference manually
(Copied from my comment)
If you insist to have the "Remove" done automatically, there is another workaround (may not always works, depends on situation):
make your method body empty,
and then use Refactor -> Inline (you can even choose to delete method declaration)
But I believe there are some cases that this approach is not working. Worth a try to see if it works for your case anyway
Use ctrl+F short cut to open find/replace dialog.Than write your method name in space front of "Find" textbox .Do not write anything in "Replace With" textbox.
Then press "Replace All" button.
This is the most shortcut method.
enjoy :)

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.

find all method invocations

How can I using eclipse Java search find all calls of methods that are declared in specific class?
I tried p1.p2.p3.MyClass.* but this search string finds equals, getClass calls which is not necessary. I only need to find methods that are declared explicitly in p1.p2.p3.MyClass.
Select the method name and hit CTRL-SHIFT-G, which searches for all references to this method.
If you want to find all of the usages of all of the methods, you can't do that, but you can find all usages of the class, by selecting the class name and doing CTRL-SHIFT-G.
If you select the method pressing CRTL ALT H you can see the call hierarchy and configure its view.
In the outline view you can select multiple methods, then right button and open in call hierarchy. Again CRTL ALT H .
You can find references to a class by selecting the class name and CRTL SHIFT G (references).
Go to the method that you want to find. Right click and select the option to see all references.
Eclipse is amazing for this. It should show all references in a panel of results
I have found how it can be done using Java search. I have filtered out overrode methods from search result and got only methods defined in type.
Thank you all for your time spent on this question nevertheless :)

Categories

Resources