• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array reflection in Java

 
Theodore David Williams
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Newbie to reflection w/ array so please be nice

I am working w/ code that returns an Object. That object is an array of something.

i.e.


How do I use these 'o' objects to get elements out of the array? Can I determine type dynamically? i.e. Without knowing what the getArray method will really return how can I infer type to do stuff with my array?

Thanks.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

using reflections is considered a hint for a bad architecture of your program, so unless there is good reason and you really know what you're doing, don't use it.

If you can't help it and still have to use reflections:

The getClass() method of Object provide useful method you might want to check into.
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're always nice! Sebastian has a good point about reflections. However, it's an interesting theoretical question. If the arrays were of object types, then you could just cast to (Object []), but since they're primitives, the best I could come up with is something like:



It seems like there must be a more elegant way to do that.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:We're always nice! Sebastian has a good point about reflections. However, it's an interesting theoretical question. If the arrays were of object types, then you could just cast to (Object []), but since they're primitives, the best I could come up with is something like:



It seems like there must be a more elegant way to do that.



That is soooo bad
 
Theodore David Williams
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can on agree not to use reflection, but in this case I do not control that code so I am stuck with it.

As an aside what is a good solution without reflection (since I cannot think of an idea)?

I.E. how do you infer type? I know that I could have a collection of interfaces and use polymorphism to drive behavior. But lets say I am storing data and that data can be ints, floats, strings, etc. How do I return the correct type based on the data I am asking for.

 
Mos Jeff
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the component type...

 
Theodore David Williams
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you can get the component type but you are still stuck with
if (componentType == "some class) {
} else if (componentType == "some other class" {
}

So again is there a more elegant solution not using reflection?
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theodore David Williams wrote:I can on agree not to use reflection, but in this case I do not control that code so I am stuck with it.


OK, let's back up a step here. What exactly are you stuck with? You have "something", an instance of a class that only exposes its member data via a getArray(fieldName) method, and the arrays it returns could possibly be arrays of primitives? That seems like a tragically bad interface. Is that really all you have to work with?

As I said, if you knew the arrays were arrays of Objects, even the wrappers like Integer or Byte, then you could cast to an Object array and at least access the individual elements, though of course you wouldn't be able to do much else with them. For primitive arrays though, there's just not a generic way to handle them except as Objects.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha.....yes I really am working with an interface that returns an Object. That object is an array of primitives. The suggested use is something like:



I know this really sucks. It is not a third party api, so I may be able to make/suggest some changes as this is very old (and bad code). However I am not really sure how to tackle this. If I am going to suggest some changes here I would need a good solution and know what I am talking about, however I am feeling coders block ;)

I.E. in general if my code knew behind the scenes that:
FieldA was a Double
FieldB was a String
FieldC was an Integer

how can the getData(SomeField f) method return the correct type?

The only thing I can think of is:


BIG however is that I still need to know the type each field is when I get the data. But I am not really sure there is a magic bullet here, i.e. when you query a DB using JDBC you have to specify getDouble(), getInt()...

Hmmmm, what about a generic on the Field class, would that work?
[/code]
Field<Double> fieldA = new Field<Double>();


I.E. could I somehow use the fact that fieldA was using a generic type of Double to ensure the I get a List of Doubles back???

Any more insight would be of great help!!!

Thanks

 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:We're always nice! Sebastian has a good point about reflections. However, it's an interesting theoretical question. If the arrays were of object types, then you could just cast to (Object []), but since they're primitives, the best I could come up with is something like:



It seems like there must be a more elegant way to do that.

Or just do it the proper way:
Just like you can use String.class, you can also use String[].class. And because any int[], byte[], etc is an object you can also use int[].class, byte[].class, etc. You can't do int.class but you can use Integer.TYPE for that. Integer.TYPE is very different from Integer.class; the latter is the Class object for Integer, the former the Class object for int.
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I'm taking a lot of grief for something I only intended as a proof of concept. However, it's interesting to know that int[].class is valid syntax. I'd never really thought about that before.

Still, while Rob's suggestion is a slight improvement, the java.lang.reflect.Array is really what I was trying to remember, but couldn't come up with. That will let you iterate through your array of primitives, and even get some useful information out of it. The getDouble() method, for example, will work on any array of primitives type, except boolean[] ... as long as you're not too picky about integers being converted to floating point values.

If you know behind the scenes what type you're getting back, you can just cast it to that type, and do whatever you want with it. There would be no need for reflection in that case. You could use generics, but that would only save you the need to cast.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic