Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!news.albasani.net!.POSTED!not-for-mail From: Sebastian Newsgroups: comp.lang.java.programmer Subject: Re: Problem with generics and dynamic array copy Date: Fri, 22 Jul 2011 22:37:40 +0200 Organization: albasani.net Lines: 59 Message-ID: References: <98u18jF6l7U1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net zxQEHR7lyfYRqo7vpMGym9enIvwttOtM8zsDrkmnTuL3X4Lj2B1X5MI8xivXgd8Zav94GCao/oGqMb1VibsJjL+kFYHNRPzryWxW2R8SXWC9i1oV8Ggsz6YwHYzXKrCz NNTP-Posting-Date: Fri, 22 Jul 2011 20:37:40 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="A8POY+6UTEiYoU3mxRd9zLRIjawsiGgje+7F93sDkGv2Hl1YtTTpVhClBmuVAggdDwht26YU9lkYQZYm6LK+uf5kGEws/uW3ZuoSV2EHBviwhOiCjz+jWRqhZ+6JRRtl"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: <98u18jF6l7U1@mid.individual.net> Cancel-Lock: sha1:WvK7UMABNL6wqn4McAXmSYdV4BA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6416 Am 22.07.2011 21:27, schrieb Robert Klemme: > On 22.07.2011 20:36, Sebastian wrote: >> Am 22.07.2011 20:23, schrieb markspace: >>> On 7/22/2011 10:30 AM, Sebastian wrote: >>> >>>>> public static final T[] arraycopy( T[] src ) >>>>> { >>>>> Class componentType = src.getClass().getComponentType(); // !!!! >>> >>> >>> RTFM. getComponentType() returns Class, not Class. >>> >>> >>> >>> >>> >>> >>> public Class getComponentType() >>> >>> Returns the Class representing the component type of an array. If this >>> class does not represent an array class this method returns null. >>> >> >> well, yes, but given that src.getClass() must give one the class object >> for arrays with component type T, why is the compiler not smart enough >> to infer that the unknown class parameter in the return value of >> getComponentType() must be T? As a human I can see that, that's why I >> can cast to Class, but I don't believe that I'm smarter than javac... > > The compiler has no idea what the semantics of getComponentType() is. It > could be implemented as > > public Class getComponentType() { return Object.class; } > > and still be conformant to the declaration. Hence it cannot do any > automatic inference based on the fact you know that the array is T[]. > Btw, you can actually pass B[] where B is a subclass of T. > > Since Array.newInstance() accepts Class you should simply use that - > that cast to T[] is needed anyway. > > Of course, even better you scrap your implementation and use > > http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf%28T[],%20int%29 > > > Cheers > > robert > Thanks. Java has a confusing variety of ways to copy an array: - the copyOf and copyOfRange methods in Arrays - System.arraycopy - clone and do a cast I'll go with your recommendation. -- Sebastian