The invokespecial
JVM instruction is used for calling initialisation methods (<init>
) when creating new objects. The description of the instruction suggests (but doesn't clarify) that the decision on whether to call the constructor of a superclass or a constructor of the current class depends on the state of the ACC_SUPER
flag set within the class
file.
From the Sun JVM Specification:
Next, the resolved method is selected for invocation unless all of the following conditions are true:
- The ACC_SUPER flag (see Table 4.1, "Class access and property modifiers") is set for the current class.
-- Source (invokespecial
opcode definition)
The setting of the ACC_SUPER flag indicates which of two alternative semantics for its invokespecial instruction the Java virtual machine is to express; the ACC_SUPER flag exists for backward compatibility for code compiled by Sun's older compilers for the Java programming language. All new implementations of the Java virtual machine should implement the semantics for invokespecial documented in this specification. All new compilers to the instruction set of the Java virtual machine should set the ACC_SUPER flag. Sun's older compilers generated ClassFile flags with ACC_SUPER unset. Sun's older Java virtual machine implementations ignore the flag if it is set.
-- Source (ClassFile
format)
The definition states that the flag is for backward compatibility with old compilers. However it goes on to contradict with Sun's older Java virtual machine implementations ignore the flag if it is set.
Is the flag still used with the invokespecial
opcode? From what I can tell, it seems to hold no purpose and I can't find a resource to suggest it ever did.
Thanks.
ACC_SUPER was introduced to correct a problem with the invocation of super methods. The ACC_SUPER flag marks a class as compiled for the changed semantics of the opcode 183 instruction. It's purpose is similar to that of the class file version number as it allows the JVM to detect whether a class was compiled for the older or newer semantics of that instruction. Java 1.0.2 did not set and ignored ACC_SUPER while Java 1.1 and later always sets ACC_SUPER.
Before Java 1.1 the byte code instruction with opcode 183 that is now called
invokespecial
was calledinvokenonvirtual
and had a partially different specification. It was used whenever instance methods had to be invoked without a virtual method lookup. This was the case for private methods, instance initializers (constructors) and to implement method invocations onsuper
. But the latter case caused problems with evolving class libraries.A method reference in byte code (
CONSTANT_Methodref_info
) not only defines the name and the argument and return types of a method but also the class to which it belongs. Opcode 183 gets such a method reference parameter and was meant to directly invoke the referenced method from the specified class without further lookups. In the case of invocations onsuper
it was the compilers responsibility to resolve the closest super class that implements this method and generate a reference to it into the byte code.Since Java 1.1 it was changed to essentially ignore the class referenced in
CONSTANT_Methodref_info
and to instead do the lookup for the closest super method with the given method name and signature in the JVM. This is usually done now when the class gets loaded or right before the instruction is executed or JIT compiled the first time.Here is an example why this change was neccessary. In Java 1.0.2 the AWT classes Container and Component were defined this way:
In Java 1.1 the class Conatiner was changed to have it's own implementation of
paint
:Now when you had a direct or indirect subclass of Container that made a call on
super.paint(g)
and compiled it for 1.0.2 it generated ainvokenonvirtual
instruction forComponent.paint
since this was the first parent that had this method. But if you used this compiled class on a JVM that also hadContainer.paint
it would still have calledComponent.paint
which is not what you would expect.On the other hand, when you compiled the class for 1.1 and executed it on a 1.0.2 JVM it would throw a AbstractMethodError or more likely for VMs of that era simply crash. To avoid the crash you had to write
((Component)super).paint(g)
and compile it with a 1.1 compiler to get the desired behaviour in either VM. This would set ACC_SUPER but still generate the instruction to callComponent.paint
. A 1.0.2 VM would ignore ACC_SUPER and go straight to invokeComponent.paint
which is fine while a 1.1 VM would find ACC_SUPER set and thus do the lookup itself which would make it invokeContainer.paint
even though the byte code method reference wasComponent.paint
.You can find more about this in this old post on the ikvm.net weblog.