由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 问一个generic的问题吧
相关主题
[转载] Java 1.5 Generic 问题What do you think about AOP?
error: generic array creationSun的JDK 1.5有什么新特点吗?
pipe & filter 问题再请教一个 编译错误
Interview的人问我最新java 版本是多少一个Java程序员的话(3)
generic 太长怎么办?type erasure weird problem
如何定义这样的数组?新手请教java generic method 调用问题
Alternative way to swap IntegerJava Object 一定在 Heap 里吗
请问有没有generic的array问两个语法问题
相关话题的讨论汇总
话题: integer话题: sortedset话题: type话题: object话题: erased
进入Java版参与讨论
1 (共1页)
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.

1 (共1页)
进入Java版参与讨论
相关主题
问两个语法问题generic 太长怎么办?
JDK HashMap 的 Entry class如何定义这样的数组?
how to copy an Object?Alternative way to swap Integer
Java里面有没有可能写个带generic parameter的class对built-in type也适用?请问有没有generic的array
[转载] Java 1.5 Generic 问题What do you think about AOP?
error: generic array creationSun的JDK 1.5有什么新特点吗?
pipe & filter 问题再请教一个 编译错误
Interview的人问我最新java 版本是多少一个Java程序员的话(3)
相关话题的讨论汇总
话题: integer话题: sortedset话题: type话题: object话题: erased