title: How to ensure parametrized type correctness at runtime in Java?
body: <p>I have a simple generic list class. I'm trying to achieve this: given a list instance of which we know only that it contains instances of a specific subclass of a class, and given an instance of this class, add this instance to the list if it is instance of the contained (subclass) type, otherwise throw an exception (eg. ClassCastException). I tried the following:</p>
  
  <pre><code>class MyList&lt;T&gt;
  {
      private Class&lt;T&gt; genericClass;
      private List&lt;T&gt; list = new ArrayList&lt;&gt;();
  
      public MyList(Class&lt;T&gt; genericClass)
      {
          this.genericClass = genericClass;
      }
  
      public void add(T elem)
      {
          list.add(elem);
      }
  
      //...
  
      public Class&lt;T&gt; getGenericParamClass()
      {
          return genericClass;
      }
  }
  
  class A{}
  class B extends A{}
  class C extends A{}
  
  class Program
  {
      public static void main(String... args)
      {
          MyList&lt;B&gt; list1 = new MyList&lt;&gt;(B.class);
          MyList&lt;C&gt; list2 = new MyList&lt;&gt;(C.class);
  
          MyList&lt;? extends A&gt; ls = checkStuff() ? list1 : list2;
  
          ls.add(ls.getGenericParamClass().cast(lotsOfStuff())); //ERROR ?!!
      }
  
      static boolean checkStuff()
      {
          Random random = new Random();
          return random.nextBoolean();
      }
  
      static A lotsOfStuff()
      {
          return new B();
      }
  }
  </code></pre>
  
  <p>I thought that given a Class object whose type parameter is the same as the type of a parameter of a method, I would be able to cast something using the former to be able to pass it to the latter. Alas, it seems I cannot: I get a compile-time error!</p>
  
  <p>I could throw generics out the window, go full unchecked and just say:</p>
  
  <pre><code>A val = lotsOfStuff();
  if (myList.getGenericParamClass().isInstance(val))
      ls.add(val)
  else
      throw new SomeException();
  </code></pre>
  
  <p>But, that would probably create more problems than it would solve, and also it would really bug me.</p>
  
  <p>Am I missing something here, or is it simply not possible the way I thought it out?</p>
  
  <p><strong>Edit</strong>:
  I understand fully well why something like this cannot work:</p>
  
  <blockquote>
  <pre><code>List&lt;? extends Number&gt; abc=new ArrayList&lt;Integer&gt;();
  abc.add(new Integer(10));
  </code></pre>
  </blockquote>
  
  <p>But in my mind, the following transitivity holds: the type of the parameter of add() Is-The-Same-As the type parameter of MyList Is-The-Same-As the type parameter of the Class returned by getGenericParamClass() Is-The-Same-As the return type of the cast() method of that Class. I (as a human) <em>can</em> know that those unknown types are the same, because I am getting them from the same object.</p>
  
  <p>Is there a fault in my logic, or is this a limitation of Java?</p>