title: How can I dynamically access property of Java object in GWT?
body: <p>Using GWT I have a Java class:</p>
  
  <pre><code>public class Pojo {
    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
  }
  </code></pre>
  
  <p>The above class is shared between the client and server side code.</p>
  
  <p>From the client code I would like to dynamically access the property. That is, I would like to write a method with the following signature:</p>
  
  <pre><code>public String getProperty(Object o, String propertyName)
  </code></pre>
  
  <p>Such that the following code would work:</p>
  
  <pre><code>Pojo pojo = new Pojo();
  pojo.setName("Joe");
  getProperty(pojo, "name");    // this should return "Joe"
  </code></pre>
  
  <p>Java reflection is obviously out. And I have tried the following JSNI method:</p>
  
  <pre><code>public static native String getProperty(Object o, String name) /*-{
    return o[name];
  }-*/;
  </code></pre>
  
  <p>But that does not work.</p>
  
  <p>The <a href="http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields" rel="noreferrer">special syntax</a> for accessing Java objects from JavaScript can't be used either as I want this to be dynamic.</p>
  
  <p>Any ideas on how I can do this?</p>
  
  <p>For completeness, I will also want to be able to set a property dynamically as well.</p>
  
  <p>EDIT: blwy10's answer was a great tip to get me searching using "gwt reflection" instead of with terms like "dynamic property access". This lead me to <a href="http://code.google.com/p/gwt-ent/" rel="noreferrer">gwt-ent</a>, which has an very elegant reflection solution. I am going to try this one, as it does not require a separate code generation step.</p>