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
1
vote
1 answer

Get List of MethodHandle from key fields which are private in an object

I have a list of object with many private fields and I would want to group them according to a few key fields that come from a database. The method class is in another package. My object would look like public class MyObject { private String…
Yuxi
  • 11
  • 2
1
vote
1 answer

Local variables in MethodHandle

java.lang.invoke.MethodHandle and friends allow runtime code generation eligible for JIT via a series of combinators. How can I create local variables in MethodHandles? Ideally, the method signature I want looks something like MethodHandle…
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1
vote
2 answers

Create BiConsumer as Field setter without reflection

I try to get the maximum performance in one of my scripts, without doing a major refactor. I spotted method that creates a BiConsumer from a Field using reflection. return (c, v) -> { try { field.set(c, v); } catch (final Throwable…
ST-DDT
  • 2,615
  • 3
  • 30
  • 51
1
vote
1 answer

NoSuchMethodError: No virtual method invoke in MethodHandle, Android

I am trying to use the MethodHandle on Android for a project. At the moment, I'm converting some existing Java code to be compatible with Android, but I've run into a problem. In plain Java 7/8, the following compiles and prints "Jim": import…
Toast Number
  • 33
  • 1
  • 6
1
vote
1 answer

MethodHandle invoke during debugging

I'm trying to wrap my brain around how to use effectively use MethodHandles, one thing that's throwing me off is trying to execute MethodHandles during debugging. Here's some example code which illustrates my issue. public class MetricianTest { …
nickrobison
  • 150
  • 2
  • 14
1
vote
1 answer

Pool capturing lambdas

I grok that for capturing lambdas, there needs to be an object allocated (be it Object[] or some abc$Lambda$xyz type). Is it possible to customize this process anyhow? Let's say I have this code: private void test() { int x = 5; …
Radim Vansa
  • 5,686
  • 2
  • 25
  • 40
1
vote
1 answer

How to change MethodHandle arguments after being inserted?

Suppose you have MethodHandle and some arguments have been specified, how to change those arguments after being set? import static java.lang.invoke.MethodType.*; import static java.lang.invoke.MethodHandles.*; import…
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
1
vote
1 answer

Java MethodHandle; use parameter in multiple locations

In Java, I am able to combine multiple method handle with each its parameters, like this: foo( a, bar( 2, b ) ) ..by using MethodHandles.collectArguments(). The method handle I get can be called like this : myHandle.invokeExact( 5, 6 ); // invokes…
Gui13
  • 12,993
  • 17
  • 57
  • 104
1
vote
1 answer

Why checkcast-instruction is absent for (int)MethodHandle.invokeExact?

I have created 2 simple classes for try Java MethodHandle-API: public class Foo { private static int staticField; public static Object getStaticField() { return staticField; } } another class for invoke method…
Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39
1
vote
2 answers

lookup.findvirtual: classParameter is being added as a method parameter

My problem is the following: I'm trying to use invoke dynamic but I'm having problems with findVirtual and invoke. Class returnTypeClass = Class.forName("com.etc1.foo.FIXML"); MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodType…
dbenor
  • 55
  • 1
  • 8
1
vote
1 answer

MethodHandle InvokeExact parameter

I am confused by method parameters for method handles. I first build a guardwithtest method handle as shown below: public class App { public static void trueTarget(String str, String own, String t){ System.out.println("This is true…
shijie xu
  • 1,975
  • 21
  • 52
1
vote
1 answer

How to compare MethodHandle instances?

How do I compare two MethodHandle instances? I am expecting the function to return true if the handles point to the same method. It doesn't look like the class overrides equals() or hashcode(). Is it safe to use the equality operator (==)?
Gili
  • 86,244
  • 97
  • 390
  • 689
1
vote
2 answers

Is it possible to obtain java.lang.reflection.Method directly from java class file's Constant_Method_REF?

I am using BCEL to transform method byte code to achieve method interceptor with anonymous inner class style, while intercepting the method, I need to process some annotations on the intercepted method. I use BCEL to intercept method access other…
Barry Zhong
  • 470
  • 3
  • 17
1
vote
1 answer

fail to use invokeExact of MethodHandle on dynamic load class object

I have some demo code as follwing. Class myClass = cl.loadClass("com.hp.ac.scriptengine.test." + generateClassName); Object my_obj = myClass.newInstance(); MethodType mt; MethodHandle mh; MethodHandles.Lookup lookup =…
Steven Guan
  • 43
  • 1
  • 5
0
votes
1 answer

How to get around IllegalAccessError when creating a MethodHandle from unnamed module

I am trying to get a MethodHandle for any of the constructors of the com.sun.tools.javac.code.TypeMetadata.Annotations record on JDK 21. Here is the source code of TypeMetadata (from…
msridhar
  • 2,846
  • 4
  • 22
  • 23