problem with vargs method
Jim Laskey (Oracle)
james.laskey at oracle.com
Tue Feb 7 08:31:58 PST 2012
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.
Cheers,
-- Jim
On 2012-02-07, at 12:14 PM, Jochen Theodorou wrote:
> The problem can be easily reproduced using this:
>> 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]);
>
> can someone tell me the mistake?
>
> bye Jochen
>
> Am 07.02.2012 17:04, schrieb Jochen Theodorou:
>> Hi all,
>>
>> maybe someone can explain to me why method handles behave this way in my
>> case.
>>
>> Bascially I have a handle for Class#getDeclaredConstructor(Class...) I
>> created via unreflect.
>>
>> http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredConstructor%28java.lang.Class...%29
>>
>> this handle is then created as a AdapterMethodHandle$AsVargsCollector.
>> My targetType is Object(Object,Object) and the arguments to the call are
>> the Class, and an empty Class[]. So the next steps are asType with the
>> target type, adding a guard with catch, then my guards for the arguments
>> and last I do an invokeWithArguments.
>>
>> What I get now is a ClassCastException with the message required class
>> java.lang.Class but encountered class [Ljava.lang.Class;
>>
>> The guards are unrelated to the problem, since it happens without them
>> as well.
>>
>> Now can anyone explain me why I get that exception? It doesn't really
>> make sense to me atm.
>>
>> bye Jochen
>>
>
>
> --
> 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
>
> _______________________________________________
> mlvm-dev mailing list
> mlvm-dev at openjdk.java.net
> http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev
More information about the mlvm-dev
mailing list