src/JavaClassRegistry.st
author vranyj1
Wed, 23 Nov 2011 15:26:06 +0000
branchjk_new_structure
changeset 1152 040cba55a7d2
parent 1120 b65e199200a4
child 1155 d6f6d5fc0343
permissions -rw-r--r--
Copyright fixes

"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modification 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

 Parts of the code written by Claus Gittinger are under following
 license:

 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 contain a signature
     of one of the above copright owners. For exact set of such code
     see the differences between this version and version stx:lib
     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 modification 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

 Parts of the code written by Claus Gittinger are under following
 license:

 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 contain a signature
     of one of the above copright owners. For exact set of such code
     see the differences between this version and version stx:lib
     as of 1.9.2010

"
!

documentation
"
    documentation to be added.

    [author:]
        Jan Vrany <jan.vrany@fit.cvut.cz>

    [instance variables:]

    [class variables:]

    [see also:]

"
! !

!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: aJavaClass
    | classes |

    self assert: (aJavaClass name includes: $.) not.
    loaders keysAndValuesDo:[:loader :classes|

        (classes includesKey: aJavaClass name) ifTrue:[
            loader == aJavaClass classLoader ifTrue:[
                (classes at: aJavaClass name) ~~ aJavaClass ifTrue:[
                    self error:'Trying to register class twice!!'
                ].
            ] ifFalse:[
                ((aJavaClass name == #Script1) 
                    and:[aJavaClass superclass name == #'groovy/lang/Script']) ifFalse:[
                        self breakPoint: #jv.
                    ]
            ]
        ]                        
    ].

    classes := loaders at: aJavaClass classLoader ifAbsent: nil.
    classes isNil ifTrue:[
        classes := loaders at: aJavaClass classLoader put: Dictionary new.
    ].
    classes at: aJavaClass name put: aJavaClass.

    "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: / 16-11-2011 / 23:57:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !