title: How to pass a generic interface to a method where it is reified?
body: <p>I have a generic interface</p>
  
  <pre><code>public interface MyInterface&lt;T&gt; {
      T method(T input);
  }
  </code></pre>
  
  <p>and a couple of implementations of it, via ordinary classes like</p>
  
  <pre><code>public class MyClass&lt;T&gt; implements MyInterface&lt;T&gt; {
      @Override
      public T method(T input) {
          T output = input; // whatever
          return output;
      }
  }
  </code></pre>
  
  <p>and anonymous classes (see below). Now I want to test these implementations:</p>
  
  <pre><code>class TestClass1 {
      // ...
  }
  
  class TestClass2 {
      final int n;
      final String s;
  
      TestClass2(int n, String s) {
          this.n = n;
          this.s = s;
      }
      // ...
  }
  
  public class TestGenericImplementation {
      private static &lt;T&gt; void makeTest(T testObject, MyInterface&lt;T&gt; impl) {
          T output = impl.method(testObject);
          if (output == null)
              throw new NullPointerException();
          // verify output further
      }
  
      // Question 1. How to specify the parameter here properly?
      public static void testImplementation(MyInterface impl) {
          // Question 2. How to avoid compiler warning about unchecked cast below?
  
          // Doesn't work if impl is of type MyInterface&lt;?&gt; above
          makeTest(new TestClass1(), impl);
          makeTest(new TestClass2(1, "ABC"), impl); 
  
          // Ugly typecasts. Compiler complains.
          makeTest(new TestClass1(), (MyInterface&lt;TestClass1&gt;) impl);  
          makeTest(new TestClass2(1, "ABC"), (MyInterface&lt;TestClass2&gt;) impl); 
      }
  
      public static void main(String[] args) {
          // Question 3. How to pass the interface argument here?
  
          // Works but issues compiler warning about raw type
          testImplementation(new MyClass());
          testImplementation(new MyInterface() {
              @Override
              public Object method(Object input) {
                  return null; // whatever
              }
          });
  
          // Looks ugly
          testImplementation(new MyClass&lt;Object&gt;()); 
          testImplementation(new MyInterface&lt;Object&gt;() {
              @Override
              public Object method(Object input) {
                  return null;
              }
          });
  
          /* Diamond operator appeared only in Java 7,
           * while generics were introduced in Java 5.
           * What was the recommended way to solve this problem between 2004 and 2011?
           * Besides that, this doesn't work for anonymous classes.
           */
          testImplementation(new MyClass&lt;&gt;()); 
          testImplementation(new MyInterface&lt;&gt;() { // Doesn't work
              @Override
              public Object method(Object input) {
                  return null;
              }
          });
  
          testImplementation(x -&gt; x); // Lambda exprssions are for simple cases only
      }
  }
  </code></pre>
  
  <p>The problem is that compiler is issuing a series of errors and warnings due to transition from a generic interface to its reified versions (those I need to use with the concrete classes <code>TestClass1</code> and <code>TestClass2</code> in place of the generic type variable <code>T</code>). Is it possible to avoid these warnings completely? If not (i.e. if they can only be suppressed), are there any pitfalls arising from this?</p>