问题
This is a Trying to improve my code question. About generics and the unchecked warning; I do know about the @suppresswarnings
annotation. The question is what am I suppose to code to suppress it. Take, please, the following code.
public class NumericBox<T extends Number> implements Box<T> {
private T element;
public NumericBox(T element) {
this.element = element;
}
@Override public T getElement() {
return element;
}
public T insert(Number newElement) {
T oldElement = this.element;
this.element = (T) newElement; // warning("unchecked")
return oldElement;
}
}
What is the coding way to do this (not the annotation). In the docs I've found The term "unchecked" means that the compiler does not have enough type information to perform all type checks necessary to ensure type safety. But, don't get it. This seems ok for me:
void foo(Number n) {
Short asShort = (Short) n; //OK
T asTypeWhichExtendsNumber = (T) n; // warning
}
Extra. if I want to add an empty constructor, is any way to keep a "ZERO" in element?
NumeircBox<Short> o = new NumericBox<>(); // expectes 0 in the element
回答1:
Lets look at the code in question, your class
declaration:
public class NumericBox<T extends Number> implements Box<T>
So we have some type T
that extends Number
. This might be Long
, Integer
etc. But some specific and defined at compile time type.
Now you have a method:
public T insert(Number newElement) {
T oldElement = this.element;
this.element = (T) newElement;
return oldElement;
}
Which takes some Number
of any type - neither specfic nor defined at compile time and you coerce it into a T
. This is a ClassCastException
waiting to happen.
final NumericBox<BigDecimal> box = new NumericBox<>();
box.insert(3);
final BigDecimal val = box.getElement(); <-- OOPS!
There is no way to avoid this warning because the compiler is telling you example this, you have an unchecked cast that could, at runtime, cause a ClassCastException
. The compiler is washing its hands of that line.
So, to solve your problem, change the code to:
public T insert(T newElement) {
T oldElement = this.element;
this.element = (T) newElement;
return oldElement;
}
As generics are erased at compile time, there is not really any way of fixing that hack. If you want to guarantee runtime type safety you need to change the signature. If you want to take "any old Number
" then you will have to deal with the consequences.
回答2:
Declare the "insert" method as taking a parameter of type T, not Number. Right now the caller can pass any Number, not just the one which matches the T type parameter of your class, and you'll get a ClassCastException at runtime if the type doesn't match.
来源:https://stackoverflow.com/questions/27720243/solve-unchecked-warning-in-java-avoiding-suppresswarnings