简体   繁体   English

Java反射处理数组

[英]Java Reflection Dealing with Arrays

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). 问题是,我拥有的对象(PointCloud)中有2个字段:一个int时间戳和一个类型为4的数组(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. 当我尝试在array元素上获取field.getName()时,它返回一个列表类型[Lcom.joy.fb20.dds.LidarLayerTag,而不是我期望的非列表版本。

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. 我想要的东西比仅在通过递归发送的字符串的开头替换“ [L]”更优雅,但是如果最坏的情况发生了,我可以这样做。

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;
    }

For that there is the Class.getComponentType() : 为此,存在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. 递归组件类型,并对结果做一些事情,添加"[]"等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM