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

Why do I get a WrongMethodTypeException on invokeExact even though MethodHandle is OK

I am trying to call an invoke method and there is an error that I cannot explain. I have heard that the Invoke Exact method has to return but it did not work even then. public boolean exec(String id, ChannelHandlerContext ctx, ByteBuf byteBuf) { …
Moldiy
  • 21
  • 5
2
votes
0 answers

Lookup for private classes

I'm in the process of migrating from Java 7 (yipes) to Java 11 for a web application but am having difficulties in migrating some code due to illegal reflective access warnings (it doesn't prevent things from working, but should they ever actually…
adamdc78
  • 1,153
  • 8
  • 18
2
votes
1 answer

Generic method to convert primitive arrays

I have old code with a lot of methods like long[] toLongArray(int[] array) but for many different primitive types configurations (on both sides) and I just wonder if it is possible to make one generic method for this - without losing…
GotoFinal
  • 3,585
  • 2
  • 18
  • 33
2
votes
0 answers

LinkageError when using MethodHandle.publicLookup() in WAR files

we have a problem that we can not explain why it happens. perhaps someone here can us give some hints whats going on in this case. we have tis simple reproucer written that shows the problem package com.example.demo; import…
BigMichi1
  • 307
  • 1
  • 11
2
votes
0 answers

Java 8 Update 51 (sun-jdk-8u51) Runtime Fatal Error after null MethodHandle invokeExact call with 448 System::gc Runnable arguments

Summary I was able to compile an interface with a main method that calls a null MethodHandle's invokeExact method with 448 instances of a Runnable method reference (System::gc). When I ran the interface, a fatal error was then detected by the Java…
srborlongan
  • 4,460
  • 4
  • 26
  • 33
2
votes
1 answer

Why does the return type (force cast) play a critical rule in MethodHandler performance?

I am working on a simple project and need to retrieve a bean property. First I use reflection. Then I did some investigation on invokedynamic and Method Handler for better performance. Although invokeExact is much faster than reflection, invoke is…
DeepNightTwo
  • 4,809
  • 8
  • 46
  • 60
1
vote
1 answer

LambdaMetaFactory with Generic Static Methods?

So I'm creating a library that allows users to pass a Class and collect all static methods with a specific annotation (and other criteria, such as a certain parameter count and types) and convert them into lambda FunctionalInterfaces that my…
1
vote
0 answers

Javassist call PolymorphicSignature method

I am trying to call MethodHandle.invokeExact in a generated Javassist method, but I always get Exception in thread "main" javassist.CannotCompileException: [source error] invokeExact(int) not found in java.lang.invoke.MethodHandle This seems to be…
Jire
  • 9,680
  • 14
  • 52
  • 87
1
vote
1 answer

Why a WrongMethodTypeException from MethodHandle? Is my object type incorrect?

I have encountered a problem while I am trying to switch my event system from reflection to MethodHandle. I am using an event bus (version 3.0.0) by KyoriPowered on Github (https://github.com/KyoriPowered/event). My code is following: public class…
1
vote
0 answers

Transform non-direct MethodHandle to instance of interface

there is a possibility to transform MethodHandle to the instance of LambdaForm via: LambdaMetaFactory.metafactory() and invoke it via invokeinterface call. I know that it is possible to make it for direct MethodHandle. Is it possible to do it for…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
1
vote
1 answer

Fastest way to invoke method handle fields

I'm generating bytecode roughly equivalent to a class like: final class MyCls { final MethodHandle handle1; final MethodHandle handle2; // and so on // This needs to invoke `handle1`, `handle2`, etc. in it somehow final static myMethod()…
Alec
  • 31,829
  • 7
  • 67
  • 114
1
vote
1 answer

MethodHandle cast return type

I try to link methods together through methodhandles, some of them are from generic types. If a function returns a generic type I have to specify Object.class for the MethodType but I see no easy way to convert it back into the generic type…
mknjc
  • 1,577
  • 1
  • 9
  • 9
1
vote
1 answer

Can a MethodHandle constant be used in such a way as to bypass access control?

I am using JDK 15. (I am using ByteBuddy 1.10.16 to generate some classes but it's mostly irrelevant here, I think, except as background information.) In one of these generated classes, I am calling invokeExact() on a MethodHandle constant I've…
Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
1
vote
2 answers

Creating a transformer to add a class from an agent using ByteBuddy

I am trying to load classes from an agent implemented using ByteBuddy. I have a class defined in the agent and want to load it in the target program. here is what my transformer looks like: public class ClassLoaderTransformer implements…
kabinja
  • 135
  • 2
  • 10
1
vote
1 answer

Initialization final and not final static fields in static block

Here I found the following code that shows the difference in perfomance for MethodHandles and Reflection: @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5, time = 1, timeUnit =…
Pavel_K
  • 10,748
  • 13
  • 73
  • 186