🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #12608

Class�ļ��ṹ+ʵ�����

#12608
From: ��ţ
Date: Thu, 22 Jul 2004 07:43
475 lines
23362 bytes
���ߣ�watercloud < watercloud@nsfocus.com >
ǰЩʱ���о���һ��JVM����Ҫ������һ��CLASS�ļ��ṹ��
    �����������ޣ���Ҷ�����.   by watercloud    ( watercloud@nsfous.com )
    ����:u1   1���ֽ�Ϊ��λ�ķǸ�ֵ
         u2   2���ֽ�Ϊ��λ�ķǸ�ֵ
         u3   . . . . . . . .  (�����Դ����� )
    Java�ļ��ṹ������struct����������:
    ClassFile {
            u4 magic;               // ����Ϊ: 0xCAFEBABE
            u2 minor_version;
            u2 major_version;       //CLASS�ļ��ṹ���ΰ汾�� JAVA2֧��45.0-
46.0
            u2 constant_pool_count; //��¼������Ϣ
            cp_info constant_pool[constant_pool_count-1];   //�����1��ʼ
            u2 access_flags;        //class/interface����Ȩ��
            u2 this_class;          //ָ��constant_poll�е���Ч����ֵ
            u2 super_class;         //0��ָ��constant_poll�е���Ч����ֵ,����
interface������0
            u2 interfaces_count;    //superinterfaces�ĸ���
            u2 interfaces[interfaces_count];  //����[0,count-1) ��Ӧconstant
_pool�е�һ������ֵ
            u2 fields_count;
            field_info fields[fields_count];  //��Ҫ���ڼ�¼class��ʵ���еı�
��
            u2 methods_count;
            method_info methods[methods_count];
            u2 attributes_count;
            attribute_info attributes[attributes_count];
    }
    cp_info {
            u1 tag;
            u1 info[];
    }
        tag ��������:
        CONSTANT_Class                7
        CONSTANT_Fieldref             9
        CONSTANT_Methodref            10
        CONSTANT_InterfaceMethodref   11
        CONSTANT_String               8
        CONSTANT_Integer              3
        CONSTANT_Float                4
        CONSTANT_Long                 5
        CONSTANT_Double               6
        CONSTANT_NameAndType          12
        CONSTANT_Utf8                 1
        ��ʱcp_info�ֱ��Ӧ�ṹ�仯Ϊ
        1. CONSTANT_Class
        CONSTANT_Class_info {
                u1 tag;
                u2 name_index;
        }
        2. CONSTANT_Fieldref
          CONSTANT_Fieldref_info {
                    u1 tag;
                    u2 class_index;  //constant_pool�������ӦCONSTANT_Cla
ss_info
                    u2 name_and_type_index;//constant_pool�������ӦCONSTA
NT_NameAndType_info
          }
        3. CONSTANT_Methodref
           CONSTANT_Methodref_info {
                    u1 tag;
                    u2 class_index;
                    u2 name_and_type_index;
           }
        4. CONSTANT_InterfaceMethodref
           CONSTANT_InterfaceMethodref_info {
                    u1 tag;
                    u2 class_index;
                    u2 name_and_type_index;
           }
        5. CONSTANT_String
           CONSTANT_String_info {
                    u1 tag;
                    u2 string_index;
            }
        6. CONSTANT_Integer
            CONSTANT_Integer_info {
                    u1 tag;
                    u4 bytes;
            }
        7. CONSTANT_Float
           CONSTANT_Float_info {
                    u1 tag;
                    u4 bytes;
           }
        8. CONSTANT_Long
            CONSTANT_Long_info {
                    u1 tag;
                    u4 high_bytes;
                    u4 low_bytes;
             }
        9. CONSTANT_Double
           CONSTANT_Double_info {
                    u1 tag;
                    u4 high_bytes;
                    u4 low_bytes
           }
        10.CONSTANT_NameAndType
            CONSTANT_NameAndType_info {
                    u1 tag;
                    u2 name_index;
                    u2 descriptor_index;
            }
        11.CONSTANT_Utf8
            CONSTANT_Utf8_info {
                    u1 tag;
                    u2 length;
                    u1 bytes[length];
            }
    access_flags��������:
        ACC_PUBLIC     0x0001
        ACC_FINAL      0x0010
        ACC_SUPER      0x0020
        ACC_INTERFACE  0x0200
        ACC_ABSTRACT   0x0400
      �����interface��ô������ACC_INTERFACE,���û����ACC_INTERFACE�����
