home *** CD-ROM | disk | FTP | other *** search
/ Hacker / Hacker.iso / HACKER / DECOMP / DECAF / clasfile.adb < prev    next >
Encoding:
Text File  |  1996-09-19  |  10.3 KB  |  306 lines

  1. --
  2. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  3. -- Author: Gilles Demailly
  4. --
  5. --
  6. -- Permission to use, copy, modify, and distribute this software and its
  7. -- documentation for any purpose and without fee is hereby granted,
  8. -- provided that the above copyright and authorship notice appear in all
  9. -- copies and that both that copyright notice and this permission notice
  10. -- appear in supporting documentation.
  11. -- 
  12. -- The ARA makes no representations about the suitability of this software
  13. -- for any purpose.  It is provided "as is" without express
  14. -- or implied warranty.
  15. -- 
  16.  
  17. with Ada.Text_Io;
  18. with Flags;
  19.  
  20. with Field.Method;
  21. with Field.Variable;
  22.  
  23. package body Class_File is
  24.  
  25.    use Byte_Utilities;
  26.    use Attribute;
  27.  
  28.    -- the reference each Java class file must contain
  29.    --------------------------------------------------
  30.    Magic_Reference : constant Unsigned_32 := 16#CAFEBABE#;
  31.  
  32.    -- Current major and minor versions of the Java Compiler
  33.    --------------------------------------------------------
  34.    Current_Major_Version : constant Unsigned_16 := 45;
  35.    Current_Minor_Version : constant Unsigned_16 := 3;
  36.  
  37.  
  38.    function Read_Class (From_File : Byte_Utilities.File_Type)      
  39.             return Class_File is
  40.       New_Class : Class_File;
  41.       I         : Unsigned_16;
  42.    begin
  43.  
  44.       -- Reads the Java file code and checks it
  45.       Read (From_File, New_Class.Magic);
  46.       if New_Class.Magic /= Magic_Reference then
  47.          raise Bad_File;
  48.       end if;
  49.  
  50.       -- Reads the Minor version and checks it
  51.       Read (From_File, New_Class.Minor_Version);
  52.       if New_Class.Minor_Version /= Current_Minor_Version then
  53.          Ada.Text_Io.Put_Line
  54.             (" Warning : Minor version different than expected.");
  55.       end if;
  56.  
  57.       -- Reads the Major version and checks it
  58.       Read (From_File, New_Class.Major_Version);
  59.       if New_Class.Major_Version /= Current_Major_Version then
  60.          Ada.Text_Io.Put_Line
  61.             (" Warning : Major version different than expected.");
  62.       end if;
  63.  
  64.       -- Read the number of constants pool informations and allocate 
  65.       -- the table
  66.       Read (From_File, New_Class.Constant_Pool_Count);
  67.       
  68.       New_Class.Constant_Pool := 
  69.         new CP.CP_infos'(0..New_Class.Constant_Pool_Count - 1 => null);
  70.  
  71.       -- Stores all the constant pool informations
  72.       I := 1;
  73.       while I <= New_Class.Constant_Pool_Count - 1 loop
  74.          New_Class.Constant_Pool (I) := CP.Read_Constant (From_File);
  75.          if CP.Use_Two_Indexes_In_Table (New_Class.Constant_Pool (I)) then
  76.             I := I + 2;
  77.          else
  78.             I := I + 1;
  79.          end if;
  80.       end loop;
  81.  
  82.       -- Reads access flags, class and superclass indexes, number of
  83.       -- interfaces implemented by the class
  84.       Read (From_File, New_Class.Access_Flags);
  85.       Read (From_File, New_Class.This_Class);
  86.       Read (From_File, New_Class.Super_Class);
  87.       Read (From_File, New_Class.Interfaces_Count);
  88.  
  89.       -- Reads the interface name indexes (if necessary)
  90.       if New_Class.Interfaces_Count > 0 then
  91.          New_Class.Interfaces := 
  92.            new Unsigned_16_Array (1 .. New_Class.Interfaces_Count);
  93.          for J in 1..New_Class.Interfaces_Count loop
  94.             Read (From_File, New_Class.Interfaces (J));
  95.          end loop;
  96.       else
  97.          New_Class.Interfaces := null;
  98.       end if;
  99.  
  100.       -- Reads the number of variables or constants
  101.       Read (From_File, New_Class.Fields_Count);
  102.  
  103.       -- Reads the variables or constants (if necessary)
  104.       if New_Class.Fields_Count > 0 then
  105.          New_Class.Fields := new Field.Field_Infos
  106.                                     (1 .. New_Class.Fields_Count);
  107.          for J in 1 .. New_Class.Fields_Count loop
  108.             New_Class.Fields (J) := Field.Variable.Read_Field 
  109.                                        (From_File, New_Class.Constant_Pool);
  110.          end loop;
  111.       else
  112.          New_Class.Fields := null;
  113.       end if;
  114.  
  115.       -- Reads the number of methods
  116.       Read (From_File, New_Class.Methods_Count);
  117.  
  118.       -- Reads the methods (if necessary)
  119.       if New_Class.Methods_Count > 0 then
  120.          New_Class.Methods := new Field.Field_Infos
  121.                                      (1 .. New_Class.Methods_Count);
  122.          for J in 1 .. New_Class.Methods_Count loop
  123.             New_Class.Methods (J) := Field.Method.Read_Field 
  124.                                         (From_File, New_Class.Constant_Pool);
  125.          end loop;
  126.       else
  127.          New_Class.Methods := null;
  128.       end if;
  129.  
  130.       -- Reads the number of class attributes
  131.       Read (From_File, New_Class.Attribute_Count);
  132.  
  133.       return New_Class;
  134.    
  135.    end Read_Class;
  136.  
  137.  
  138.    -- should disappear soon
  139.    procedure Display_Class (Infos : in Class_File) is
  140.    begin
  141.       Ada.Text_Io.Put_Line ("            Java Class Report ");
  142.       Ada.Text_Io.Put_Line ("            ----------------- ");
  143.       Ada.Text_Io.New_Line;
  144.       Ada.Text_Io.Put_Line 
  145.          (" minor version : " & 
  146.           unsigned_16'Image (Infos.Minor_Version));
  147.       Ada.Text_Io.Put_Line 
  148.          (" major version : " & 
  149.           unsigned_16'Image (Infos.Major_Version));
  150.       Ada.Text_Io.Put_Line 
  151.          (" constant pool number : " & 
  152.           unsigned_16'Image (Infos.Constant_Pool_Count));
  153.       Ada.Text_Io.Put_Line 
  154.          (" access flags : " & 
  155.           unsigned_16'Image (Infos.Access_Flags));
  156.       if not Flags.Is_Correct (Infos.Access_Flags, Flags.Class_Flag) then
  157.          Ada.Text_Io.Put_Line (" this flag is not correct for a class");
  158.       end if;
  159.       Ada.Text_Io.Put_Line 
  160.          (" this class : " & 
  161.           unsigned_16'Image (Infos.This_Class));
  162.       CP.Display (Infos.Constant_Pool (Infos.This_Class),
  163.                Infos.Constant_Pool);
  164.       Ada.Text_Io.Put_Line 
  165.          (" super class : " & 
  166.           unsigned_16'Image (Infos.Super_Class));
  167.       if Infos.Super_Class /= 0 then
  168.          CP.Display (Infos.Constant_Pool (Infos.Super_Class),
  169.                           Infos.Constant_Pool);
  170.       else
  171.          Ada.Text_Io.Put_Line (" No SuperClass");
  172.       end if;
  173.       Ada.Text_Io.Put_Line 
  174.          (" interfaces count : " & 
  175.           unsigned_16'Image (Infos.Interfaces_Count));
  176.       Ada.Text_Io.Put_Line 
  177.          (" fields count : " & 
  178.           unsigned_16'Image (Infos.Fields_Count));
  179.       if Infos.Fields_Count = 0 then
  180.          Ada.Text_Io.Put_Line (" No instance variables");
  181.       else
  182.          for I in 1..Infos.Fields_Count loop
  183.             Field.Display (Infos.Fields (I), Infos.Constant_Pool);
  184.          end loop;
  185.       end if;
  186.       Ada.Text_Io.Put_Line 
  187.          (" methods count : " & 
  188.           unsigned_16'Image (Infos.Methods_Count));
  189.       Ada.Text_Io.Put_Line 
  190.          (" attribute count : " & 
  191.           unsigned_16'Image (Infos.Attribute_Count));
  192.    end Display_Class;
  193.  
  194.  
  195.    procedure Display_Java_Spec (Infos     : in Class_File;
  196.                                 For_Body  : in Boolean := False) is
  197.    begin
  198.       -- is the Class public, private ...
  199.       Flags.Display (Infos.Access_Flags, Flags.Class_Flag);
  200.       
  201.       -- test if it is a class or an interface
  202.       if Flags.Is_Interface (Infos.Access_Flags) then
  203.          -- display the Interface name
  204.          Ada.Text_Io.Put
  205.             (CP.Java_Decoded_String
  206.                 (Infos.Constant_Pool (Infos.This_Class),
  207.                  Infos.Constant_Pool,
  208.                  CP.Class_Name));      
  209.       else
  210.          -- display the Class name
  211.          Ada.Text_Io.Put
  212.             ("class " & 
  213.              CP.Java_Decoded_String
  214.                 (Infos.Constant_Pool (Infos.This_Class),
  215.                  Infos.Constant_Pool,
  216.                  CP.Class_Name));
  217.       end if;
  218.       
  219.       -- display the Superclass name
  220.       if Infos.Super_Class /= 0 then
  221.          Ada.Text_Io.Put
  222.             (" extends " & 
  223.              CP.Java_Decoded_String
  224.                 (Infos.Constant_Pool (Infos.Super_Class),
  225.                  Infos.Constant_Pool,
  226.                  CP.Class_Name));
  227.       end if;
  228.       
  229.       -- display the Interfaces implemented bye the class if applicable
  230.       if Infos.Interfaces_Count > 0 then
  231.          Ada.Text_Io.Put (" implements ");
  232.          for I in 1 .. Infos.Interfaces_Count loop
  233.             if I > 1 then
  234.                Ada.Text_Io.Put (", ");
  235.             end if;
  236.             Ada.Text_Io.Put
  237.                (CP.Java_Decoded_String
  238.                   (Infos.Constant_Pool (Infos.Interfaces (I)),
  239.                    Infos.Constant_Pool,
  240.                    CP.Class_Name));
  241.          end loop;
  242.       end if;
  243.    
  244.       Ada.Text_Io.Put_Line (" { ");
  245.       
  246.       -- display the Class ans Instance variables
  247.       if Infos.Fields_Count /= 0 then
  248.          Ada.Text_Io.Put_Line ("   // instance and class variables");
  249.          for I in 1..Infos.Fields_Count loop
  250.             Field.Display_Java_Spec
  251.                (Infos     => Infos.Fields (I),
  252.                 Context   => Infos.Constant_Pool,
  253.                 For_Class => Infos.This_Class,
  254.                 For_Body  => For_Body);
  255.          end loop;
  256.       end if;
  257.       
  258.       -- display the Class methods
  259.       if Infos.Methods_Count /= 0 then
  260.          Ada.Text_Io.Put_Line ("   // methods");
  261.          for I in 1..Infos.Methods_Count loop
  262.             if For_Body then
  263.                Field.Display_Java_Body
  264.                   (Infos     => Infos.Methods (I),
  265.                    Context   => Infos.Constant_Pool,
  266.                    For_Class => Infos.This_Class);
  267.             else
  268.                Field.Display_Java_Spec
  269.                   (Infos     => Infos.Methods (I),
  270.                    Context   => Infos.Constant_Pool,
  271.                    For_Class => Infos.This_Class,
  272.                    For_Body  => False);
  273.             end if;
  274.          end loop;
  275.       end if;
  276.       
  277.       -- well, this is the end ...
  278.       Ada.Text_Io.Put_Line ("   }");
  279.       Ada.Text_Io.New_Line;
  280.  
  281.    end Display_Java_Spec;
  282.  
  283.    procedure Display_Ada_Spec (Infos : in Class_File) is
  284.    begin
  285.       Ada.Text_Io.Put_Line (" sorry, Ada spec generation not available ...");
  286.    end Display_Ada_Spec;
  287.  
  288.    procedure Display_Ada_Body (Infos : in Class_File) is
  289.    begin
  290.       Ada.Text_Io.Put_Line (" sorry, Ada body generation not available ...");
  291.    end Display_Ada_Body;
  292.  
  293.    procedure Display_Stk_Spec (Infos : in Class_File) is
  294.    begin
  295.       Ada.Text_Io.Put_Line
  296.          (" sorry, Smalltalk kind-of spec generation not available ...");
  297.    end Display_Stk_Spec;
  298.  
  299.    procedure Display_Stk_Body (Infos : in Class_File) is
  300.    begin
  301.       Ada.Text_Io.Put_Line
  302.          (" sorry, Smalltalk body generation not available ...");
  303.    end Display_Stk_Body;
  304.  
  305. end Class_File;
  306.