Reflect invoke getMethods toString

Using java reflect invoke getMethods toString

public String toString() {
    String s = "";
    s += "\n"+this.getClass().getName()+"\n";
    try {
        for(Method m : this.getClass().getMethods()) {
            if(m.getName().startsWith("get") && !m.getName().equals("getClass")) {
                s += m.getName().replace("get","")+"="+m.invoke(this, null)+"\n";
            }
        }
    } catch (Exception e) {
        s = e.getLocalizedMessage();
    }
    return s;
}

Comments

Leave a Reply