��һ�����ǽӿڡ�
      ���������ACC_INTERFACE����ôACC_ABSTRACTλҲ���뱻���ã���ȻҲ��������
ACC_PUBLIC��
      ACC_SUPER���Ա���invokespecial����,Sun��˾�ϵ�JAVA������û������ACC_SU
PER,�����ϵ�JVM
    ����ACC_SUPERλ�����µı�����Ӧ��ʵ��invokespecial���塣
      ����δָ����λ��������ʹ�ã����ұ�����Ӧ��������Ϊ0��ͬʱJava�����Ӧ��
�������ǡ�
   this_class:  constant_pool�е�����ֵ,ָ���Ԫ�ص�cp_info�ȼ�ΪCONSTANT_Cl
ass_info
        CONSTANT_Class_info {
            u1 tag;                 //����ΪCONSTANT_Class (7)
            u2 name_index;          //Ϊָ��constant_pool�е�һ������ֵ
        }
            name_index :ָ���Ԫ�ص�cp_info�ȼ�ΪCONSTANT_Utf8_info
                CONSTANT_Utf8_info {
                       u1 tag;               //����ΪCONSTANT_Utf8 (1)
                       u2 length;
                       u1 bytes[length];     //Utf8������ַ�
                }
    field_info {
            u2 access_flags;   //���ʿ���Ȩ
            u2 name_index;     //constant_pool�е������Ӧ��CONSTANT_Utf8_
info������
            u2 descriptor_index; //constant_pool�е������Ӧ��CONSTANT_Utf
8_info������
            u2 attributes_count;
            attribute_info attributes[attributes_count]; //attribute_info����
mothods��������
    }
       field_info��access_flages��������:
           ACC_PUBLIC     0x0001
           ACC_PRIVATE    0x0002
           ACC_PROTECTED  0x0004
           ACC_STATIC     0x0008
           ACC_FINAL      0x0010
           ACC_VOLATILE   0x0040
           ACC_TRANSIENT  0x0080
           ���к���Ȼ����ͬʱΪACC_FINAL��ACC_VOLATILE ;��ǰ�����ǻ���ġ�
           interface������ACC_PUBLIC, ACC_STATIC,ACC_FINALλ���Ҳ���������λ
��
           ����δָ����λ��������ʹ�ã����ұ�����Ӧ��������Ϊ0��ͬʱJava����
