SlideShare a Scribd company logo
1 of 48
Download to read offline
なれる!クラスローダー
                   @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

Recently uploaded

TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
YukiTerazawa
 
The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
koheioishi1
 

Recently uploaded (8)

生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
 
The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
 
2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
 
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
 
世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

なれる!クラスローダー

  • 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日土曜日