Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8204925

6.5: Clarify invokespecial

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 11
    • specification
    • vm

      Some longstanding complications around invokespecial:

      - Historically, a private method could only be invoked by an instruction in the same class. Both `invokespecial` and `invokevirtual` would suit, though `javac` habitually used `invokespecial`. With the introduction of nestmates, the distance between a class invoking a private method and the class declaring the private method can be substantial. In addition, `invokeinterface` was extended in Java SE 11 to be able to invoke a private method in an interface.

      - `invokespecial` rehearses an explicit "lookup procedure" for selecting a method. This dispatch algorithm is obfuscatory because it just repeats the logic of [interface] method resolution.

      - Since ACC_SUPER has been set for all class files since Java SE 8, it's time to remove it from the main narrative of `invokespecial`. A mention in the "Notes" section, pointing to 4.1, would suffice.

      A proposal, which introduces the familiar "method to be invoked" terminology from `invokevirtual`, and avoids a longwinded dispatch algorithm:

      -----
      The _method to be invoked_ is selected as follows:

      - If the symbolic reference names an instance initialization method (§2.9.1), then the _method to be invoked_ is the resolved method (that is, the method found already by successful resolution).

      - Otherwise, if the symbolic reference names the current class or interface, then the _method to be invoked_ is the resolved method. (This scenario is known as "direct self invocation".)

      - Otherwise, if the symbolic reference names a _direct_ supertype of the current class or interface, then the _method to be invoked_ is the resolved method. (This scenario is known as "direct super invocation". A direct supertype of a class is either the direct superclass of the class, or one of the direct superinterfaces of the class. A direct supertype of an interface is either `Object`, or one of the direct superinterfaces of the interface.)

      - Otherwise, the symbolic reference necessarily names an _indirect_ superclass, S, of the current class. The _method to be invoked_ is the method that would be found by successful resolution of the symbolic reference _if it named the direct superclass of the current class rather than S_. (This scenario is known as "re-based super invocation", because the invocation is not based on the indirect superclass named by the symbolic reference, but rather based on the direct superclass.)
      -----

      (The last bullet implies additional checks arising from resolution. This is a good thing because the ability of the longstanding "lookup procedure" to select a different method (than the resolved method) could be a hole. For example, if the selected method doesn't override the resolved method, there are missing class loader constraints.)

            dlsmith Dan Smith
            darcy Joe Darcy
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: