src/JavaClassRegistry.st
branchjk_new_structure
changeset 1037 3dc5755c5fe4
parent 1036 4ff03464c3b3
child 1038 bf2ece5a5362
equal deleted inserted replaced
1036:4ff03464c3b3 1037:3dc5755c5fe4
   115 
   115 
   116 new
   116 new
   117 ^ self basicNew initialize.
   117 ^ self basicNew initialize.
   118 ! !
   118 ! !
   119 
   119 
   120 !JavaClassRegistry methodsFor:'class loaders'!
   120 !JavaClassRegistry methodsFor:'accessing'!
   121 
   121 
   122 addClassLoader: aJavaClassLoader
   122 classForName: className loader: classLoader ifAbsent: aBlock 
   123 
   123     "Get class loaded by given classLoader from registry or evaluate aBlock if class is not yet registered"
   124 classLoadersAndClasses at: aJavaClassLoader put: Dictionary new.
   124         
   125 !
   125     ^ (classLoadersAndClasses at: classLoader
   126 
   126         ifAbsent: [ self error: 'cannot find given classLoader in registry' ]) 
   127 getClassesDefinedBy:classLoader 
   127             at: className
   128     ^classLoadersAndClasses at: classLoader ifAbsent: [nil].
   128             ifAbsent: aBlock.
   129 !
   129 
   130 
   130     "Modified: / 21-10-2011 / 12:39:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
   131 getSystemClassLoader
   131     "Created: / 23-10-2011 / 11:40:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   132     systemClassLoader 
   132 !
   133         ifNil: [
   133 
   134             systemClassLoader := (JavaVM classForName: 'java.lang.ClassLoader') 
   134 classForName: className loader: classLoader ifAbsentPut: block 
   135                         perform: #'getSystemClassLoader()Ljava/lang/ClassLoader;'.
   135 
   136         ].
   136     "Get class loaded by classLoader from registry. if absent block is evaluated and resulting class in registered in registry, "
   137     ^ systemClassLoader.
   137     
   138 !
   138     ^ (classLoadersAndClasses at: classLoader
   139 
   139         ifAbsent: [ self halt: 'cannot find given classLoader in registry' ]) 
   140 removeClassLoader: aJavaClassLoader 
   140             at: className
   141     classLoadersAndClasses removeKey: aJavaClassLoader.
   141             ifAbsentPut: block.
       
   142 
       
   143     "Created: / 21-10-2011 / 12:00:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
       
   144     "Created: / 23-10-2011 / 11:36:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   145 !
       
   146 
       
   147 classNamed: className
       
   148 
       
   149     "Return a class for given classname loaded by 'current classloader' or 
       
   150      nil if not yet loaded"
       
   151 
       
   152     ^self classNamed: className loader: JavaClassReader classLoaderQuerySignal query
       
   153 
       
   154     "Created: / 23-10-2011 / 12:23:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   155 !
       
   156 
       
   157 classNamed: className loader: classLoader
       
   158 
       
   159     "Return a class for given classname loaded by given classloader or 
       
   160      nil if not yet loaded"
       
   161 
       
   162     ^self classForName: className loader: classLoader ifAbsent:[nil].
       
   163 
       
   164     "Created: / 23-10-2011 / 12:23:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   165 ! !
       
   166 
       
   167 !JavaClassRegistry methodsFor:'accessing-obsolete'!
       
   168 
       
   169 at: className 
       
   170 
       
   171      <resource: #obsolete> 
       
   172 
       
   173     self breakPoint:#jv info: 'Use #classForName:'.
       
   174     self breakPoint:#mh info: 'Use #classForName:'.
       
   175 
       
   176     ^ self classNamed: className
       
   177 
       
   178     "Modified: / 23-10-2011 / 12:23:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   179 !
       
   180 
       
   181 at: classLoader at: className
       
   182 
       
   183     <resource: #obsolete> 
       
   184 
       
   185     self breakPoint:#jv info: 'Use #classForName:loader:'.
       
   186     self breakPoint:#mh info: 'Use #classForName:loader:'.
       
   187 
       
   188     ^self classNamed: className loader: classLoader
       
   189 
       
   190     "Modified: / 23-10-2011 / 12:23:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   191 !
       
   192 
       
   193 at: classLoader at: className ifAbsent: aBlock 
       
   194 
       
   195     <resource: #obsolete>
       
   196 
       
   197     self breakPoint:#mh info: 'Use #classForName:loader:isAbsent:'.
       
   198     self breakPoint:#jv info: 'Use #classForName:loader:isAbsent:'.
       
   199 
       
   200     ^self classForName: className loader: classLoader ifAbsent: aBlock.
       
   201 
       
   202     "Modified: / 21-10-2011 / 12:39:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
       
   203     "Modified: / 23-10-2011 / 11:47:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   204 !
       
   205 
       
   206 at: classLoader at: className ifAbsentPut: aBlock 
       
   207 
       
   208     <resource: #obsolete> 
       
   209 
       
   210     self breakPoint:#jv info: 'Use #classForName:loader:isAbsentPut:'.
       
   211     self breakPoint:#mh info: 'Use #classForName:loader:isAbsentPut:'.
       
   212 
       
   213     ^self classForName: className loader: classLoader ifAbsentPut: aBlock
       
   214 
       
   215     "Created: / 21-10-2011 / 12:00:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
       
   216     "Modified: / 23-10-2011 / 11:41:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   142 ! !
   217 ! !
   143 
   218 
   144 !JavaClassRegistry methodsFor:'class loading'!
   219 !JavaClassRegistry methodsFor:'class loading'!
   145 
   220 
   146 loadFile: aFilename 
   221 loadFile: aFilename 
   149     | aClass |
   224     | aClass |
   150 
   225 
   151     self breakPoint: #mh.
   226     self breakPoint: #mh.
   152     aClass := JavaClassReader readFile: aFilename ignoring: Set new.
   227     aClass := JavaClassReader readFile: aFilename ignoring: Set new.
   153     aClass isJavaClass ifFalse:[self breakPoint:#mh].
   228     aClass isJavaClass ifFalse:[self breakPoint:#mh].
   154     self addClass: aClass definedBy: nil.
   229     self registerClass: aClass.
   155     ^ aClass.
   230     ^ aClass.
   156 
   231 
   157     "Created: / 15.4.1996 / 14:58:53 / cg"
   232     "Created: / 15-04-1996 / 14:58:53 / cg"
   158     "Modified: / 12.5.1998 / 22:06:52 / cg"
   233     "Modified: / 12-05-1998 / 22:06:52 / cg"
       
   234     "Modified: / 23-10-2011 / 11:55:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   159 !
   235 !
   160 
   236 
   161 loadStream: javaClassDataStream loader: aJavaClassLoader 
   237 loadStream: javaClassDataStream loader: aJavaClassLoader 
   162     "reads a class from aStream and returns it.
   238     "reads a class from aStream and returns it.
   163      The JavaClass is installed as global.
   239      The JavaClass is installed as global.
   173                         ignoring: (Set new).
   249                         ignoring: (Set new).
   174             javaClass 
   250             javaClass 
   175                 ifNil: [
   251                 ifNil: [
   176                     Logger 
   252                     Logger 
   177                         log: 'JavaClassReader was not able to read given data stream'
   253                         log: 'JavaClassReader was not able to read given data stream'
   178                         severity: #severe
   254                         severity: #warn
   179                         facility: #JavaClassRegistry.
   255                         facility: #JavaClassRegistry.
   180                     self breakPoint: #mh.
   256                     self breakPoint: #mh.
   181                     ^ nil.
   257                     ^ nil.
   182                 ].
   258                 ].
   183             javaClass isJavaClass ifFalse: [ self breakPoint: #mh. ].
   259             javaClass isJavaClass ifFalse: [ self breakPoint: #mh. ].
   184             self addClass: javaClass definedBy: aJavaClassLoader.
   260             javaClass classLoader: aJavaClassLoader.
       
   261             self registerClass: javaClass.
   185         ].
   262         ].
   186     ^ javaClass
   263     ^ javaClass
       
   264 
       
   265     "Modified: / 23-10-2011 / 11:57:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   266 ! !
       
   267 
       
   268 !JavaClassRegistry methodsFor:'helpers'!
       
   269 
       
   270 addClassLoader: aJavaClassLoader
       
   271 
       
   272 classLoadersAndClasses at: aJavaClassLoader put: Dictionary new.
       
   273 !
       
   274 
       
   275 getClassesDefinedBy:classLoader 
       
   276     ^classLoadersAndClasses at: classLoader ifAbsent: [nil].
       
   277 !
       
   278 
       
   279 getSystemClassLoader
       
   280     systemClassLoader 
       
   281         ifNil: [
       
   282             systemClassLoader := (JavaVM classForName: 'java.lang.ClassLoader') 
       
   283                         perform: #'getSystemClassLoader()Ljava/lang/ClassLoader;'.
       
   284         ].
       
   285     ^ systemClassLoader.
       
   286 !
       
   287 
       
   288 removeClassLoader: aJavaClassLoader 
       
   289     classLoadersAndClasses removeKey: aJavaClassLoader.
   187 ! !
   290 ! !
   188 
   291 
   189 !JavaClassRegistry methodsFor:'initialization'!
   292 !JavaClassRegistry methodsFor:'initialization'!
   190 
   293 
   191 flush
   294 flush
   197 initialize
   300 initialize
   198     classLoadersAndClasses := Dictionary new.
   301     classLoadersAndClasses := Dictionary new.
   199     classLoadersAndClasses at: nil put: Dictionary new.
   302     classLoadersAndClasses at: nil put: Dictionary new.
   200 ! !
   303 ! !
   201 
   304 
   202 !JavaClassRegistry methodsFor:'public api'!
   305 !JavaClassRegistry methodsFor:'registering'!
   203 
   306 
   204 addClass: aJavaClass definedBy: classLoader 
   307 registerClass: aJavaClass
   205     | classesDefinedByClassLoader |
   308     | classesDefinedByClassLoader |
   206 
   309 
   207     classesDefinedByClassLoader := classLoadersAndClasses at: classLoader
   310     classesDefinedByClassLoader := classLoadersAndClasses at: aJavaClass classLoader
   208                 ifAbsentPut: [ Dictionary new ].
   311                 ifAbsentPut: [ Dictionary new ].
   209     classesDefinedByClassLoader at: aJavaClass name put: aJavaClass.
   312     classesDefinedByClassLoader at: aJavaClass name put: aJavaClass.
   210 !
   313 
   211 
   314     "Created: / 23-10-2011 / 11:53:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   212 at: className 
       
   213     ^ self at: nil at: className ifAbsent: nil.
       
   214 !
       
   215 
       
   216 at: classLoader at: className 
       
   217     ^self at: classLoader at: className ifAbsent:nil.
       
   218 !
       
   219 
       
   220 at: classLoader at: className ifAbsent: aBlock 
       
   221     "get class loaded by classLoader from registry. Don't load if not found in registry"
       
   222         
       
   223     ^ (classLoadersAndClasses at: classLoader
       
   224         ifAbsent: [ self halt: 'cannot find given classLoader in registry' ]) 
       
   225             at: className
       
   226             ifAbsent: aBlock.
       
   227 
       
   228     "Modified: / 21-10-2011 / 12:39:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
       
   229 !
       
   230 
       
   231 at: classLoader at: className ifAbsentPut: aBlock 
       
   232     "get class loaded by classLoader from registry. if absent, aBlock should return class to be added"
       
   233     
       
   234     ^ (classLoadersAndClasses at: classLoader
       
   235         ifAbsent: [ self halt: 'cannot find given classLoader in registry' ]) 
       
   236             at: className
       
   237             ifAbsentPut: aBlock.
       
   238 
       
   239     "Created: / 21-10-2011 / 12:00:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
       
   240 ! !
   315 ! !
   241 
   316 
   242 !JavaClassRegistry class methodsFor:'documentation'!
   317 !JavaClassRegistry class methodsFor:'documentation'!
   243 
   318 
   244 version_SVN
   319 version_SVN