Find invocations of subclass method

174 Views Asked by At

For context, say I have these classes:

public class Foo {
     public void doStuff() {}
}

public class Bar extends Foo {
     @Override
     public void doStuff() {}
}

public class Zig extends Foo {
     @Override
     public void doStuff() {}
}

I'm trying to find only calls to Bar#doStuff().

I've tried:

  • Java Search for Bar.doStuff
  • "Open Call Hierarchy" on Bar#doStuff()

but they appear to return calls to both Bar.doStuff() and Foo.doStuff() and Zig.doStuff(). Is there something else I need to do, or am I misunderstanding the results I'm getting?

2

There are 2 best solutions below

0
On

To find the calls which are definitely from Bar, you can mark Bar#doStuff as @Deprecated, and then create a new "Problems" view with a filter to find just these messages.

to create the new Problems view:

  1. open default Problems view
  2. click triangle and choose New Problems View
  3. in new view, click triangle and choose Configure Contents
  4. On the left, check only Errors/Warnings in Project
  5. set the Text contains filter (under Description heading) as appropriate

Note: this will not find cases where Bars are stored as Foos, e.g.:

Foo f = new Bar();
f.doStuff();
1
On

Well, due to polymorphism, there's not a good way for the IDE to know that a given Foo isn't potentially a Bar -- so it will show you all calls to doStuff. I suppose it could do more analysis to determine that the concrete type of a Foo really is a Foo -- for example, in the case:

final Foo foo = new Foo();

it is definitely not a Bar -- but that's a lot of work for little benefit.

You will notice the same holds true for interfaces and their implementations, at least in Eclipse.