methodhandle

BootstrapMethodError caused by LambdaConversionException caused by using MethodHandle::invokeExact as a method reference

余生颓废 提交于 2019-12-07 11:10:43
问题 I was trying to check if it is possible to use MethodHandle::invoke or MethodHandle::invokeExact as method references for a functional interface that accepts a MethodHandle and returns a generified output. (I know that invoke and invokeExact are signature polymorphic, hence the metafactory call in InvokeExact. However, I wanted to know if the compiler is able to elide the things that I had to do to derive a suitable version of invoke/invokeExact.) invoke.InvokeExact0 package invoke; import

How to call MethodHandle.invokeExact() with an array of Object[]?

陌路散爱 提交于 2019-12-07 01:02:52
问题 Java's MethodHandle.invokeExact(Object...args) takes a variable-length list of arguments. When I try to pass an array of Object [] instead of a list, though, I get an error. See below: 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

How to call MethodHandle.invokeExact() with an array of Object[]?

為{幸葍}努か 提交于 2019-12-05 04:51:14
Java's MethodHandle.invokeExact(Object...args) takes a variable-length list of arguments. When I try to pass an array of Object [] instead of a list, though, I get an error. See below: 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); // throws Exception } class

MethodHandle performance

牧云@^-^@ 提交于 2019-12-03 15:18:06
问题 I wrote a little benchmark that tests performance of java.lang.invoke.MethodHandle , java.lang.reflect.Method and direct calls of methods. I read that MethodHandle.invoke() performance almost the same as direct calls. But my test results show another: MethodHandle invoke about three times slower than reflection. What is my problem? May be this is result of some JIT optimisations? public class Main { public static final int COUNT = 100000000; static TestInstance test = new TestInstance();

MethodHandle performance

不想你离开。 提交于 2019-12-03 05:52:23
I wrote a little benchmark that tests performance of java.lang.invoke.MethodHandle , java.lang.reflect.Method and direct calls of methods. I read that MethodHandle.invoke() performance almost the same as direct calls. But my test results show another: MethodHandle invoke about three times slower than reflection. What is my problem? May be this is result of some JIT optimisations? public class Main { public static final int COUNT = 100000000; static TestInstance test = new TestInstance(); static void testInvokeDynamic() throws NoSuchMethodException, IllegalAccessException { int [] ar = new int

InvokeExact on the object, whose type is dynamically loaded by classloader

十年热恋 提交于 2019-12-01 22:38:10
I have spend whole day on this problem. My problem is how to make an MethodHandle.invokeExact invocation on an instance, whose class type is dynamically loaded at program runtime. To make problem more clear, i show my sample code below: Class<?> expClass = new MyClassLoader().load(....) //expClass is AddSample.class which is subclass of BaseTemplate BaseTemplate obj = expClass.getConstructor(...) .newInstance(...); MethodHandle myMH = MethodHandles.lookup().findVirtual(expClass, methodName,..); System.out.println("Object type "+obj.getClass()); //Print AddSample // If obj is declared as

Invoke private method with java.lang.invoke.MethodHandle

一个人想着一个人 提交于 2019-11-30 13:55:03
How can I invoke private method using method handles ? As far as I can see there are only two kinds of publicly accessible Lookup instances: MethodHandles.lookup() MethodHandles.publicLookup() and neither allows unrestricted private access. There is the non-public Lookup.IMPL_LOOKUP that does what I want. Is there some public way to obtain it (assuming that SecurityManager allows it) ? Turns out it's possible with Lookup#unreflect(Method) and temporarily making method accessible (potentially introducing small security issue unless done during program initialization). Here is modified main

Invoke private method with java.lang.invoke.MethodHandle

大憨熊 提交于 2019-11-29 19:09:32
问题 How can I invoke private method using method handles ? As far as I can see there are only two kinds of publicly accessible Lookup instances: MethodHandles.lookup() MethodHandles.publicLookup() and neither allows unrestricted private access. There is the non-public Lookup.IMPL_LOOKUP that does what I want. Is there some public way to obtain it (assuming that SecurityManager allows it) ? 回答1: Turns out it's possible with Lookup#unreflect(Method) and temporarily making method accessible

How do I remove lambda expressions/method handles that are used as listeners?

给你一囗甜甜゛ 提交于 2019-11-29 13:42:33
Java 8 has introduced lambda expressions , which is a great thing. But now consider rewriting this code: class B implements PropertyChangeListener { void listenToA(A a) { a.addPropertyChangeListener(this); } void propertyChange(PropertyChangeEvent evt) { switch(evt.getPropertyName()) { case "Property1": doSomething(); break; case "Property2": doSomethingElse(); case "Property1": doSomething; break; break; } void doSomething() { } void doSomethingElse() { } } class A { final PropertyChangeSupport pcs = new PropertyChangeSupport(this); void addPropertyChangeListener(PropertyChangeListener

How do I remove lambda expressions/method handles that are used as listeners?

拥有回忆 提交于 2019-11-28 08:01:26
问题 Java 8 has introduced lambda expressions, which is a great thing. But now consider rewriting this code: class B implements PropertyChangeListener { void listenToA(A a) { a.addPropertyChangeListener(this); } void propertyChange(PropertyChangeEvent evt) { switch(evt.getPropertyName()) { case "Property1": doSomething(); break; case "Property2": doSomethingElse(); case "Property1": doSomething; break; break; } void doSomething() { } void doSomethingElse() { } } class A { final