title: Java di module with help of generics and reflection
body: <p>I want to build my own simple di module, for that purpose I created: </p>
  
  <pre><code>public interface Factory&lt;T&gt; {
  T getInstance();
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class DiImpl implments Di{
  
  private Map&lt;Class, Factory&gt; map = new HashMap&lt;&gt;();
  
  @Override
  public &lt;T&gt; Factory&lt;T&gt; getFactory(Class&lt;T&gt; type) {
      return (Factory&lt;T&gt;) map.get(type);
  }
  
  @Override
  public &lt;T&gt; void bind(Class&lt;T&gt; base, Class&lt;? extends T&gt; impl) {
      map.put(base, new Factory&lt;Class&lt;?&gt;&gt;() {
          @Override
          public Class getInstance() {
              return  new Class&lt;?&gt;.newInstance; //here should be new instance of impl class
          }
      });
  }
  }
  </code></pre>
  
  <p>But this code not pass simple test:</p>
  
  <pre><code>interface SimpleService {
  ....
  }
  
  FirstVaraintService implements SimpleService {
  ...
  }
  
  @Test
  void test(){
    Di di= new DiImpl();
    di.bind(SimpleService.class, FirstVaraintService.class);       
    Factory&lt;SimpleService&gt; factory = di.getFactory(SimpleService.class);
    assertSame(FirstVaraintService.class, factory.getInstance().getClass());
  }
  </code></pre>
  
  <p>compiler returns: </p>
  
  <pre><code>java.lang.ClassCastException: java.lang.Class cannot be cast to ... SimpleService
  </code></pre>
  
  <p>I know that, such di structure can be implemented with help of reflection API, but I don't know how to properly use it in this example. How can I change my code to pass the test?</p>