I'm working with a class just like this:
abstract class TypeBase<X extends TypeBase<?>>{
public abstract <Z extends Object> Z toJavaType();
}
This class models a type that could be equivalent to an existing java type.
Generic type Z allows subclasses to return appropriate java type thorugh toJavaType() method.
Few examples of implementations:
class MyIntType extends TypeBase<MyIntType>{
int i;
@Override
public Integer toJavaType() {
return new Integer(i);
}
}
class MyDoubleType extends TypeBase<MyDoubleType>{
double d;
@Override
public <Z> Z toJavaType() {
return (Z)new Double(d);
}
}
Problem comes here, both implementations generate warning.
The first:
Type safety: The return type Integer for toJavaType() from the type MyIntType needs unchecked conversion to conform to Z from the type TypeBase<X>
The second:
Type safety: Unchecked cast from Double to Z
Any way to avoid these warnings without using @Suppress("unchecked") annotations?