src/JavaClassRegistry.st
author vranyj1
Thu, 23 Feb 2012 12:06:34 +0000
branchjk_new_structure
changeset 1377 79eafebce880
parent 1372 dea574a1b6b3
child 1441 0faa0f8eda0c
permissions -rw-r--r--
Fixes...

"
 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'
	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>"
!

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>"
! !

!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].
!

removeClassLoader: aJavaClassLoader 
    loaders removeKey: aJavaClassLoader.
! !

!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.

    "Modified: / 24-10-2011 / 16:42:49 / 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"
                    classesPerLoader
                        at: newClass name 
                        put: (JavaClassReloader reload: oldClass 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.
    "/Smalltalk changed: #newClass with: newClass.

    "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: / 23-02-2012 / 11:38:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !