Java 8 interface/class loader changes?

冷暖自知 提交于 2019-12-05 11:58:12

The formal specification describes the process of finding the target method of an invocation expression as first searching all applicable methods and then selecting the most specific one, succeeding if there is no ambiguity.

Compare JLS 15.12.2.1. Identify Potentially Applicable Methods

The class or interface determined by compile-time step 1 (§15.12.1) is searched for all member methods that are potentially applicable to this method invocation; members inherited from superclasses and superinterfaces are included in this search.

In your case it is possible to deduce that the method found in ClassA is an exact match for which the compiler can’t find a more specific method in InterfaceA, however, the specification does not mandate that the compiler has to stop at this point, short-circuiting the search. That’s an optimization a compiler might have, but implementing the search just like formally specified, i.e. searching the entire type hierarchy first and choosing then, is appropriate.

Given how subtle and complex the process is with all the new Java 8 features and type inference, it is understandable that the current implementation is more conservative rather that optimized.

I can think of two possible explanations:

  1. Maybe the addition of default methods, or type annotations, or something else to Java 8 meant that the compiler needed to be changed to load the classfiles for indirectly referenced interfaces.

  2. Maybe it was just an harmless side-effect of some other restructuring of the compiler.

Either way, it doesn't necessarily make any difference to what happens at runtime. And the "fix" at compile time is to not remove the interface classfile like that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!