title: Passing a class with type parameter as type parameter for generic method in Java
body: <p><strong>Problem summary:</strong>
  I would like to pass a class with a type parameter (such as <code>ArrayList&lt;SomeClass&gt;</code>, for example) to a generic method as a type parameter.</p>
  
  <p>Let's say I have a method:</p>
  
  <pre><code>    public static &lt;T&gt; T getGenericObjectFromJson(String json, Class&lt;T&gt; genericType){
          // details unimportant, basically returns an object of specified type
          return JsonParser.fromJson(json, genericType); 
      }
  </code></pre>
  
  <p>This method, of course, will work perfectly fine for any kind of class. I can call the method like so, for example:</p>
  
  <pre><code>getGenericObjectFromJson(jsonString, User.class)
  </code></pre>
  
  <p><strong>The problem:</strong> I discovered I cannot do this:</p>
  
  <pre><code>getGenericObjectFromJson(jsonString, ArrayList&lt;User&gt;.class)
  </code></pre>
  
  <p>Syntactically, this is obviously invalid. However, I am not certain how I would even accomplish something like this. I can, of course, pass <code>ArrayList.class</code>, however the addition of the generic type makes it no longer syntactically valid, and I cannot think of a way around it.</p>
  
  <p>The only direct solution has been something like this (which seems rather goofy):</p>
  
  <pre><code>getGenericObjectFromJson(jsonString, new ArrayList&lt;User&gt;().getClass())
  </code></pre>
  
  <p>However we end up losing the generic type anyways, and merely get back an ArrayList of unknown type (though it can be cast). Plus, unnecessarily instantiating an object.</p>
  
  <p>My only solution thus far has been to wrap that method in a class that contains a generic type parameter which can be instantiated, like so:</p>
  
  <pre><code>public class JsonDeserializer&lt;T&gt;...
  </code></pre>
  
  <p>In this case, the <code>getGenericObjectFromJson</code> method will use the class's generic type.</p>
  
  <p><strong>The Question(s):</strong> Ultimately, I am curious why I cannot pass a class with a type parameter, AND whether there is a way to accomplish what I attempted to do.</p>
  
  <p>As always, let me know if there are any problems with this question.</p>