在 Java 中,大多数方法都是虚拟方法。在调用点,可以根据对象调用不同的实际方法。这是动态调度。
问题:我们可以静态获取呼叫站点的目标数量吗?例如,来自字节码。
仅供参考:“虚拟方法”的答案有一个动态调度的简单示例。
在 Java 中,大多数方法都是虚拟方法。在调用点,可以根据对象调用不同的实际方法。这是动态调度。
问题:我们可以静态获取呼叫站点的目标数量吗?例如,来自字节码。
仅供参考:“虚拟方法”的答案有一个动态调度的简单示例。
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.