Invokestatic by static method in interface

Parsing Java 8 code, I found that some calls invokestaticto static methods in an interface (especially this one java.util.function.Function.identity()) use InterfaceMethodRef in the constant pool; this is what javap -s -c -v pshow me:

    15: invokestatic  #66  // InterfaceMethod java/util/function/Function.identity:()Ljava/util/function/Function;

According to the JVM 8 spec, this is not possible, and when I used this instruction in a classfile with Java version 7 ( major version=51), it ran VerifyError in this instruction.

However, when I changed the main version to 52, it started working like a charm. Please note that I am working on Oracle JDK 1.8.0_60. I wonder why this change was necessary (the called method is linked statically, right?) And no matter where it is documented.

+4
source share
1 answer

Well, up to 8 Java methods staticin interfacewere not allowed, so, obviously, any attempt to use them in the previous version, or in a class file that has an older version, it is doomed to failure, no matter how it is implemented in Java 8.

Prior to Java 8, we had the following two rules:

  • The class_index element of the structure CONSTANT_Methodref_infomust be a class type, not an interface type.

    The class_index element of the structure CONSTANT_InterfaceMethodref_infomust be an interface type.

    (see JVMSpec 7 §4.4.2 )

  • The method descriptor invokestaticmust reference the entryCONSTANT_Methodref_info

    (. JVMSpec 7 §6.5)

    (§5.1), (§4.3.3) , .

    , , " " , Java 8. JVM Java 8 , static.

, static interface s, invokestatic, .

, , , - , " " CONSTANT_Methodref_info " ". , :

(JVMSpec 8 §6.5):

(§5.1), (. 4.3.3) , , .

, invokestatic , , . , , static. , interface .


, , CONSTANT_Methodref_info CONSTANT_InterfaceMethodref_info, . , .

: - default default invokespecial , abstract . , invokespecial interface.

, , , default , invokeinterface invokespecial.

+8

Source: https://habr.com/ru/post/1620793/


All Articles