JavaClassRegistry.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 25 Jan 2013 00:15:51 +0000
branchrefactoring-classpath
changeset 2000 13a10b73aa83
parent 1995 b129fd0a81e4
child 2037 6b93bf17087a
permissions -rw-r--r--
Closing branch refactoring-classpath

"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modifications done at SWING Research Group [1]:

 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 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.

 [1] Code written at SWING Research Group contains a signature
     of one of the above copright owners. For exact set of such code,
     see the differences between this version and version stx:libjava
     as of 1.9.2010
"
"{ Package: 'stx:libjava' }"

Object subclass:#JavaClassRegistry
	instanceVariableNames:'vm loaders notifier'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Support'
!

!JavaClassRegistry class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modifications done at SWING Research Group [1]:

 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 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.

 [1] Code written at SWING Research Group contains a signature
     of one of the above copright owners. For exact set of such code,
     see the differences between this version and version stx:libjava
     as of 1.9.2010

"
! !

!JavaClassRegistry class methodsFor:'instance creation'!

for: aJavaVM

    ^self new setVM: aJavaVM.

    "Created: / 21-12-2010 / 19:42:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

new

    ^ self basicNew initialize.

    "Modified (format): / 30-10-2011 / 12:07:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'accessing'!

classForName: className loader: classLoader ifAbsent: aBlock 
    "Get class loaded by given classLoader from registry or evaluate aBlock if class is not yet registered"

    | classes class |

    self assert: (className includes: $.) not.

    "Classes loaded by primordial classloader are always
     used"

    classLoader notNil ifTrue:[
        classes := loaders at: nil ifAbsent: nil.
        class := classes at: className ifAbsent: nil.
        class notNil ifTrue:[
            ^class.
        ]
    ].

    "No bootstrap class found, search given classloader"
    classes := loaders at: classLoader ifAbsent: [^aBlock value].
    ^classes at: className ifAbsent: aBlock

    "Modified: / 21-10-2011 / 12:39:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Created: / 23-10-2011 / 11:40:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classForName: className loader: classLoader ifAbsentPut: block 
    "Get class loaded by classLoader from registry. if absent block is evaluated and resulting class in registered in registry, "
    
    | class |
    self assert: (className includes: $.) not.
     "if class is already registered with the same cl, just return it"
    class := self classNamed: className loader: classLoader.
    class notNil ifTrue: [ ^ class ].
    "if java vm is booted, nil class loader means system class loader, lets check it too"
    (vm booted and: [classLoader isNil]) ifTrue: [
        class := self classNamed: className loader: vm systemClassLoader. 
        class notNil ifTrue: [^class]
    ].
     "otherwise evaluate block"
    class := block value.
    class notNil ifTrue: [
    self registerClass: class.].
    ^ class.

    "Created: / 21-10-2011 / 12:00:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Created: / 23-10-2011 / 11:36:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 02-11-2011 / 17:29:20 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified (format): / 24-01-2013 / 11:19:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classNamed: className

    "Return a class for given classname loaded by 'current classloader' or 
     nil if not yet loaded"

    ^self classNamed: className loader: JavaClassReader classLoaderQuerySignal query

    "Created: / 23-10-2011 / 12:23:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classNamed: className loader: classLoader

    "Return a class for given classname loaded by given classloader or 
     nil if not yet loaded"

    ^self classForName: className loader: classLoader ifAbsent:[nil].

    "Created: / 23-10-2011 / 12:23:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classes

    ^Iterator on:[:whatToDo|self classesDo: whatToDo]

    "Created: / 23-10-2011 / 20:14:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

