Array construction using Reflection

K

Karl Koster

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.
 
G

Gordon Beaton

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
 
S

Steve W. Jackson

: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 =
 
O

Oscar kind

Karl Koster said:
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
 
H

Harish Madhavan K

How about using Collection.toArray(new TheClass[0]);
This can be type-casted to array of TheClass.
 
M

Michael Borgwardt

Harish said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Trouble with reflection 0
Problem with reflection in C# 4
Reflection 2
How do I push into an array using an event listener? 5
reflection 2
Help with array 4
reflection 2
Randomly remove elements from array 1

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top