title: How to encapsulate an unchecked cast to only allow unchecked generic type conversion?
body: <p>I'm looking for a "proper" way to reduce the Java boilerplate involved in retrieving/modifying the generic type arguments at compile time. Usually, this boilerplate involves:</p>
  
  <ul>
  <li>Using <code>@SuppressWarnings("unchecked")</code>.</li>
  <li>Spelling out explicitly the target generic type arguments.</li>
  <li>Usually, creating an otherwise useless local variable just so that the supression can be applied to that staement only.</li>
  </ul>
  
  <p>As a <em>theoretical</em> example, suppose I want to keep a map of <code>Class</code> to <code>Supplier</code> such that for each <code>keyClass</code>, its associated <code>valueSupplier</code> produces objects extending <code>keyClass</code>.</p>
  
  <p><em>Edit 2</em>: I changed the example from a map of <code>Class</code> to <code>Class</code>, to a map of <code>Class</code> to <code>Supplier</code>, because (the value) <code>Class</code> objects are special with respect to casts, and the original example had another solution not involving unchecked casts (thanks @Holger). Again, I'm only adding an example to illustrate the problem, I don't need to solve any particular example.</p>
  
  <p><em>Edit 1</em>: More precisely, a single <code>SupplierMap</code> object is populated say from a config file, and holds info such as "objects implementing interface I1 are provided by supplier S1", "I2 by S2", and so on. At runtime, we get calls such as <code>I1 i1 = supplierMap.get(I1.class).get()</code> which should produce an object with <code>i1.getClass() == C1.class</code>. I'm not interested in fixes/shortcuts, e.g. moving the cast to where it does not belong, such as having <code>get()</code> return <code>Supplier&lt;Object&gt;</code>. The cast belongs conceptually inside the <code>SupplierMap</code>. Also, I don't much care about this specific example, but about the general language problem.</p>
  
  <p>With <code>SupplierMap</code>, I don't believe there is a way to capture the key-value generic parameter relation in Java so that the <code>get()</code> does not involve an unchecked compile-time cast. Concretely I could have:</p>
  
  <pre><code>class SupplierMap {
  
      // no way to say in Java that keys and values are related
      Map&lt;Class&lt;?&gt;, Supplier&lt;?&gt;&gt; map;
  
      // can check the relation at compile time for put()
      &lt;T&gt; void put(Class&lt;T&gt; keyClass, Supplier&lt;? extends T&gt; valueSupplier) {
          map.put(keyClass, valueSupplier);
      }
  
      // but not for get()
      &lt;T&gt; Supplier&lt;? extends T&gt; get(Class&lt;T&gt; keyClass) {
          @SuppressWarnings("unchecked")
          final Supplier&lt;? extends T&gt; castValueSupplier = (Supplier&lt;? extends T&gt;) map.get(keyClass);
          return castValueSupplier;
      }
  }
  </code></pre>
  
  <p>As an alternative, one could have:</p>
  
  <pre><code>@SupressWarnings("unchecked")
  &lt;T&gt; T uncheckedCast(Object o) {
      return (T) o;
  }
  
  &lt;T&gt; Supplier&lt;? extends T&gt; get(Class&lt;T&gt; keyClass) {
      return uncheckedCast(map.get(keyClass));
  }
  </code></pre>
  
  <p>That looks much better, but the problem is that <code>uncheckedCast</code> is arguably too powerful: it can potentially cast anything into anything else at compile time (by hiding warnings). At runtime we'd still get CCE's, but that's not the point here. (The argument goes that ...) If one were to put this <code>uncheckedCast</code> into a library, the function could be abused to hide problems otherwise detectable at compile-time.</p>
  
  <p>Is there a way to define such a similar unchecked cast function so that the compiler can enforce that it is only used to change generic type parameters?</p>
  
  <p>I tried:</p>
  
  <pre><code>// error at T&lt;U&gt;: T does not have type parameters
  &lt;T, U&gt; T&lt;U&gt; uncheckedCast(T t) {
      return (T&lt;U&gt;) t;
  }
  </code></pre>
  
  <p>also </p>
  
  <pre><code>&lt;T, U extends T&gt; U uncheckedCast(T t) {
      return (U) t;
  }
  
  void test() {
      Class&lt;?&gt; aClass = String.class;
      // dumb thing to do, but illustrates cast error:
      // type U has incompatible bounds: Class&lt;capture of ?&gt; and Class&lt;Integer&gt;
      Class&lt;Integer&gt; iClass = uncheckedCast(aClass);
  }
  </code></pre>
  
  <p><em>Edit</em>: Have you seen this kind of an unchecked cast (even the all-powerful one above) in a common library? I looked in Commons Lang and Guava, but the only one I could find is Chronicle Core's <a href="https://github.com/OpenHFT/Chronicle-Core/blob/913e8dfc7ada599f6585ff14ec42230f1d839e4e/src/main/java/net/openhft/chronicle/core/util/ObjectUtils.java#L162" rel="nofollow noreferrer"><code>ObjectUtils.convertTo()</code></a>: there, passing <code>eClass == null</code> is equivalent to the all-powerful <code>uncheckedCast</code>, except that it also produces an undesired <code>@Nullable</code> (that is used by other branches).</p>