systemPackage: syspkg
    "Return name (as string) of system package named syspkg or nil if no such package is loaded"

    | classes syspkgSlashed |

    classes := loaders at: nil ifAbsent:[nil].
    classes isEmptyOrNil ifTrue:[ ^ #() ].
    syspkgSlashed := syspkg copyReplaceAll: $. with: $/.
    classes keysDo:[:name|
        (name startsWith:syspkgSlashed) ifTrue:[
            ^ syspkg
        ]
    ].
    ^ nil

    "Created: / 16-08-2012 / 17:40:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

systemPackages
    "Return a collection system packages (as smalltalk strings) currently loaded"

    | classes syspkgs |

    classes := loaders at: nil ifAbsent:[nil].
    classes isEmptyOrNil ifTrue:[ ^ #() ].
    syspkgs := Set new.
    classes keysDo:[:name|
        | slashpos |

        slashpos := name lastIndexOf: $/.
        slashpos ~~ 0 ifTrue:[
            | syspkg |

            syspkg := name copyTo: slashpos - 1.
            syspkg replaceAll: $/ with: $..
            syspkgs add: syspkg.
        ]
    ].
    ^syspkgs

    "Created: / 16-08-2012 / 17:32:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'class loading'!

loadFile: aFilename 
    "reads a class from aFilename, installs and returns it."
    
    | aClass |

    self breakPoint: #mh.
    aClass := JavaClassReader readFile: aFilename ignoring: Set new.
    aClass isJavaClass ifFalse:[self breakPoint:#mh].
    self registerClass: aClass.
    ^ aClass.

    "Created: / 15-04-1996 / 14:58:53 / cg"
    "Modified: / 12-05-1998 / 22:06:52 / cg"
    "Modified: / 23-10-2011 / 11:55:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

loadStream: javaClassDataStream loader: aJavaClassLoader 
    "reads a class from aStream and returns it.
     The JavaClass is installed as global.
     If new classes are required to be loaded, aClassLoader is
     asked to do it."
    
    | javaClass |

    self breakPoint: #mh.
    JavaClassReader classLoaderQuerySignal answer: aJavaClassLoader
        do: [
            javaClass := JavaClassReader readStream: javaClassDataStream
                        ignoring: (Set new).
            javaClass 
                ifNil: [
                    Logger 
                        log: 'JavaClassReader was not able to read given data stream'
                        severity: #warn
                        facility: #JVM.
                    self breakPoint: #mh.
                    ^ nil.
                ].
            javaClass isJavaClass ifFalse: [ self breakPoint: #mh. ].
            javaClass classLoader: aJavaClassLoader.
            self registerClass: javaClass.
        ].
    ^ javaClass

    "Modified: / 30-10-2011 / 12:24:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'enumerating'!

classesDo: aBlock

    loaders do:[:classes|
        classes do:[:class|
            aBlock value: class
        ]        
    ]

    "Created: / 23-10-2011 / 20:13:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'helpers'!

addClassLoader: aJavaClassLoader

loaders at: aJavaClassLoader put: Dictionary new.
!

getClassesDefinedBy:classLoader 
    ^loaders at: classLoader ifAbsent: [nil].
! !

!JavaClassRegistry methodsFor:'initialization'!

flush

    self initialize.

    "Modified: / 30-10-2011 / 12:06:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

initialize
    loaders := WeakIdentityDictionary new.
    loaders at: nil put: Dictionary new.
    notifier := BackgroundQueueProcessingJob 
                    named: 'Java class registry notifier'
                    on:[:typeAndClass|Smalltalk changed: typeAndClass first with: typeAndClass second].
    notifier priority: Processor userBackgroundPriority - 1.

    "Modified: / 16-08-2012 / 17:31:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

setVM: aJavaVM

    "Now, aJavaVM == JavaVM (i.e, the class JavaVM 
    itself, not its instance)"

    vm := aJavaVM

    "Created: / 21-12-2010 / 19:44:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'registering'!

registerClass: newClass
    | classes oldClass |

    self assert: (newClass name includes: $.) not.
    loaders keysAndValuesDo:[:loader :classesPerLoader|

        (classesPerLoader includesKey: newClass name) ifTrue:[
            loader == newClass classLoader ifTrue:[
                (oldClass := classesPerLoader at: newClass name) ~~ newClass ifTrue:[
                    "Class already exists, reload & reinstall"

                    | reloadedClass |
                    reloadedClass := JavaClassReloader reload: oldClass with: newClass.
                    "/OK, full reload, not just method dictionary update"
                    reloadedClass ~~ oldClass ifTrue:[
                        classesPerLoader
                            at: newClass name 
                            put: reloadedClass.
                        self registerClassInSmalltalk: reloadedClass notify: false.
                    ].
                    Smalltalk changed: #classDefinition with: newClass.
                    ^self.             
                ].
            ] ifFalse:[
                ((newClass name == #Script1) 
                    and:[newClass superclass name == #'groovy/lang/Script']) ifFalse:[
                        "/self breakPoint: #jv.
                    ]
            ]
        ]                        
    ].

    classes := loaders at: newClass classLoader ifAbsent: nil.
    classes isNil ifTrue:[
        classes := loaders at: newClass classLoader put: Dictionary new.
    ].
    classes at: newClass name  put: newClass.
    self registerClassInSmalltalk: newClass notify: true.

    "Created: / 23-10-2011 / 11:53:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 02-11-2011 / 18:40:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 24-01-2013 / 11:31:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

registerClassInSmalltalk: javaclass notify: doNotify
    "Registers class in Smalltalk system dictionary and
     notifies system, so the class become visible by
     Smalltalk"

    | nameComponents accessor ns |


    nameComponents := javaclass name asCollectionOfSubstringsSeparatedBy:$/.
    nameComponents size > 1 ifTrue:[
        javaclass setCategory:((nameComponents asStringWith:$/ from:1 to:(nameComponents size - 1))
                            replaceAll:$/ with:$. ).
    ].

    nameComponents size > 1 ifTrue:[
        javaclass setPackage:((nameComponents copyWithoutLast:1) asStringWith:$/) asSymbol
    ] ifFalse:[
        javaclass setPackage:javaclass name asSymbol
    ].

    "/ break up the package and create nameSpaces
    "/ for each package component.
    "/ This allows java.foo.bar to be visible in ST/X
    "/ under the name JAVA::java::foo::bar

    accessor := javaclass"JavaClassAccessor fullName: aString".    
    ns := JAVA.

    nameComponents size > 1 ifTrue:[
        | s |

        s := '' writeStream.
        s nextPutAll:'JAVA'.
        nameComponents from:1 to:(nameComponents size - 1) do:[:aPart |
            s nextPutAll:'::'.
            s nextPutAll:aPart
        ].
        Metaclass confirmationQuerySignal answer:false do:[
            Class withoutUpdatingChangesDo:[
                ns := JavaPackage fullName:(s contents).
            ]
        ]
    ].

    ns isNameSpace ifTrue:[
        ns at:nameComponents last asSymbol put:accessor.
    ].


    doNotify ifTrue:[
        notifier add:(Array with: #newClass with: javaclass).
    ].

    "Created: / 04-04-2012 / 10:01:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

registerClasses: classes
    classes do:[:cls|self registerClass: cls].

    "Created: / 02-01-2013 / 17:01:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

unregisterClass: oldClass
    | classes |

    self assert: (oldClass name includes: $.) not.
    self unregisterClassInSmalltalk: oldClass notify: true.
    classes := loaders at: oldClass classLoader ifAbsent: nil.
    classes notNil ifTrue:[
        classes removeKey: oldClass name.
    ].

    "Created: / 04-04-2012 / 02:43:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

unregisterClassInSmalltalk: javaclass notify: doNotify
    "Unregisters given class from Smalltalk system dictionary
     so the class is no longer visible from Smalltalk"

    | nameComponents ns |

    ns := JAVA.

    nameComponents size > 1 ifTrue:[
        | s |

        s := '' writeStream.
        s nextPutAll:'JAVA'.
        nameComponents from:1 to:(nameComponents size - 1) do:[:aPart |
            s nextPutAll:'::'.
            s nextPutAll:aPart
        ].
        Metaclass confirmationQuerySignal answer:false do:[
            Class withoutUpdatingChangesDo:[
                ns := JavaPackage fullName:(s contents).
            ]
        ]
    ].

    ns isNameSpace ifTrue:[
        "/Hmmm hmm, how to remove class from a namespace?.
        "/ns at:nameComponents last asSymbol put:nil
    ].

    doNotify ifTrue:[
        notifier add:(Array with: #classRemove with: javaclass).
    ].

    "Created: / 04-04-2012 / 10:01:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

unregisterClassLoader: aJavaClassLoader 
    loaders removeKey: aJavaClassLoader ifAbsent: nil.

    "Created: / 16-12-2012 / 16:39:45 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

unregisterClasses: classes
    classes do:[:cls|self unregisterClass: cls].

    "Created: / 02-01-2013 / 17:01:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '§Id§'
! !