Tag: invokedynamic

invokedynamic和隐式方法

正如我在阅读这篇关于JDK 7中新的invokedynamic字节码指令的post所理解的那样,它可以调用对象类中没有静态定义的对象上的方法,并将这些方法调用解析为某些具体的静态方法。其他类通过拦截方法调用目标分辨率(post给出一个例子)。 这是否意味着Java 7类可以拥有像Scala这样的隐式方法? 如果不是,Scala中的隐式方法解析与invokedynamic方法解析有何不同?

如何使用Object 数组调用MethodHandle.invokeExact()?

Java的MethodHandle.invokeExact(Object … args)采用可变长度的参数列表。 但是当我尝试传递一个Object []数组而不是一个列表时,我收到一个错误。 见下文: private void doIt() throws Throwable { Method meth = Foo.class.getDeclaredMethods()[0]; MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle mh = lookup.unreflect(meth); Foo foo = new Foo(); String myStr = “aaa”; Integer myInt = new Integer(10); Object [] myArray = {foo, myStr, myInt}; mh.invokeExact(foo, myStr, myInt); // prints “Called aaa 10” mh.invokeExact(myArray); // […]

使用java.lang.invoke.MethodHandle调用私有方法

如何使用方法句柄调用私有方法? 据我所知,只有两种可公开访问的Lookup实例: MethodHandles.lookup() MethodHandles.publicLookup() 并且都不允许不受限制的私人访问。 有非公开的Lookup.IMPL_LOOKUP我的需求。 是否有一些公共方法来获取它(假设SecurityManager允许它)?

将MethodHandle转换为方法引用(此处为Function)

MethodType methodType = MethodType.methodType(void.class, ByteBuffer.class); MethodHandle handle = MethodHandles.publicLookup().findConstructor(type, methodType); Function = handle; // ??? 是否有可能获得最后的作业? 反转方式不起作用: 是否可以将方法引用转换为MethodHandle? 这是另一个可复制的例子: new Integer(“123”); MethodType methodType = MethodType.methodType(void.class, String.class); MethodHandle handle = MethodHandles.publicLookup().findConstructor(Integer.class, methodType); Function function1 = Integer::new; Function function2 = handle.toLambda(); // ???