src/JavaConstantPool.st
author vranyj1
Sun, 01 May 2011 12:52:23 +0000
branchjk_new_structure
changeset 761 43e017ec7958
parent 758 be8e84381ce0
child 772 0f92c23b80ee
permissions -rw-r--r--
Merged with /branches/jk

"
 COPYRIGHT (c) 1997 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libjava' }"

Array variableSubclass:#JavaConstantPool
	instanceVariableNames:'owner'
	classVariableNames:'ConstantPools'
	poolDictionaries:''
	category:'Languages-Java-Reader-Support'
!

!JavaConstantPool class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"


! !

!JavaConstantPool class methodsFor:'initialization'!

initialize
    ConstantPools := OrderedCollection new: 1000.

    "Modified: / 08-04-2011 / 17:28:07 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 09-04-2011 / 09:25:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaConstantPool class methodsFor:'instance creation'!

new: size 
    "return an initialized instance"
    
    ^ ConstantPools add: ((super new: size)
                initialize;
                yourself).

    "Created: / 08-04-2011 / 16:56:18 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 09-04-2011 / 09:24:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 11-04-2011 / 18:46:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
! !

!JavaConstantPool class methodsFor:'accessing'!

allConstantPools
    "linked list of all constant pools in system"
    ^ ConstantPools.

    "Created: / 08-04-2011 / 16:53:58 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 09-04-2011 / 09:24:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

allConstantPools: anOorderedCollection 
    "linked list of all constant pools in system"
    
    ConstantPools := anOorderedCollection.

    "Created: / 08-04-2011 / 17:07:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 09-04-2011 / 09:24:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 11-04-2011 / 18:47:58 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
! !

!JavaConstantPool class methodsFor:'special'!

compressPools
    "unify all constants"

    |strings nameandTypes jStrings|

    strings := Set new.
    nameandTypes := Set new.

    self allInstancesDo:[:aPool |
        aPool keysAndValuesDo:[:idx :aConst |
            |existing nt|

            aConst isString ifTrue:[
                existing := strings elementAt:aConst ifAbsent:nil.
                existing isNil ifTrue:[
                    strings add:aConst
                ] ifFalse:[
                    aPool at:idx put:existing
                ]
            ] ifFalse:[
                (aConst isMemberOf:JavaFieldref) ifTrue:[
                    nt := aConst nameandType.
                    existing := nameandTypes elementAt:nt ifAbsent:nil.
                    existing isNil ifTrue:[
                        nameandTypes add:nt
                    ] ifFalse:[
                        aConst nameandType:existing
                    ]
                ] ifFalse:[
                    (aConst isMemberOf:JavaNameandType) ifTrue:[
                        existing := nameandTypes elementAt:aConst ifAbsent:nil.
                        existing isNil ifTrue:[
                            nameandTypes add:aConst
                        ] ifFalse:[
                            aPool at:idx put:existing
                        ]
                    ]
                ]
            ]
        ].
    ]

    "
     self compressPools
    "

    "Modified: 19.8.1997 / 14:04:25 / cg"
!

invalidateForClass: internalJavaClassName 
    "Only alias, everybody calls invalidateForClass so why not me :)"
    
    ^self invalidateReferencesToClass: internalJavaClassName.

    "Created: / 08-04-2011 / 16:52:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

invalidateReferencesToClass: internalJavaClassName 
    "Go over all constant pools and call invalidateForClass on all 
     references. (usable when given class was unloaded etc)"
    ConstantPools do: [:each | each invalidateForClass: internalJavaClassName].

    "Created: / 08-04-2011 / 16:09:03 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
! !

!JavaConstantPool methodsFor:'accessing'!

owner
    "return owner"

    ^ owner

    "Created: 28.6.1996 / 21:12:22 / cg"
!

owner:something
    "set owner"

    owner := something.

    "Created: 28.6.1996 / 21:12:22 / cg"
! !

!JavaConstantPool methodsFor:'initialization'!

