Java方法描述了需要改进的任何Object的所有字段

时间:2015-10-02 15:01:53

标签: java

同事们,这是我的方法,它描述了任何对象的所有属性(字段):

public static void describeObjectFields(Object o) {

    System.out.println("Object description: " + o.getClass().getCanonicalName() + ": ");

    for (Field field: o.getClass().getDeclaredFields()) {

        field.setAccessible(true);
        String name = field.getName();

        Object value;
        try {
            value = field.get(o);
            /*
                if (value.toString().contains("com.comp."))
                {
                    LOG.info("There is an Object in the described class " + value.getClass().getCanonicalName() );
                    System.out.println(value.getClass().getFields().toString() );
                    for (Field field1 :  value.getClass().getDeclaredFields()) {
                        field1.setAccessible(true);
                        String name1 = field1.getName();
                        Object value1 = null;
                        value1 = field1.get(value1);
                        System.out.println(name1 + " -> " + value1);
                    }

                } else {
                */
            System.out.println(name + " -> " + value);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

它适用于原始类型(字符串,数据,int ...) 任何人都可以改进这种方法来打印非原始类型但复杂(当一个对象字段也是一个对象时)。

更新

根据@Jon Skeet建议让程序看起来像

package App;

public class Kitchen {


    double sqr;
    int windows;


    public Kitchen(float sqr, int windows) {
        this.sqr = sqr;
        this.windows = windows;
    }


    public double getSqr() {
        return sqr;
    }

    public void setSqr(double sqr) {
        this.sqr = sqr;
    }

    public int getWindows() {
        return windows;
    }

    public void setWindows(int windows) {
        this.windows = windows;
    }
}


package App;
public class Flat {

    Kitchen kitchen;

    double sqr;
    int windows;
    int rooms;


    public Flat(Kitchen kitchen, double sqr, int windows, int rooms) {
        this.kitchen = kitchen;
        this.sqr = sqr;
        this.windows = windows;
        this.rooms = rooms;
    }

    public Kitchen getKitchen() {
        return kitchen;
    }

    public void setKitchen(Kitchen kitchen) {
        this.kitchen = kitchen;
    }

    public double getSqr() {
        return sqr;
    }

    public void setSqr(float sqr) {
        this.sqr = sqr;
    }

    public int getWindows() {
        return windows;
    }

    public void setWindows(int windows) {
        this.windows = windows;
    }

    public int getRooms() {
        return rooms;
    }

    public void setRooms(int rooms) {
        this.rooms = rooms;
    }
}



package App;
import java.lang.reflect.Field;
public class App {


    public static void  main (String [] args)
    {

        Kitchen kit = new Kitchen(12, 1 );

        Flat flat = new Flat(kit, 32.32, 5, 4);


        App.describeObjectFields(flat);

    }


    public static void describeObjectFields(Object o) {

        System.out.println("Object description: " + o.getClass().getCanonicalName() + ": ");

        for (Field field: o.getClass().getDeclaredFields()) {

            field.setAccessible(true);
            String name = field.getName();

            Object value;
            try {
                value = field.get(o);
            /*
                if (value.toString().contains("com.comp."))
                {
                    LOG.info("There is an Object in the described class " + value.getClass().getCanonicalName() );
                    System.out.println(value.getClass().getFields().toString() );
                    for (Field field1 :  value.getClass().getDeclaredFields()) {
                        field1.setAccessible(true);
                        String name1 = field1.getName();
                        Object value1 = null;
                        value1 = field1.get(value1);
                        System.out.println(name1 + " -> " + value1);
                    }

                } else {
                */
                System.out.println(name + " -> " + value);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

结果我收到了

Object description: App.Flat: 
kitchen -> App.Kitchen@140e19d
sqr -> 32.32
windows -> 5
rooms -> 4

但希望

Object description: App.Flat:    
     Object description: App.Kitchen: 
      sqr -> 12
      windows -> 1 
sqr -> 32.32 
windows -> 5 
rooms -> 4

如何?

1 个答案:

答案 0 :(得分:1)

您应该为要打印的每种类型覆盖toString方法。

例如,如果要以可读的方式打印YourObject的实例,则应编写如下内容:

public class YourObject
{
    private int yourField1;
    private int yourField2;

    public YourObject(int f1, int f2)
    {
        yourField1 = f1;
        yourField2 = f2;
    }

    @Override
    public String toString()
    {
         return "YourObject[yourField1=" + yourField1 + ", yourField2=" + yourField2 + "]");
    }

}

这个,你可以做这样的事情:

AnotherObject obj = new AnotherObject();
obj.setYourObject(new YourObject(1,2));

当你打电话时:

describeObjectFields(obj);

你会得到:

YourObject[yourField1=1, yourField2=2]

<强>更新

在你的情况下,你应该在你的Kitchen类中做这样的事情:

public class Kitchen {
    // your instance variables, constructor, getters and setters, etc.

    @Override
    public String toString() {
        return ("sqr: " + sqr + ", windows: " + windows);
    }

}