title: Understanding generics and reflection
body: <p>I need some help with generics and reflection in java.</p>
  
  <p>What I try to achieve is a factory class that based on a class <code>MyClass</code> returns another class <code>MyClassStore</code> that implements an interface <code>Store&lt;MyClass&gt;</code>. </p>
  
  <p>How can I make sure that the right type of store is returned with reflection.</p>
  
  <p>See sample code:</p>
  
  <pre><code>public class Application {
  
      public static void main(String[] args) {
  
          Store&lt;MyClass&gt; store = Factory.getFactory(MyClass.class);
          MyClass myClass = store.get();
          System.out.println(myClass.getId());
  
      }
  
  }
  
  public class Factory {
  
      public static &lt;T&gt; Store&lt;T&gt; getFactory(Class&lt;T&gt; type) {
  
          Store&lt;T&gt; store = null;
          // TODO: How to implement this
          return store;
  
      }
  
  }
  
  public class MyClassStore implements Store&lt;MyClass&gt;{
  
      public MyClass get() {
          return new MyClass("1000");
      }
  
  }
  
  public interface Store&lt;T&gt; {
  
      public T get();
  
  }
  
  public class MyClass {
  
      private String id;
  
      public MyClass(String id) {
          this.id = id;
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
  }
  </code></pre>
  
  <p>I have tried different ways but can't get it to work the way I want. This is the implementation I have tried. But is there a better way?</p>
  
  <pre><code>    public static &lt;T&gt; Store&lt;T&gt; getFactory(Class&lt;T&gt; type) {
  
          Store&lt;T&gt; store = null;
  
          if (type == MyClass.class) {
              store = (Store&lt;T&gt;) new MyClassStore();
          }
  
          return store;
  
      }
  </code></pre>