title: Java generics: get class of generic method's return type
body: <h2>Background</h2>
  
  <p>I once wrote this method:</p>
  
  <pre><code>private &lt;T&gt; SortedSet&lt;T&gt; createSortedSet() {
    return new TreeSet&lt;T&gt;();
  }
  </code></pre>
  
  <p>It's supposed to be called like this:</p>
  
  <pre><code>Set&lt;String&gt; set = createSortedSet();
  </code></pre>
  
  <p>This works fine (although I've seen in answers here when researching the current question that this is error-prone).</p>
  
  <h2>The current situation</h2>
  
  <p>Anyway, now I am writing the following code (in a class that extends javax.servlet.jsp.tagext.TagSupport):</p>
  
  <pre><code>private &lt;T&gt; T evaluate(String expression) {
    ExpressionEvaluator evaluator = pageContext.getExpressionEvaluator();
    return evaluator.evaluate(expression, T, null, pageContext.getVariableResolver());
  }
  </code></pre>
  
  <p>The purpose is to be able to call this like:</p>
  
  <pre><code>Integer width = evaluate(widthStr);
  </code></pre>
  
  <p>The code in my evaluate method obviously doesn't work. The second argument to <code>evaluator.evaluate()</code> is supposed to be a Class object. This leads me to:</p>
  
  <h2>My question</h2>
  
  <p>How can I get the Class of a generic (return) type? What should I write in place of T as the second argument to evaluate?</p>
  
  <h2>EDIT: Conclusion</h2>
  
  <p>Nicolas seems to be right, it can not be done and I need to pass the class as an argument to my method. The upside is that since his solution makes the argument parametrized on the generic type I get a compilation check on that argument.</p>