title: Java avoid class cast warning in hierarchical Builder pattern
body: <p>Is there a way to avoid the unchecked class cast in this hierarchical Builder pattern?</p>
  
  <pre><code>public abstract class BaseBuilder &lt;T, B extends BaseBuilder&lt;T,B&gt;&gt; {
  
    public B setB1(String b1) {
      this.b1 = b1;
      return (B) this; // can I make this unchecked cast go away?
    }
  
    abstract public T build();
  
    String b1;
  }
  </code></pre>
  
  <p>and no, the answer is not:</p>
  
  <pre><code>return B.class.cast(this);
  </code></pre>
  
  <p>and yes, I know I could use @SuppressWarnings</p>