src/JavaClassRegistry.st
branchjk_new_structure
changeset 1026 5badd1d31864
child 1027 43dc608e14aa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/JavaClassRegistry.st	Wed Oct 19 16:18:27 2011 +0000
@@ -0,0 +1,299 @@
+"
+ COPYRIGHT (c) 1996-2011 by Claus Gittinger
+ 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.
+
+ Parts of the code written at SWING Reasearch Group [1] are MIT licensed:
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the 'Software'), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+
+ [1] Code written at SWING Research Group contain a signature
+     of one of the above copright owners.
+"
+"{ Package: 'stx:libjava' }"
+
+Object subclass:#JavaClassRegistry
+	instanceVariableNames:'classLoadersAndClasses systemClassLoader'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Languages-Java-Support'
+!
+
+!JavaClassRegistry class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1996-2011 by Claus Gittinger
+ 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.
+
+ Parts of the code written at SWING Reasearch Group [1] are MIT licensed:
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the 'Software'), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+
+ [1] Code written at SWING Research Group contain a signature
+     of one of the above copright owners.
+
+"
+! !
+
+!JavaClassRegistry class methodsFor:'instance creation'!
+
+new
+^ self basicNew initialize.
+! !
+
+!JavaClassRegistry methodsFor:'* uncategorized *'!
+
+getSystemClassLoader
+    systemClassLoader 
+        ifNil: [
+            systemClassLoader := (Java classForName: 'java.lang.ClassLoader') 
+                        perform: #'getSystemClassLoader()Ljava/lang/ClassLoader;'.
+        ].
+    ^ systemClassLoader.
+!
+
+handleNilClassLoaderFor: aJavaClassName 
+! !
+
+!JavaClassRegistry methodsFor:'class loaders'!
+
+addClassLoader: aJavaClassLoader
+
+classLoadersAndClasses at: aJavaClassLoader put: Dictionary new.
+!
+
+getClassesDefinedBy:classLoader 
+    ^classLoadersAndClasses at: classLoader ifAbsent: [nil].
+!
+
+removeClassLoader: aJavaClassLoader 
+    classLoadersAndClasses removeKey: aJavaClassLoader.
+! !
+
+!JavaClassRegistry methodsFor:'class loading'!
+
+loadClassesIn: directory 
+"load all classes (.class files, strictly speaking) found in given directory.
+The argument is string containing the name of the directory.
+
+Returns a set of loaded classes."
+
+^ self loadClassesIn: directory matching: '*'.
+
+"Modified: / 11-06-2011 / 13:35:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+loadClassesIn: directory matching: pattern 
+"load all classes (.class files, strictly speaking) found in given directory.
+The arguments are a string containing the name of the directory
+and a class name pattern. Only classes matching the pattern are loaded.
+class names are matched using String>>matches:.
+ALL CLASSES ARE INSTALLED!!
+Returns a set of loaded classes."
+
+| dir  dirString  className  loadedClasses |
+
+dir := directory asFilename asAbsoluteFilename.
+loadedClasses := Set new.
+Java addToClassPath: dir pathName.
+dir 
+recursiveDirectoryContentsAsFilenamesDo: [
+:filename | 
+(filename isRegularFile and: [ filename suffix = #class ]) ifTrue: [
+dirString := dir pathName.
+className := filename withoutSuffix pathName.
+className := className subString: dirString size + 2 to: className size.
+className := className copyReplaceAll: (Filename separator) with: $..
+(className matches: pattern) ifTrue: [
+|loadedClass|
+loadedClass := JavaClassReader readClass: className.
+self addClass: loadedClass definedBy:nil.
+loadedClasses add: loadedClass.
+]
+]
+].
+^ loadedClasses.
+
+"Modified: / 29-04-2011 / 17:43:32 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+"Created: / 11-06-2011 / 13:34:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+"Modified: / 11-06-2011 / 16:19:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+loadFile: aFilename 
+    "reads a class from aFilename, installs and returns it."
+    
+    | aClass |
+
+    aClass := JavaClassReader readFile: aFilename ignoring: Set new.
+    self addClass: aClass definedBy: nil.
+    ^ aClass.
+
+    "Created: / 15.4.1996 / 14:58:53 / cg"
+    "Modified: / 12.5.1998 / 22:06:52 / cg"
+!
+
+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. If aClassLoader is nil, a new standard loader
+     is created."
+    
+    | javaClass |
+
+    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: #severe
+                        facility: #JavaClassRegistry.
+                    self breakPoint: #mh.
+                    ^ nil.
+                ].
+            self addClass: javaClass definedBy: aJavaClassLoader.
+        ].
+    ^ javaClass
+! !
+
+!JavaClassRegistry methodsFor:'classes'!
+
+addClass:aJavaClass definedBy:classLoader
+|classesDefinedByClassLoader|
+classesDefinedByClassLoader := classLoadersAndClasses at: classLoader ifAbsentPut:[Dictionary new].
+classesDefinedByClassLoader at: aJavaClass name put: aJavaClass.
+!
+
+at: className 
+    ^ (classLoadersAndClasses at: nil) at: className ifAbsent: nil.
+!
+
+at: classLoader at: className
+    ^ (classLoadersAndClasses at: classLoader) at: className ifAbsent: nil.
+!
+
+classForName: className
+    "you should be damn sure you don't have class loader"
+   ^ self classForName: className definedBy:nil.
+!
+
+classForName: className definedBy: aJavaClassLoader 
+    | classesLoadedByClassLoader  result  classLoader |
+
+    classesLoadedByClassLoader := classLoadersAndClasses at: classLoader
+                ifAbsent: [
+                    "classloader not found?"
+                    self breakPoint: #mh.
+                ].
+    result := classesLoadedByClassLoader at: className
+                ifAbsent: [
+                    |   newJavaClass |
+                    
+                    aJavaClassLoader 
+                        ifNil: [ newJavaClass := self loadClassNamed: className. ]
+                        ifNotNil: [ newJavaClass := self loadClassNamed: className using: aJavaClassLoader. ].
+                ].
+    ^ result.
+!
+
+loadClassNamed: className 
+    | result |
+
+    result := JavaClassReader readClass: className.
+    self addClass: result definedBy: result classLoader.
+    ^ result.
+!
+
+loadClassNamed: className using: classLoader 
+    | classObject  result |
+
+    classObject := classLoader 
+                perform: #'loadClass(Ljava/lang/String;)Ljava/lang/Class;'
+                with: (Java as_String: (className copyReplaceAll: $/ with: $.)).
+    classObject ifNil: [ self breakPoint: #mh ].
+    result := JavaVM classForJavaClassObject: classObject.
+    self addClass: result definedBy: result classLoader.
+    ^ result.
+! !
+
+!JavaClassRegistry methodsFor:'initialization'!
+
+flush
+    classLoadersAndClasses := Dictionary new.
+    classLoadersAndClasses at: nil put: Dictionary new.
+    systemClassLoader := nil.
+!
+
+initialize
+    classLoadersAndClasses := Dictionary new.
+    classLoadersAndClasses at: nil put: Dictionary new.
+! !
+
+!JavaClassRegistry class methodsFor:'documentation'!
+
+version_SVN
+    ^ '$Id$'
+! !