Java.st
branchdevelopment
changeset 2737 83f8416e153c
parent 2732 7d1a1fb5b01a
child 2807 46f6ef83cce6
equal deleted inserted replaced
2736:7cb269d572d3 2737:83f8416e153c
  1207     "Created: / 18.4.1996 / 00:05:31 / cg"
  1207     "Created: / 18.4.1996 / 00:05:31 / cg"
  1208     "Modified: / 19.10.1998 / 20:57:44 / cg"
  1208     "Modified: / 19.10.1998 / 20:57:44 / cg"
  1209 !
  1209 !
  1210 
  1210 
  1211 removeClass:aJavaClass
  1211 removeClass:aJavaClass
  1212     "/ remove it from myself
  1212     "Remove the class from the system, as well as all its subclasses
  1213 
  1213      (or users of the interface, if class is interface)"
  1214     |javaName sym cls p ns|
  1214 
  1215 
  1215     JavaVM unloadClass: aJavaClass.
  1216 "/    self breakPoint: #jv.
  1216 
  1217 "/    self breakPoint: #mh.
  1217     "Modified: / 19-10-1998 / 20:58:49 / cg"
  1218 
  1218     "Modified (comment): / 14-09-2013 / 23:51:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1219     javaName := aJavaClass fullName.
       
  1220 
       
  1221     sym := javaName asSymbolIfInterned.
       
  1222     sym notNil ifTrue:[
       
  1223         cls := JavaVM classForName: sym definedBy: aJavaClass classLoader.
       
  1224     ].
       
  1225     (cls notNil and:[cls == aJavaClass]) ifTrue:[
       
  1226         self updateClassRefsFrom:aJavaClass to:nil.
       
  1227     ].
       
  1228     Smalltalk removeKey:('JAVA::' , javaName) asSymbol.
       
  1229     Smalltalk removeKey:javaName asSymbol.
       
  1230     Smalltalk removeKey:((javaName , '') copyReplaceAll:$/ with:$.) asSymbol.
       
  1231 
       
  1232     "/ remove myself from the JAVA::-::-:: namespace
       
  1233     "/ (which exists for convenient smalltalk access only)
       
  1234 
       
  1235     p := aJavaClass nameSpacePath.
       
  1236     p knownAsSymbol ifTrue:[
       
  1237         ns := Smalltalk at:p asSymbol.
       
  1238         (ns notNil and:[ns isNameSpace]) ifTrue:[
       
  1239             Smalltalk removeKey:(p , '::' , aJavaClass lastName) asSymbol
       
  1240         ]
       
  1241     ].
       
  1242 
       
  1243     JavaVM registry unregisterClass: aJavaClass.
       
  1244 
       
  1245     "Modified: / 17-04-2013 / 21:29:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
  1246     "Modified: / 16-07-2013 / 19:48:40 / cg"
       
  1247 !
  1219 !
  1248 
  1220 
  1249 unresolvedClassRefFor:aClassName
  1221 unresolvedClassRefFor:aClassName
  1250     UnresolvedClassRefs isNil ifTrue:[^ nil].
  1222     UnresolvedClassRefs isNil ifTrue:[^ nil].
  1251     ^ UnresolvedClassRefs at:aClassName ifAbsent:nil.
  1223     ^ UnresolvedClassRefs at:aClassName ifAbsent:nil.
  1273     "Modified: 12.8.1997 / 03:04:44 / cg"
  1245     "Modified: 12.8.1997 / 03:04:44 / cg"
  1274 ! !
  1246 ! !
  1275 
  1247 
  1276 !Java class methodsFor:'source management'!
  1248 !Java class methodsFor:'source management'!
  1277 
  1249 
  1278 classSource:filename package:package in:dirOrZipFile
  1250 classSourceStreamFor: aClass 
  1279     |fn pn zar f |
  1251     | jpackage  dirName  sourceFileName  sourceFile  loader  codeBaseURL  protocol  codeBaseURLIdx  src |
  1280 
  1252 
  1281     fn := dirOrZipFile asFilename.
  1253     aClass isNil ifTrue: [
  1282     pn := dirOrZipFile asString.
       
  1283     fn isDirectory
       
  1284 	ifTrue:
       
  1285 	    [ package notNil
       
  1286 		ifTrue:[ (f:= fn / package / filename) exists ifTrue:[ ^ f contents asString ]. ].
       
  1287 	    (f := fn / filename) exists ifTrue:[ ^ f contents asString ]. ]
       
  1288 	ifFalse:
       
  1289 	    [ ((pn last == $p and: [fn hasSuffix:'zip']) or:
       
  1290 	      [(pn last == $r and: [fn hasSuffix:'jar'])])
       
  1291 		ifTrue:
       
  1292 		    [ zar := SourceArchiveCache
       
  1293 				at: dirOrZipFile
       
  1294 				ifAbsentPut:[ZipArchive oldFileNamed:fn pathName].
       
  1295 		    zar notNil
       
  1296 			ifTrue:
       
  1297 			    [   package notNil
       
  1298 				ifTrue:[
       
  1299 				    OperatingSystem fileSeparator ~~ $/ ifTrue: [
       
  1300 					f := (package copyReplaceAll: OperatingSystem fileSeparator with: $/) , '/' , filename
       
  1301 				    ] ifFalse:[
       
  1302 					f := package , '/' , filename.
       
  1303 				    ]]
       
  1304 				ifFalse:[ f := filename].
       
  1305 			    (zar findMember: f) ifNotNil:
       
  1306 				[
       
  1307 				    "Kludge because of broken ZipArchive"
       
  1308 				    |  cache |
       
  1309 				    cache := Java cacheDirectory / Release name / 'src'.
       
  1310 				    cache exists ifFalse:[cache recursiveMakeDirectory].
       
  1311 				    (cache / f) exists ifTrue:[^(cache / f) contents asString].
       
  1312 				    OperatingSystem
       
  1313 					executeCommand:('unzip "%1" "%2"' bindWith: fn asAbsoluteFilename asString
       
  1314 							    with: f asString)
       
  1315 					inDirectory: cache asString.
       
  1316 				    (f := cache / f) exists ifTrue:[^f contents asString]
       
  1317 				].
       
  1318 			    zar closeFile.
       
  1319 			    ]]].
       
  1320 
       
  1321     ^ nil
       
  1322 
       
  1323     "
       
  1324 	Java classSource: 'Object.java' package:'java/lang' in:'/home/jv/Projects/JavaX/java-6-openjdk/src'
       
  1325 	Java classSource: 'Object.java' package:'java/lang' in:'/usr/lib/jvm/java-6-openjdk/src.zip'
       
  1326 
       
  1327     "
       
  1328 
       
  1329     "Modified: / 29-03-1998 / 21:46:40 / cg"
       
  1330     "Created: / 30-11-2010 / 12:32:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
  1331     "Modified: / 04-12-2012 / 00:43:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
  1332 !
       
  1333 
       
  1334 classSourceOf:aClass
       
  1335     |jpackage dirName sourceFileName sourceFile loader codeBaseURL protocol codeBaseURLIdx src |
       
  1336 
       
  1337     aClass isNil ifTrue:[
       
  1338         ^ nil
  1254         ^ nil
  1339     ].
  1255     ].
       
  1256     
  1340     "/ look at the cache"
  1257     "/ look at the cache"
  1341     SourceCache at: aClass ifPresent: [:src|^src].
       
  1342 
       
  1343 
  1258 
  1344     "/ maybe it was loaded by a java classLoader ...
  1259     "/ maybe it was loaded by a java classLoader ...
  1345     (loader := aClass classLoader) notNil ifTrue:[
  1260     
  1346         codeBaseURLIdx := loader class instVarOffsetOf:'codeBaseURL'.
  1261     (loader := aClass classLoader) notNil ifTrue: [
  1347         codeBaseURLIdx notNil ifTrue:[
  1262         codeBaseURLIdx := loader class instVarOffsetOf: 'codeBaseURL'.
  1348             (codeBaseURL := loader instVarAt:codeBaseURLIdx) notNil ifTrue:[
  1263         codeBaseURLIdx notNil ifTrue: [
  1349                 (protocol := codeBaseURL instVarNamed:'protocol') notNil ifTrue:[
  1264             (codeBaseURL := loader instVarAt: codeBaseURLIdx) notNil ifTrue: [
  1350                     (Java as_ST_String:protocol) = 'file' ifTrue:[
  1265                 (protocol := codeBaseURL instVarNamed: 'protocol') notNil ifTrue: [
  1351                         dirName := Java as_ST_String:(codeBaseURL instVarNamed:'file').
  1266                     (Java as_ST_String: protocol) = 'file' ifTrue: [
       
  1267                         dirName := Java as_ST_String: (codeBaseURL instVarNamed: 'file').
  1352                         dirName := dirName asFilename.
  1268                         dirName := dirName asFilename.
  1353                         dirName exists ifTrue:[
  1269                         dirName exists ifTrue: [
  1354                             aClass sourceFile notNil ifTrue:[
  1270                             aClass sourceFile notNil ifTrue: [
  1355                                 sourceFile := sourceFileName := dirName construct:aClass sourceFile.
  1271                                 sourceFile := sourceFileName := dirName construct: aClass sourceFile.
  1356                             ]
  1272                             ]
  1357                         ]
  1273                         ]
  1358                     ]
  1274                     ]
  1359                 ]
  1275                 ]
  1360             ]
  1276             ]
  1361         ].
  1277         ].
       
  1278         
  1362         "/ HACK HACK HACK: The eXpecco JImport plugin load the Java class and
  1279         "/ HACK HACK HACK: The eXpecco JImport plugin load the Java class and
  1363         "/ sets fake classloader - an instance of Expecco::JIClassLoaderPlaceholder
  1280         "/ sets fake classloader - an instance of Expecco::JIClassLoaderPlaceholder
  1364         "/ which in turn references codelibrary in an instvar. If that's the case,
  1281         "/ which in turn references codelibrary in an instvar. If that's the case,
  1365         "/ ask that library for source
  1282         "/ ask that library for source
  1366         loader class name == #'Expecco::JIClassLoaderPlaceholder' ifTrue:[
  1283         
  1367             ^ loader library classSourceOf: aClass name.
  1284         loader class name == #'Expecco::JIClassLoaderPlaceholder' ifTrue: [
       
  1285             ^ (loader library classSourceOf: aClass name) readStream.
  1368         ]
  1286         ]
  1369     ].
  1287     ].
  1370 
  1288     
  1371     "/ if that fails, look in standard places
  1289     "/ if that fails, look in standard places
  1372 
  1290     
  1373     (sourceFile isNil or:[sourceFile exists not]) ifTrue:[
  1291     (sourceFile isNil or: [ sourceFile exists not ]) ifTrue: [
  1374         sourceFile := aClass sourceFile.
  1292         sourceFile := aClass sourceFile.
  1375         sourceFile isNil ifTrue:[
  1293         sourceFile isNil ifTrue: [
  1376             "Hmm, hmm...just a guess"
  1294             "Hmm, hmm...just a guess"
  1377             sourceFile := (aClass lastName upTo: $$) , '.java'.
  1295             sourceFile := (aClass lastName upTo: $$) , '.java'.
  1378         ].
  1296         ].
  1379         jpackage := aClass javaPackageAsDirname.
  1297         jpackage := aClass javaPackageAsDirname.
  1380         self effectiveSourceDirectories do:[:dir|
  1298         self effectiveSourceDirectories do: [:dir | 
  1381             src := self classSource: sourceFile package: jpackage in: dir.
  1299             src := self 
  1382             src notNil ifTrue:[ SourceCache at: aClass put: src. ^ src]
  1300                          classSourceStreamForFile: sourceFile
       
  1301                          package: jpackage
       
  1302                          in: dir.
       
  1303             src notNil ifTrue: [
       
  1304                 ^ src
       
  1305             ]
  1383         ]
  1306         ]
  1384     ].
  1307     ].
  1385 
  1308     sourceFile isFilename ifFalse: [
  1386     sourceFile isFilename ifFalse:[^nil].
  1309         ^ nil
       
  1310     ].
  1387     src := sourceFile contentsOfEntireFile asString.
  1311     src := sourceFile contentsOfEntireFile asString.
  1388     SourceCache at: aClass put: src. 
       
  1389     ^ src
  1312     ^ src
  1390 
  1313 
  1391     "
  1314     "
  1392         Java classSourceOf: JAVA::java::lang::Object
  1315         Java classSourceOf: JAVA::java::lang::Object"
  1393     "
       
  1394 
  1316 
  1395     "Modified: / 27-01-1999 / 20:40:30 / cg"
  1317     "Modified: / 27-01-1999 / 20:40:30 / cg"
  1396     "Modified: / 08-09-2013 / 10:38:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1318     "Modified: / 19-09-2013 / 12:48:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
  1319 !
       
  1320 
       
  1321 classSourceStreamForFile: filename package: jpackage in: dirOrZipFile 
       
  1322     | fn  pn  zar  f |
       
  1323 
       
  1324     fn := dirOrZipFile asFilename.
       
  1325     pn := dirOrZipFile asString.
       
  1326     fn isDirectory ifTrue: [
       
  1327         jpackage notNil ifTrue: [
       
  1328             (f := fn / jpackage / filename) exists ifTrue: [
       
  1329                 ^ f readStream
       
  1330             ].
       
  1331         ].
       
  1332         (f := fn / filename) exists ifTrue: [
       
  1333             ^ f readStream
       
  1334         ].
       
  1335     ] ifFalse: [
       
  1336         ((pn last == $p and: [ fn hasSuffix: 'zip' ]) 
       
  1337             or: [ (pn last == $r and: [ fn hasSuffix: 'jar' ]) ]) 
       
  1338                 ifTrue: [
       
  1339                     zar := SourceArchiveCache at: dirOrZipFile
       
  1340                             ifAbsentPut: [ ZipArchive oldFileNamed: fn pathName ].
       
  1341                     zar notNil ifTrue: [
       
  1342                         jpackage notNil ifTrue: [
       
  1343                             OperatingSystem fileSeparator ~~ $/ ifTrue: [
       
  1344                                 f := (jpackage copyReplaceAll: OperatingSystem fileSeparator with: $/) 
       
  1345                                         , '/' , filename
       
  1346                             ] ifFalse: [
       
  1347                                 f := jpackage , '/' , filename.
       
  1348                             ]
       
  1349                         ] ifFalse: [
       
  1350                             f := filename
       
  1351                         ].
       
  1352                         (zar findMember: f) notNil ifTrue: [
       
  1353                             | "Kludge because of broken ZipArchive" cache |
       
  1354 
       
  1355                             cache := Java cacheDirectory / Release name / 'src'.
       
  1356                             cache exists ifFalse: [
       
  1357                                 cache recursiveMakeDirectory
       
  1358                             ].
       
  1359                             (cache / f) exists ifTrue: [
       
  1360                                 ^ (cache / f) readStream
       
  1361                             ].
       
  1362                             OperatingSystem 
       
  1363                                 executeCommand: ('unzip "%1" "%2"' bindWith: fn asAbsoluteFilename asString
       
  1364                                         with: f asString)
       
  1365                                 inDirectory: cache asString.
       
  1366                             (f := cache / f) exists ifTrue: [
       
  1367                                 ^ f readStream
       
  1368                             ]
       
  1369                         ].
       
  1370                         zar closeFile.
       
  1371                     ]
       
  1372                 ]
       
  1373     ].
       
  1374     ^ nil
       
  1375 
       
  1376     "
       
  1377  Java classSource: 'Object.java' package:'java/lang' in:'/home/jv/Projects/JavaX/java-6-openjdk/src'
       
  1378  Java classSource: 'Object.java' package:'java/lang' in:'/usr/lib/jvm/java-6-openjdk/src.zip'"
       
  1379 
       
  1380     "Modified: / 29-03-1998 / 21:46:40 / cg"
       
  1381     "Created: / 30-11-2010 / 12:32:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
  1382     "Modified: / 19-09-2013 / 12:36:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1397 !
  1383 !
  1398 
  1384 
  1399 sourceDirectories
  1385 sourceDirectories
  1400     ^self sourcePath
  1386     ^self sourcePath
  1401 "/
  1387 "/