SlideShare a Scribd company logo
なれる!クラスローダー
                   @hakurai




12年12月29日土曜日
クラスファイルフォーマット
       http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html

12年12月29日土曜日
クラスファイル構造

               ClassFile {
                  u4          magic;
                  u2          minor_version;
                  u2          major_version;
                  u2          constant_pool_count;
                  cp_info       constant_pool[constant_pool_count-1];
                  u2          access_flags;
                  u2          this_class;
                  u2          super_class;
                  u2          interfaces_count;
                  u2          interfaces[interfaces_count];
                  u2          fields_count;
                  field_info fields[fields_count];
                  u2          methods_count;
                  method_info methods[methods_count];
                  u2          attributes_count;
                  attribute_info attributes[attributes_count];
               }




12年12月29日土曜日
magic
   ClassFile {                                              マジックナンバー
      u4          magic;                                    0xCAFEBABE
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
minor_version, major_version
   ClassFile {                                              クラスファイルのバージョン
      u4          magic;
      u2          minor_version;                            Java SE 7 = 51
      u2          major_version;
      u2          constant_pool_count;                      Java SE 6 = 50
      cp_info       constant_pool[constant_pool_count-1];   J2SE 5.0 = 49
      u2          access_flags;                              J2SE 1.4 = 48
      u2          this_class;
                                                            J2SE 1.3 = 47
      u2          super_class;
      u2          interfaces_count;                         J2SE 1.2 = 46
      u2          interfaces[interfaces_count];             JDK 1.1 = 45∼
      u2          fields_count;                              JDK 1.0 = 45∼45.3
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
constant_pool_count
   ClassFile {                                              constant_poolのエントリー数+1
      u4          magic;
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
constant_pool
   ClassFile {                                        文字列定数
      u4          magic;                              クラスインタフェース名
      u2          minor_version;                      フィールド名
      u2          major_version;
      u2          constant_pool_count;                クラスファイルの構造
      cp_info constant_pool[constant_pool_count-1];   などの定義を格納した配列
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
access_flags
   ClassFile {                                              このクラスのアクセス宣言
      u4          magic;
      u2          minor_version;                            ACC_PUBLIC
      u2          major_version;
      u2          constant_pool_count;                      ACC_FINAL
      cp_info       constant_pool[constant_pool_count-1];   ACC_SUPER
      u2          access_flags;                              ACC_INTERFACE
      u2          this_class;
                                                            ACC_ABSTRACT
      u2          super_class;
      u2          interfaces_count;                         ACC_SYNTHETIC
      u2          interfaces[interfaces_count];             ACC_ANNOTATION
      u2          fields_count;                              ACC_ENUM
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
this_class
   ClassFile {                                              このクラスを表すconstant_poolの
      u4          magic;                                    エントリのインデックス
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
super_class
   ClassFile {                                              このクラスの親クラスを表す
      u4          magic;                                    constant_poolのエントリのイン
      u2          minor_version;
      u2          major_version;                            デックス
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
interfaces_count
   ClassFile {                                              このクラスが実装するインターフェ
      u4          magic;                                    イスの数
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
interfaces
   ClassFile {                                              このクラスが実装するインターフェ
      u4          magic;                                    イスのconstant_poolのインデッ
      u2          minor_version;                            クスの配列
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
fields_count
   ClassFile {                                              このクラスのフィールドの数
      u4          magic;
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
fields
   ClassFile {                                              フィールド構造の配列
      u4          magic;
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
methods_count
   ClassFile {                                              このクラスのメソッドの数
      u4          magic;
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
method
   ClassFile {                                              メソッド構造の配列
      u4          magic;
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
attributes_count
   ClassFile {                                              属性の数
      u4          magic;
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
attributes
   ClassFile {                                              属性構造の配列
      u4          magic;
      u2          minor_version;
      u2          major_version;
      u2          constant_pool_count;
      cp_info       constant_pool[constant_pool_count-1];
      u2          access_flags;
      u2          this_class;
      u2          super_class;
      u2          interfaces_count;
      u2          interfaces[interfaces_count];
      u2          fields_count;
      field_info fields[fields_count];
      u2          methods_count;
      method_info methods[methods_count];
      u2          attributes_count;
      attribute_info attributes[attributes_count];
   }




12年12月29日土曜日
constant_pool




12年12月29日土曜日
cp_info
   cp_info {                constant_poolエントリーの共通
     u1 tag;                構造
     u1 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
                            など




12年12月29日土曜日
CONSTANT_Methodref
   CONSTANT_Methodref_info {      tag = 10
     u1 tag;                      class_indexはこのメソッドを持つ
     u2 class_index;
                                  クラスを定義した
     u2 name_and_type_index;
   }                              CONSTANT_Classエントリへのイ
                                  ンデックス


                                  name_and_type_index




12年12月29日土曜日
CONSTANT_Class
   CONSTANT_Class_info {             tag = 7
     u1 tag;                         name_indexはこの
     u2 name_index;
                                     CONSTANT_Classの表すクラス名
   }
                                     を定義したCONSTANT_Utf8エン
                                     トリへのインデックス




12年12月29日土曜日
constant_Utf8
   CONSTANT_Utf8_info {              tag = 1
     u1 tag;                         lengthは続くbytesの長さ
     u2 length;
                                     bytesはUTF-8で符号化された文字
     u1 bytes[length];
   }                                 列


                                     クラス中の文字列リテラル
                                     クラスの完全修飾名
                                     フィールド名
                                     メソッド名
                                     メソッドのシグニチャ
                                     など、クラス中のほぼすべての文字
                                     列を定義




12年12月29日土曜日
CONSTANT_NameAndType
   CONSTANT_NameAndType_info {   tag = 12
     u1 tag;                     name_indexはフィールド名もしく
     u2 name_index;
                                 はメソッド名を表す
     u2 descriptor_index;
   }                             CONSTANT_Utf8のエントリへの
                                 インデックス


                                 descriptor_indexはフィールドも
                                 しくはメソッドの詳細を表す
                                 CONSTANT_Utf8のエントリへの
                                 インデックス




12年12月29日土曜日
メソッド String.startsWith

                                   Methodref


                           Class           NameAndType


               Utf8                    Utf8                    Utf8
        java/lang/String             startsWith     (Ljava/lang/String;I)Z

                                                    引数                 戻り値
                                                  String,int          boolean




12年12月29日土曜日
フィールド String.hash

                                   Fieldref


                           Class           NameAndType


               Utf8                   Utf8             Utf8
        java/lang/String            hash                   I

                                                   型 int




12年12月29日土曜日
fields




12年12月29日土曜日
field
   field_info {                                              フィールド1つ分を表す構造体
      u2          access_flags;                              name_indexはフィールド名の
      u2          name_index;
                                                            CONSTANT_Utf8へのインデック
      u2          descriptor_index;
      u2          attributes_count;                         ス
      attribute_info attributes[attributes_count];
   }                                                        descriptor_indexは型名の
                                                            CONSTANT_Utf8へのインデック
                                                            ス


                                                            attributesはこのフィールドの持つ
                                                            属性の配列




12年12月29日土曜日
フィールドの属性
  フィールドの持てる属性は以下の6種類


  ConstantValue
   フィールドの初期値
  Synthetic
   A class member that does not appear in the source code must be marked using a Synthetic attribute
  Signature (Java SE 5.0)
   総称型を含めたフィールドの型情報
  Deprecated
    @deprecated
  RuntimeVisibleAnnotations (Java SE 5.0)
   RetentionPolicy.SOURCEまたはRetentionPolicy.CLASSなアノテーション
  RuntimeInvisibleAnnotations (Java SE 5.0)
   RetentionPolicy.RUNTIMEなアノテーション




12年12月29日土曜日
methods




12年12月29日土曜日
method
   method_info {                                       メソッド1つ分を表す構造体
     u2          access_flags;                          name_indexはメソッド名の
     u2          name_index;
                                                       CONSTANT_Utf8へのインデック
     u2          descriptor_index;
     u2          attributes_count;                     ス
     attribute_info attributes[attributes_count];
   }                                                   descriptor_indexは(引数)戻り値の
                                                       型名のCONSTANT_Utf8へのイン
                                                       デックス


                                                       attributesはこのメソッドの持つ属
                                                       性の配列




12年12月29日土曜日
メソッドの属性
  メソッドの持てる属性は以下の10種類


  Code
  Exceptions
  Synthetic
  Signature (Java SE 5.0)
  Deprecated
  RuntimeVisibleAnnotations (Java SE 5.0)
  RuntimeInvisibleAnnotations (Java SE 5.0)
  RuntimeVisibleParameterAnnotations (Java SE 5.0)
  RuntimeInvisibleParameterAnnotations (Java SE 5.0)
  AnnotationDefault (Java SE 5.0)




12年12月29日土曜日
code




12年12月29日土曜日
code
   Code_attribute {                                    max_stack
     u2 attribute_name_index;                           メソッドの使用する最大オペラ
     u4 attribute_length;
                                                       ンドスタックサイズ
     u2 max_stack;
     u2 max_locals;
     u4 code_length;                                   max_loacls
     u1 code[code_length];                              メソッド内の最大ローカル変数
     u2 exception_table_length;
     { u2 start_pc;                                    数
        u2 end_pc;
        u2 handler_pc;                                 code
        u2 catch_type;
                                                        メソッドのコード本体
     } exception_table[exception_table_length];
     u2 attributes_count;
     attribute_info attributes[attributes_count];      exception_table
   }                                                    例外テーブル




12年12月29日土曜日
code
  バイトコード
  以上




12年12月29日土曜日
exception_table




12年12月29日土曜日
exception_table




        try




12年12月29日土曜日
exception_table




        try




      catch

12年12月29日土曜日
exception_table




        try




      catch

12年12月29日土曜日
exception_table




        try




      catch

12年12月29日土曜日
exception_table
                   CONSTANT_Class
                   Exception




        try




      catch

12年12月29日土曜日
元のコード
     private void readObject(ObjectInputStream s)
        throws IOException, ClassNotFoundException
     {
        s.defaultReadObject();
        ReadObjectCallback cb = readObjectCallbacks.get(s);
        if (cb == null) {
            try {
               readObjectCallbacks.put(s, cb = new ReadObjectCallback(s));
            }
            catch (Exception e) {
               throw new IOException(e.toString());
            }
        }
        cb.registerComponent(this);




12年12月29日土曜日
コード属性の属性
  コード属性の持てる属性は以下の4種類


  LineNumberTable
   PCと元のソースコードの行番号の対応付け
  LocalVariableTable
   ローカル変数の情報
  LocalVariableTypeTable (Java SE 5.0)
   総称型を含めたローカル変数の情報
  StackMapTable (Java SE 6)
   クラスローダがクラスを読み込むときの検証を高速化するために使用
   This attribute is used during the process of verification by typechecking ( 4.10.1).




12年12月29日土曜日
LineNumberTable




12年12月29日土曜日
LocalVariableTable
   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];
   }




12年12月29日土曜日
LocalVariableTypeTable
   LocalVariableTypeTable_attribute {
     u2 attribute_name_index;
     u4 attribute_length;
     u2 local_variable_type_table_length;
     { u2 start_pc;
         u2 length;
         u2 name_index;
         u2 signature_index;
         u2 index;
     } local_variable_type_table[local_variable_type_table_length];
   }




12年12月29日土曜日
クラスの属性
  クラスの持てる属性は以下の10種類


  InnerClasses
   インナークラス
  EnclosingMethod
   無名クラス
  Synthetic
  Signature (Java SE 5.0)
  SourceFile
  SourceDebugExtension
  Deprecated
  RuntimeVisibleAnnotations (Java SE 5.0)
  RuntimeInvisibleAnnotations (Java SE 5.0)
  BootstrapMethods (Java SE 7)




12年12月29日土曜日
EOF




12年12月29日土曜日

More Related Content

PDF
例外設計における大罪
Takuto Wada
 
PDF
JavaFX8
Kazuhiro Eguchi
 
PDF
実践Knockout
Kazuhiro Eguchi
 
KEY
Knockout
Kazuhiro Eguchi
 
KEY
Java 並行処理の基礎update1
Kazuhiro Eguchi
 
KEY
Starting java fx
Kazuhiro Eguchi
 
KEY
並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~
Kazuhiro Eguchi
 
例外設計における大罪
Takuto Wada
 
実践Knockout
Kazuhiro Eguchi
 
Knockout
Kazuhiro Eguchi
 
Java 並行処理の基礎update1
Kazuhiro Eguchi
 
Starting java fx
Kazuhiro Eguchi
 
並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~
Kazuhiro Eguchi
 
Ad

なれる!クラスローダー

  • 1. なれる!クラスローダー @hakurai 12年12月29日土曜日
  • 2. クラスファイルフォーマット http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html 12年12月29日土曜日
  • 3. クラスファイル構造 ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 4. magic ClassFile { マジックナンバー u4 magic; 0xCAFEBABE u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 5. minor_version, major_version ClassFile { クラスファイルのバージョン u4 magic; u2 minor_version; Java SE 7 = 51 u2 major_version; u2 constant_pool_count; Java SE 6 = 50 cp_info constant_pool[constant_pool_count-1]; J2SE 5.0 = 49 u2 access_flags; J2SE 1.4 = 48 u2 this_class; J2SE 1.3 = 47 u2 super_class; u2 interfaces_count; J2SE 1.2 = 46 u2 interfaces[interfaces_count]; JDK 1.1 = 45∼ u2 fields_count; JDK 1.0 = 45∼45.3 field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 6. constant_pool_count ClassFile { constant_poolのエントリー数+1 u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 7. constant_pool ClassFile { 文字列定数 u4 magic; クラスインタフェース名 u2 minor_version; フィールド名 u2 major_version; u2 constant_pool_count; クラスファイルの構造 cp_info constant_pool[constant_pool_count-1]; などの定義を格納した配列 u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 8. access_flags ClassFile { このクラスのアクセス宣言 u4 magic; u2 minor_version; ACC_PUBLIC u2 major_version; u2 constant_pool_count; ACC_FINAL cp_info constant_pool[constant_pool_count-1]; ACC_SUPER u2 access_flags; ACC_INTERFACE u2 this_class; ACC_ABSTRACT u2 super_class; u2 interfaces_count; ACC_SYNTHETIC u2 interfaces[interfaces_count]; ACC_ANNOTATION u2 fields_count; ACC_ENUM field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 9. this_class ClassFile { このクラスを表すconstant_poolの u4 magic; エントリのインデックス u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 10. super_class ClassFile { このクラスの親クラスを表す u4 magic; constant_poolのエントリのイン u2 minor_version; u2 major_version; デックス u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 11. interfaces_count ClassFile { このクラスが実装するインターフェ u4 magic; イスの数 u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 12. interfaces ClassFile { このクラスが実装するインターフェ u4 magic; イスのconstant_poolのインデッ u2 minor_version; クスの配列 u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 13. fields_count ClassFile { このクラスのフィールドの数 u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 14. fields ClassFile { フィールド構造の配列 u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 15. methods_count ClassFile { このクラスのメソッドの数 u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 16. method ClassFile { メソッド構造の配列 u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 17. attributes_count ClassFile { 属性の数 u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 18. attributes ClassFile { 属性構造の配列 u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } 12年12月29日土曜日
  • 20. cp_info cp_info { constant_poolエントリーの共通 u1 tag; 構造 u1 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 など 12年12月29日土曜日
  • 21. CONSTANT_Methodref CONSTANT_Methodref_info { tag = 10 u1 tag; class_indexはこのメソッドを持つ u2 class_index; クラスを定義した u2 name_and_type_index; } CONSTANT_Classエントリへのイ ンデックス name_and_type_index 12年12月29日土曜日
  • 22. CONSTANT_Class CONSTANT_Class_info { tag = 7 u1 tag; name_indexはこの u2 name_index; CONSTANT_Classの表すクラス名 } を定義したCONSTANT_Utf8エン トリへのインデックス 12年12月29日土曜日
  • 23. constant_Utf8 CONSTANT_Utf8_info { tag = 1 u1 tag; lengthは続くbytesの長さ u2 length; bytesはUTF-8で符号化された文字 u1 bytes[length]; } 列 クラス中の文字列リテラル クラスの完全修飾名 フィールド名 メソッド名 メソッドのシグニチャ など、クラス中のほぼすべての文字 列を定義 12年12月29日土曜日
  • 24. CONSTANT_NameAndType CONSTANT_NameAndType_info { tag = 12 u1 tag; name_indexはフィールド名もしく u2 name_index; はメソッド名を表す u2 descriptor_index; } CONSTANT_Utf8のエントリへの インデックス descriptor_indexはフィールドも しくはメソッドの詳細を表す CONSTANT_Utf8のエントリへの インデックス 12年12月29日土曜日
  • 25. メソッド String.startsWith Methodref Class NameAndType Utf8 Utf8 Utf8 java/lang/String startsWith (Ljava/lang/String;I)Z 引数 戻り値 String,int boolean 12年12月29日土曜日
  • 26. フィールド String.hash Fieldref Class NameAndType Utf8 Utf8 Utf8 java/lang/String hash I 型 int 12年12月29日土曜日
  • 28. field field_info { フィールド1つ分を表す構造体 u2 access_flags; name_indexはフィールド名の u2 name_index; CONSTANT_Utf8へのインデック u2 descriptor_index; u2 attributes_count; ス attribute_info attributes[attributes_count]; } descriptor_indexは型名の CONSTANT_Utf8へのインデック ス attributesはこのフィールドの持つ 属性の配列 12年12月29日土曜日
  • 29. フィールドの属性 フィールドの持てる属性は以下の6種類 ConstantValue  フィールドの初期値 Synthetic  A class member that does not appear in the source code must be marked using a Synthetic attribute Signature (Java SE 5.0)  総称型を含めたフィールドの型情報 Deprecated @deprecated RuntimeVisibleAnnotations (Java SE 5.0)  RetentionPolicy.SOURCEまたはRetentionPolicy.CLASSなアノテーション RuntimeInvisibleAnnotations (Java SE 5.0)  RetentionPolicy.RUNTIMEなアノテーション 12年12月29日土曜日
  • 31. method method_info { メソッド1つ分を表す構造体 u2 access_flags; name_indexはメソッド名の u2 name_index; CONSTANT_Utf8へのインデック u2 descriptor_index; u2 attributes_count; ス attribute_info attributes[attributes_count]; } descriptor_indexは(引数)戻り値の 型名のCONSTANT_Utf8へのイン デックス attributesはこのメソッドの持つ属 性の配列 12年12月29日土曜日
  • 32. メソッドの属性 メソッドの持てる属性は以下の10種類 Code Exceptions Synthetic Signature (Java SE 5.0) Deprecated RuntimeVisibleAnnotations (Java SE 5.0) RuntimeInvisibleAnnotations (Java SE 5.0) RuntimeVisibleParameterAnnotations (Java SE 5.0) RuntimeInvisibleParameterAnnotations (Java SE 5.0) AnnotationDefault (Java SE 5.0) 12年12月29日土曜日
  • 34. code Code_attribute { max_stack u2 attribute_name_index;  メソッドの使用する最大オペラ u4 attribute_length; ンドスタックサイズ u2 max_stack; u2 max_locals; u4 code_length; max_loacls u1 code[code_length];  メソッド内の最大ローカル変数 u2 exception_table_length; { u2 start_pc; 数 u2 end_pc; u2 handler_pc; code u2 catch_type;  メソッドのコード本体 } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; exception_table }  例外テーブル 12年12月29日土曜日
  • 35. code バイトコード 以上 12年12月29日土曜日
  • 37. exception_table try 12年12月29日土曜日
  • 38. exception_table try catch 12年12月29日土曜日
  • 39. exception_table try catch 12年12月29日土曜日
  • 40. exception_table try catch 12年12月29日土曜日
  • 41. exception_table CONSTANT_Class Exception try catch 12年12月29日土曜日
  • 42. 元のコード private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); ReadObjectCallback cb = readObjectCallbacks.get(s); if (cb == null) { try { readObjectCallbacks.put(s, cb = new ReadObjectCallback(s)); } catch (Exception e) { throw new IOException(e.toString()); } } cb.registerComponent(this); 12年12月29日土曜日
  • 43. コード属性の属性 コード属性の持てる属性は以下の4種類 LineNumberTable  PCと元のソースコードの行番号の対応付け LocalVariableTable  ローカル変数の情報 LocalVariableTypeTable (Java SE 5.0)  総称型を含めたローカル変数の情報 StackMapTable (Java SE 6)  クラスローダがクラスを読み込むときの検証を高速化するために使用  This attribute is used during the process of verification by typechecking ( 4.10.1). 12年12月29日土曜日
  • 45. LocalVariableTable 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]; } 12年12月29日土曜日
  • 46. LocalVariableTypeTable LocalVariableTypeTable_attribute { u2 attribute_name_index; u4 attribute_length; u2 local_variable_type_table_length; { u2 start_pc; u2 length; u2 name_index; u2 signature_index; u2 index; } local_variable_type_table[local_variable_type_table_length]; } 12年12月29日土曜日
  • 47. クラスの属性 クラスの持てる属性は以下の10種類 InnerClasses  インナークラス EnclosingMethod  無名クラス Synthetic Signature (Java SE 5.0) SourceFile SourceDebugExtension Deprecated RuntimeVisibleAnnotations (Java SE 5.0) RuntimeInvisibleAnnotations (Java SE 5.0) BootstrapMethods (Java SE 7) 12年12月29日土曜日