How to find usages of ONE implementation of an interface's method

Viewed 542

I have a project with hundreds of implementations of an interface. I'd like to find the usages of a method in just ONE implementation of that interface. Intellij (and Eclipse) both show me all usages of that method in all of the implementations - hundreds of them.

I highlight the method in one implementation of the interface, right-click and select "find usages", and it shows me all usages of that method in all implementations.

None of the usage options in Intellij seem to get me just one implementation.

public interface Dog {
    void bark(int volume);
}
public class Lab implements Dog {
    public void bark(int volume) {
        System.out.println("woof");
    }
}
public class Collie implements Dog {
    public void bark(int volume) {
        System.out.println("woof");
    }
}
public class Main {

    public static void main(String[] args) {
        Dog onyx = new Lab();
        Dog lassie = new Collie();


        onyx.bark(50);
        lassie.bark(20);
    }
}

If I highlight the Lab bark method, click show usages, I want to just see the onyx.bark(50) line in main.java, not both bark usages in main.java.

I realize that if I use Lab onyx = new Lab() and Collie lassie = new Collie() in the main class, find references will work, but that's not how the project is written. Maybe there's no way for the IDE to determine the implementing class when the type is the interface, but it sure would be useful.

1 Answers

As the comments say, the implementation being used is determined at runtime, so Intellij can't limit usage results to one implementation. However, Intellij support suggested creating a custom scope to limit the usage search. That helps a bit, although creating a scope requires that you know the general area where you usage is, which is maybe what you were doing the usage search for in the first place. In my case, I could create a scope that only includes the code our company has made to the project. That reduced the results from 500+ to about 80.

To create a custom scope in Intellij, select File-settings-appearance & behavior-Scopes. Click the plus sign in the middle of the dialog box, select local or shared, and give it a name. In the right half of the dialog box, you can include/exclude/include recursively/exclude recursively libraries, packages etc.

Then when you do your find usages (or other searches) click the gear icon in the results and select your new scope in the drop down list.

Related