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.1).
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.