Redian新闻
>
问一个generic的问题吧
avatar
问一个generic的问题吧# Java - 爪哇娇娃
T*e
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
avatar
T*e
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.

avatar
w*g
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.

avatar
T*e
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.

avatar
m*t
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

avatar
T*e
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.

avatar
m*t
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.

avatar
T*e
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-)

avatar
m*t
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。