Reputation: 1422
I have classes
abstract class A {
//....
}
class B extends A {
//....
}
class C extends A {
//....
}
Then I have
interface Maker<T extends A> {
SomeClass make(T obj);
}
implementations for Maker class
class MakerForB implements Maker<B> { /*... */ }
class MakerForC implements Maker<C> { /*... */ }
and class Factory with one static method
class Factory {
public static SomeClass makeSomeClass(A obj) {
Maker maker = null;
if(obj instanceof B) maker = new MakerForB();
/* ... */
return maker.make(obj);
}
}
In that case I get warning that Maker is a raw type, when I declare Maker that way
Maker<?> maker = null;
I get exception (make is not applicable for the arguments A) on
return maker.make(obj);
What is the best way to get rid of these warnings without using
@SuppressWarnings("unchecked")
Upvotes: 4
Views: 546
Reputation: 597294
Get rid of all generics on the Maker
- you don't need it:
interface Maker {
SomeClass make(A obj);
}
class MakerForB implements Maker {
SomeClass make(A obj);
}
Or if you still want to use it, go with an unsafe cast or with the SuppressWarnings
.
To understand why you get an error if your try defining:
Maker<? extends A> maker = null;
Imagine the case when you (by accident) get maker = new MakerForC()
, and try applying it to B
.
Upvotes: 5
Reputation: 19702
If I understand your question right, I think you could try replacing
class Factory {
public static SomeClass makeSomeClass(A obj) {
Maker maker = null;
if(obj instanceof B) maker = new MakerForB();
/* ... */
return maker.make(obj);
}
}
by
class Factory<S extends A> {
public static SomeClass makeSomeClass(S obj) {
Maker<S> maker = null;
if(obj instanceof B) maker = new MakerForB();
/* ... */
return maker.make(obj);
}
}
didn't test it, so if you get other errors now, please let me know.
Upvotes: -2