Another reflection bug; compatibility results
Stuart Ballard
kaffe@rufus.w3.org
Wed, 14 Jun 2000 13:57:13 -0400
Artur Biesiadowski wrote:
>
> Stuart Ballard wrote:
>
> > However, I think that asking for the value of a field, even a public
> > static final one, is enough to cause <clinit> to get called... right? :(
>
> It depends if it is compile time constant. If yes, then clinit will not
> be called. From JLS 12.4.1
>
<snipped>
>
> It seems that it is duty of compiler to not emit such calls. I wonder
> what will happen if getstatic will be called anyway (by creating method
> by hand). From JLS I understand it is illegal code and should be
> verified away ?? Quite implementation dependent I'm afraid.
Unfortunately, I'm not generating the code by hand, but using
Reflection; reflection hadn't even been written when the JLS was created
and the reflection javadocs are pretty lame. As far as I can tell the
JLS has never been adequately updated to cover it. The reflection
specification from the 1.1 docs (at
http://java.sun.com/products/jdk/1.1/docs/guide/reflection/) is just a
copy of the javadocs in a different format, with some other basic
information about security models and things but nothing about the
obscure situations that come up, like this one.
What I really want is some sort of reflective call that will *tell* me
if a field is a compile-time constant (well, technically a primitive
constant, which additionally requires that it be static and final) and
allow me to get its value *if it is* without calling <clinit>. There's
clearly no such method in the standard reflection classes :( It would be
easy to add one to Kaffe, but I want to be writing portable Java code...
I may have to resort to one of these bytecode-interpretation libraries
eventually :(
Stuart.