JavaDescriptor.st
changeset 749 e898eaeff091
child 2152 1cbdfbcc685c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JavaDescriptor.st	Fri Aug 19 08:58:19 2011 +0000
@@ -0,0 +1,336 @@
+"
+ 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:#JavaDescriptor
+	instanceVariableNames:''
+	classVariableNames:'BaseTypes BaseTypesByTypeName ArrayTypes'
+	poolDictionaries:''
+	category:'Languages-Java-Support'
+!
+
+!JavaDescriptor 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.
+
+"
+! !
+
+!JavaDescriptor class methodsFor:'instance creation'!
+
+fromString: aString
+
+    ^self readFromString: aString
+
+    "Created: / 25-11-2010 / 18:20:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+readFrom: aStream
+
+    ^ self
+        readFrom:aStream
+        onError:[ self conversionErrorSignal
+                    raiseErrorString:'invalid java descriptor']
+
+    "Created: / 25-11-2010 / 18:04:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+readFrom:stream onError:exceptionBlock
+
+    (stream peek == $( or:[stream peek == $<])
+        ifTrue: [^self readMethodDescriptorFrom: stream onError: exceptionBlock]
+        ifFalse:[^self readFieldDescriptorFrom:  stream onError: exceptionBlock].
+
+    "
+        JavaDescriptor fromString:'Ljava/lang/Object;'
+        JavaDescriptor fromString:'[I'
+        JavaDescriptor fromString:'[[I'
+        JavaDescriptor fromString:'(ILjava/lang/Object;)V'
+        JavaDescriptor fromString:'(ILjava/lang/Object;)Ljava/lang/Class;'    
+        JavaDescriptor fromString:'<T:Ljava/lang/Object;>(Ljava/util/Collection<TT;>;I)Ljava/util/Collection<TT;>;'
+
+    "
+
+    "Created: / 25-11-2010 / 17:50:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 13-08-2011 / 01:37:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaDescriptor class methodsFor:'accessing'!
+
+baseTypes
+
+    ^BaseTypes
+
+    "Created: / 22-05-2011 / 17:45:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+baseTypesByTypeName
+
+    ^BaseTypesByTypeName
+
+    "Created: / 03-02-2011 / 21:08:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaDescriptor class methodsFor:'class initialization'!
+
+initialize
+
+    "
+    Tools::NewSystemBrowser basicNew spawnClassBrowserFor: BaseTypes values label:'Java primitive types' in:#newBrowser       
+    "
+
+    "/WARNING: If you change something here, you MUST also
+    "/         change JavaField class>>#initialize !!!!!!
+    BaseTypes := IdentityDictionary new
+        at: $B  put: Byte;
+        at: $C  put: Character;
+        at: $D  put: Float;
+        at: $F  put: ShortFloat;
+        at: $I  put: Integer;       "Kludge: instances of java int are in fact SmallIntegers"
+        at: $J  put: LargeInteger;
+        at: $S  put: Short;
+        at: $Z  put: Boolean;
+        at: $V  put: UndefinedObject;
+        yourself.
+
+    BaseTypesByTypeName := Dictionary new
+        at: 'byte'      put: Byte;
+        at: 'char'      put: Character;
+        at: 'double'    put: Float;
+        at: 'float'     put: ShortFloat;
+        at: 'int'       put: Integer;       "Kludge: instances of java int are in fact SmallIntegers"
+        at: 'long'      put: LargeInteger;
+        at: 'short'     put: Short;
+        at: 'boolean'   put: Boolean;
+        at: 'void'      put: UndefinedObject;
+        yourself.
+
+    "
+    Tools::NewSystemBrowser basicNew spawnClassBrowserFor: ArrayTypes values label:'Java primitive array types' in:#newBrowser       
+    "
+
+    "/WARNING: If you change something here, you MUST also
+    "/         change JavaField class>>#initialize !!!!!!
+
+    ArrayTypes := IdentityDictionary new
+        at: $B  put: ByteArray;
+        at: $C  put: Unicode16String;
+        at: $D  put: DoubleArray;
+        at: $F  put: FloatArray;
+        at: $I  put: SignedIntegerArray;    
+        at: $J  put: SignedLongIntegerArray;
+        at: $S  put: WordArray;
+        at: $Z  put: BooleanArray;
+        yourself
+
+    "Created: / 25-11-2010 / 17:51:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 10-08-2011 / 00:48:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaDescriptor class methodsFor:'reading-private'!
+
+readArrayTypeFrom:  stream onError: exceptionBlock
+
+    | p c |
+    stream next. "eat $["
+    p := stream peek.
+    (#(
+         $B "byte"
+         $C "char"
+         $D "double"
+         $F "float"
+         $I "int"
+         $J "long"
+         $S "short"
+         $Z "boolean"
+    ) includes: p) ifTrue:[^JavaFieldDescriptor javaClass: (ArrayTypes at: stream next)].
+
+    "OK, not a primitive array..."
+    c := self readFrom: stream onError: exceptionBlock.
+    ^JavaFieldDescriptor javaClass: (JavaVM javaArrayClassFor: c javaClass)
+
+    "Created: / 25-11-2010 / 18:12:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 06-02-2011 / 23:15:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+readBaseTypeFrom:  stream onError: exceptionBlock
+
+    ^JavaFieldDescriptor javaClass: (BaseTypes at: (stream next) ifAbsent: exceptionBlock)
+
+    "Created: / 25-11-2010 / 18:11:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+readFieldDescriptorFrom:  stream onError: exceptionBlock
+
+    | p |
+    p := stream peek.
+
+    p == $L ifTrue:[^self readObjectTypeFrom: stream onError: exceptionBlock].
+    p == $[ ifTrue:[^self readArrayTypeFrom: stream onError: exceptionBlock].
+    (#(
+         $B "byte"
+         $C "char"
+         $D "double"
+         $F "float"
+         $I "int"
+         $J "long"
+         $S "short"
+         $Z "boolean"
+         $V "void"
+    ) includes: p) ifTrue:[^self readBaseTypeFrom: stream onError: exceptionBlock].
+
+    exceptionBlock value
+
+    "Created: / 25-11-2010 / 17:55:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 21-12-2010 / 22:47:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+readMethodDescriptorFrom: stream onError: exceptionBlock
+
+    | parameterDescriptors returnDescriptor |
+
+    stream peek == $< ifTrue:[
+        self readTypeVariableFrom: stream onError: exceptionBlock
+    ].
+    stream next. "eat $("
+    parameterDescriptors := OrderedCollection new.
+    [ stream peek ~= $) ] whileTrue:[
+        parameterDescriptors add:( self readFieldDescriptorFrom: stream onError: exceptionBlock).
+    ].
+    stream next. "eat $)"
+    stream peek == $V ifFalse:[
+        returnDescriptor := self readFieldDescriptorFrom: stream onError: exceptionBlock.
+    ] ifTrue:[
+        stream next. "eat $V"
+    ].
+    ^JavaMethodDescriptor parameters: parameterDescriptors return: returnDescriptor
+
+    "
+        JavaDescriptor fromString:'(ILjava/lang/Object;)V'
+        JavaDescriptor fromString:'(ILjava/lang/Object;)Ljava/lang/Class;'  
+    "
+
+    "Created: / 25-11-2010 / 18:36:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 13-08-2011 / 01:39:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+readObjectTypeFrom:  stream onError: exceptionBlock
+
+    | clsName out |
+
+    stream next. "/eat $L.
+    out := String new writeStream.
+    [ stream peek ~~ $; and:[stream peek ~~ $< ]] whileTrue:[out nextPut: stream next].
+    clsName := out contents.
+    stream peek == $< ifTrue:[
+        self readTypeVariableFrom: stream onError: exceptionBlock
+    ].
+
+    "Just a check"
+    stream next ~= $; ifTrue:[exceptionBlock value].
+    ^JavaFieldDescriptor javaClassName: clsName.
+
+    "Created: / 25-11-2010 / 18:08:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+readTypeVariableFrom:  stream onError: exceptionBlock
+
+    | nangles |
+
+    stream next. "/eat $<.
+    nangles := 1.
+    [ nangles ~~ 0 ] whileTrue:[
+        stream peek == $< ifTrue:[nangles := nangles + 1].
+        stream peek == $> ifTrue:[nangles := nangles - 1].
+        stream next.
+    ]
+
+    "Created: / 13-08-2011 / 01:39:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaDescriptor class methodsFor:'documentation'!
+
+version_SVN
+    ^ '$Id$'
+! !
+
+JavaDescriptor initialize!
\ No newline at end of file