MethodHandles and class initialization
John Rose
john.r.rose at oracle.com
Thu Jul 19 19:35:30 PDT 2012
On Jul 19, 2012, at 7:04 PM, Dain Sundstrom wrote:
> I have some code that tries to get the value of a static final field using a MethodHandle, and I find that the field is null because the class isn't initialized. Is calling a method handle supposed to cause a class to initialize (run the <clinit> method)?
Yes, the behavior of findStaticGetter is based on the getstatic bytecode, which includes (as you expected) the initialization barrier. Same point for putstatic and invokestatic, and REF_newInvokeSpecial (combination of new + <init>).
The JDK 7 implementation has bugs regarding some of these guys. The upcoming integration of "lazy method handles" includes complete coverage for these initialization paths.
> In a related question, what is the best way to force initialization and when do you do the initialization? in the bootstrap method?
The best way to force initialization of a class is to fetch a static field or call a static method. You can do this with reflection, or (if the above bugs are fixed) via method handles. You can also allocate an instance of the class.
— John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20120719/a51d9d2d/attachment.html
More information about the mlvm-dev
mailing list