Skip to content

Instantly share code, notes, and snippets.

@vyazelenko
Created October 28, 2013 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyazelenko/7204874 to your computer and use it in GitHub Desktop.
Save vyazelenko/7204874 to your computer and use it in GitHub Desktop.
MethodHandle#invokeExact() invocation example
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
class Base {
int x = 13;
}
class Derived extends Base {
public long m;
}
public class TestMH {
public static void main(String[] args) throws Throwable {
MethodHandle mh = MethodHandles.lookup().findGetter(Base.class, "x", int.class);
Derived obj = new Derived();
// mh.invokeExact(obj); => throws WrongMethodTypeException:expected(Base)int but found (Derived)void
// mh.invokeExact((Base) obj); => throws WrongMethodTypeException:expected(Base)int but found (Base)void
int value = (int) mh.invokeExact((Base) obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment