Skip to content

Signature polymorphism

Brian S. O'Neill edited this page Oct 31, 2023 · 2 revisions

The Java indvokedynamic ("indy") framework supports signature polymorphic methods. Most of the methods in the MethodHandle or VarHandle class which declare a variable number of arguments can be invoked as signature polymorphic. This allows the caller to pass the arguments exactly as they should be without being transformed into an Object array. Primitive values can be passed in without being boxed, and the return value can also be primitive.

When the invoke method is called on a Variable which refers to a MethodHandle or VarHandle, a signature polymorphic binding is applied. The expected return type and parameter types define the exact signature. If the parameter types are omitted, they are inferred from the parameter values.

This example shows how to invoke a MethodHandle which has a signature of int <name>(String, int):

Variable mhVar = ... // variable which refers to a MethodHandle
Class[] paramTypes = {String.class, int.class};
Variable resultVar = mhVar.invoke(int.class, "invokeExact", paramTypes, strVar, intVar);
Clone this wiki locally