src/JavaClassReader.st
branchjk_new_structure
changeset 877 f5a5b93e1c78
parent 863 135fa7c03356
child 879 8bb82d1ea058
--- a/src/JavaClassReader.st	Tue Jul 26 16:06:42 2011 +0000
+++ b/src/JavaClassReader.st	Tue Jul 26 16:31:02 2011 +0000
@@ -1,6 +1,10 @@
 "
- COPYRIGHT (c) 1997 by eXept Software AG
-              All Rights Reserved
+ 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
@@ -8,6 +12,32 @@
  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' }"
 
@@ -16,7 +46,7 @@
 		constSlot classBeingLoaded'
 	classVariableNames:'Verbose AnnotationsVerbose Silent AbsolutelySilent
 		LazyClassLoading InvalidClassFormatSignal ClassLoaderQuerySignal
-		JavaArchiveCache UsedJavaClassReaderClass'
+		JavaArchiveCache'
 	poolDictionaries:''
 	category:'Languages-Java-Support'
 !
@@ -25,8 +55,12 @@
 
 copyright
 "
- COPYRIGHT (c) 1997 by eXept Software AG
-              All Rights Reserved
+ 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
@@ -34,9 +68,34 @@
  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.
+
 "
-
-
 ! !
 
 !JavaClassReader class methodsFor:'initialization'!
@@ -45,7 +104,6 @@
     InvalidClassFormatSignal := Signal new mayProceed: true.
     InvalidClassFormatSignal notifierString: 'class load failure'.
     InvalidClassFormatSignal nameClass: self message: #invalidClassFormatSignal.
-    UsedJavaClassReaderClass := JavaClassReader2.
     Verbose := false.
     Silent := true.
     AbsolutelySilent := false.
@@ -62,8 +120,8 @@
 
     "Modified: / 27-01-1998 / 17:54:23 / cg"
     "Modified: / 17-12-2010 / 17:37:45 / Marcel Hlopko <hlopik@gmail.com>"
-    "Modified: / 10-02-2011 / 23:16:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 10-05-2011 / 23:59:53 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 26-07-2011 / 17:21:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaClassReader class methodsFor:'Signal constants'!
@@ -453,9 +511,8 @@
     
     | rslt |
 
