aspose file tools*
The moose likes Java in General and the fly likes Array reflection in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Array reflection in Java" Watch "Array reflection in Java" New topic
Author

Array reflection in Java

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Dec 21, 2009
Posts: 102
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

Joined: Sep 15, 2010
Posts: 14
You can get the component type...

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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

Joined: Oct 01, 2001
Posts: 2771
    
  10

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

Joined: Dec 21, 2009
Posts: 102
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

Joined: Oct 27, 2005
Posts: 19556
    
  16

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

Joined: Oct 01, 2001
Posts: 2771
    
  10

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
 
subject: Array reflection in Java
 
Similar Threads
Method.invoke()
Casting arrays
FBN: criteriaFind(String criteria) method; is this ok ?
Testing Array of Primitive Vs Array of Object
How to call a generic method when I only have an Object