title: Type safety: Unchecked cast from Object to List
body: <pre><code>SqlOutParameter out = new SqlOutParameter(&quot;out_refcursor&quot;,OracleTypes.CURSOR
                                             ,new StudentRowMapper());
  </code></pre>
  <p>// some code..</p>
  <pre><code>MapSqlParameterSource parameters = createMapSqlParameterSource();
  parameters.addValue(&quot;in_studentid&quot;,&quot;101&quot;); 
  
  Map&lt;String, Object&gt; result = simpleJdbcCall.execute(parameters);
  List&lt;Student&gt; students = (List&lt;Student&gt;) result.get(&quot;out_refcursor&quot;); // here I get a warning
  </code></pre>
  <p>Definition of <code>execute()</code> method:</p>
  <pre><code>Map&lt;String, Object&gt; org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SqlParameterSource parameterSource)
  </code></pre>
  <p>&quot;Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations..&quot;</p>
  <p>The warning on the above line :  <code>List&lt;Student&gt; students = (List&lt;Student&gt;) result.get(&quot;out_refcursor&quot;);</code> is:</p>
  <blockquote>
  <p>Type safety: Unchecked cast from Object to List</p>
  </blockquote>
  <p>I understand it is only a compile time warning, and Off course I can do a <code>@SuppressWarnings(&quot;unchecked&quot;)</code> to suppress it.</p>
  <p><strong>Question:</strong> But how do I properly cast it?</p>
  <ol>
  <li><p>One way I tried out is</p>
  <pre><code>List&lt;Student&gt; students = castObject( result.get(&quot;out_refcursor&quot;));
  
  @SuppressWarnings(&quot;unchecked&quot;)
      private static &lt;T extends List&lt;?&gt;&gt; T castObject(Object obj){
      return (T)obj;
  }
  </code></pre>
  </li>
  </ol>
  <p>Still I had to put <code>@SuppressWarnings(&quot;unchecked&quot;)</code> in the <code>castObject()</code> method. And I don't know if this is correct way of doing.</p>
  <ol start="2">
  <li><p>Second way I tried is,</p>
  <pre><code>List&lt;?&gt; students = castObject( result.get(&quot;out_refcursor&quot;));
  
  Student student = (Student)students.get(0);
  
  private static &lt;T extends List&lt;?&gt;&gt; List&lt;?&gt; castObject(Object obj){
        if(obj instanceof List&lt;?&gt;) {
             return (List&lt;Student&gt;)obj;
        }
        return null;
   }
  </code></pre>
  </li>
  </ol>
  <p>Any suggestions/advice are welcome.</p>