java动态语言invokedynamic,2

  从某种程度上,invokedynamic与MethodHandle机制的作用是一样的,都是为了解决原来的4条指令"invoke*"指令方法将分派规则固化在虚拟机中的问题,如何将查找方法的决定权从虚拟机转移到具体的用户代码中。可将它们想象成一个使用上层的java API实现,另一个使用字节码中和class中的其它属性,常量来完成。

  含有invokedynamic指令的位置被称为动态调用点(Dynamic Call Site),这个指令的第一个参数不再是代表方法符号引用 的CONSTANT_Methodref_info常量,而是jdk1.7中的CONSTANT_InvokeDynamic_info常量,里面有3个信息:

1.引导方法(Bootstrap Method),有固定的参数,且返回值是java.lang.invoke.CallSite对象,代表真正要执行的方法调用。

2.方法类型(MethodType)

3.方法名称

  这样我们就可以根据CONSTANT_InvokeDynamic_info常量中的信息找到并执行引导方法,并得到一个CallSite对象,最终调用要执行的方法。

package vmrun;

import java.lang.invoke.CallSite;
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class InvokeDynamicTest {
        public static void main(String [] args) throws Throwable{
                INDY_BootstrapMethod().invokeExact("lsj");
        }
        public static void testMethod(String s ){
                System.out.println("hello:"+ s);
        }
        
        public static CallSite BootstrapMethod(MethodHandles.Lookup lookup, String name ,MethodType type) throws Throwable{
                System.out.println("BootstrapMethod");
                return new ConstantCallSite(lookup.findStatic(InvokeDynamicTest.class, name, type)) ;
        }
        
        private static MethodType MT_BootstrapMethod(){
                System.out.println("MT_BootstrapMethod");
                return MethodType.fromMethodDescriptorString(
                                "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;",
                                null);
        }
        
        private static MethodHandle MH_BootsrapMethod()throws Throwable{
                System.out.println("MH_BootsrapMethod");
                return MethodHandles.lookup().findStatic(InvokeDynamicTest.class,"BootstrapMethod", MT_BootstrapMethod());
        }
        
        private static MethodHandle INDY_BootstrapMethod() throws Throwable{
                System.out.println("INDY_BootstrapMethod");
                CallSite sCallSite = (CallSite)MH_BootsrapMethod().invokeWithArguments(MethodHandles.lookup(), "testMethod",
                                MethodType.fromMethodDescriptorString("(Ljava/lang/String;)V", null));
                        
                return sCallSite.dynamicInvoker();
        }
}

输出为

INDY_BootstrapMethod

MH_BootsrapMethod

MT_BootstrapMethod

BootstrapMethod

hello:lsj

invokedynamic方法与前面的invoke*方法最大的不同在于它的分派逻辑不是由虚拟机决定的,而是由程序员决定的。

如下面的问题

public class Test {
        class GrandFather {
                void thinking (){
                        System.out.println("grandfather");
                }
        }
        class Father extends GrandFather {
                void thinking (){
                        System.out.println("father");
                }
        }
        class Son  extends Father{
                void thinking (){
                        /**
                         * 如何 实现 调用 grandfather的方法
                         */
                }
        }
}

在jdk1.7前,可以用super实现调用father的方法,但是无法实现调用 grandfaher的方法,原因是在son中无法得到一个实际类型是grandfather的对象引用(overriding),而invokevirtual的分派逻辑就是按照方法的实际接收者的实际类型来进行分派 ,这是固化在虚拟机中的,无法在代码中改变。在jdk1.7中,我们可以在代码中解决这个问题。  

package vmrun;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;


public class Test {
        class GrandFather {
                void thinking (){
                        System.out.println("grandfather");
                }
        }
        class Father extends GrandFather {
                void thinking (){
                        System.out.println("father");
                }
        }
        class Son  extends Father{
                void thinking (){
                        System.out.println("son");
                        /**
                         * 如何 实现 调用 grandfather的方法
                         */
                        try {
                                MethodType mt = MethodType.methodType(void.class);
                                MethodHandle mh = MethodHandles.lookup().findSpecial(GrandFather.class,
                                                "thinking", mt, getClass()) ;
                                mh.invoke(this) ;
                        } catch (Throwable e) {
                                e.printStackTrace();
                        }
                }
        }
        
        public static void main(String [] args){
                (new Test().new Son()).thinking();
        }
}

输出为

son

father

注:

5种invoke方法:

1.invokestatic调用静态方法。

2.invokespecial:调用 实例构造器<init>,私有方法,父类方法。

3.invokevirtual:调用所有的虚方法,final方法也由其调用 ,但不是虚方法。

4.invokeinterface:调用接口方法,会在运行时再确定一个实现此接口的类。

5.invokedynamic:在运行时动态解析出CallSite所调用的方法,然后再执行。