An undefined behavior w.r.t. <clinit>

Yuting Chen chenyt.cs.sjtu at gmail.com
Sun Jul 12 19:15:59 UTC 2015


In jvms, 4.2.2, it also says,

"Note that a field name or interface method name may be <init> or
<clinit>, but no method invocation instruction may reference <clinit>
and only the invokespecial instruction (§invokespecial) may reference
<init>."

Still cannot find any words in jvms for this:
class XXX{ //version 51
   public abstract void <clinit>();
}

It could be better if in jvms 2.9, we have

Other methods named <clinit> in a class file are of no consequence.
They are not class or interface initialization methods. They cannot be
invoked by any Java Virtual Machine instruction and are never invoked
by the Java Virtual Machine itself.
=>
Other methods named <clinit> are not class or interface initialization
methods. They cannot be invoked by any Java Virtual Machine
instruction and are never invoked by the Java Virtual Machine itself.
Methods named <clinit> of no consequence if they are not class or
interface initialization methods.

On Sun, Jul 12, 2015 at 10:02 AM, Yuting Chen <chenyt.cs.sjtu at gmail.com> wrote:
> Hi, Gilles, thank you for your information. I read jvms again, and
> indeed, it says
>
> "
> 1. A class or interface has at most one class or interface
> initialization method and is initialized (§5.5) by invoking that
> method. The initialization method of a class or interface has the
> special name <clinit>, takes no arguments, and is void (§4.3.3).
>
> 2. Other methods named <clinit> in a class file are of no consequence.
> They are not class or interface initialization methods. They cannot be
> invoked by any Java Virtual Machine instruction and are never invoked
> by the Java Virtual Machine itself.
>
> 3. In a class file whose version number is 51.0 or above, the method
> must additionally have its ACC_STATIC flag (§4.6) set in order to be
> the class or interface initialization method.
>
> 4. This requirement was introduced in Java SE 7. In a class file whose
> version number is 50.0 or below, a method named <clinit> that is void
> and takes no arguments is considered the class or interface
> initialization method regardless of the setting of its ACC_STATIC
> flag.
> "
>
> Wait..., the sentence still reads a little strange, because the method
> in my class is "public abstract void <clinit>();" and I still cannot
> decide whether it should have any consequence or not, even though it
> may not be an initialization method because it does not have an
> ACC_STATIC flag. (it seems that the order of the paragraphs should be
> changed to 1->3->4->2.)
>
> In any case, the method has an invalid name (because it has a name
> <clinit>) or invalid signature (because it does not have an ACC_STATIC
> flag). I am afraid that it still should be rejected by HotSpot for
> some format error.
>
> For you question, if the class has a version number less than 51, a
> format error will be thrown out when it is run on HotSpot and IBMJ9
> ("Exception in thread "main" java.lang.ClassFormatError: Absent Code
> attribute in method that is not native or abstract in class file
> M1436188543_V46"). The results are different only when the version
> number is greater than 51.
>
> The two classes are attached. Hope that you can get them.
>
> Yuting Chen


More information about the hotspot-runtime-dev mailing list