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
6
votes
3 answers

How to mimic `tableswitch` using `MethodHandle`?

Context: I've been benchmarking the difference between using invokedynamic and manually generating bytecode (this is in the context of deciding whether a compiler targeting the JVM should emit more verbose "traditional" bytecode or just an…
Alec
  • 31,829
  • 7
  • 67
  • 114
6
votes
2 answers

How to invoke a MethodHandle with varargs

I'm trying to replace a reflective invocation with a MethodHandle, but varargs seem to be impossible to deal with. My reflective invoker currently looks like this: public class Invoker { private final Method delegate; public Invoker(Method…
kaqqao
  • 12,984
  • 10
  • 64
  • 118
6
votes
2 answers

How do I lookup an array constructor MethodHandle with MethodHandles.Lookup?

How do I get the MethodHandle for an array constructor like int[]::new? This doesn't work: public static void main(String[] args) throws Throwable { MethodHandles.Lookup lookup = MethodHandles.publicLookup(); MethodHandle mh =…
Archie
  • 4,959
  • 1
  • 30
  • 36
5
votes
2 answers

MethodHandle example throws WrongMethodTypeException on invokeExact call

The example shown in the description of the MethodHandle class throws a WrongMethodTypeException in the invocation of the statement mh.invokeExact("daddy",'d','n') with the following description: (CC)Ljava/lang/String; cannot be called with a…
Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94
5
votes
1 answer

How do I install and use a constant MethodHandle in ByteBuddy?

I'm playing with the support that ByteBuddy has for constant MethodHandles. I am trying to (effectively) look up a MethodHandle on one class, and then use it from a ByteBuddy-generated subclass. (I am aware that I could do this using a static…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
5
votes
1 answer

Implement duck typing using java MethodHandles

I have two classes A and B, both define method foo() with common signature (accept nothing, return void). They don't have the common base class (or interface) that declares this method. I want to call this method on regardless As or Bs as long as…
Netherwire
  • 2,669
  • 3
  • 31
  • 54
5
votes
2 answers

I want to print hi GrandFather;but it seems to print hi father

I want to print hi GrandFather But it seems to print hi father. And I am not understand how to diff the use between findSpecial and findVirtual I want someone can help me. Thank you class GrandFather{ void thinking(){ …
Chronos
  • 69
  • 3
5
votes
2 answers

LambdaMetafactory to access class on a different ClassLoader

I have this code which works fine: Method getterMethod = Person.class.getDeclaredMethod("getName"); MethodHandles.Lookup lookup = MethodHandles.publicLookup(); Class declaringClass = getterMethod.getDeclaringClass(); Class returnType =…
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
5
votes
1 answer

Java code to be compiled into MethodHandle in Constant Pool

I am trying to have Java 8 Nashorn with complete source (not instrumented). As you may know, it uses Nasgen to modify the .classes, and the output is shipped in JRE/lib/ext/nashorn.jar. On disassembling the output, using javap: 0: aload_0 1: ldc…
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
5
votes
1 answer

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

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…
srborlongan
  • 4,460
  • 4
  • 26
  • 33
4
votes
1 answer

Is there a way to tell if the runtime type was erased

This is going to be a bit complicated to explain, but I will try. Suppose you have a generic class: static class Box { private T value; public T getValue() { return value; } public void setValue(T value)…
Eugene
  • 117,005
  • 15
  • 201
  • 306
4
votes
1 answer

How can I find out which method a lambda applies to?

If a method in a library is called with a Java Lambda expression, these are often just wrapped method calls. Is it possible to find out what method that originally was, just for logging purposes? (Another question is about what object it applies to…
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
4
votes
1 answer

MethodHandle with general non-void return filter?

I'm trying to establish a MethodHandle that has a general purpose filter for the return values, by using the MethodHandles.filterReturnValue() to do the work. The problem I have is that I don't know (nor care about) the return type, so I was hoping…
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
4
votes
2 answers

Use LambdaMetafactory.metafactory() for plain non-static getter

I have a simple Person class with a getName() that returns a String: public class Person { public String getName() {...} } How do I use LambdaMetafactory to create a lambda for that non-static method getName() at runtime? Here's what I got…
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
4
votes
1 answer

Methodhandle private method called using findVirtual

Java doc of MethodHandle says that private method should invoke via findSpecial.But in the following example I am able to invoke it via findVirtual. Could somebody please explain what am I missing here? import java.lang.invoke.MethodHandles; import…
shilrast
  • 41
  • 2