problem with vargs method
Jim Laskey
jlaskey at me.com
Tue Feb 7 09:29:31 PST 2012
Worked okay for me. So must be addressed in a later release. :-/
On 2012-02-07, at 1:17 PM, Jochen Theodorou wrote:
>
> That will produce a
>
> java.lang.invoke.WrongMethodTypeException: (Ljava/lang/Object;[Ljava/lang/Object;)V cannot be called without a receiver argument as ([Ljava/lang/Object;)Ljava/lang/Object;
>
> imho casting to Object and Object[] in
>
> mh.invokeExact((Object)Class.class, (Object[])new Class[0]);
>
> is without effect, since invokeExact is a vargs method an everything will just be put into an Object[] anyway. We had not a compiler change for this kind of thing, had we?
>
> bye Jochen
>
> Am 07.02.2012 17:52, schrieb Jim Laskey:
>> 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
>>>
>>
>
>
> --
> 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