-    rslt := "UsedJavaClassReaderClass"JavaClassReader2 loadClassLazy: aClassName
-                ignoring: (Set new).
-    rslt notNil ifTrue: [ "UsedJavaClassReaderClass"JavaClassReader2 postLoadActions ].
+    rslt := JavaClassReader loadClassLazy: aClassName ignoring: (Set new).
+    rslt notNil ifTrue: [ JavaClassReader postLoadActions ].
     self assert: rslt constantPool owner == rslt.
     rslt ifNil: [self breakPoint:#mh].
     ^ rslt
@@ -471,7 +528,7 @@
 
     "Created: / 20-10-1998 / 17:24:40 / cg"
     "Modified: / 13-05-2011 / 17:50:55 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-    "Modified: / 22-05-2011 / 13:44:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 26-07-2011 / 17:21:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 postLoadActions
@@ -612,7 +669,7 @@
     
     | javaClass |
 
-    javaClass := "UsedJavaClassReaderClass"JavaClassReader2 new readStream: aStream ignoring: classesBeingLoaded.
+    javaClass := JavaClassReader new readStream: aStream ignoring: classesBeingLoaded.
     AbsolutelySilent 
         ifFalse: 
             [ '  ... loaded ' print.
@@ -621,6 +678,7 @@
 
     "Modified: / 30-03-1998 / 18:14:40 / cg"
     "Modified: / 09-05-2011 / 23:15:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 26-07-2011 / 17:21:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 readStream:aStream loader:aClassLoader
@@ -652,7 +710,7 @@
             [ javaClass := self readStream: aStream ignoring: (Set new).
             javaClass notNil 
                 ifTrue: 
-                    [ "UsedJavaClassReaderClass"JavaClassReader2 postLoadActions: loadUnresolved.
+                    [ JavaClassReader postLoadActions: loadUnresolved.
                     Java at: (javaClass fullName asSymbol) put: javaClass.
                     JavaUnresolvedConstant resolveFor: javaClass. ]. ].
     ^ javaClass
@@ -660,6 +718,7 @@
     "Created: / 15-08-1997 / 00:59:24 / cg"
     "Modified: / 23-01-1998 / 17:14:09 / cg"
     "Modified: / 09-05-2011 / 23:15:23 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 26-07-2011 / 17:21:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 resolveClass:aJavaClass
@@ -725,12 +784,6 @@
 
 !JavaClassReader class methodsFor:'mode setting'!
 
-useNewClassReader
-    UsedJavaClassReaderClass := JavaClassReader2.
-
-    "Created: / 09-05-2011 / 23:11:12 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-!
-
 useOldClassReader
     self error: 'Old class reader should not be used anymore!!'.
 UsedJavaClassReaderClass := JavaClassReader.
@@ -1588,56 +1641,47 @@
 !
 
 readConstantPool
-    |constantPoolCount const i|
+    | constantPoolSize  const |
 
     "/
     "/ get constant pool
     "/
-    constantPoolCount := inStream nextUnsignedShortMSB:msb.
-    Verbose ifTrue:[Transcript show:'constantPoolCount = '; showCR:constantPoolCount].
-
-    constants := JavaConstantPool "Array" new:constantPoolCount-1.
-
+    constantPoolSize := inStream nextUnsignedShortMSB: msb.
+    self info: 'constantPoolSize = ' , constantPoolSize printString.
+    constants := JavaConstantPool new: constantPoolSize - 1.
     constSlot := 1.
-    [constSlot < constantPoolCount] whileTrue:[
-        Verbose ifTrue:[Transcript show:'const: '; showCR:constSlot].
-        const := self readConstant.
-        constants at:constSlot put:const.
-
-        "/ long & double consts take 2 slots
-        "/ (only first is used)
-        constNeeds2Slots ifTrue:[
-            constSlot := constSlot + 2.
-        ] ifFalse:[
-            constSlot := constSlot + 1.
-        ]
-    ].
-
+    [ constSlot < constantPoolSize ] whileTrue: [
+            self info: 'const slot: ' , constSlot printString.
+            const := self readConstant.
+            constants at: constSlot put: const.
+            
+            "/ long & double consts take 2 slots
+            "/ (only first is used)
+            
+            constNeeds2Slots 
+                ifTrue: [ constSlot := constSlot + 2. ]
+                ifFalse: [ constSlot := constSlot + 1. ]].
     constSlot := -1.
-
-    "/ preresolve what can be (especially, strings are resolved here)
-
-    1 to:constantPoolCount-1 do:[:i |
-        |const value|
-
-        const := constants at:i.
-        const notNil ifTrue:[   "/ kludge for 2-slot constants (which only take 1 slot in ST/X)
-            (const isKindOf:JavaUnresolvedConstant) ifTrue:[
-                value := const preResolve.
-                value ~~ const ifTrue:[
-                    constants at:i put:value.
-                ]
-            ]
-        ]
-    ].
+    
+    1 to: constantPoolSize - 1
+        do: 
+            [:i | 
+            | const  value |
+
+            const := constants at: i.
+            const ifNotNil: 
+                    [ "/ kludge for 2-slot constants (which only take 1 slot in ST/X)
+                    (const isKindOf: JavaUnresolvedConstant) 
+                        ifTrue: 
+                            [ value := const preResolve.
+                            value ~~ const ifTrue: [ constants at: i put: value. ] ] ] ].
 
     "
      JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
-     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/Alignable.class'
-    "
-
-    "Created: / 15.4.1996 / 15:14:11 / cg"
-    "Modified: / 7.5.1998 / 11:44:06 / cg"
+     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/Alignable.class'"
+
+    "Modified: / 07-05-1998 / 11:44:06 / cg"
+    "Created: / 13-05-2011 / 16:52:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
 readConstant_Asciz
@@ -1667,29 +1711,17 @@
 !
 
 readConstant_Class
-    |name_index name|
-
-    name_index := inStream nextUnsignedShortMSB:msb.
-
-    Verbose ifTrue:[Transcript show:'class; index= '; showCR:name_index].
-
-    name := constants at:name_index.
-    name notNil ifTrue:[
-        Verbose ifTrue:[Transcript showCR:'name in constant_class already resolved'].
-        "/ self halt
-    ].
-
-    ^ JavaUnresolvedClassConstant 
-        pool:constants
-        poolIndex:constSlot
-        nameIndex:name_index
-
-    "
-     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
-    "
-
-    "Created: / 15.4.1996 / 15:21:13 / cg"
-    "Modified: / 8.5.1998 / 21:53:16 / cg"
+    | nameIndex |
+
+    nameIndex := inStream nextUnsignedShortMSB: msb.
+    ((constants at: nameIndex) isNil) 
+        ifTrue: 
+            [ self info: ('reading class; index=' , nameIndex printString , ' name=' 
+                        , (constants at: nameIndex) printString) ]
+        ifFalse: [ self info: ('reading class; index= ' , nameIndex printString) ].
+    ^ JavaClassRef2 in: constants withNameAt: nameIndex.
+
+    "Modified: / 18-05-2011 / 18:21:16 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
 readConstant_Double
@@ -1744,27 +1776,22 @@
     "Modified: / 8.5.1998 / 22:06:07 / cg"
 !
 
-readConstant_Fieldref
-    |class_index name_and_type_index|
-
-    class_index := inStream nextUnsignedShortMSB:msb.
-    name_and_type_index := inStream nextUnsignedShortMSB:msb.
-
-    Verbose ifTrue:[Transcript show:'fieldref; classindex= ';     showCR:class_index].
-    Verbose ifTrue:[Transcript show:'fieldref; name&typeindex= '; showCR:name_and_type_index].
-
-    ^ JavaUnresolvedFieldrefConstant
-		pool:constants
-		poolIndex:constSlot
-		classIndex:class_index
-		nameandTypeIndex:name_and_type_index
-
-    "
-     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
-    "
-
-    "Created: 15.4.1996 / 15:22:18 / cg"
-    "Modified: 15.4.1996 / 16:07:01 / cg"
+readConstant_Fieldref    
+    "incrementally changing resolving logic, fields will come later"
+
+    | classIndex  nameAndTypeIndex |
+
+    classIndex := inStream nextUnsignedShortMSB: msb.
+    nameAndTypeIndex := inStream nextUnsignedShortMSB: msb.
+    self 
+        info: 'reading fieldref; classindex=' , classIndex printString 
+                , ' nameAndTypeIndex=' , nameAndTypeIndex printString.
+    ^ JavaFieldRef2 
+        in: constants
+        withNameAndTypeAt: nameAndTypeIndex
+        andClassAt: classIndex.
+
+    "Modified: / 15-05-2011 / 14:20:23 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
 readConstant_Float
@@ -1832,26 +1859,19 @@
 !
 
 readConstant_InterfaceMethodref
-    |class_index name_and_type_index|
-
-    class_index := inStream nextUnsignedShortMSB:msb.
-    name_and_type_index := inStream nextUnsignedShortMSB:msb.
-
-    Verbose ifTrue:[Transcript show:'methodref; classindex= ';     showCR:class_index].
-    Verbose ifTrue:[Transcript show:'methodref; name&typeindex= '; showCR:name_and_type_index].
-
-    ^ JavaUnresolvedInterfaceMethodrefConstant 
-		pool:constants
-		poolIndex:constSlot
-		classIndex:class_index
-		nameandTypeIndex:name_and_type_index
-
-    "
-     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
-    "
-
-    "Created: 15.4.1996 / 15:22:37 / cg"
-    "Modified: 15.4.1996 / 16:07:19 / cg"
+    | classIndex  nameAndTypeIndex  |
+
+    classIndex := inStream nextUnsignedShortMSB: msb.
+    nameAndTypeIndex := inStream nextUnsignedShortMSB: msb.    
+    self 
+        info: 'reading interface methodref; classindex=' , classIndex printString                 
+                , ' nameAndTypeIndex=' , nameAndTypeIndex printString.                
+    ^ JavaInterfaceMethodRef2 
+        in: constants
+        withNameAndTypeAt: nameAndTypeIndex
+        andClassAt: classIndex.
+
+    "Modified: / 12-05-2011 / 18:57:47 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
 readConstant_Long
@@ -1879,49 +1899,35 @@
 !
 
 readConstant_Methodref
-    |class_index name_and_type_index|
-
-    class_index := inStream nextUnsignedShortMSB:msb.
-    name_and_type_index := inStream nextUnsignedShortMSB:msb.
-
-    Verbose ifTrue:[Transcript show:'methodref; classindex= ';     showCR:class_index].
-    Verbose ifTrue:[Transcript show:'methodref; name&typeindex= '; showCR:name_and_type_index].
-
-    ^ JavaUnresolvedMethodrefConstant 
-		pool:constants
-		poolIndex:constSlot
-		classIndex:class_index
-		nameandTypeIndex:name_and_type_index
-
-    "
-     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
-    "
-
-    "Created: 15.4.1996 / 15:22:37 / cg"
-    "Modified: 15.4.1996 / 16:07:19 / cg"
+    | classIndex  nameAndTypeIndex   |
+
+    classIndex := inStream nextUnsignedShortMSB: msb.
+    nameAndTypeIndex := inStream nextUnsignedShortMSB: msb.   
+    self 
+        info: 'reading methodref; classindex=' , classIndex printString , ' nameAndTypeIndex=' 
+                , nameAndTypeIndex printString.
+    ^ JavaMethodRef2 
+        in: constants
+        withNameAndTypeAt: nameAndTypeIndex
+        andClassAt: classIndex.
+
+    "Modified: / 15-05-2011 / 14:20:38 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
 readConstant_NameAndType
-    |name_index signature_index|
-
-    name_index := inStream nextUnsignedShortMSB:msb.
-    signature_index := inStream nextUnsignedShortMSB:msb.
-
-    Verbose ifTrue:[Transcript show:'methodref; nameindex= ';     showCR:name_index].
-    Verbose ifTrue:[Transcript show:'methodref; signatureindex= '; showCR:signature_index].
-
-    ^ JavaUnresolvedNameandTypeConstant 
-		pool:constants
-		poolIndex:constSlot
-		nameIndex:name_index
-		signatureIndex:signature_index  
-
-    "
-     JavaClassReader readFile:'/phys/ibm3/hotjava/classes/browser/AddButton.class'
-    "
-
-    "Created: 15.4.1996 / 15:23:43 / cg"
-    "Modified: 15.4.1996 / 16:17:16 / cg"
+    | nameIndex  descriptorIndex |
+
+    nameIndex := inStream nextUnsignedShortMSB: msb.
+    descriptorIndex := inStream nextUnsignedShortMSB: msb.
+    self 
+        info: 'reading nameAndType; nameindex=' , nameIndex printString 
+                , ' descriptorIndex= ' , descriptorIndex printString.
+    ^JavaNameAndType2 
+                in: constants
+                withNameAt: nameIndex
+                andDescriptorAt: descriptorIndex.
+
+    "Modified: / 10-05-2011 / 17:09:47 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
 readConstant_String
@@ -2594,37 +2600,25 @@
 !JavaClassReader methodsFor:'helpers'!
 
 loadSuperclassIdentifiedBy: something ignoring: classesBeingLoaded 
-    | loader  result  jSuperClass  superClassName |
-
-
-     "there are two possible types which can go in here - JavaUnresolvedClassConstant and JavaClass. Not to mention overriden behavior in subclasses. Be careful"
-    something ifNil: [ self halt: 'Cmon how could I find nil superclass?' ].
-    something isJavaClass 
-        ifTrue: 
-            [ "nothing to be done here, it's already resolved (and thus its strange somebody called this method"
-            ^ something ].
-    superClassName := something fullName.
-    (something isMemberOf: JavaUnresolvedClassConstant) 
-        ifTrue: 
-            [ self info: 'load superClass: ' , superClassName printString.
-            loader := ClassLoaderQuerySignal query.
-            loader isNil 
-                ifTrue: 
-                    [ result := self class loadClassLazy: superClassName
-                                ignoring: classesBeingLoaded. ]
-                ifFalse: 
-                    [ jSuperClass := loader 
-                                perform: #'loadClass(Ljava/lang/String;)Ljava/lang/Class;'
-                                with: (Java as_String: superClassName).
-                    result := JavaVM reflection classForJavaClassObject: jSuperClass. ].
-            result isNil 
-                ifTrue: 
-                    [
-                    self halt:('cannot find superclass: ' , superClassName). ]. ]
-        ifFalse: [ self halt: 'oops - superclass ?' ].
-    ^ result.
-
-    "Created: / 18-05-2011 / 14:48:37 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "overriding method, here we expect something to be new JavaClassRef2"
+
+    self assert: something isJavaRef
+        message: 'class identifier is not a java reference'.
+
+    self assert: something isJavaClassRef
+        message: 'class identifier is not a java class reference'.
+
+    (classesBeingLoaded includes: something fullName) ifTrue: 
+        [self error: 'class we want to load is being loaded and that means something went wrong. tell mh'].
+
+    "Do NOT use >>something resolve<< here, since something
+     has no owner set and therefore access check will cause DNU.
+     Ask JV for details."
+
+    ^Java classForName: something javaClassName.
+
+    "Created: / 18-05-2011 / 14:55:32 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 22-05-2011 / 13:58:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 updateOwnerInCPItem: each 
@@ -2653,7 +2647,7 @@
 !JavaClassReader class methodsFor:'documentation'!
 
 version
-    ^ '$Id$'
+    ^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaClassReader.st,v 1.92 2009/10/09 14:21:53 cg Exp $'
 !
 
 version_CVS