methodhandle

MethodHandle - What is it all about?

谁说胖子不能爱 提交于 2019-11-28 04:38:48
I am studying new features of JDK 1.7 and I just can't get it what MethodHandle is designed for? I understand (direct) invocation of the static method (and use of Core Reflection API that is straightforward in this case). I understand also (direct) invocation of the virtual method (non-static, non-final) (and use of Core Reflection API that requires going through Class's hierarchy obj.getClass().getSuperclass() ). Invocation of non-virtual method can be treated as special case of the former one. Yes, I aware that there is an issue with overloading. If you want to invoke method you have to

Is it possible to convert method reference to MethodHandle?

给你一囗甜甜゛ 提交于 2019-11-27 23:54:52
Is it possible to convert a method reference (e.g. SomeClass::someMethod ) to a MethodHandle instance? I want the benefits of compile-time checking (ensuring that the class and method exists) as well as the ability to introspect the method using the MethodHandle API. Use-case: I've got code that needs to execute if and only if the request was not triggered by a specific method (to avoid endless recursion). I want a compile-time check to ensure the class/method exists but a runtime check to compare the caller to the method. So to recap: Is it possible to convert a method reference to a

Finding most specific overloaded method using MethodHandle

点点圈 提交于 2019-11-27 12:08:57
问题 Suppose I have three methods inside a given type (class/interface): public void foo(Integer integer); public void foo(Number number); public void foo(Object object); Using a MethodHandle or reflection, I'd like to find the most specific overloaded method for an object of which the type is only known at runtime. i.e. I'd like to do JLS 15.12 at runtime. For instance, suppose I have the following in a method of the type mentioned above that contains those three methods: Object object = getLong(

How can I improve performance of Field.set (perhap using MethodHandles)?

感情迁移 提交于 2019-11-27 11:20:21
I'm writing some code that calls Field.set and Field.get many many thousands of times. Obviously this is very slow because of the reflection . I want to see if I can improve performance using MethodHandle in Java 7. So far here's what I have: Instead of field.set(pojo, value) , I'm doing: private static final Map<Field, MethodHandle> setHandles = new HashMap<>(); MethodHandle mh = setHandles.get(field); if (mh == null) { mh = lookup.unreflectSetter(field); setHandles.put(field, mh); } mh.invoke(pojo, value); However, this doesn't seem to perform better than the Field.set call using reflection.

MethodHandle - What is it all about?

一笑奈何 提交于 2019-11-27 04:17:21
问题 I am studying new features of JDK 1.7 and I just can't get it what MethodHandle is designed for? I understand (direct) invocation of the static method (and use of Core Reflection API that is straightforward in this case). I understand also (direct) invocation of the virtual method (non-static, non-final) (and use of Core Reflection API that requires going through Class's hierarchy obj.getClass().getSuperclass() ). Invocation of non-virtual method can be treated as special case of the former

How can I improve performance of Field.set (perhap using MethodHandles)?

孤街浪徒 提交于 2019-11-26 17:59:33
问题 I'm writing some code that calls Field.set and Field.get many many thousands of times. Obviously this is very slow because of the reflection. I want to see if I can improve performance using MethodHandle in Java 7. So far here's what I have: Instead of field.set(pojo, value) , I'm doing: private static final Map<Field, MethodHandle> setHandles = new HashMap<>(); MethodHandle mh = setHandles.get(field); if (mh == null) { mh = lookup.unreflectSetter(field); setHandles.put(field, mh); } mh