title: How can I get the parameterized-type class from a parameterized-type method argument?
body: <p>Consider this contrived class:</p>
  
  <pre><code>import java.util.List;
  public class Test {
      public String chooseRandom(List&lt;String&gt; strings) {
          return null;
      }
  }
  </code></pre>
  
  <p>When using reflection to inspect this method, how can I get the <code>Class</code> object representing <code>java.lang.String</code> (or even the string <code>"java.lang.String"</code>) when looking at the arguments for <code>chooseRandom</code>?</p>
  
  <p>I know that Java erases types at compile-time, but they must still be in there, since <code>javap</code> can print them properly. Running <code>javap</code> on this compiled class results in:</p>
  
  <pre><code>Compiled from "Test.java"
  public class Test {
    public Test();
    public java.lang.String chooseRandom(java.util.List&lt;java.lang.String&gt;);
  }
  </code></pre>
  
  <p>The parameterized type for <code>java.util.List</code> (<code>java.lang.String</code>) is definitely available... I just can't find out what it is.</p>
  
  <p>I've tried this:</p>
  
  <pre><code>Class clazz = [grab the type of chooseRandom's parameter list's first argument];
  String typeName = clazz.getName();
  TypeVariable[] genericTypes = clazz.getTypeParameters();
  if(null != genericTypes &amp;&amp; 0 &lt; genericTypes.length)
  {
      boolean first = true;
      typeName += "&lt;";
      for(TypeVariable type : genericTypes)
      {
          if(first) first = false;
          else typeName += ",";
  
          typeName += type.getTypeName();
      }
  
      typeName = typeName + "&gt;";
  }
  </code></pre>
  
  <p>This gives me a <code>typeName</code> of <code>java.util.List&lt;E&gt;</code>. What I'm hoping for is <code>java.util.List&lt;java.lang.String&gt;</code>.</p>
  
  <p>I've read a few other questions and answers on SO and none of them really seem to answer this question, or if they do, there is no working code associated with them and the explanations are ... thin.</p>