----( Java .class file structure )---------------------------------------------- this gives a hint on how the java .class files are structured. for details see the Java VM Specification. supporting files can be found on http://plain.at/vpavlu/class/ ClassFile { u4 magic = 0xCAFEBABE; u2 minor_version; //class file version u2 major_version; //class file version u2 constant_pool_count; // number of cp_info elems (1 = empty pool) indexed by 1..N-1 cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; //valid index to CONSTANT_Class_info elem in constant_pool u2 super_class; //like this_class or zero (but then this class must be the class Object) u2 interfaces_count; //number of super interfaces 0..N-1 u2 interfaces[interfaces_count]; u2 fields_count; //number of class/instance variables (not inherited ones) field_info fields[fields_count]; u2 methods_count; //number of methods (not inherited ones) method_info methods[methods_count]; u2 attributes_count; //number of attributes (SourceFile, Deprecated, ...) attribute_info attributes[attributes_count]; } -- CONSTANT POOL (cp.c) -- cp_info { u1 tag; u1 info[]; } derived type cp_info.tag ----------------------------------------- CONSTANT_Utf8 1 CONSTANT_Integer 3 CONSTANT_Float 4 CONSTANT_Long 5 CONSTANT_Double 6 CONSTANT_Class 7 CONSTANT_String 8 CONSTANT_Fieldref 9 CONSTANT_Methodref 10 CONSTANT_InterfaceMethodref 11 CONSTANT_NameAndType 12 cp_info derived types: CONSTANT_Utf8_info { u1 tag=1; u2 bytes_length; //number of bytes (not null-terminated) u1 bytes[length]; } CONSTANT_Integer_info { //or Float u1 tag; //3 (int), or 4 (float) u4 bytes; //big-endian bytes determining value } CONSTANT_Long_info { //or Double u1 tag; //5 (long), 6 (double) u4 high_bytes; u4 low_bytes; } CONSTANT_Class_info { u1 tag=7; u2 name_index; //CONSTANT_Utf8_info in cp } CONSTANT_String_info { u1 tag=8; u2 string_index; //CONSTANT_Utf8_info in cp } CONSTANT_XXXref_info { //XXX = {Field|Method|InterfaceMethod} u1 tag; //9, 10, or 11 u2 class_index; //CONSTANT_Class_info in cp u2 name_and_type_index; //CONSTANT_NameAndType_info in cp } CONSTANT_NameAndType_info { u1 tag=12; u2 name_index; //CONSTANT_Utf8_info in cp u2 descriptor_index; //CONSTANT_Utf8_info in cp } -- FIELDS (members.c) -- field_info { u2 access_flags; //access permission u2 name_index; //CONSTANT_Utf8_info (name) in cp u2 descriptor_index; //CONSTANT_Utf8_info (field descriptor) in cp u2 attributes_count; attribute_info attributes[attributes_count]; } -- ACCESS MODIFER (access.c) -- ACC_PUBLIC -- 0x0001, accessed from outside package ACC_PRIVATE -- 0x0002, only within defining class ACC_PROTECTED -- 0x0004, within subclasses ACC_STATIC -- 0x0008, static (class variable) ACC_FINAL -- 0x0010, no further assignment after initialization additional for fields ACC_VOLATILE -- 0x0040, cannot be cached (FIELDS) ACC_TRANSIENT -- 0x0080, not written/read by persistent object manager (FIELDS) additional for methods ACC_SYNCHRONIZED -- 0x0020, invocation is wrapped in monitor lock ACC_NATIVE -- 0x0100, implemented in non-java ACC_ABSTRACT -- 0x0400, no implementation ACC_STRICT -- 0x0800, floating-point mode is strict FP -- METHODS (members.c) -- method_info { u2 access_flags; //access permission u2 name_index; //CONSTANT_Utf8_info (name, , ) in cp u2 descriptor_index; //CONSTANT_Utf8_info (method descriptor) in cp u2 attributes_count; attribute_info attributes[attributes_count]; } -- ATTRIBUTES -- attribute_info { u2 attribute_name_index; //CONSTANT_Utf8_info (name) in cp u4 attribute_length; //length in bytes u1 info[attribute_length]; //array of attribute bytes } attribute_info derived types: ConstantValue_attribute { u2 attribute_name_index; //CONSTANT_Utf8_info ("ConstantValue") in cp u4 attribute_length; u2 constantvalue_index; //CONSTANT_XXX (the value) in cp } Code_attribute { u2 attribute_name_index; //CONSTANT_Utf8_info ("Code") in cp u4 attribute_length; //length without initial 6 bytes u2 max_stack; //max depth of stack at any point u2 max_locals; //number of locals and params u4 code_length; //>0 bytes in code array u1 code[code_length]; //byte code instructions u2 exception_table_length; //number of entries in exception table { u2 start_pc; //starting program counter (valid index in code) u2 end_pc; //first pc value where execption is inactive (>start_pc, max code_length) u2 handler_pc; //index in code, specifying the entry point for exception handler u2 catch_type; //zero or CONSTANT_Class_info in cp giving exception type } exception_table[exception_table_length]; u2 attributes_count; //attributes of code attribute_info attributes[attributes_count]; } Exceptions_attribute { u2 attribute_name_index; //CONSTANT_Utf8_info ("Exceptions") in cp u4 attribute_length; //length without initial 6 bytes u2 number_of_exceptions; u2 exception_index_table[number_of_exceptions]; } InnerClasses_attribute { u2 attribute_name_index; u4 attribute_length; u2 number_of_classes; { u2 inner_class_info_index; u2 outer_class_info_index; u2 inner_name_index; u2 inner_class_access_flags; } classes[number_of_classes]; } SourceFile_attribute { u2 attribute_name_index; u4 attribute_length; u2 sourcefile_index; //CONSTANT_Utf8_info in cp } LineNumberTable_attribute { u2 attribute_name_index; u4 attribute_length; u2 line_number_table_length; { u2 start_pc; u2 line_number; } line_number_table[line_number_table_length]; } LocalVariableTable_attribute { u2 attribute_name_index; u4 attribute_length; u2 local_variable_table_length; { u2 start_pc; u2 length; u2 name_index; u2 descriptor_index; u2 index; } local_variable_table[local_variable_table_length]; } ---------------------------------------------------------(vpavlu, 22May2004)----