Questions tagged [methodhandle]

Use this tag for questions regarding MethodHandle/MethodHandles Java classes from java.lang.invoke API.

MethodHandle is a Java class which is an essential part of java.lang.invoke API. It allows to create executable references to existing methods (like it's done via ) and constructors, as well as generate new handlers on the fly (for example field getters/setters, bind arguments of existing MethodHandle and so on). Unlike reflection, MethodHandles have consistent security model which allows to control the access to non-public methods.

MethodHandle exists since .

API docs (Java-9):

99 questions
58
votes
4 answers

MethodHandle - What is it all about?

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)…
alexsmail
  • 5,661
  • 7
  • 37
  • 57
45
votes
4 answers

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

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…
aloo
  • 5,331
  • 7
  • 55
  • 94
34
votes
2 answers

Why use reflection to access class members when MethodHandle is faster?

With the release of Java 7 came the MethodHandle, which allows a user to invoke a method as if using its underlying bytecode. In particular, the MethodHandles.Lookup class provides factory methods to create method handles to access class…
FThompson
  • 28,352
  • 13
  • 60
  • 93
25
votes
5 answers

Finding most specific overloaded method using MethodHandle

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…
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
25
votes
3 answers

MethodHandle performance

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…
DoSofRedRiver
  • 420
  • 5
  • 9
16
votes
2 answers

Why can't I .invokeExact() here, even though the MethodType is OK?

For one of my project I have to make dynamic invocations of constructor. But since this is Java 7, instead of the "classic" reflection API, I use java.lang.invoke. Code: @ParametersAreNonnullByDefault public class PathMatcherProvider { private…
fge
  • 119,121
  • 33
  • 254
  • 329
14
votes
3 answers

Invoke private method with java.lang.invoke.MethodHandle

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…
Marcin Wisnicki
  • 4,511
  • 4
  • 35
  • 57
12
votes
1 answer

Is it possible to convert method reference to MethodHandle?

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…
Gili
  • 86,244
  • 97
  • 390
  • 689
12
votes
2 answers

On signature polymorphic methods in Java-7

As far as I can tell, with the introduction of MethodHandle in Java 7 came the introduction of compiler-generated method overloads. The javadoc for MethodHandle states (I've trimmed the examples): Here are some examples of usage: Object x, y;…
Michael Deardeuff
  • 10,386
  • 5
  • 51
  • 74
11
votes
2 answers

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

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…
Axel
  • 13,939
  • 5
  • 50
  • 79
9
votes
1 answer

MethodHandles or LambdaMetafactory?

At my job we have a DSL for specfying mathematical formulas, that we later apply to a lot of points (in the millions). As of today, we build an AST of the formula, and visit each node to produce what we call an "Evaluator". We then pass that…
Gui13
  • 12,993
  • 17
  • 57
  • 104
9
votes
1 answer

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

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 =…
ccleve
  • 15,239
  • 27
  • 91
  • 157
8
votes
1 answer

java.lang.NoSuchMethodError: VarHandle.compareAndSet(VariableHandlesExample,State,State)void

VarHandle is showing below error - Exception in thread "main" java.lang.NoSuchMethodError: VarHandle.compareAndSet(VarHandleExample,int,int)void at…
7
votes
2 answers

LambdaMetaFactory with concrete implementation of generic type

I am trying to use Java's LambdaMetaFactory to dynamically implement a generic lambda, Handler: public class RoutingContext { // ... } @FunctionalInterface public interface Handler { public void handle(X arg); } public…
Luke Hutchison
  • 8,186
  • 2
  • 45
  • 40
6
votes
1 answer

Why does LambdaMetafactory fail when using a custom functional interface (but Function works fine)?

Given: import java.lang.invoke.LambdaMetafactory; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.util.function.Function; class Testcase { @FunctionalInterface …
Gili
  • 86,244
  • 97
  • 390
  • 689
1
2 3 4 5 6 7