The code for a method, instance initialization method, or class or interface initialization method (§2.9) is stored in the code array of the Code attribute of a method_info structure of a class file (§4.7.3). This section describes the constraints associated with the contents of the Code_attribute structure.
4.9.1. Static Constraints
The static constraints on a class file are those defining the well-formedness of the file. These constraints have been given in the previous sections, except for static constraints on the code in the class file. The static constraints on the code in a class file specify how Java Virtual Machine instructions must be laid out in the code array and what the operands of individual instructions must be.
The static constraints on the instructions in the code array are as follows:
-
Only instances of the instructions documented in §6.5 may appear in the
codearray. Instances of instructions using the reserved opcodes (§6.2) or any opcodes not documented in this specification must not appear in thecodearray.If the
classfile version number is 51.0 or above, then neither the jsr opcode or the jsr_w opcode may appear in thecodearray. -
The opcode of the first instruction in the
codearray begins at index0. -
For each instruction in the
codearray except the last, the index of the opcode of the next instruction equals the index of the opcode of the current instruction plus the length of that instruction, including all its operands.The wide instruction is treated like any other instruction for these purposes; the opcode specifying the operation that a wide instruction is to modify is treated as one of the operands of that wide instruction. That opcode must never be directly reachable by the computation.
-
The last byte of the last instruction in the
codearray must be the byte at indexcode_length - 1.
The static constraints on the operands of instructions in the code array are as follows:
-
The target of each jump and branch instruction (jsr, jsr_w, goto, goto_w, ifeq, ifne, ifle, iflt, ifge, ifgt, ifnull, ifnonnull, if_icmpeq, if_icmpne, if_icmple, if_icmplt, if_icmpge, if_icmpgt, if_acmpeq, if_acmpne) must be the opcode of an instruction within this method.
The target of a jump or branch instruction must never be the opcode used to specify the operation to be modified by a wide instruction; a jump or branch target may be the wide instruction itself.
-
Each target, including the default, of each tableswitch instruction must be the opcode of an instruction within this method.
Each tableswitch instruction must have a number of entries in its jump table that is consistent with the value of its low and high jump table operands, and its low value must be less than or equal to its high value.
No target of a tableswitch instruction may be the opcode used to specify the operation to be modified by a wide instruction; a tableswitch target may be a wide instruction itself.
-
Each target, including the default, of each lookupswitch instruction must be the opcode of an instruction within this method.
Each lookupswitch instruction must have a number of match-offset pairs that is consistent with the value of its npairs operand. The match-offset pairs must be sorted in increasing numerical order by signed match value.
No target of a lookupswitch instruction may be the opcode used to specify the operation to be modified by a wide instruction; a lookupswitch target may be a wide instruction itself.
-
The operand of each ldc instruction and each ldc_w instruction must be a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of type:CONSTANT_Integer,CONSTANT_Float, orCONSTANT_Stringif theclassfile version number is less than 49.0.CONSTANT_Integer,CONSTANT_Float,CONSTANT_String, orCONSTANT_Classif theclassfile version number is 49.0 or 50.0.CONSTANT_Integer,CONSTANT_Float,CONSTANT_String,CONSTANT_Class,CONSTANT_MethodType, orCONSTANT_MethodHandleif theclassfile version number is 51.0 or above.
-
The operands of each ldc2_w instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_LongorCONSTANT_Double.The subsequent constant pool index must also be a valid index into the constant pool, and the constant pool entry at that index must not be used.
-
The operands of each getfield, putfield, getstatic, and putstatic instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_Fieldref. -
The indexbyte operands of each invokevirtual instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_Methodref. -
The indexbyte operands of each invokespecial and invokestatic instruction must represent a valid index into the
constant_pooltable. If theclassfile version number is less than 52.0, the constant pool entry referenced by that index must be of typeCONSTANT_Methodref; if theclassfile version number is 52.0 or above, the constant pool entry referenced by that index must be of typeCONSTANT_MethodreforCONSTANT_InterfaceMethodref. -
The indexbyte operands of each invokeinterface instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_InterfaceMethodref.The value of the count operand of each invokeinterface instruction must reflect the number of local variables necessary to store the arguments to be passed to the interface method, as implied by the descriptor of the
CONSTANT_NameAndType_infostructure referenced by theCONSTANT_InterfaceMethodrefconstant pool entry.The fourth operand byte of each invokeinterface instruction must have the value zero.
-
The indexbyte operands of each invokedynamic instruction must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_InvokeDynamic.The third and fourth operand bytes of each invokedynamic instruction must have the value zero.
-
Only the invokespecial instruction is allowed to invoke an instance initialization method (§2.9).
No other method whose name begins with the character '
<' ('\u003c') may be called by the method invocation instructions. In particular, the class or interface initialization method specially named<clinit>is never called explicitly from Java Virtual Machine instructions, but only implicitly by the Java Virtual Machine itself. -
The operands of each instanceof, checkcast, new, and anewarray instruction, and the indexbyte operands of each multianewarray instruction, must represent a valid index into the
constant_pooltable. The constant pool entry referenced by that index must be of typeCONSTANT_Class. -
No new instruction may reference a constant pool entry of type
CONSTANT_Classthat represents an array type (§4.3.2). The new instruction cannot be used to create an array. -
No anewarray instruction may be used to create an array of more than 255 dimensions.
-
A multianewarray instruction must be used only to create an array of a type that has at least as many dimensions as the value of its dimensions operand. That is, while a multianewarray instruction is not required to create all of the dimensions of the array type referenced by its indexbyte operands, it must not attempt to create more dimensions than are in the array type.
The dimensions operand of each multianewarray instruction must not be zero.
-
The atype operand of each newarray instruction must take one of the values
T_BOOLEAN(4),T_CHAR(5),T_FLOAT(6),T_DOUBLE(7),T_BYTE(8),T_SHORT(9),T_INT(10), orT_LONG(11). -
The index operand of each iload, fload, aload, istore, fstore, astore, iinc, and ret instruction must be a non-negative integer no greater than
max_locals - 1.The implicit index of each iload_<n>, fload_<n>, aload_<n>, istore_<n>, fstore_<n>, and astore_<n> instruction must be no greater than
max_locals - 1. -
The index operand of each lload, dload, lstore, and dstore instruction must be no greater than
max_locals - 2.The implicit index of each lload_<n>, dload_<n>, lstore_<n>, and dstore_<n> instruction must be no greater than
max_locals - 2. -
The indexbyte operands of each wide instruction modifying an iload, fload, aload, istore, fstore, astore, iinc, or ret instruction must represent a non-negative integer no greater than
max_locals - 1.The indexbyte operands of each wide instruction modifying an lload, dload, lstore, or dstore instruction must represent a non-negative integer no greater than
max_locals - 2.
4.9.2. Structural Constraints
The structural constraints on the code array specify constraints on relationships between Java Virtual Machine instructions. The structural constraints are as follows:
-
Each instruction must only be executed with the appropriate type and number of arguments in the operand stack and local variable array, regardless of the execution path that leads to its invocation.
An instruction operating on values of type
intis also permitted to operate on values of typeboolean,byte,char, andshort.As noted in §2.3.4 and §2.11.1, the Java Virtual Machine internally converts values of types
boolean,byte,short, andcharto typeint.) -
If an instruction can be executed along several different execution paths, the operand stack must have the same depth (§2.6.2) prior to the execution of the instruction, regardless of the path taken.
-
At no point during execution can the operand stack grow to a depth greater than that implied by the
max_stackitem. -
At no point during execution can more values be popped from the operand stack than it contains.
-
At no point during execution can the order of the local variable pair holding a value of type
longordoublebe reversed or the pair split up. At no point can the local variables of such a pair be operated on individually. -
No local variable (or local variable pair, in the case of a value of type
longordouble) can be accessed before it is assigned a value. -
Each invokespecial instruction must name an instance initialization method (§2.9), a method in the current class or interface, a method in a superclass of the current class, a method in a direct superinterface of the current class or interface, or a method in
Object.When an instance initialization method is invoked, an uninitialized class instance must be in an appropriate position on the operand stack. An instance initialization method must never be invoked on an initialized class instance.
If an invokespecial instruction names an instance initialization method and the target reference on the operand stack is an uninitialized class instance for the current class, then invokespecial must name an instance initialization method from the current class or its direct superclass.
If an invokespecial instruction names an instance initialization method and the target reference on the operand stack is a class instance created by an earlier new instruction, then invokespecial must name an instance initialization method from the class of that class instance.
If an invokespecial instruction names a method which is not an instance initialization method, then the type of the target reference on the operand stack must be assignment compatible with the current class (JLS §5.2).
-
Each instance initialization method, except for the instance initialization method derived from the constructor of class
Object, must call either another instance initialization method ofthisor an instance initialization method of its direct superclasssuperbefore its instance members are accessed.However, instance fields of
thisthat are declared in the current class may be assigned before calling any instance initialization method. -
When any instance method is invoked or when any instance variable is accessed, the class instance that contains the instance method or instance variable must already be initialized.
-
If there is an uninitialized class instance in a local variable in code protected by an exception handler, then i) if the handler is inside an
<init>method, the handler must throw an exception or loop forever, and ii) if the handler is not inside an<init>method, the uninitialized class instance must remain uninitialized. -
There must never be an uninitialized class instance on the operand stack or in a local variable when a jsr or jsr_w instruction is executed.
-
The type of every class instance that is the target of a method invocation instruction must be assignment compatible with the class or interface type specified in the instruction (JLS §5.2).
-
The types of the arguments to each method invocation must be method invocation compatible with the method descriptor (JLS §5.3, §4.3.3).
-
Each return instruction must match its method's return type:
- If the method returns a
boolean,byte,char,short, orint, only the ireturn instruction may be used. - If the method returns a
float,long, ordouble, only an freturn, lreturn, or dreturn instruction, respectively, may be used. - If the method returns a
referencetype, only an areturn instruction may be used, and the type of the returned value must be assignment compatible with the return descriptor of the method (JLS §5.2, §4.3.3). - All instance initialization methods, class or interface initialization methods, and methods declared to return
voidmust use only the return instruction.
- If the method returns a
-
The type of every class instance accessed by a getfield instruction or modified by a putfield instruction must be assignment compatible with the class type specified in the instruction (JLS §5.2).
-
The type of every value stored by a putfield or putstatic instruction must be compatible with the descriptor of the field (§4.3.2) of the class instance or class being stored into:
- If the descriptor type is
boolean,byte,char,short, orint, then the value must be anint. - If the descriptor type is
float,long, ordouble, then the value must be afloat,long, ordouble, respectively. - If the descriptor type is a
referencetype, then the value must be of a type that is assignment compatible with the descriptor type (JLS §5.2).
- If the descriptor type is
-
The type of every value stored into an array by an aastore instruction must be a
referencetype.The component type of the array being stored into by the aastore instruction must also be a
referencetype. -
Each athrow instruction must throw only values that are instances of class
Throwableor of subclasses ofThrowable.Each class mentioned in a
catch_typeitem of theexception_tablearray of the method'sCode_attributestructure must beThrowableor a subclass ofThrowable. -
If getfield or putfield is used to access a
protectedfield declared in a superclass that is a member of a different run-time package than the current class, then the type of the class instance being accessed must be the same as or a subclass of the current class.If invokevirtual or invokespecial is used to access a
protectedmethod declared in a superclass that is a member of a different run-time package than the current class, then the type of the class instance being accessed must be the same as or a subclass of the current class. -
Execution never falls off the bottom of the
codearray. -
No return address (a value of type
returnAddress) may be loaded from a local variable. -
The instruction following each jsr or jsr_w instruction may be returned to only by a single ret instruction.
-
No jsr or jsr_w instruction that is returned to may be used to recursively call a subroutine if that subroutine is already present in the subroutine call chain. (Subroutines can be nested when using
try-finallyconstructs from within afinallyclause.) -
Each instance of type
returnAddresscan be returned to at most once.If a ret instruction returns to a point in the subroutine call chain above the ret instruction corresponding to a given instance of type
returnAddress, then that instance can never be used as a return address.