title: Java Generics restrict to interface
body: <p>I'm not sure if this is possible or not, but what I want to accomplish is this:</p>
  
  <pre><code>public static &lt;A,B extends SomeClass &amp; A&gt; B makeB(A thing) {...}
  </code></pre>
  
  <p>Essentially, using a reflection/generation driven process, I want to provide a thing of type B, where B is of class <code>SomeClass</code> and implements interface A, and A is user-supplied through Generics.</p>
  
  <p>I am not asking about the mechanics of generating B - I have that under control.  What I'm looking for is a way to restrict generic type argument <code>&lt;A&gt;</code> to interfaces, not classes, so that I can use the syntax <code>B extends SomeClass &amp; A</code> for clean type safety.</p>
  
  <p>Is this possible?  Is anyone aware of an alternative approach to this problem?</p>
  
  <hr>
  
  <p>Edit:  I guess I didn't express myself very clearly, as it seems to be causing confusion in the comments:</p>
  
  <p><code>B</code> is intended to be a placeholder for a wildcard, so that the client can get a single object that is both a <code>SomeClass</code> and an <code>A</code> without having to do casting based on trust.  The client will not have access to the name of the actual class that implements <code>SomeClass</code> and <code>A</code>, because it's being generated at compile time, hence this issue regarding type safety.</p>