initialize
    "Invoked when a new instance is created."

    "/ please change as required (and remove this comment)
    "/ owner := nil.

    "/ super initialize.   -- commented since inherited method does nothing
! !

!JavaConstantPool methodsFor:'printing & storing'!

displayString
    owner isNil ifTrue:[
        ^ '*** unowned ConstantPool'
    ].
    ^ 'ConstantPool of ' , owner fullName

    "Created: 28.6.1996 / 21:13:41 / cg"
    "Modified: 28.6.1996 / 21:20:26 / cg"
! !

!JavaConstantPool methodsFor:'queries'!

refersToMethod:aJavaMethod
    self do:[:constItem |
        (constItem == aJavaMethod) ifTrue:[^ true].
        (constItem isMemberOf:JavaMethodref) ifTrue:[
            self halt.
        ].
        (constItem isMemberOf:JavaUnresolvedMethodrefConstant) ifTrue:[
            self halt.
        ]
    ].
    ^ false

    "Modified: 29.7.1997 / 17:36:49 / cg"
    "Created: 29.7.1997 / 17:39:19 / cg"
!

refersToMethodNamed:aJavaMethodName
    self do:[:constItem |
        (constItem isKindOf:JavaMethod) ifTrue:[
            constItem name = aJavaMethodName ifTrue:[^ true].
        ] ifFalse:[
            (constItem isMemberOf:JavaMethodref) ifTrue:[
                constItem name = aJavaMethodName ifTrue:[^ true].
            ].
            (constItem isMemberOf:JavaUnresolvedMethodrefConstant) ifTrue:[
                self halt.
            ]
        ]
    ].
    ^ false

    "Created: / 29.7.1997 / 17:39:24 / cg"
    "Modified: / 16.10.1998 / 01:22:02 / cg"
! !

!JavaConstantPool methodsFor:'special'!

classReferencesDo:aBlock
    self do:[:constItem |
        constItem isJavaClass ifTrue:[
            aBlock value:constItem
        ] 
    ]

    "Modified: / 7.8.1997 / 19:17:38 / cg"
    "Created: / 4.1.1998 / 00:40:11 / cg"
!

invalidateForClass: internalJavaClassName
"go over all entries and call invalidateForClass on all references"

self do: [:entry | entry isJavaRef ifTrue:[entry invalidateForClass: internalJavaClassName]].

    "Created: / 08-04-2011 / 16:11:36 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

updateClassRefsFrom:oldClass to:newClass
owner == oldClass ifTrue:[
    self halt.
    ^ self
].

    self keysAndValuesDo:[:index :constItem |
        |nameIndex|

        constItem == oldClass ifTrue:[
            self at:index put:newClass
        ] ifFalse:[
            (constItem isNil
            or:[constItem isNumber]) ifFalse:[
                constItem isString ifTrue:[
                    "/ nothing done ...
                ] ifFalse:[
                    (constItem isMemberOf:(Java java_lang_String)) ifTrue:[
                        "/ nothing done ...
                    ] ifFalse:[
                        constItem isBehavior ifFalse:[
                            constItem updateClassRefsFrom:oldClass to:newClass
                        ] ifTrue:[
                            constItem isJavaClass ifTrue:[
                                constItem fullName = oldClass fullName ifTrue:[
                                    'JAVA: class update by name [in pool].' infoPrintCR.
                                    newClass isNil ifTrue:[
                                        "/ mhm - must find a slot for the classes name
                                        nameIndex := -1.
                                        self 
                                            at:index
                                            put:(JavaUnresolvedClassConstant
                                                    pool:self
                                                    poolIndex:index
                                                    nameIndex:nameIndex)
                                    ] ifFalse:[
                                        self at:index put:newClass
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]

    "Modified: / 4.2.1998 / 22:12:03 / cg"
! !

!JavaConstantPool class methodsFor:'documentation'!

version
    ^ '$Id$'
!

version_SVN
    ^ '$Id$'
! !

JavaConstantPool initialize!