JavaClassReader.st
author cg
Mon, 15 Apr 1996 15:08:12 +0000
changeset 1 083530508d9c
child 3 e3c2a84b2a72
permissions -rw-r--r--
intitial checkin

Object subclass:#JavaClassReader
	instanceVariableNames:'inStream msb constants this_class super_class'
	classVariableNames:'Verbose'
	poolDictionaries:''
	category:'Java-Support'
!


!JavaClassReader class methodsFor:'initialization'!

initialize
    Verbose := false
! !

!JavaClassReader class methodsFor:'debugging'!

verbose:aBoolean
    Verbose := aBoolean

    "
     JavaClassReader verbose:true
     JavaClassReader verbose:false
    "
! !

!JavaClassReader class methodsFor:'file reading'!

readFile:aFilename
    |inStream retVal|

    inStream := aFilename asFilename readStream.
    inStream isNil ifTrue:[^ nil].
    retVal := self readStream:inStream.
    inStream close.
    ^ retVal

    "Created: 15.4.1996 / 14:58:53 / cg"
!

readStream:aStream
    ^ self new readStream:aStream

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Modified: 15.4.1996 / 15:01:30 / cg"
! !

!JavaClassReader methodsFor:'file reading'!

readAttribute
    |attribute_name attribute_length attribute_info|

    attribute_name := self nextU2.
    attribute_length := self nextU4.
    attribute_info := ByteArray new:(attribute_length).
    inStream nextBytes:attribute_length into:attribute_info startingAt:1.

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Modified: 15.4.1996 / 15:33:28 / cg"
    "Created: 15.4.1996 / 15:40:17 / cg"
!

readClassFile
    |magic version 
     access_flags this_class_index super_class_index|

    "/
    "/ read magic, determine byte order
    "/
    msb := true.
    magic := self nextU4.
    magic = 16rCAFEBABE ifFalse:[
        magic = 16rBEBAFECA ifFalse:[
            self error:'not a java class file'.
            ^ nil
        ].
        msb := false.
        Verbose ifTrue:[Transcript showCr:'file is lsb'].
    ] ifTrue:[
        Verbose ifTrue:[Transcript showCr:'file is msb'].
    ].

    "/
    "/ get version
    "/
    version := self nextU4.
    Verbose ifTrue:[Transcript show:'version = '; showCr:version].

    "/
    "/ get constant pool
    "/
    self readConstantPool.

    "/
    "/ access flags
    "/
    access_flags := self nextU2.
    this_class_index := self nextU2.
    super_class_index := self nextU2.

    this_class := constants at:this_class_index.
    super_class := constants at:super_class_index.

    this_class setAccessFlags:access_flags.

    "/
    "/ get interfaces
    "/
    self readInterfaces.

    "/
    "/ get fields
    "/
    self readFieldInfofields.

    "/
    "/ get methods
    "/
    self readMethods.

self halt.
    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/Alignable.class'
    "

    "Created: 15.4.1996 / 15:02:47 / cg"
    "Modified: 15.4.1996 / 16:51:48 / cg"
!