��Ӧ���������ǡ�
    methodsָ�������е����з�����
        method_info {
                    u2 access_flags;
                    u2 name_index;   //ָ��constant_pool����ڣ���ӦΪCONSTA
NT_Utf8_info
                    u2 descriptor_index;  //ָ��constant_pool����ڣ���ӦΪC
ONSTANT_Utf8_info
                    u2 attributes_count;
                    attribute_info attributes[attributes_count];
                    //�˴�ֻ�ܳ���Code��Exceptions��Synthetic��Deprecated����
���͵�����
        }
           access_flags����Ȩ��������:
            ACC_PUBLIC        0x0001
            ACC_PRIVATE       0x0002
            ACC_PROTECTED     0x0004
            ACC_STATIC        0x0008
            ACC_FINAL         0x0010
            ACC_SYNCHRONIZED  0x0020
            ACC_NATIVE        0x0100
            ACC_ABSTRACT      0x0400
            ACC_STRICT        0x0800
    attribute_info {
            u2 attribute_name_index;  //constant_pool�е������Ӧ��CONSTAN
T_Utf8_info������
            u4 attribute_length;
            u1 info[attribute_length];
    }
    �����Ѿ�Ԥ�����������:
      1. SourceFile : attribute_info�����Ϊ:
           SourceFile_attribute {
                    u2 attribute_name_index;
                    u4 attribute_length;
                    u2 sourcefile_index; //ָ��constant_pool�е�һ��CONSTANT
_Utf8_info �ṹ��
           }
      2. ConstantValue : attribute_info�����Ϊ:
          ConstantValue_attribute {
                    u2 attribute_name_index;
                    u4 attribute_length;    //����Ϊ2
                    u2 constantvalue_index;
          }
          ����constantvalue_index��������:
                  long                              CONSTANT_Long
                  float                             CONSTANT_Float
                  double                            CONSTANT_Double
                  int, short, char, byte, boolean   CONSTANT_Integer
                  String                            CONSTANT_String
          ConstantValue����field_info �У���������һ��static����,
          �Ҵ�ʱfield_info��access_flagsӦΪACC_STATIC
      3. Code : attribute_info�����Ϊ:
           Code_attribute {
                    u2 attribute_name_index;
                    u4 attribute_length;
                    u2 max_stack;  //ִ�д˺���ʱ���õ�ջ��������
                    u2 max_locals; //ִ�д˺�����õ�����󱾵ر�����Ŀ������
����
                                   // ע��:һ��long/double�൱��2��������Ŀ.

                    u4 code_length; //�������õ��Ĵ��볤�ȡ�
                    u1 code[code_length]; //ʵ�ֱ�����������ֽ���
                    u2 exception_table_length;
                    {   u2 start_pc;
                        u2 end_pc; //����Υ��ʱִ�д��������е�[start_pc, en
d_pc)����
                        u2  handler_pc; //���ڻ������������Ǹ����!!
                        u2  catch_type; //ָ��constant_pool�������ӦCONST
ANT_Class_info
                    }exception_table[exception_table_length];
                    u2 attributes_count;
                    attribute_info attributes[attributes_count];
           }
                CONSTANT_Class_info {
                    u1 tag;         //����ΪCONSTANT_Class (7)
                    u2 name_index;  //��������˵�˰�?
                 }
           Code��������method_info�ṹ�С�
      4. Exceptions : attribute_info�����Ϊ:
           Exceptions_attribute {
                  u2 attribute_name_index;
                  u4 attribute_length;
                  u2 number_of_exceptions;
                  u2 exception_index_table[number_of_exceptions];
           }
      5. InnerClasses  : attribute_info�����Ϊ:
           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];
            }
      6. Synthetic : attribute_info�����Ϊ:
           Synthetic_attribute {
                 u2 attribute_name_index;  //���÷ϻ��˰�?
                 u4 attribute_length;     //����Ϊ0
           }
          Synthetic���� field_info�� method_info ��
          һ��û�г�����Դ�����еı�������ʹ��Synthetic��ǡ�
      7. LineNumberTable : attribute_info�����Ϊ:
          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];
          }
          LineNumberTable����Code�����У�ͨ�����ڵ��ԡ�
      8. LocalVariableTable : attribute_info�����Ϊ:
              LocalVariableTable_attribute {
                    u2 attribute_name_index;
                    u4 attribute_length;
                    u2 local_variable_table_length;
                    {   u2 start_pc;
                        u2 length; //�����͵����������[start_pc,start_pc+le
ngth]
                                   //ʱ�������뱻��ֵ??
                        u2 name_index;
                        u2 descriptor_index;
                        u2 index;  //�����ر��������һ������
                    } local_variable_table[local_variable_table_length];
              }
      9. Deprecated : attribute_info�����Ϊ:
              Deprecated_attribute {
                        u2 attribute_name_index;
                    u4 attribute_length;   //����Ϊ0
              }
      ��Ȼ��Ҳ���Զ����Լ������ԣ���Ҫ���Լ��ı������������ʵ�֡�JVM��������
