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
4
votes
2 answers

MethodHandle to a getter/setter from another class gives a NoSuchFieldError

Suppose I have simple javabean MyPerson with a name getter and setter: public class MyPerson { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; …
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
4
votes
3 answers

Express a general `MethodType` with return type as any object

Let the following be a class in my problem: class MyClass { String name() { return toString(); } } I want to create an instance of MethodType which describes a method with a return that is "any" object. I am trying the…
nobeh
  • 9,784
  • 10
  • 49
  • 66
3
votes
1 answer

How to create a varhandle to access elements of an array in Java 9+

I'm attempting to convert some code that uses Unsafe to perform memory accesses on local variables in classes, and the code also seems to use Unsafe to access elements in an array. I have the following code to create a VarHandle for the single…
BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
3
votes
2 answers

MethodHandle invokeExact a static method with return and parameter

import java.lang.invoke.*; public class InvokeDynamicDemo { public static double doubleIt(double d){ System.out.print("Doubling it"); return d*2; } public static void main(String[] args) throws Throwable { …
optional
  • 3,260
  • 5
  • 26
  • 47
3
votes
2 answers

Can MethodHandle be used by frameworks/libraries (instead of traditional reflection)?

In my framework I have a class like this: public class Foo { private final Method getterMethod; ... public V executeGetter(B bean) { try { return getterMethod.invoke(bean); } catch ... } } This…
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
3
votes
2 answers

How can I conveniently wrap a caller-sensitive API?

Certain Java APIs are caller-sensitive. One (woefully underdocumented IMO) example is System.load(), which loads some JNI code into the caller's ClassLoader only. I have a wrapper that looks roughly like JniUtils.loadLibrary("nameoflibrary"). It…
Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
3
votes
2 answers

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

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
shijie xu
  • 1,975
  • 21
  • 52
3
votes
1 answer

Forward call on a functional interface using method handles

TL;DR: I'm trying to find the most effective to dynamically create an implementation of a functional interface that forwards calls to a set of instances of that interface. The goal is to simplify the implementation of interfaces which allow client…
Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
3
votes
2 answers

MethodType transformation for MethodHandle to accept Array Object paramters

I want to adapt String.startsWith from (String, String)boolean to (String[])boolean, so that it can accepts String[] parameters, in which first two parameters will be mapped to the (String, String). Therefore, I wrote below sample code:…
shijie xu
  • 1,975
  • 21
  • 52
3
votes
1 answer

Combining MethodHandles.publicLookup() with Method.setAccessible(true)

I understand that publicLookup() is faster than a lookup() for public methods, and I would like to make use of that. If I was to use MethodHandles.publicLookup().unreflect(Method) on a Method which is not inherently public but I have called…
OllieStanley
  • 712
  • 7
  • 25
3
votes
1 answer

Caching the result of MethodHandles.lookup()?

I am working in a situation where the MethodHandles.Lookup class is utilized often. In this situation, is it a wise idea to keep the value of MethodHandles.lookup() between multiple lookups? (Specifically, the only lookup method I am using is…
OllieStanley
  • 712
  • 7
  • 25
2
votes
0 answers

android doesn't check called MethodHandle type in java.lang.invoke.Transformers

This strange behavior allows you to call methods with the wrong type For example: static void test(Integer v) { System.out.println("test " + (Object)v); } MethodHandle test = MethodHandles.lookup().findStatic(/*this class*/, "test", …
Vladimir
  • 21
  • 3
2
votes
2 answers

Can't load hidden class with VarHandle

I am trying to load a class as a hidden class in Java, but run into a VerifyError. The class uses a VarHandle, which has a PolymorphicSignature. I believe the error is implying that the polymorphic call is using the non-hidden class, but it's not…
Carl Mastrangelo
  • 5,970
  • 1
  • 28
  • 37
2
votes
1 answer

LambdaMetaFactory and Private Methods

I would like to use LambdaMetaFactory to efficiently access a private method. public class Foo { private void bar() { // here's what I want to invoke System.out.println("bar!"); } } I know it is not a security violation, because the…
Dragon
  • 173
  • 10
2
votes
2 answers

LambdaMetaFactory boxing / unboxing parameters and return types

I've got the following two methods: public static IGetter createGetterViaMethodname( final Class clazz, final String methodName, final Class fieldType ) …
Marcel
  • 1,509
  • 1
  • 17
  • 39