title: How to determine the Type of an object at runtime in Java for gson
body: <p>Take a look at the following code, which works fine:</p>
  
  <pre><code>        MyData myData = new MyData(1, "one");
      MyData myData2 = new MyData(2, "two");
      MyData [] data = {myData, myData2};
  
      String toJson = gson.toJson(data, MyData[].class);
  
      MyData [] newData = gson.fromJson(toJson, MyData[].class);
  </code></pre>
  
  <p>MyData is just a simple class with int and String fields. This works perfectly fine. But what if the class is not known until runtime? e.g. It might not be "MyData", but could be a completely different class. The only thing we know is the name of the class (represented as a string), which was previously determined by the sender using Class.forName.</p>
  
  <p>It works fine if the object is not an array, like so:</p>
  
  <pre><code>    final Class&lt;?&gt; componentType = Class.forName(componentClassName);
  context.deserialize(someElement, componentType);
  </code></pre>
  
  <p>The above technique doesn't work for arrays though. </p>
  
  <p>Any suggestions?</p>