Problem with generics and dynamic array copy

Discussion in 'Java' started by Sebastian, Jul 22, 2011.

  1. Sebastian

    Sebastian Guest

    Hello there,

    how can I make the compile error go away? The marked line is wrong:
    Type mismatch: cannot convert from Class<capture#2-of ?> to Class<T>

    /**
    * Copies the specified array to a new array with the same component
    * type and length as the given array.
    * @param <T> the type of the array elements
    * @param src the array to be copied
    * @return the copy
    */
    public static final <T> T[] arraycopy( T[] src )
    {
    Class<T> componentType = src.getClass().getComponentType(); // !!!!
    @SuppressWarnings("unchecked")
    T[] dest = (T[]) Array.newInstance( componentType, src.length );
    System.arraycopy( src, 0, dest, 0, src.length );
    return dest;
    }

    I'd be grateful for a hint.
    -- Sebastian
     
    Sebastian, Jul 22, 2011
    #1
    1. Advertising

  2. Sebastian

    Sebastian Guest

    Am 22.07.2011 19:26, schrieb Sebastian:
    > Hello there,
    >
    > how can I make the compile error go away? The marked line is wrong:
    > Type mismatch: cannot convert from Class<capture#2-of ?> to Class<T>
    >
    > /**
    > * Copies the specified array to a new array with the same component
    > * type and length as the given array.
    > * @param <T> the type of the array elements
    > * @param src the array to be copied
    > * @return the copy
    > */
    > public static final <T> T[] arraycopy( T[] src )
    > {
    > Class<T> componentType = src.getClass().getComponentType(); // !!!!
    > @SuppressWarnings("unchecked")
    > T[] dest = (T[]) Array.newInstance( componentType, src.length );
    > System.arraycopy( src, 0, dest, 0, src.length );
    > return dest;
    > }
    >
    > I'd be grateful for a hint.
    > -- Sebastian


    to be mor exact: of course I could just cast componentType to Class<T>.
    But why should that be necessary in view of the declaration of src?
    Is the thinking behind the method wrong?

    -- Sebastian
     
    Sebastian, Jul 22, 2011
    #2
    1. Advertising

  3. Sebastian

    markspace Guest

    On 7/22/2011 10:30 AM, Sebastian wrote:

    >> public static final <T> T[] arraycopy( T[] src )
    >> {
    >> Class<T> componentType = src.getClass().getComponentType(); // !!!!



    RTFM. getComponentType() returns Class<?>, not Class<T>.

    <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29>

    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.
     
    markspace, Jul 22, 2011
    #3
  4. Sebastian

    Sebastian Guest

    Am 22.07.2011 20:23, schrieb markspace:
    > On 7/22/2011 10:30 AM, Sebastian wrote:
    >
    >>> public static final <T> T[] arraycopy( T[] src )
    >>> {
    >>> Class<T> componentType = src.getClass().getComponentType(); // !!!!

    >
    >
    > RTFM. getComponentType() returns Class<?>, not Class<T>.
    >
    > <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29>
    >
    >
    > 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<T>, but I don't believe that I'm smarter than javac...

    -- Sebastian
     
    Sebastian, Jul 22, 2011
    #4
  5. 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> T[] arraycopy( T[] src )
    >>>> {
    >>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!

    >>
    >>
    >> RTFM. getComponentType() returns Class<?>, not Class<T>.
    >>
    >> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29>
    >>
    >>
    >>
    >> 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<T>, 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(T[],%20int%29

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jul 22, 2011
    #5
  6. Sebastian

    lewbloch Guest

    On Jul 22, 12:27 pm, Robert Klemme <> wrote:
    > 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> T[] arraycopy( T[] src )
    > >>>> {
    > >>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!

    >
    > >> RTFM. getComponentType() returns Class<?>, not Class<T>.

    >
    > >> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#get....>

    >
    > >> 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<T>, 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#co...[],%20int%29
    >


    I'd say _best_ yet. In general, unless it really, really doesn't do
    what you need, stick to the standard API instead of reinventing it.

    This does, of course, require that you be aware of the API, but things
    like 'java.util.Arrays' had better be part of your standard vocabulary
    in any event.

    Otherwise you have some upgrading to do on your skills.

    --
    Lew
     
    lewbloch, Jul 22, 2011
    #6
  7. Sebastian

    Sebastian Guest

    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> T[] arraycopy( T[] src )
    >>>>> {
    >>>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!
    >>>
    >>>
    >>> RTFM. getComponentType() returns Class<?>, not Class<T>.
    >>>
    >>> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29>
    >>>
    >>>
    >>>
    >>>
    >>> 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<T>, 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(T[],%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
     
    Sebastian, Jul 22, 2011
    #7
  8. Sebastian

    lewbloch Guest

    On Jul 22, 1:37 pm, Sebastian <> wrote:
    > 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> T[] arraycopy( T[] src )
    > >>>>> {
    > >>>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!

    >
    > >>> RTFM. getComponentType() returns Class<?>, not Class<T>.

    >
    > >>> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#get...>

    >
    > >>> 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<T>, 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#co...[],%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.


    'clone()' and cast is the least optimal. 'System.arraycopy()' is the
    "old" way. 'Arrays.copyOf()' is the "new" way and most typesafe, and
    is somewhat easier to use than the others.

    --
    Lew
     
    lewbloch, Jul 22, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    6,033
    John C. Bollinger
    May 20, 2005
  2. Alex
    Replies:
    2
    Views:
    1,254
  3. Replies:
    26
    Views:
    2,140
    Roland Pibinger
    Sep 1, 2006
  4. Kaiser S.
    Replies:
    3
    Views:
    1,605
    Ingo R. Homann
    May 3, 2007
  5. Soul
    Replies:
    0
    Views:
    532
Loading...

Share This Page