由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - type erasure weird problem
相关主题
JSP is rubbish!关于得到generic type的问题求助!
It's a Java Bugself-modifying code?
Question on J2EE containerGetting bytecode of a class
入门Java CLASSPATH问题:java的接口runnable
one java question under ubuntujboss hot deployment in eclipse
ant javac error in eclipse 3.0?generics这样改对马?
How big is the penalty for compile with debugging mode on?菜鸟问题一问
也问一个Eclipse的问题Java on AIX
相关话题的讨论汇总
话题: string话题: integer话题: list话题: byte话题: java
进入Java版参与讨论
1 (共1页)
c******n
发帖数: 4965
1
the following code compiles fine under commandline
but it fails in eclipse, with error msg:
....having the same erasure ......
I checked the .class bytecode , it is exactly the same for the 2
methods (except for return and exception), caller code is able to pick
up the differences because javac actually did add annotation about the
generics parameter, so "erasure" is a lie!!!!!
import java.util.Map;
class ffff{
private static Integer fun(Map s) throws Exception {
return null;}
private static String fun(Map s){
return null;
}
}
N***m
发帖数: 4460
2
MyEclipse没有任何问题

【在 c******n 的大作中提到】
: the following code compiles fine under commandline
: but it fails in eclipse, with error msg:
: ....having the same erasure ......
: I checked the .class bytecode , it is exactly the same for the 2
: methods (except for return and exception), caller code is able to pick
: up the differences because javac actually did add annotation about the
: generics parameter, so "erasure" is a lie!!!!!
: import java.util.Map;
: class ffff{
: private static Integer fun(Map s) throws Exception {

F****n
发帖数: 3271
3
What will happen if I call
Map m = new HashMap();
fun(m);
How can it pick up the difference??

【在 c******n 的大作中提到】
: the following code compiles fine under commandline
: but it fails in eclipse, with error msg:
: ....having the same erasure ......
: I checked the .class bytecode , it is exactly the same for the 2
: methods (except for return and exception), caller code is able to pick
: up the differences because javac actually did add annotation about the
: generics parameter, so "erasure" is a lie!!!!!
: import java.util.Map;
: class ffff{
: private static Integer fun(Map s) throws Exception {

s******n
发帖数: 876
4
I read it somewhere but I can't be sure I remember it clearly.
Apparently, even before Generics, at java byte code level,
a class is allowed to have two methods of same signature,
if they have different return types.
class A // pseudo byte code
f(List)->String
f(List)->Integer
The byte code reference to a method actually includes the
return type, therefore the two methods can be distinguished.
// pseudo byte code invoking two methods
(A.f(List)->String) ();
(A.f(List)->Integer) ();
Of course, at java source level, we can't have such 2 methods.
That's only a compile time restriction. If you manually
create a class file with such methods, there's no problem with JVM.
With Generics, at source level,
class B
String f(List x);
Integer f(List x);
The two signatures can be distinguished at compile time, and
javac6 allows them; But only if they have different return types!
class C
void f(List x);
void f(List x);
C is illegal, because the compiled class file would
contain two methods that are identical at byte code level,
so javac6 doesn't allow C.
The language spec actually says nothing about return type.
Both B and C should be legal. Compilers should have no problem.
But C is impossible to be compiled into current byte code format.
Given the conflicting specs about java and byte code,
what should compilers do? Javac 5 disallowed B&C.
Java 6 allows B, not C. And Java 7 will disallow B&C again,
probably accompanied by amendment to spec.
Which is really a shame. It seems a perfectly legitimate need
to overload methods with different generic types.
F****n
发帖数: 3271
5
That's because you confuse Java language vs JVM byte code. They are not the
same by specification and JVM byte code can have a lot of illegal Java
constructs.
In fact today's best obfuscator utilizes this, for instance, directly write
a compiled byte code to replace Java names with weird illegal characters to prevent
decompiling.
Back to the question, I think LZ simply made a wrong observation during compiling. The class can be compiled but the caller will have problem if no Generic parameters are specified. He must have used javac to ONLY compile the class declaration file, so it appeared OK. But there is another file which contains caller, and Eclipse try to build them all and the problem shows.

【在 s******n 的大作中提到】
: I read it somewhere but I can't be sure I remember it clearly.
: Apparently, even before Generics, at java byte code level,
: a class is allowed to have two methods of same signature,
: if they have different return types.
: class A // pseudo byte code
: f(List)->String
: f(List)->Integer
: The byte code reference to a method actually includes the
: return type, therefore the two methods can be distinguished.
: // pseudo byte code invoking two methods

1 (共1页)
进入Java版参与讨论
相关主题
Java on AIXone java question under ubuntu
help:tomcat5 on FC4ant javac error in eclipse 3.0?
scala - I 服了 UHow big is the penalty for compile with debugging mode on?
震惊:java 的矩阵操作比 c++ 快?也问一个Eclipse的问题
JSP is rubbish!关于得到generic type的问题求助!
It's a Java Bugself-modifying code?
Question on J2EE containerGetting bytecode of a class
入门Java CLASSPATH问题:java的接口runnable
相关话题的讨论汇总
话题: string话题: integer话题: list话题: byte话题: java