I am trying to implement "duck typing" with invokedynamic
in JVM7. I created two different classes, both of them have the method greet()
which returns String
. I randomly select between one of them, store the instance on the stack and call invokedynamic
(using ASM).
I wonder if I can access arguments in bootstrap method. Because I cannot return appropriate CallSite
with target
without knowing which object is on the stack and which of the greet()
methods should be returned.
If my approach is wrong, what is the correct approach?
I will answer myself.
CallSite
with your own static method (for example lookup(CallSite cs, Object[] args)
args[0]
is the receiver, the rest of them are arguments. Do whatever you wish in the lookup method.If you are interested in Inline Caching (I was), have a look at these two links: