JVMS v8: possible ambiguity in definition of invokevirtual lookup

Pietro Braione pietro.braione at unimib.it
Tue Nov 21 21:42:55 UTC 2023


Hello to everyone. I am reading the JVMS v8 definition of the invokevirtual lookup procedure (section 6.5) in the case the method is not signature polymorphic, and I am somehow confused. The specification reports:

"Let C be the class of objectref. The actual method to be invoked is selected by the following lookup procedure:
1. If C contains a declaration for an instance method m that overrides (§5.4.5) the resolved method, then m is the method to be invoked.
2. Otherwise, if C has a superclass, a search for a declaration of an instance method that overrides the resolved method is performed, starting with the direct superclass of C and continuing with the direct superclass of that class, and so forth, until an overriding method is found or no further superclasses exist. If an overriding method is found, it is the method to be invoked.
3. Otherwise, if there is exactly one maximally-specific method (§5.4.3.3) in the superinterfaces of C that matches the resolved method's name and descriptor and is not abstract, then it is the method to be invoked."

The fact is, nothing in this specification prohibits C from not being a subclass of the resolved method’s class (say, M). In principle C could even be a superclass of M. In such case, e.g., point 3 would allow a superclass C of M, having one maximally-specific superinterface method that matches, to “override” the implementation declared in the subclass M, quite an unnatural situation. I think it would be obvious to expect that C must be either a subclass of M or M itself, but I cannot find anything in the specification that enforces this invariant. I had a look at the specifications from versions 11 to 21 but, while different, do not seem to differ under this aspect. 
Am I misunderstanding something?

Best,
Pietro Braione
University of Milano-Bicocca


More information about the jls-jvms-spec-comments mailing list