Created
October 28, 2013 21:16
-
-
Save vyazelenko/7204874 to your computer and use it in GitHub Desktop.
MethodHandle#invokeExact() invocation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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