[kaffe] libraries/clib/native/Field.c

Dalibor Topic robilad at yahoo.com
Wed May 22 04:02:26 PDT 2002


hi godmar,

--- Godmar Back <gback at cs.utah.edu> wrote:
> The idea would of course be to improve on Sun's
> error messages
> where possible.  For instance, this program:
> 
>     // Cast.java
>     public class Cast {
> 	public static void main(String []av) {
> 	    Object o = new Integer(42);
> 	    Byte b = (Byte)o;
> 	}
>     }
> 
> outputs under Sun's JDK:
> 
> Exception in thread "main"
> java.lang.ClassCastException: java.lang.Integer
> 	at Cast.main(Cast.java:5)
> 
> Kaffe's output is more informative:
> 
> java.lang.ClassCastException: can't cast
> `java/lang/Integer' to `java/lang/Byte'
> 	at Cast.main(Cast.java:5)

Yeah, it is better.

> (Especially when you get into classloaders and
> reflection.)

Yeah, class loaders complicate things further. You
want to give as much context information as necessary
in an error message.

So given that you can have different ClassLoaders in
different threads with non-matching object
hierarchies, you could actually have a case where an
instance of the class A can be cast to class B in one
namespace, but an instance of another class A can't be
cast to class B in a different namespace. With Java
1.2 class loading semantics you need the whole class
loader chain. So improving on the message would give
us:

Exception in thread "t" in thread group "tg":
java.lang.ClassCastException: can't cast A to B.

Class A was loaded by system class loader from
file://A.class.
Class B was loaded by YetAnotherClassLoader from
http://b-class.org/B.class.
Class YetAnotherClassLoader was loaded by system class
loader from file://YetAnotherClassLoader.class

	at Cast.main(Cast.java:5).

But we can improve even this one, by being more
precise on the reason for the ClassCastException: we
could cite the JVMSpec to reference the broken rule
that led to the exception.

And for bonus points: i18n! Here is my germanized
version:

Ausnahme im Thread "t" in der Threadgruppe "tg":
java.lang.ClassCastException: kann A nicht zu B casten
[nach der JVM Spezifikation 2.0 §1.2.3.4.].

Klasse A wurde von system class loader geladen, aus
file://A.class.
Klasse B wurde von YetAnotherClassLoader geladen, aus
http://b-class.org/B.class.
Klasse YetAnotherClassLoader wurde von system class
loader geladen, aus file://YetAnotherClassLoader.class

	bei Cast.main(Cast.java:5).

Finally, you might want to add some advice how to
resolve the problem:

Exception in thread "t" in thread group "tg":
java.lang.ClassCastException: can't cast A to B
[according to JVM Specification 2.0 §1.2.3.4.].

A program can only cast instances of class A to
classes A, C, D and java.lang.Object, and to
interfaces E and F.

If the problem occured with classes named
java.something or kaffe.something, then you may have
found a bug in kaffe. Please report it to the kaffe
mailing list at kaffe at kaffe.org. See
FAQ/FAQ.reporting-bugs for more information on how to
report bugs.

Class A was loaded by system class loader from
file://A.class.
Class B was loaded by YetAnotherClassLoader from
http://b-class.org/B.class.
Class YetAnotherClassLoader was loaded by system class
loader from file://YetAnotherClassLoader.class.

	at Cast.main(Cast.java:5).

Actually, we could print the kaffe bug report info on
demand ;)

If you want to hack on it, I think could write some
code that gives you loader hierarchies & the
cast-closure of a class. The extended messages could
be inserted within exception constructors. I18N could
go into the getLocalizedMessage part.

best regards,

dalibor topic

__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com




More information about the kaffe mailing list