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
0
votes
1 answer

Java 8 | Using MethodHandle#invokeExact on fields dynamically

My goal is to create a MethodHandle that, by passing a class and a field name, reads that field with a MethodHandle getter and returns the value. With this method I wanted to return an arbitrary object: return new…
0
votes
1 answer

Constants that use other constants in Java

In my class, I need both an SLF4J Logger with the class as the parameter, as well as a simple class name. Both the logger and CLASS_NAME constants use MethodHandles.lookup(). I was wondering if it makes more sense/is more efficient to have something…
moesyzlack23
  • 105
  • 1
  • 11
0
votes
1 answer

Transform Method from file into LambdaExpresion

I'm trying to transform a Method (read from a file) into a lambda expression, so I can measure the time needed to execute that Method ignoring the slow Method.invoke(...) function. I've been trying to accomplish my objective using LambdaMetafactory,…
0
votes
0 answers

Why MethodHandle is slow than Reflection in JDK 1.8?

I have two performance tests using JMH. The code is very easy,one is using Java Reflection,Another is using MethodHandle(Introduced in JDK1.7),By The way,isEmptyMethod and MH_isEmpty is declared as static final,like this: private static final…
0
votes
0 answers

Call Arbitrary Constructors with LambdaMetaFactory

Lets say I'm writing a deserialization utility, and would like to have performance better than Reflection. I have a Map, where the MethodHandle is the desired constructor for the given Class. When a user calls T…
kantianethics
  • 671
  • 5
  • 21
0
votes
1 answer

How to chain MethodHandle invocations?

I have a problem where I have multiple methods with identical signatures (all returning void). I want to be able to combine the methodHandles for those methods to obtain a methodHandle that will invoke each method in turn. The only way I have…
gregw
  • 2,354
  • 20
  • 21
0
votes
0 answers

MethodHandle slower than Reflection?

I have run a simple test. public static void main (String[] args) throws java.lang.Throwable { //Field field = Unsafe.class.getDeclaredField("theUnsafe"); //field.setAccessible(true); //Unsafe unsafe = (Unsafe)…
Bernard
  • 304
  • 2
  • 11
0
votes
2 answers

using java.lang.invoke.MethodHandle in clojure

I'm following a tutorial here: https://www.baeldung.com/java-method-handles In clojure, I've got a simple example: (import (java.lang.invoke MethodHandles MethodHandles$Lookup MethodType …
zcaudate
  • 13,998
  • 7
  • 64
  • 124
0
votes
1 answer

Java MethodHandle api seems to produce incorrect type

Given this code: MethodType mt = MethodType.methodType(void.class, DomainObject.class); NOOP_METHOD = RULE_METHOD_LOOKUP.findVirtual(RulesEngine.class, "noOpRule", mt); the NOOP_METHOD produced is MethodHandle(RulesEngine,DomainObject)void Why is…
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
0
votes
2 answers

Using MethodHandles to call a method on of class that isn't known at compile time (without reflection)

I need to call the getName() method of a Person class, without knowing the Person class as compilation time and by using a MethodHandle (instead of normal reflection). So I'd want this code to work (this code cannot change): MyGetterAccessor myGA =…
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
0
votes
0 answers

ScriptEngineManager.getEngineByName("nashorn") fails because of java.lang.invoke.WrongMethodTypeException

I am unable to create ScriptEngine for nashorn in Java 8 (IBM J9JVM). In the following code, the engine is null. private static void runOther(String[] args){ ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine engine…
shijie xu
  • 1,975
  • 21
  • 52
0
votes
0 answers

java.lang.NoClassDefFoundError: jdk.nashorn.internal.scripts.JO28P0

I finished a library for Java and I used it to replace java.lang.invoke package (i.e., add it to the boot class loader prior to normal JVM library). In the package, it dynamically generates bytecodes for method handles. Thanks if you have any…
shijie xu
  • 1,975
  • 21
  • 52
0
votes
1 answer

MethodHandles.filterArguments() example gives an exception

With Java 1.8.0_92, on trying to run the example of MethodHandles.filterArguments(), the following exception is thrown: Exception in thread "main" java.lang.invoke.WrongMethodTypeException: expected (String,String)String but found…
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
0
votes
1 answer

Java Method Handles: propagate unbound arguments across functions

I would like to create a method handle that allows me to pass as argument a value that will be bound to a placeholder way down the method handle tree. Figure, something like this: f(x) = plus( minus( x, 2), 3) where x is passed at invoke, and 2 and…
Gui13
  • 12,993
  • 17
  • 57
  • 104
0
votes
1 answer

How can I use method handles with JDK 6?

Strange situation here. I need to invoke the same class instance method many many times (1000/second range) without the ability to import or build against the library needed. I am trying to call a method within my JEE project that uses a class from…
Mike
  • 526
  • 7
  • 18