在java中反射数组 [英] Reflection of an array in java

查看:202
本文介绍了在java中反射数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java很新,遇到了一个问题。我的任务是创建一个类,它包含一个方法write(Object obj),它将对象的类型以及所有属性的名称和类型写入文件。涉及到递归,因为对象可能将对象/对象数组作为属性。



这是代码:

  public void write(Object obj)throws Exception {

if(obj == null)
{
out.close();
返回;
}

类c = obj.getClass();
D类;
Field fields_c [] = c.getDeclaredFields();
System.out.println(class_name:+ c.getName());

int i,j;
String tab = new String();


for(i = 0; i< indent_level; i ++)
{
tab = tab +\t;
}

out.write(tab +class_name:+ c.getName()+\\\
);对于(i = 0; i< fields_c.length; i ++){
System.out.println(field name:+ fields_c [i] .getName()+)的

);

c = fields_c [i] .getType();
fields_c [i] .setAccessible(true);

if(c.isPrimitive()){
out.write(\t+ tab +field_name:+ c.toString()+\\\
) ;
}
else if(c.isArray()){
System.out.println(array of type with types of type:+ c.getComponentType());

for(j = 0; j< Array.getLength(c); j ++)
{
d = Array.get(c,j).getClass();
indent_level = indent_level + 1;
this.write(d);
indent_level = indent_level - 1;
}
}
else
{
System.out.println(field is not primitive of type:+ c.getName());
对象foo = fields_c [i] .get(obj);
indent_level = indent_level + 1;
this.write(foo);
indent_level = indent_level - 1;
}
}
}

如果我打电话该方法并给出具有数组属性的Object;所有属性,直到数组正确写入输出文件。
异常是java.lang.IllegalArgumentException:Argument不是数组。

解决方案

d = Array.get(c,j).getClass(); c 类型为java.lang.Class,但是您应该将其更改为 d = Array.get(fields_c [i] .get(obj),j)



并使用 c#getComponentType 获取数组的类型。


I'm fairly new to java and have come across a problem. My task is to create a class which contains a method write(Object obj) that writes the type of the object as well as the name and type of all attributes into a file. Recursion is involved since the object may have objects/arrays of objects as attributes.

Here is the code:

    public void write(Object obj) throws Exception {

    if(obj == null)
    {
        out.close();
        return;
    }

    Class c = obj.getClass();
    Class d;
    Field fields_c[] = c.getDeclaredFields();
    System.out.println("class_name:" + c.getName());

    int i, j;
    String tab = new String("");


    for(i = 0; i < indent_level; i++)
    {
        tab = tab + "\t";
    }

    out.write(tab + "class_name:" + c.getName() + "\n");

    for(i = 0; i < fields_c.length; i++) {
        System.out.println("field name: " + fields_c[i].getName() + " ");

        c = fields_c[i].getType();
        fields_c[i].setAccessible(true);

        if(c.isPrimitive()) {
            out.write("\t" + tab + "field_name:" + c.toString() + "\n");
        }
        else if(c.isArray()) {
            System.out.println("field of type array with elements of type:" + c.getComponentType());

            for(j = 0; j < Array.getLength(c); j++)
            {
                d = Array.get(c, j).getClass();
                indent_level = indent_level + 1;
                this.write(d);
                indent_level = indent_level - 1;            
            }
        }
        else
        {
            System.out.println("field is not primitive of type:" + c.getName());
            Object foo = fields_c[i].get(obj);
            indent_level = indent_level + 1;
            this.write(foo);
            indent_level = indent_level - 1;
        }
    }
}

An exception arises if I call the method and give an Object that has an array attribute; all attributes until the array are written properly to the output file. The exception is "java.lang.IllegalArgumentException: Argument is not an array".

解决方案

In d = Array.get(c, j).getClass(); c is of type java.lang.Class, but an array is expected.

You should change it to d = Array.get(fields_c[i].get(obj), j) and use c#getComponentType for get the type of the array.

这篇关于在java中反射数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