problem with vargs method
Jim Laskey
jlaskey at me.com
Tue Feb 7 08:52:59 PST 2012
Try
MethodType type = MethodType.methodType(Constructor.class, Class[].class);
MethodHandle mh = MethodHandles.lookup().findVirtual(Class.class, "getDeclaredConstructor", type);
MethodType target = MethodType.methodType(void.class, Object.class, Object[].class);
mh = MethodHandles.explicitCastArguments(mh, target);
mh.invokeExact((Object)Class.class, (Object[])new Class[0]);
Cheers,
-- Jim
On 2012-02-07, at 12:37 PM, Jochen Theodorou wrote:
> Am 07.02.2012 17:29, schrieb Jim Laskey:
>>>>> MethodType type = MethodType.methodType(Constructor.class, Class[].class);
>>>>> MethodHandle mh = MethodHandles.lookup().findVirtual(Class.class, "getDeclaredConstructor", type);
>>>>> MethodType target = MethodType.methodType(Object.class, Object.class, Object.class);
>>>>> mh = mh.asType(target);
>>>>> mh.invokeWithArguments(Class.class,new Class[0]);
>>
>> As soon as you mh = mh.asType(target); it is no longer vararg, so it
>> is treating new Class[0] as the second argument cast to Object. If
>> you are trying to type as (Object , Object[]). I think you are going
>> to run into difficulties validating (Class[]) Object[]. You may have
>> to add a wrapper to get what you want, but you could also try using
>> asCollector.
>
> or in other words: I should not use invokeWithArguments for this.
>
> If I wanted to use the same target type... since that is what my call site gives me... and I wanted to use invokeExact instead, how would I have to change the program?
>
> bye blackdrag
>
> --
> Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
> blog: http://blackdragsview.blogspot.com/
> german groovy discussion newsgroup: de.comp.lang.misc
> For Groovy programming sources visit http://groovy-lang.org
>
More information about the mlvm-dev
mailing list