title: Java Reflection - listing properties (getters & setters) of a class
body: <pre><code>public class foo
  {
      private String _name;
      private String _bar;
  
      public String getName() {
          return _name;
      }
  
      public void setName(String name) {
          _name = name;
      }
  
      public String getBar() {
          return _bar;
      }
  
      public void setBarn(String bar) {
          _bar = bar;
      }
  }
  </code></pre>
  
  <p>If I have the above class can I use reflection to list the properties defined by the getters and setters? I've tried the method below but it doesn't work, <code>Field[] fields</code> is left empty. I know I can do this in .Net but Java is a very different animal. Am I barking up the wrong tree altogether? </p>
  
  <pre><code>private HashMap&lt;String, String&gt; getHashMap(Object obj) {
      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
      Class&lt;?&gt; cls = obj.getClass();
  
      Field fields[] = cls.getFields();
      for(Field f : fields) {
          String name = f.getName();
          String value = f.get(obj).toString();
          map.put(name, value);
      }
      return map;
  }
  </code></pre>
  
  <p><em>Also</em> setters and getters maybe evil, should I just drop this?</p>