JavaClassRegistry.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 14 Sep 2013 15:48:03 +0100
branchdevelopment
changeset 2729 ac412f6ea6d4
parent 2705 e693added0af
child 2711 a00302fe5083
child 2730 272689c14005
permissions -rw-r--r--
More support for method's source display. Not yet working. The behaviour can (whether to show full class source or just method's source) be now controlled by JavaMethod class>>showFullSource: More work has to be done to fully support this.

"
 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:'JavaVMData'
	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 class methodsFor:'others'!

version_HG

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

!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 notNil and:[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.
    self breakPoint: #jv.
    javaClass := JavaClassReader readStream: javaClassDataStream loader: aJavaClassLoader.
    javaClass isNil ifTrue: [
            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: / 02-05-2013 / 09:55:15 / 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: / 08-02-2013 / 00:58:28 / 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>"
    "Modified (format): / 07-02-2013 / 17:36:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'registering'!

registerClass: newClass
    | classes oldClass |

    ((newClass name == #'$do$It$') 
        and:[newClass superclass name == #'groovy/lang/Script']) ifTrue:[
        "/ Mhhh...Groovy do-it. No need to register this!!
        ^ self
    ].


    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.
                    ].
                    "Transfer all possible problems from old class to new class"
                    JavaCompilerProblemRegistry notNil ifTrue:[
                        | problems |

                        problems := JavaCompilerProblemRegistry problemsFor: newClass.
                        JavaCompilerProblemRegistry problemsFor: reloadedClass put: problems.
                    ].
                    Smalltalk changed: #classDefinition with: reloadedClass.
                    ^self.             
                ].
            ].
        ].
    ].

    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: / 29-08-2013 / 01:03:31 / 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 nsName ns  |

    javaclass isSynthetic ifTrue:[ ^ self ].
    nameComponents := javaclass name asCollectionOfSubstringsSeparatedBy:$/.
    nameComponents size > 1 ifTrue:[
        javaclass setCategory:((nameComponents asStringWith:$/ from:1 to:(nameComponents size - 1))
                            replaceAll:$/ with:$. ).
    ] ifFalse:[
        javaclass setCategory:#'(default)'
    ].

    nameComponents size > 1 ifTrue:[
        javaclass setPackage:((nameComponents copyButLast:1) asStringWith:$/) asSymbol
    ] ifFalse:[
        javaclass setPackage:#'java/(default)'
    ].

    "/ 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".    
    nsName := javaclass topNameSpaceName.
    ns := Smalltalk at: nsName.
    ns isNil ifTrue:[
        JavaPackage name:nsName.
        ns := Smalltalk at: nsName
    ].

    nameComponents size > 1 ifTrue:[
        | s |

        s := '' writeStream.

        s nextPutAll: nsName.       
        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.
        javaclass setEnvironment: ns.
    ].


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

    "Created: / 04-04-2012 / 10:01:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 03-09-2013 / 00:41:37 / 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>"
!

registerClasses: classes andWait: doWait
    "Register given set of classes and if
     `doWait` is true, wait until all pending notifications
     are delivered"

    classes do:[:cls|self registerClass: cls].
    doWait ifTrue:[
        notifier waitUntilProcessed.
    ]

    "Created: / 04-08-2013 / 03:55:49 / 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"

    | ns |

    javaclass isSynthetic ifTrue:[ ^ self ].
    ns := javaclass nameSpace.

    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>"
    "Modified: / 03-09-2013 / 00:41:18 / 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_CVS
    ^ '$Header: /cvs/stx/stx/libjava/JavaClassRegistry.st,v 1.4 2013-04-25 13:10:25 stefan Exp $'
!

version_SVN
    ^ '§Id§'
! !