473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

reflect: is a Field an Array?

Hello!

I'm going through the fields of a class one at a time and need to handle
differently depending on whether they are arrays or scalars.

What's the right way to make the distinction? The snippet below fails to
detect arrays :(

Thanks!

-mi

import java.lang.reflect.*;
.....
for (Field field : getClass().getFields()) {
Type type = field.getGenericType();

System.err.println("Type of " + field + " is " + type + type.getClass());
if (type instanceof GenericArrayType)
System.err.println(field + " is an array!");
else
System.err.println(field + " is not an array");
}
Dec 12 '07 #1
2 2761
Mikhail Teterin wrote:
Hello!

I'm going through the fields of a class one at a time and need to handle
differently depending on whether they are arrays or scalars.

What's the right way to make the distinction? The snippet below fails to
detect arrays :(

Thanks!

-mi

import java.lang.reflect.*;
....
for (Field field : getClass().getFields()) {
Type type = field.getGenericType();

System.err.println("Type of " + field + " is " + type + type.getClass());
if (type instanceof GenericArrayType)
System.err.println(field + " is an array!");
else
System.err.println(field + " is not an array");
}
robert-desktop:~ $ cat Test.java
import java.lang.reflect.*;

public class Test {
private int nonArray;
private int array[];

public Test() {
}

public static void main(String args[]) throws Exception {
Class c = Test.class;
for (Field f : c.getDeclaredFields()) {
System.out.println("Field: " + f + " Is array: " +
f.getType().isArray());
}
}
}
robert-desktop:~ $ javac Test.java
robert-desktop:~ $ java Test
Field: private int Test.nonArray Is array: false
Field: private int[] Test.array Is array: true
robert-desktop:~ $
Dec 12 '07 #2
Robert Larsen wrote:
f.getType().isArray()
Many thanks!

-mi
Dec 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: Cheng Mo | last post by:
I just swtiched my working language from java to C++. In java, there is one mechanism called reflect which enable us to have knowledge of internals of a given class at run-time. That's one basic...
5
by: John Bailo | last post by:
Is it possible to Reflect a variable inside a method? I can Reflect a class field using GetField, but can I reflect a variable/field from inside member? For example, I want to Reflect samp...
11
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I...
2
by: MLH | last post by:
I have a form based on a table. The table has a Yes/No field. Its an A97 table. The form has a button that updates the value in the control from No to Yes for the record currently being edited. ...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
0
by: josh.23.french | last post by:
Here's the code i have: $db = array(); //main array $db = array(); //table `main` $db = array('id'=>0, 'username'=>'joshfrench','userpass'=>'password','userlevel'=>'admin'); //row $db =...
1
by: Latha | last post by:
I have bound a array of objects to datagridview in vb.net. If I add new objects to array, that doesn't reflect in datagridview. What I have to do? Thanks, Latha.P
1
DebadattaMishra
by: DebadattaMishra | last post by:
Introduction In case of rich applications, you must have observed that a text field behaves like a dynamic combo box. When the user enters some characters in the text field, a popup will come up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.