Return to Snippet

Revision: 65752
at February 13, 2014 00:58 by mcaordie


Updated Code
for (java.lang.reflect.Method m : object.getClass().getMethods()) {
    try {
        if (m.getName().startsWith("get") 
                && m.getReturnType() != void.class
                && m.getParameterTypes().length == 0) {
            System.out.println(m.getName().replace("get", "") + ": " + m.invoke(object));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Revision: 65751
at January 15, 2014 00:49 by mcaordie


Updated Code
for (Method m : object.getClass().getMethods()) {
    try {
        if (m.getName().startsWith("get") 
                && m.getReturnType() != void.class
                && m.getParameterTypes().length == 0) {
            System.out.println(m.getName().replace("get", "") + ": " + m.invoke(object));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Revision: 65750
at January 15, 2014 00:46 by mcaordie


Initial Code
for (Method m : object.getClass().getMethods()) {
    try {
        if (m.getName().startsWith("get") 
                && m.getReturnType() != void.class
                && m.getParameterTypes().length == 0) {
            System.out.println(m.getName().replace("get", "") + ": " + m.invoke(event));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Initial URL

                                

Initial Description
Surprisingly often I want to print all the values of some object for debugging reasons, and if the object has a lot of different get methods (and toString is not good for this), it is a pain to write all the print commands. That's when this snippet is useful (yes, it is not perfect, and if get methods are not named as "getXXX", it does not find those, but it is your own fault then).

Initial Title
Java: print values of all get-methods of an object

Initial Tags
debug, java

Initial Language
Java