Search by Implementation class in eclipse - java

Can one search by the implementation or its methods in eclipse? It would be really useful.
An example is given below.
public interface Foo {
public void method();
}
public class FooImpl implements Foo {
// I should be able to select this and search and it should
// show the whoever called Foo.method
public void method() {
}
}

If you see a method, called from interface, then just position mouse cursor over it, press ctrl and you see menu with options "Open Declaration | Open Implementation". In your case press "Open Implementation".
In case this method is implemented by many classes you will get a popup with "Types implementing or defining...". Quite nice feature :)
UPDATE: according to your example... If I understood it right, then when you select method, press ctrl+shift+G and you see all places where this method is called.

Select "Foo" in the editor -> right-click -> Open Type Hierarchy (or F4). The "Type Hierarchy" View will open -> On this View, click the icon for the function "Show the Sub-Type Hierarchy" (or "Show All Inherited Members").
Eclipse will show all the Child classes.

Another method is mentioned in this post. Instead of using CTRL+H or CTRL+SHIFT+G you can use CTRL+T to open a pop-up in place instead of a new Eclipse tab as the other methods do.

You can use default search option. Access it by pressing CTRL+H. Go to Java search tab. Here you can write method name and search for it.

Related

Intellij idea doesn't give me Create Class intention action if a class name is available to import

I've just started using IntelliJ IDEA and I've noticed something I don't understand.
Given a class:
public class Foo {
public void doStuff() {
Bar = new Bar();
}
}
This leaves Bar undefined with an error message of
Cannot resolve symbol 'Bar'.
If I open the intention action list (i.e. the ALT+Enter shortcut), I get one of two behaviours:
If any classes called Bar are available on the classpath then I get options to import them.
If no class called Bar is on the classpath then I get the option to create it.
In my case, Bar is on the classpath but I wanted to create a new one instead. However because the name matches another class on the classpath then I drop into option 1 and never get the option to create it from the intentions list. Is there a way to turn this on or some other shortcut to use?
Thanks for any help.
You could untick the "Show import popup for classes" option to get rid of that small blue popup that always shows up when there is one unambiguous import (called "import popup").
It would turn it off and cmd+Enter (or alt+Enter for Windows) will open all intention actions available.
Even if the feature is turned on, you still can access the all-options menu by changing the mouse position (there should be a more convenient way).

Shortcut to see class hierarchy in Eclipse

I have one query that is I am using eclipse and I have imported the project and as usual in a project there are 30 to 40 packages and each package contains set of classes , Now say in package named A I have a class Named abc and in later subpackages ,let say in package named W i have extended and override some of the methods of that class(abc) and construct a new class named def which extend the parent class abc, now please let me let me know I have opened the parent class abc in eclipse is there any shortcut through which I can find out which subclasses in later package overrides it's method and which subclasses are extending it..!
Yes: When your cursor is on the class name (as in: public class A|bc), you can press Ctrl+T to see the inheritance hierarchy of Abc.
If you press CTRL-T twice, it will show you the supertype hierarchy, which also includes any Interfaces that your class implements. Occasionally very useful.
Also,
Right click on the file in the project explorer -> Open type hierarchy.
Same works for right click on the class name in editor window.
Shortcut is F4.
Also, apart from worthy shortcut that Aaron posted, you can also do this: -
Go to Windows -> Show Views -> Open Outline view (If not found - go to Others)
It will open an Outline View which shows your class hierarchy of your current class

Why open Call Hierarchy on a method is not working

I am using Eclipse IDE 3.6 version .
Inside my code i have a method as
public APIRes execute(APIReq request) throws Exception {
// Some code
}
Could you please tell me , when i did open Call Hierarchy , nothing is shown .
Why is it so ??
The obvious solution is that there may be nothing calling that method - at least within the code that Eclipse knows about. Beyond that, we really can't say...
Maybe the problem is that Open Call Hierarchy was not searching the entire Workspace.
Click on the small down arrow (in the Call Hierarchy view window on the right; it is the "View Menu" arrow -- a triangle pointing down) in Call Hierarchy view, set the Search Scope > Workspace.
Do you mean Call Hierarchy or Class Referencing.
If your create a method and nobody calls it, what do you expect to see in the call hierarchy.
Your method is referencing the class in which the method is placed. This would be shown in the reference view.
first clear the project and refresh it and select the method press right click open call hierarchy you will get the class where method declaration is defined.

How to get eclipse to go to the ONLY implementation of an interface's method?

If I'm in an interface and pointing to a method name, what can I do to quickly go to the ONLY implementation of that method?
Using Eclipse 3.6.
F3 is the typical "go to implementation". For interfaces that go to the interface definition.
Instead use Ctrl + T to see all implementations of the interface definition. You can then easily go to the one you want with the arrow keys and Enter. I believe that the first one is automatically selected so that Ctrl-T + Enter will do what you need.
I just checked this on my Eclipse 3.6 install: Hold control (command on Mac), hover over the method name and select "Open Implementation".
You may assign a keyboard shortcut to this action by using Window > Preferences > General > Keys and searching for "Open Implementation".
In the keymap (General > Keys) search for "open implementation" and map it to whatever you want. I chose Ctrl + Shift + I. Make sure you select "Editing Java Source" in the When box. I tested it, and having the cursor over the method name and pressing Ctrl + Shift + I took me directly to the implementation instead of showing the hierarchy that you get with Ctrl + T.
Also you can see an answer to a nearly identical question for other options:
In eclipse, ctrl-click goes to the declaration of the method I clicked. For interfaces with one implementation, how can I just directly to that implementation?
If someone still need this information nowadays (Eclipse version 2022), to jump into interface method definition starting from an #Override method, now in Eclipse you can see on the left, next to the method signature, a little white triangle. By clicking on that you will jump to the implemented interface method. here an image of the little triangle
Oterwise, if you are on a interface method definition and you need to jump to one of the implementations, you must use CTRL+T shortcut to see the list of available implementations and than click on one of them.

How to find forward reference in eclipse?

Is there any way by which I can find out all classes which implement a particular interface?
For example, if I have a interface as follows:
public interface myIntFac{}
I want to find all classes inside current project that implement this interface. How can I find it out in Eclipse?
At the interface level, you can use (assuming the cursor in on the interface)
CTRL+T to show the Quick Type Hierarchy
F4 to Open Type Hierarchy
At the method level, if you hover a method while holding down the control key, a pop-up box gives you the following choices:
Open Declaration
Open Implementation
(source: eclipse.org)
And from anywhere, you can use:
CTRL+SHIFT+H to open the Open Type Hierarchy pop-up and search for any type/interface.
Place your cursor on the interface, and try Ctrl-T.
Put your cursor in myIntFac and press F4 to show the type hierarchy as a separate window, or Ctrl-T to show it as a pop-up. Personally I like the "separate window" approach as it makes it easier to browse, refocus on a different type etc - but if you only want to show the hierarchy very briefly, the pop-up is good too.

Categories