I'm trying to write a method which gets an Object and does some logic with the Object's fields.
My method looks like:
public void exampleCast(Object obj) {
Field[] fields = obj.getClass().getFields();
for (Field field : fields) {
if (field.getClass().isArray()) {
/*
HOW CAN I GO OVER THE ARRAY FIELDS , WITHOUT CREATING NEW INSATCNE ?
SOMETHING LIKE:
for (int i = 0; i < array.length; i++) {
array[i] ...
}
*/
} else {
...
...
}
}
}
And an example of objects:
class TBD1 {
public int x;
public int y;
public int[] arrInt = new int[10];
public byte[] arrByte = new byte[10];
}
And the call to my method:
TBD1 tbd1 = new TBD1();
exampleCast(tbd1);
In my method, I don't know how can I get the array values without create new instance (using the newInstance method)
Is it possible? (please see the comment I wrote in my example)
I have read those 2 websites:
- http://jroller.com/eyallupu/entry/two_side_notes_about_arrays
- http://tutorials.jenkov.com/java-reflection/arrays.html
... but I didn't get what I wanted to.