0

In Java, most methods are virtual methods. At a call site, different actual methods may be invoked depending on the objects. This is dynamic dispatch.

Question: Can we get the number of targets for a call site statically? For example, from bytecodes.

FYI: the answer to "virtual method" has a simple example of dynamic dispatch.

Community
  • 1
  • 1
JackWM
  • 10,085
  • 22
  • 65
  • 92
  • The number of possible call targets is in principle unbounded, depending on how many classes are available that implement/override the method. – user207421 Jul 11 '13 at 23:18
  • Out of curiosity, why are you even interested in doing this? – DaoWen Jul 11 '13 at 23:21

1 Answers1

2

The targets at a given call site would be every class that is a subtype of the type of the target. (I'm using "subtype" here as a "less than or equal" relationship, like instanceof). This means you're essentially asking this same question:

How do you find all subclasses of a given class in Java?

The answer is that there isn't an easy way to do it. You basically just have to enumerate all possible classes and test for subtype relationships.

If you really want to do some sort of static analysis with this, you could build up data structures with all the class hierarchy relationships so you can do faster lookups over a large set of call sites.

Community
  • 1
  • 1
DaoWen
  • 32,589
  • 6
  • 74
  • 101