Note that there are some explanatory texts on larger screens.

plurals
  1. POConfusing Java generics usage: how do I properly parameterize this?
    primarykey
    data
    text
    <p>I'm looking at some code with this form:</p> <pre> <code> 1 package com.stackoverflow.java.questions; 2 import java.util.ArrayList; 3 import java.util.List; 4 public class B extends A&lt;B&gt; { 5 6 private 7 &lt;C extends A&gt; 8 List&lt;C&gt; getList(Class&lt;C&gt; cls) { 9 10 List&lt;C&gt; res = new ArrayList&lt;C&gt;(); // "snip"... some stuff happening in here, using cls 11 return res; 12 } 13 public 14 List&lt;A&gt; getList() { 15 return getList(A.class); 16 } 17 } 18 abstract class A&lt;C extends A&lt;C&gt;&gt; { 19 } </code> </pre> <p>Yes, I know it's ugly, but I reduced it as much as I could manage.</p> <p>My question is how do I properly parameterize the use of A on lines 7, 14, and 15?</p> <p>Currently, I get warnings (A is a raw type. References to generic type A should be parameterized) from Eclipse for lines 7 and 14. I suspect that I would get one for line 15 as well once I fix the other two, but I'm not certain. Either way, it's not currently parameterized and probably ought to be, but I have no idea what the syntax is supposed to be.</p> <p>Any thoughts on how to add the proper parameter semantics and syntax to get rid of these warnings?</p> <p>Note: I'm not sure if it matters whether or not A is a recursive generic. I still get the same warnings if I simply use "abstract class A {}" in place of its current definition.</p> <p>Honestly, if it were my own code I'd change it all to be a heck of a lot simpler. Unfortunately, it's part of an API so I'm trying to change it as little as possible.</p> <p>Thanks!</p> <p>UPDATE:</p> <p>I'm able to address the warnings by parameterizing them as one would expect, but at the cost of an error. It comes down to this, how do I get a Class&lt;A&lt;B&gt;&gt; from an abstract class?</p> <p>Something like,</p> <p>Class&lt;A&lt;B&gt;&gt; cls = A.class;</p> <p>However, this causes a type mismatch error.</p> <p>UPDATE, part 2:</p> <p>Turns out, ya just can't do it due to type erasure. See my other question <a href="https://stackoverflow.com/questions/2390662/java-how-do-i-get-a-class-literal-from-a-generic-type">here.</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload