Problem using Java generics: how can I parameterize this correctly?

I am looking at code with this form:

1 package com.stackoverflow.java.questions; 2 import java.util.ArrayList; 3 import java.util.List; 4 public class B extends A<B> { 5 6 private 7 <C extends A> 8 List<C> getList(Class<C> cls) { 9 10 List<C> res = new ArrayList<C>(); // "snip"... some stuff happening in here, using cls 11 return res; 12 } 13 public 14 List<A> getList() { 15 return getList(A.class); 16 } 17 } 18 abstract class A<C extends A<C>> { 19 } 

Yes, I know this is ugly, but I reduced it as much as I could.

My question is: how to correctly parameterize the use of A on lines 7, 14 and 15?

I am currently receiving warnings (A is a raw type. References to generic type A must be parameterized) from Eclipse for lines 7 and 14. I suspect that I will get one for line 15 as soon as I fix the other two, but I don’t sure. In any case, this is not currently parameterized and probably should be, but I don't know what the syntax should be.

Any thoughts on how to add the correct semantics and parameter syntax to get rid of these warnings?

Note. I'm not sure if it matters if A is recursive generic. I still get the same warnings if I just use the "abstract class A {}" instead of my current definition.

Honestly, if it were my own code, I would change everything to be much simpler. Unfortunately, this is part of the API, so I'm trying to change it as little as possible.

Thanks!

UPDATE:

I can address warnings, parameterizing them, as expected, but at the cost of an error. The thing is, how do I get the class <A <B β†’> from an abstract class?

Sort of,

Class & l, A & l, B β†’ cls = A.class;

However, this causes a type mismatch error.

UPDATE, Part 2:

Turns out you just can't do this due to type erasure. See my other question here.

+4
source share
3 answers

Turns out you just can't do this due to type erasure. See my other question here: Java: how to get a class literal from a generic type?

0
source

You can start by using <C extends A<B>> and List<A<B>> <C extends A<B>> List<A<B>> in these first two instances. Passing a type key in this case is counterproductive (see how the cls parameter is never used), so just turn it off.

(You can always tell B.<Foo>getList() to specifically create an instance with a specific type, assuming you're happy that the type is not non-oriented, of course.)


Refresh to enable OP editing. Will A<?> Be used for your code? In limited cases, you may not need to specify the type completely. Unfortunately, I do not have your code, so you can find out if it will work.

+4
source

What exactly are you doing here with cls:

 // "snip"... some stuff happening in here, using cls 

Depending on what you are doing here, you could replace the class instance with an instance of the type, if you tell me a little more about what you are doing here, I can present you with an alternative solution to make it work.

0
source

Source: https://habr.com/ru/post/1303186/


All Articles