readConstant
    |tag constReader const|


    "/
    "/ get tag
    "/
    tag := self nextU1.
    Verbose ifTrue:[Transcript show:'tag = '; showCr:tag].

    constReader := #(
                        readConstant_Asciz              "/ 1
                        readConstant_Undef              "/ 2
                        readConstant_Integer            "/ 3
                        readConstant_Float              "/ 4
                        readConstant_Long               "/ 5
                        readConstant_Double             "/ 6
                        readConstant_Class              "/ 7
                        readConstant_String             "/ 8
                        readConstant_Fieldref           "/ 9
                        readConstant_Methodref          "/ 10
                        readConstant_InterfaceMethodref "/ 11
                        readConstant_NameandType        "/ 12
                    ) at:tag ifAbsent:[#readConstant_Undef].

    ^ self perform:constReader.

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:46:32 / cg"
    "Modified: 15.4.1996 / 15:47:00 / cg"
!

readConstantPool
    |constantPoolCount const|

    "/
    "/ get constant pool
    "/
    constantPoolCount := self nextU2.
    Verbose ifTrue:[Transcript show:'constantPoolCount = '; showCr:constantPoolCount].

    constants := Array new:constantPoolCount-1.

    1 to:constantPoolCount-1 do:[:i |
        Verbose ifTrue:[Transcript show:'const: '; showCr:i].
        const := self readConstant.
        constants at:i put:const
    ].

    1 to:constantPoolCount-1 do:[:i |
        |const|

        const := constants at:i.
        constants at:i put:(JavaUnresolvedConstant resolve:const from:constants).
    ].

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/Alignable.class'
    "

    "Created: 15.4.1996 / 15:14:11 / cg"
    "Modified: 15.4.1996 / 16:41:57 / cg"
!

readConstant_Asciz
    |len string|

    len := self nextU2.
    string := String new:len.
    inStream nextBytes:len into:string startingAt:1.

    Verbose ifTrue:[Transcript show:'asciz; string= ';     showCr:string].

    ^ string

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/Alignable.class'
    "

    "Created: 15.4.1996 / 15:15:35 / cg"
    "Modified: 15.4.1996 / 16:33:45 / cg"
!

readConstant_Class
    |name_index|

    name_index := self nextU2.

    Verbose ifTrue:[Transcript show:'class; index= '; showCr:name_index].

    ^ JavaUnresolvedClassConstant nameIndex:name_index

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:21:13 / cg"
    "Modified: 15.4.1996 / 15:49:32 / cg"
!

readConstant_Fieldref
    |class_index name_and_type_index|

    class_index := self nextU2.
    name_and_type_index := self nextU2.

    Verbose ifTrue:[Transcript show:'fieldref; classindex= ';     showCr:class_index].
    Verbose ifTrue:[Transcript show:'fieldref; name&typeindex= '; showCr:name_and_type_index].

    ^ JavaUnresolvedFieldrefConstant 
                classIndex:class_index
                nameandTypeIndex:name_and_type_index

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:22:18 / cg"
    "Modified: 15.4.1996 / 16:07:01 / cg"
!

readConstant_Integer
    |value|

    value := self nextU4.

    Verbose ifTrue:[Transcript show:'integer; value= ';     showCr:value].

    ^ value

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/Alignable.class'
    "

    "Modified: 15.4.1996 / 15:42:16 / cg"
    "Created: 15.4.1996 / 16:34:42 / cg"
!

readConstant_Methodref
    |class_index name_and_type_index|

    class_index := self nextU2.
    name_and_type_index := self nextU2.

    Verbose ifTrue:[Transcript show:'methodref; classindex= ';     showCr:class_index].
    Verbose ifTrue:[Transcript show:'methodref; name&typeindex= '; showCr:name_and_type_index].

    ^ JavaUnresolvedMethodrefConstant 
                classIndex:class_index
                nameandTypeIndex:name_and_type_index

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:22:37 / cg"
    "Modified: 15.4.1996 / 16:07:19 / cg"
!

readConstant_NameandType
    |name_index signature_index|

    name_index := self nextU2.
    signature_index := self nextU2.

    Verbose ifTrue:[Transcript show:'methodref; nameindex= ';     showCr:name_index].
    Verbose ifTrue:[Transcript show:'methodref; signatureindex= '; showCr:signature_index].

    ^ JavaUnresolvedNameandTypeConstant 
                nameIndex:name_index
                signatureIndex:signature_index

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:23:43 / cg"
    "Modified: 15.4.1996 / 16:17:16 / cg"
!

readConstant_String
    |tag string_index|

    string_index := self nextU2.

    Verbose ifTrue:[Transcript show:'string; index= '; showCr:string_index].

    ^ JavaUnresolvedStringConstant index:string_index

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:20:33 / cg"
    "Modified: 15.4.1996 / 16:01:37 / cg"
!

readFieldInfofield
    |access_flags name_index signature_index attributes_count|

    access_flags := self nextU2.
    name_index := self nextU2.
    signature_index := self nextU2.
    attributes_count := self nextU2.

    1 to:attributes_count do:[:i |
        self readAttribute.
    ].

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Modified: 15.4.1996 / 15:33:28 / cg"
    "Created: 15.4.1996 / 15:38:43 / cg"
!

readFieldInfofields
    |fieldsCount interface|

    "/
    "/ get interfaces
    "/
    fieldsCount := self nextU2.
    Verbose ifTrue:[Transcript show:'fieldsCount = '; showCr:fieldsCount].

    1 to:fieldsCount do:[:i |
        Verbose ifTrue:[Transcript show:'field: '; showCr:i].
        self readFieldInfofield
    ].

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:34:41 / cg"
    "Modified: 15.4.1996 / 15:35:28 / cg"
!

readInterfaces
    |interfacesCount interface|


    "/
    "/ get interfaces
    "/
    interfacesCount := self nextU2.
    Verbose ifTrue:[Transcript show:'interfacesCount = '; showCr:interfacesCount].

    1 to:interfacesCount do:[:i |
        Verbose ifTrue:[Transcript show:'interface: '; showCr:i].
        interface = self nextU2
    ].

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:31:59 / cg"
    "Modified: 15.4.1996 / 15:33:28 / cg"
!

readMethod
    |access_flags name_index signature_index attributes_counts attribute|


    "/
    "/ get a method
    "/
    access_flags := self nextU2.
    name_index := self nextU2.
    signature_index := self nextU2.
    attributes_counts := self nextU2.

    1 to:attributes_counts do:[:i |
        attribute = self readAttribute
    ].

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Modified: 15.4.1996 / 15:33:28 / cg"
    "Created: 15.4.1996 / 16:48:49 / cg"
!

readMethods
    |methodsCount method|


    "/
    "/ get methods
    "/
    methodsCount := self nextU2.
    Verbose ifTrue:[Transcript show:'methodsCount = '; showCr:methodsCount].

    1 to:methodsCount do:[:i |
        Verbose ifTrue:[Transcript show:'method: '; showCr:i].
        method = self readMethod
    ].

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Modified: 15.4.1996 / 15:33:28 / cg"
    "Created: 15.4.1996 / 16:46:30 / cg"
!

readStream:aStream
    inStream := aStream.
    inStream binary.
    self readClassFile

    "
     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
    "

    "Created: 15.4.1996 / 15:00:55 / cg"
    "Modified: 15.4.1996 / 15:06:18 / cg"
! !

!JavaClassReader methodsFor:'low level reading'!

nextU1
    ^ inStream nextByte

    "Created: 15.4.1996 / 15:15:43 / cg"
!

nextU2
    ^ inStream nextUnsignedShortMSB:msb

    "Created: 15.4.1996 / 15:12:25 / cg"
!

nextU4
    ^ inStream nextUnsignedLongMSB:msb

    "Created: 15.4.1996 / 15:04:28 / cg"
! !

!JavaClassReader class methodsFor:'documentation'!

version
    ^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaClassReader.st,v 1.1 1996/04/15 15:08:12 cg Exp $'
! !
JavaClassReader initialize!