user3573712
user3573712

Reputation: 85

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

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.

Upvotes: 7

Views: 1443

Answers (2)

Chris J
Chris J

Reputation: 1029

I've a similar case, where I've to find the usage of external library, say for eg:

implementation 'com.android.volley:volley:1.1.1'

in my Module:app only. So, for this, go to

1. Project in the left tab of android studio > Android view > Right Click app directory > Analyze > Analyaze Dependencies > In the pop up window, select Directory only > Click OK.

Then Dependency Viewer Tab will open. Select where to search in left tree. For that, I click app in left tree. Then in the right tree, I have to select the External library, that I'm looking for. For that,

Open External Dependencies > Click com/android/volley.

Then at the bottom of both left and right trees, I can see all the usages of the external library in my Module:app.

Hope it helps...

Upvotes: 0

user3573712
user3573712

Reputation: 85

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.

Upvotes: 1

Related Questions