Reflect invoke getMethods toString

Using java reflect invoke getMethods toString

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

Leave a Reply