Array construction using Reflection

Discussion in 'Java' started by Karl Koster, May 18, 2004.

  1. Karl Koster

    Karl Koster Guest

    I am trying to construct a method that will take a class and a
    collection of instances of that class and construct an array of that
    class using reflection.

    I cannot just use to the .toArray() method of the collection because
    this returns an array of Object that cannot be cast to an array of the
    class that was passed to the method.

    I have gotten to the point of loading the appropriate array class
    name. (i.e. "[" followed by the appropriate class information)
    However, there are no constructors defined for these classes when
    using reflection. It would have been nice if a constructor with a
    single integer paramater where there, but it is not.

    I have been batting this around for a few hours and have not found any
    solution. If anyone is aware of a way to do this, I would greatly
    appreciate knowing how it was accomplished.
     
    Karl Koster, May 18, 2004
    #1
    1. Advertising

  2. java.lang.reflect.Array.newInstance()
     
    Michael Borgwardt, May 18, 2004
    #2
    1. Advertising

  3. On 18 May 2004 06:05:35 -0700, Karl Koster wrote:
    > I am trying to construct a method that will take a class and a
    > collection of instances of that class and construct an array of that
    > class using reflection.
    >
    > I cannot just use to the .toArray() method of the collection because
    > this returns an array of Object that cannot be cast to an array of the
    > class that was passed to the method.
    >
    > I have gotten to the point of loading the appropriate array class
    > name. (i.e. "[" followed by the appropriate class information)
    > However, there are no constructors defined for these classes when
    > using reflection. It would have been nice if a constructor with a
    > single integer paramater where there, but it is not.


    I believe that java.lang.reflect.Array.newInstance() does exactly what
    you are asking for. You specify an element type and a length, and
    receive an empty array of that type.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, May 18, 2004
    #3
  4. In article <>,
    (Karl Koster) wrote:

    >:I am trying to construct a method that will take a class and a
    >:collection of instances of that class and construct an array of that
    >:class using reflection.
    >:
    >:I cannot just use to the .toArray() method of the collection because
    >:this returns an array of Object that cannot be cast to an array of the
    >:class that was passed to the method.
    >:
    >:I have gotten to the point of loading the appropriate array class
    >:name. (i.e. "[" followed by the appropriate class information)
    >:However, there are no constructors defined for these classes when
    >:using reflection. It would have been nice if a constructor with a
    >:single integer paramater where there, but it is not.
    >:
    >:I have been batting this around for a few hours and have not found any
    >:solution. If anyone is aware of a way to do this, I would greatly
    >:appreciate knowing how it was accomplished.


    Casting the resulting array from a toArray() to some specified type of
    array isn't as important as being able to retrieve the individual items
    in that array by their type. Why can't you simply use the toArray()
    call and then cast each object retrieved to that specified type? After
    all, if that's what they are, they already know that, as can be
    evidenced by using each one's getClass().getName() call.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, May 18, 2004
    #4
  5. Karl Koster

    Oscar kind Guest

    Karl Koster <> wrote:
    > I am trying to construct a method that will take a class and a
    > collection of instances of that class and construct an array of that
    > class using reflection.
    >
    > I cannot just use to the .toArray() method of the collection because
    > this returns an array of Object that cannot be cast to an array of the
    > class that was passed to the method.


    True, but you can use it's brother:

    Foobar[] array = collection.toArray(new Foobar[]{});


    kind regards,
    Oscar

    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, May 18, 2004
    #5
  6. How about using Collection.toArray(new TheClass[0]);
    This can be type-casted to array of TheClass.


    "Karl Koster" <> wrote in message
    news:...
    > I am trying to construct a method that will take a class and a
    > collection of instances of that class and construct an array of that
    > class using reflection.
    >
    > I cannot just use to the .toArray() method of the collection because
    > this returns an array of Object that cannot be cast to an array of the
    > class that was passed to the method.
    >
    > I have gotten to the point of loading the appropriate array class
    > name. (i.e. "[" followed by the appropriate class information)
    > However, there are no constructors defined for these classes when
    > using reflection. It would have been nice if a constructor with a
    > single integer paramater where there, but it is not.
    >
    > I have been batting this around for a few hours and have not found any
    > solution. If anyone is aware of a way to do this, I would greatly
    > appreciate knowing how it was accomplished.
     
    Harish Madhavan K, May 18, 2004
    #6
  7. Harish Madhavan K wrote:

    > How about using Collection.toArray(new TheClass[0]);


    Impossible in this case because Karl wants the code to act generically
    and construct an array from instance of *any* class.
     
    Michael Borgwardt, May 18, 2004
    #7
    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. MKoool
    Replies:
    3
    Views:
    330
    Steven D'Aprano
    Jan 1, 2006
  2. Ook
    Replies:
    10
    Views:
    566
  3. klucar
    Replies:
    3
    Views:
    481
    klucar
    Feb 17, 2006
  4. cantide5ga
    Replies:
    7
    Views:
    395
    cantide5ga
    Oct 23, 2007
  5. David Tran
    Replies:
    6
    Views:
    811
    Eric Hodel
    Mar 17, 2005
Loading...

Share This Page