0

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?

I wanted something a bit more elegant than just replacing the "[L" at the beginning of the string that I'm sending through the recursion but I can do that if worst comes to worst.

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;
    }
1
  • You get paid by the line I am guessing, eh? haha Commented Apr 24, 2018 at 14:43

1 Answer 1

1

For that there is the Class.getComponentType():

Field a; ...
if (a.getType().isArray()) {
    Class<?> elementType = a.getComponentType();
    if (elementType == float.class) { ... // float[]
        ... elementType.getSimpleName() ...;
    }
}

Recurse on the component type and do something with the result, adding "[]" or such.

Sign up to request clarification or add additional context in comments.

1 Comment

Flawless. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.