|
-
September 26th, 2009, 05:06 PM
#1
How do i check if a Field type of a variable is an array type?
So yeah, im using reflection, and im getting every variable within my class. So I start with Field f being equal to some field i have, then depending on whether the variable is an array or normal type, i would like to print it. The reason why i want to check this is because if its just a normal type, i can say object.toString() and be all set, but if its an array, i need to use Arrays.toString(object); If i left out any viable info you may need, let me know.
This is the idea of what i want to do, but it wont compile, any ideas? an example of an "array" field would be something like public String[] = {"this", "array"}; public int[] = {1, 3, 5}; ...
StringBuilder b = new StringBuilder();
if(f.getType() instanceof Array){
b.append(Arrays.toString((Object[])f.get(target)));
}
else{
b.append(f.get( target ));
}
it says
1 error found:
Error: C:\CS stuff\Prog2\MyReflectionPrinter.java:86: inconvertible types
found : java.lang.Class<capture#975 of ?>
required: java.lang.reflect.Array
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|