T*****e 发帖数: 361 | 1 该怎么样cast 一个generic的对象啊?
比方说:
SortedSet s0 = new TreeSet();
Object obj o = s0;
...
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
但是这样cast会导致warning:
Type safety: The cast from Object to SortedSet is actually checking
against the erased type SortedSet.
如果:
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
则在add的时候会有如下的warning:
Type safety: The method add(Object) belongs to the raw type Set. References to
generic type Set should be par |
T*****e 发帖数: 361 | 2 Can anybody help on this? Thanks.
【在 T*****e 的大作中提到】 : 该怎么样cast 一个generic的对象啊? : 比方说: : SortedSet s0 = new TreeSet(); : Object obj o = s0; : ... : SortedSet s1 = (SortedSet) o; : s1.add( new Integer(1) ); : 但是这样cast会导致warning: : Type safety: The cast from Object to SortedSet is actually checking : against the erased type SortedSet.
|
w*******g 发帖数: 9932 | 3 I think the problem is caused by backwards compatability of Java.
SortedSet can be casted back to TreeSet without problem
but if you let Object o = s0, then the runtime type of s0 is erased to
TreeSet to make sure that TreeSet is a subtype of Object.
The warning is fine and you can just ignore it. The warning is there because
Java compiler is not smart enough to tell whether you are doing the right thing
and the runtime check is not sufficient.
【在 T*****e 的大作中提到】 : 该怎么样cast 一个generic的对象啊? : 比方说: : SortedSet s0 = new TreeSet(); : Object obj o = s0; : ... : SortedSet s1 = (SortedSet) o; : s1.add( new Integer(1) ); : 但是这样cast会导致warning: : Type safety: The cast from Object to SortedSet is actually checking : against the erased type SortedSet.
|
T*****e 发帖数: 361 | 4 多谢!
真希望能够找到办法消除这些warnings,毕竟看着很难受。
thing
【在 w*******g 的大作中提到】 : I think the problem is caused by backwards compatability of Java. : SortedSet can be casted back to TreeSet without problem : but if you let Object o = s0, then the runtime type of s0 is erased to : TreeSet to make sure that TreeSet is a subtype of Object. : The warning is fine and you can just ignore it. The warning is there because : Java compiler is not smart enough to tell whether you are doing the right thing : and the runtime check is not sufficient.
|
m******t 发帖数: 2416 | 5
Java 5.0 added the "suppressWarning" annotation for this. Unfortunately even
Sun's own JDK 5 doesn't fully support it at this point.
【在 T*****e 的大作中提到】 : 多谢! : 真希望能够找到办法消除这些warnings,毕竟看着很难受。 : : thing
|
T*****e 发帖数: 361 | 6 Yes. I had googled for a solution to that problem and found an article,
which had detailed explanation about the warnings and type erasure.
It memtioned the annotation solution.
I guess it is a better solution for the compiler to figure out a way to
remember the erased type information. A fatal error can hide behide a
warning anyway.
【在 m******t 的大作中提到】 : : Java 5.0 added the "suppressWarning" annotation for this. Unfortunately even : Sun's own JDK 5 doesn't fully support it at this point.
|
m******t 发帖数: 2416 | 7
Well the whole point of the compiler giving out warnings rather than
errors is precisely that it can't determine whether it's a real error,
right? 8-)
【在 T*****e 的大作中提到】 : Yes. I had googled for a solution to that problem and found an article, : which had detailed explanation about the warnings and type erasure. : It memtioned the annotation solution. : I guess it is a better solution for the compiler to figure out a way to : remember the erased type information. A fatal error can hide behide a : warning anyway.
|
T*****e 发帖数: 361 | 8 Exactly. That's why I feel uncomfortable with warnings and would
like to get rid of every warning if possible.
【在 m******t 的大作中提到】 : : Well the whole point of the compiler giving out warnings rather than : errors is precisely that it can't determine whether it's a real error, : right? 8-)
|
m******t 发帖数: 2416 | 9
I hear you - I'm myself a cleaning-freak on warnings ;-) . My point, though,
was that in this case there's no way for the compiler to figure out the
erased type information (it's *erased*, remember?) - short of actually
executing the code.
【在 T*****e 的大作中提到】 : Exactly. That's why I feel uncomfortable with warnings and would : like to get rid of every warning if possible.
|