how to use recursion in reflection?

I have class test it contains other complex object private class2 e; and that object contains other complex object private class3 b;

public class class3 {
    private String x;
    private String y;
    public String getX() {
        return x;
    }
    public void setX(String x) {
        this.x = x;
    }
    public String getY() {
        return y;
    }
    public void setY(String y) {
        this.y = y;
    }
}

//class2
public class class2 {
    private String n;
    private class3 b;
    public String getN() {
        return n;
    }
    public void setN(String n) {
        this.n = n;
    }
    public class3 getB() {
        return b;
    }
    public void setB(class3 b) {
        this.b = b;
    }
}
//class test
public class test {
    private String w;
    private class2 e;

    public String getW() {
        return w;
    }

    public void setW(String w) {
        this.w = w;
    }

    public class2 getE() {
        return e;
    }

    public void setE(class2 e) {
        this.e = e;
    }
}

What i need to accomplish is having an Object from test i want to call all getters and if it returns complex object object from other class i want to to go recursively till no complex objects left

i could read all test object data , the part i'm missing is the recessive part

Here is my code :-

private static void writeInLogger(Object obj, String str) {
    Class klazz = obj.getClass();
    if (klazz.isPrimitive() || obj instanceof String
            || obj instanceof Integer || obj instanceof Double
            || obj instanceof Boolean)
        System.out.println(str + obj.toString());
    else {
        try {
            for (PropertyDescriptor propertyDescriptor : Introspector
                    .getBeanInfo(klazz).getPropertyDescriptors()) {
                Method m = propertyDescriptor.getReadMethod();
                if (m != null){
                Object object = m.invoke(obj);
                    Class klazz2 = object.getClass();
                    if(klazz2.isPrimitive() || object instanceof String|| object instanceof Integer || object instanceof Double|| object instanceof Boolean){
                        System.out.println(m + str + m.invoke(obj).toString());
                    }

                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
    }
}

Answers


Updated your writeInLogger method. I also added to check for the write method, else you get Class also as a property and the stack blows.

private static void writeInLogger(Object obj, String str) {
    Class klazz = obj.getClass();
    if (klazz.isPrimitive() || obj instanceof String || obj instanceof Integer || obj instanceof Double
            || obj instanceof Boolean)
        System.out.println(str + obj.toString());
    else {
        try {
            for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(klazz).getPropertyDescriptors()) {
                if(propertyDescriptor.getWriteMethod() == null)
                    continue;
                Method m = propertyDescriptor.getReadMethod();
                if (m != null) {
                    Object object = m.invoke(obj);
                    if(object != null)
                        writeInLogger(object, str);
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
    }
}

Need Your Help

Makefiles in eclipse and their path

eclipse path makefile

I'm trying to build a project with a specific compiler in Eclipse, and I'm using a Makefile to do the dirty work -- only problem is that it seems as if Eclipse doesn't share the same paths as my bash

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.