I am using reflection and recursion to find the fields within a class. The issue is, an object that I have (PointCloud) has 2 fields in it: an int Timestamp and an array of 4 of type (LidarLayerTag).
When I try to get the field.getName() on the array element, it returns back an list type [Lcom.joy.fb20.dds.LidarLayerTag instead of what I would expect being the non-list version.
Below is the code that I am using to recursively go through the object types. I trimmed out the other types leaving only the portion dealing with arrays since that is where the issue lies. Any ideas as to how to properly deal with getting a single element of the array instead of the list type?
public ArrayList<String> processIDLData(String topicName, String parentFieldName, Integer count)
{
Field[] fieldList = null;
String query = "";
ArrayList<String> layout = new ArrayList<String>();
try
{
fieldList = Class.forName(topicName).getDeclaredFields();
for (Field a : fieldList)
{
if (a.getType().isArray() && ((a.getType().getName().startsWith("["))))
{
// Dealing with primitive arrays
if (a.getType().getName().equals("[F"))
{
errorLog.error("Array of Floats = " + a.getName());
// Arrays of floats
for (int i = 0; i < Array.getLength(a.get(Class.forName(topicName).getConstructor().newInstance())); i++)
{
layout.add(a.getName() + "[" + i + "]");
}
} else if (a.getType().getName().equals("[Ljava.lang.String;"))
{
errorLog.error("Array of Strings = " + a.getName());
// Arrays of Strings
for (int i = 0; i < Array.getLength(a.get(Class.forName(topicName).getConstructor().newInstance())); i++)
{
layout.add(a.getName() + "[" + i + "]");
}
} else if (a.getType() == int[].class || a.getType() == double[].class || a.getType() == short[].class || a.getType() == char[].class || a.getType() == byte[].class
|| (com.rti.dds.util.Enum.class.isAssignableFrom(Class.forName(a.getType().getName().replace(";", "").replace("[L", "")))))
{
errorLog.error("Array of Primitives = " + a.getName());
// Arrays of ints, shorts, bytes, longs, chars, or enums
for (int i = 0; i < Array.getLength(a.get(Class.forName(topicName).getConstructor().newInstance())); i++)
{
layout.add(a.getName() + "[" + i + "]");
}
} else
{
errorLog.error("Array of Objects = " + a.getName() + " " + a.getType().getName());
if (count == null || count == 0)
{
// Arrays of objects
for (int i = 0; i < Array.getLength(a.get(Class.forName(topicName).getConstructor().newInstance())); i++)
{
layout.addAll(processIDLData(a.getType().getName(), a.getName(), i));
}
} else
{
for (int i = 0; i < Array.getLength(a.get(Class.forName(topicName).getConstructor().newInstance())); i++)
{
layout.addAll(processIDLData(a.getType().getName(), a.getName() + "[" + count + "]", i));
}
}
}
}
return layout;
}
Aucun commentaire:
Enregistrer un commentaire