�����Ͽɵ����ԡ�
��ʵ��һ�°�!
��дһ����򵥵ij���:
class Test
{
        public static void main(String[] args)
        {
                System.out.println("Hello World!");
        }
}
c:\work>javac Test.java
c:\work>filedump Test.class
File Dump V0.3 Beta by cloud (safesuite@363.net).
01:00    ca fe ba be 00 03 00 2d 00 1d 0a 00 06 00 0f 09   .......-........
01:01    00 10 00 11 08 00 12 0a 00 13 00 14 07 00 15 07   ................
01:02    00 16 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29   .....<init>...()
01:03    56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e   V...Code...LineN
01:04    75 6d 62 65 72 54 61 62 6c 65 01 00 04 6d 61 69   umberTable...mai
01:05    6e 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67   n...([Ljava/lang
01:06    2f 53 74 72 69 6e 67 3b 29 56 01 00 0a 53 6f 75   /String;)V...Sou
01:07    72 63 65 46 69 6c 65 01 00 09 54 65 73 74 2e 6a   rceFile...Test.j
>d
02:00    61 76 61 0c 00 07 00 08 07 00 17 0c 00 18 00 19   ava.............
02:01    01 00 0c 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 07   ...Hello World!.
02:02    00 1a 0c 00 1b 00 1c 01 00 04 54 65 73 74 01 00   ..........Test..
02:03    10 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63   .java/lang/Objec
02:04    74 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 53 79   t...java/lang/Sy
02:05    73 74 65 6d 01 00 03 6f 75 74 01 00 15 4c 6a 61   stem...out...Lja
02:06    76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61   va/io/PrintStrea
02:07    6d 3b 01 00 13 6a 61 76 61 2f 69 6f 2f 50 72 69   m;...java/io/Pri
>d
03:00    6e 74 53 74 72 65 61 6d 01 00 07 70 72 69 6e 74   ntStream...print
03:01    6c 6e 01 00 15 28 4c 6a 61 76 61 2f 6c 61 6e 67   ln...(Ljava/lang
03:02    2f 53 74 72 69 6e 67 3b 29 56 00 20 00 05 00 06   /String;)V. ....
03:03    00 00 00 00 00 02 00 00 00 07 00 08 00 01 00 09   ................
03:04    00 00 00 1d 00 01 00 01 00 00 00 05 2a b7 00 01   ............*...
03:05    b1 00 00 00 01 00 0a 00 00 00 06 00 01 00 00 00   ................
03:06    01 00 09 00 0b 00 0c 00 01 00 09 00 00 00 25 00   ..............%.
03:07    02 00 01 00 00 00 09 b2 00 02 12 03 b6 00 04 b1   ................
>d
04:00    00 00 00 01 00 0a 00 00 00 0a 00 02 00 00 00 05   ................
04:01    00 08 00 06 00 01 00 0d 00 00 00 02 00 0e         ..............
>
���һ�£�
ca fe ba be     magic
00 03 00 2d     �����汾�ţ�����һ��:   45.3 (ע�⣬���Ǹ��ֽ���ǰ,��ְҵ��
��)
00 1d           constant_poolԪ�ظ����һ  :  29  ��ôconstant_pool����[1-28
]
constant_pool: 1-28
1.  0a 00 06 00 0f
                0x0a :CONSTANT_InterfaceMethodref  0x06 :class index  0x0f :
name-type-index
2.  09 00 10 00 11
                0x09 : CONSTANT_Fieldref             0x10:  . . .       0x11
 : . . . .
3.  08 00 12     0x08 : CONSTANT_String    0x12 : string_index
4.  0a 00 13 00 14     0x0aͬ��1.
5.  07 00 15     0x07 : CONSTANT_Class     0x15 : name_index
6.  07 00 16
7.  01 00 06 3c 69 6e 69 74 3e 01  ...<init>
                0x01   CONSTANT_Utf8     0x06  : string length   "<init>" : 
���캯��
8.  01 00 03 28 29 56   ...()V   �����޲���
        0x01   . . . . . .       0x03  : . . . .         "()V"    :  . . .
9.  01 00 04 43 6f 64 65    ...Code
10. 01 00 0f 4c 69 6e 65 4e 75 6d 62 65 72 54 61 62 6c 65  ...LineNumberTabl
e
11. 01 00 04 6d 61 69 6e    ...main
12. 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b 29 
56
           ...([Ljava/lang/String;)V   �������ΪString[]����
13. 01 00 0a 53 6f 75 72 63 65 46 69 6c 65   ...SourceFile
14. 01 00 09 54 65 73 74 2e 6a 61 76 61      ...Test.java
15. 0c 00 07 00 08         0x0c:CONSTANT_NameAndType  07 : name-index 08:nam
e-type-index
16. 07 00 17
17. 0c 00 18 00 19
18. 01 00 0c 48 65 6c 6c 6f 20 57 6f 72 6c 64 21  ...Hello World!
19. 07 00 1a
20. 0c 00 1b 00 1c
21. 01 00 04 54 65 73 74   ...Test
22. 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74   ...java/lang/
Object
23. 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74 65 6d   ...java/lang/
System
24. 01 00 03 6f 75 74    ...out
25. 01 00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d 3b
       ...Ljava/io/PrintStream;
26. 01 00 13 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d...java
/io/PrintStream
27. 01 00 07 70 72 69 6e 74 6c 6e   ...println
28. 01 00 15 28 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b 29 56
       ...(Ljava/lang/String;)V
00 20    access_flags
00 05    this_class
00 06    super_class
00 00    interfaces_count
00 00    fields_count
00 02    methods_count
methods[2]:
method_info {
    u2 access_flags;
    u2 name_index;
    u2 descriptor_index;
    u2 attributes_count;
    attribute_info attributes[attributes_count];
}
0.   00 00     access_flags;
     00 07     name_index;     ����constant_pool�ĵ�7��: <init>  ������ǰ����
���캯��
     00 08     descriptor_index;
     00 01     attributes_count;
     00 09     attribute_name_index   0x09 ����constant_pool�ĵ�9��,������
Code ����
     00 00 00 1d  attribute_length   = 29
     00 01     max_stack
     00 01     max_locals
     00 00 00 05  code_length
     2a b7 00 01 b1       JVM����IJ�������������
     00 00    exception_table_length
     00 01    attributes_count     һ��,��������attribute_info�ṹ
     00 0a    attribute_name_index  ����constant_pool�ĵ�10��: LineNumberTab
le(��Ȼ������)
     00 00 00 06  attribute_length
     00 01    line_number_table_length
     00 00    start_pc
     00 01    line_number
1.   00 09    access_flags   PUBLIC & STATIC
     00 0b    name_index;                   ������ǰ����main����
     00 0c    descriptor_index;   ...([Ljava/lang/String;)V
     00 01    attributes_count;
     00 09    attribute_name_index   Code
     00 00 00 25    attribute_length   = 37
     00 02    max_stack
     00 01    max_locals
     00 00 00 09   code_length
     b2 00 02 12 03 b6 00 04 b1    ��������  codeArray1[0-8]
     00 00    exception_table_length
     00 01    attributes_count    ��������attribute_info�ṹ
     00 0a    attribute_name_index  LineNumberTable
     00 00 00 0a  attribute_length
     00 02    line_number_table_length
     00 00    start_pc
     00 05    line_number
     00 08    start_pc     : codeArray1[8] = 0xb1 -->  return
     00 06    line_number  ��6��Դ����Ϊ  }
00 01     attributes_count
00 0d     attribute_name_index   ����ΪSourceFile
00 00 00 02  attribute_length
00 0e     sourcefile_index    constant_pool[0x0e] --- >  "Test.java"
���������ǿ���main()�����Ӧ�Ĵ���:
b2 00 02 12 03 b6 00 04 b1
0xb2  0x00  0x02 : getstatic #2
         ����constant_pool[0x02] :09 00 10 00 11
                0x09 : CONSTANT_Fieldref         0x10:  class index   0x11 :
name-type-index
      constant_pool[0x10]:  --> constant_pool[0x17]  : java/lang/System
      constant_pool[0x11]:    0x18 : class index   0x19 :name-type-index
               constant_pool[0x18] : out
           constant_pool[0x19] : Ljava/io/PrintStream
             �� System.out Ϊ java.io.PrintStream ����
0x12  0x03  : ldc #3
          ���� constant_pool[3] : 08 00 12     0x08 : CONSTANT_String    0x1
2 : string_index
                    ָ��һ���ַ�  constant_pool[0x12]: "Hello World!"
          �ʸ�ָ��������ַ������ջ
0xb6  0x00  0x04: invokevirtual #4
                        ------->��constant_pool��� 0x13  :class   0x14 :nam
e-type
                                   ����constant_pool[0x13]:java/io/PrintStre
am
                           constant_pool[20] :-->  00 1b 00 1c
                           constant_pool[0x1b]:println
                           . . . .            :(Ljava/lang/String;)V
       �ʸ�ָ������� java.io.PrintStream.println(java.lang.String)
           �������Ϊ ldc #3 ���ص� "Hello World!"
0xb1  : return

--
�Ҷ���   �q�ɨr�����㣩�q�ɨr
�� ��Դ:���Ұ��Ͽ�վ nkbbs.org��[FROM: 202.113.227.189] 
Technical Details
Message-ID: <4DKJQj$AGO@bbs.nankai.edu.cn>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.moat.net!ctu-peer!news.nctu.edu.tw!netnews.csie.nctu.edu.tw!maily.cic.tsinghua.edu.cn!news2.happynet.org!NanKai

Show Full Headers
Complete Article Headers
X-Google-Thread: f6cb0,6f62d99a90b2e35c,start
X-Google-Attributes: gidf6cb0,public
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.moat.net!ctu-peer!news.nctu.edu.tw!netnews.csie.nctu.edu.tw!maily.cic.tsinghua.edu.cn!news2.happynet.org!NanKai
From: holyelle.bbs@bbs.nankai.edu.cn (��ţ)
Newsgroups: cn.bbs.comp.lang.java
Subject: Class�ļ��ṹ+ʵ�����
Date: 22 Jul 2004 07:43:43 GMT
Organization: �Ұ��Ͽ�վ
Message-ID: <4DKJQj$AGO@bbs.nankai.edu.cn>
X-Filename: Java/M.1090482189.A