findVirtual problem

John Rose John.Rose at Sun.COM
Tue Aug 18 12:45:20 PDT 2009


On Aug 18, 2009, at 6:07 AM, Raffaello Giulietti wrote:

> Hi Christian,
>
> do an upcast
>
> String result = mh.<String>invoke((Object) "foo");

Yes.  The difference between the two call sites is:
   (Ljava/lang/String;)Ljava/lang/String;
vs.
   (Ljava/lang/Object;)Ljava/lang/String;

The actual (erased) types of the arguments determine the descriptor,  
and hence the required MethodType.

To avoid all such issues, with a possible performance hit, use the  
more lenient calling sequence supplied (as a non-primitive) by  
MethodHandles.invoke:
   String result = (String) MethodHandles.invoke(mh, "foo");

MHs.invoke (which has fixed-arity and varargs versions) supplies all- 
you-can-eat boxing and casting.

This is an issue being discussed by the EG, whether the lenient  
semantics of MHs.invoke should be supported at all times by the  
primitive MH.invoke.  The cost of doing so would be increased  
complexity at every MH invocation site, in many JVMs.  The benefit  
would be ease of programming.

-- John


More information about the mlvm-dev mailing list