Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Steven Simpson Newsgroups: comp.lang.java.programmer Subject: Re: methodhandle invoke with arguments cast error Date: Sat, 07 Feb 2015 18:25:37 +0000 Organization: A noiseless patient Spider Lines: 34 Message-ID: <1u1iqb-ihc.ln1@s.simpson148.btinternet.com> References: <54d3d026$0$281$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="8418829f0c37f1486357e21dd5aac233"; logging-data="10350"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GVFIy0nLXSPwvtD4RodrSdJQCzMmAleE=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 In-Reply-To: Cancel-Lock: sha1:h6+k+efgjgZhJY6ku2TYE8L9Tc8= Xref: csiph.com comp.lang.java.programmer:31073 On 06/02/15 05:41, Philipp Kraus wrote: > On 2015-02-05 20:18:37 +0000, Arne Vajhøj said: > >> On 2/5/2015 2:57 PM, Philipp Kraus wrote: >>> I use a method handle with this code: >>> >>> List args = new LinkedList(); >>> args.add( my object ); >>> args.add( new Double(5) ); >>> >>> MethodHandle h = getMethodHandle_with_unreflect >>> h.invokedWithArguments( args ); >>> >>> The method, which is called shows >>> >>> public void doit( Integer x ) >>> >>> so I get a cast error: Cannot cast java.lang.Double to >>> java.lang.Integer >>> How can I do an explicit cast on the invokeMethodWithArguments? >> >> Stuff an Integer into the args List?? > > Sorry, I forgot the information, that the list will be build > dynamically, so into the list can be pushed any numeric typ. Is the method handle available when building the list? You could get its MethodType, and scan over the parameter types for boxed primitives. If the corresponding argument is of type Number, call the appropriate XXXValue() method and push the result into the list. -- ss at comp dot lancs dot ac dot uk