JavaObject.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 18 Aug 2011 20:42:48 +0200
changeset 2151 c0b6570c6f9b
parent 2107 f4509f6767fa
child 2201 8fc52ec820e5
permissions -rw-r--r--
Jan's version

"
 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:#JavaObject
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Classes'
!

!JavaObject 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.

"
! !

!JavaObject class methodsFor:'misc'!

resolveClassRefs
!

resolveClassRefsIgnoring:setOfClasses
! !

!JavaObject class methodsFor:'smalltalk interface'!

convertJavaObject:val signature:retValSignature
^ val.

    retValSignature = 'void' ifTrue:[
	^ #void
    ].
    retValSignature = 'boolean' ifTrue:[
	val == 0 ifTrue:[^ false].
	^ true
    ].
    retValSignature = 'int' ifTrue:[
	val isInteger ifFalse:[
	    self halt
	].
	^ val
    ].
    retValSignature = 'char[]' ifTrue:[
	"/ these are ST-strings
	^ val
    ].

    retValSignature = 'char' ifTrue:[
	"/ these are ST-characters
	val isInteger ifTrue:[
	    ^ Character value:val
	].
	self halt.
	^ val
    ].

    retValSignature = 'Object' ifTrue:[
	^ val
    ].

    retValSignature = 'String' ifTrue:[
	^ Java as_ST_String:val
    ].

    'no conversion for: ' print. val class name print. ' to: ' print. retValSignature printNL.
    ^ val.

    "Modified: 8.8.1997 / 12:07:23 / cg"
!

javaStringFrom:aString
    "hard-coding internas of java.lang.String here is bad ..."

    self halt.
    ^ Java as_String:aString

    "Modified: 7.8.1997 / 21:17:32 / cg"
!

stringFromJavaString:aJavaString
    "hard-coding internas of java.lang.String here is bad ..."

    self halt.
    ^ Java as_ST_String:aJavaString

    "Modified: 8.8.1997 / 12:07:29 / cg"
! !

!JavaObject methodsFor:'initialization'!

initializeToZero
    |sz|

    sz := self class instSize.
    1 to:sz do:[:i |
	self instVarAt:i put:0
    ]
! !

!JavaObject methodsFor:'message sending'!

doesNotUnderstand:aMessage
    "as a courtesy to the smalltalker, try to map methods"

    |args numArgs javaMethod sel retVal m|

    args := aMessage arguments.
    numArgs := args size.
    sel := aMessage selector.

    javaMethod := JavaClass lookupMethod:sel numArgs:numArgs in:self class static:false.
    javaMethod notNil ifTrue:[
        args notNil ifTrue:[
            args := JavaClass 
                        convertArgsToJava:args 
                        asSpecifiedIn:(javaMethod argSignature)
                        numArgs:numArgs.
        ].
        javaMethod isWrapped ifTrue:[
            m := javaMethod originalMethod
        ] ifFalse:[
            m := javaMethod
        ].
        retVal := javaMethod 
            valueWithReceiver:self 
            arguments:args
            selector:m selector 
            search:m javaClass
            sender:nil.

        ^ JavaClass convertToSmalltalk:retVal type:(m returnType).
    ].
    ^ super doesNotUnderstand:aMessage

    "Modified: / 16.11.1998 / 16:50:56 / cg"
! !

!JavaObject methodsFor:'printing & storing'!

displayString
    |myClass javaString|

    myClass := self class.
    myClass == Java java_lang_String ifTrue:[
        ^ '''' , (Java as_ST_String:self) , ''''
    ].
    myClass == (Java at:'java.lang.Class') ifTrue:[
        ^ super displayString , '(' 
            , (JavaVM reflection classForJavaClassObject:self) displayString , ')'
    ].
    myClass == (Java at:'java.lang.reflect.Method') ifTrue:[
        ^ super displayString , '(' 
            , (JavaVM methodForJavaMethodObject:self) displayString , ')'
    ].
    Object errorSignal 
        handle:[:ex | javaString := ''.]
        do:[
            javaString := Java 
                        as_ST_String:(self perform:#'toString()Ljava/lang/String;').
        ].
    ^ super displayString , ' (' , javaString , ')'.

    "Modified: / 04-11-1998 / 18:35:00 / cg"
    "Modified: / 28-01-2011 / 15:10:05 / Marcel Hlopko <hlopik@gmail.com>"
!

javaDisplayString
    Object errorSignal handle:[:ex |
        ex return.
    ] do:[
        ^ Java as_ST_String:(self perform:#toString).
    ].

    ^ super displayString.

    "Created: / 7.4.1997 / 17:38:13 / cg"
    "Modified: / 4.11.1998 / 21:07:19 / cg"
!

printString
    |myClass javaString|

    myClass := self class.
    myClass == Java java_lang_String ifTrue:[
        ^ '''' , (Java as_ST_String:self) , ''''
    ].
    myClass == (Java at:'java.lang.Class') ifTrue:[
        ^ super printString , '(' 
            , (JavaVM reflection classForJavaClassObject:self) displayString , ')'
    ].
    myClass == (Java at:'java.lang.reflect.Method') ifTrue:[
        ^ super printString , '(' 
            , (JavaVM methodForJavaMethodObject:self) displayString , ')'
    ].
    Object errorSignal 
        handle:[:ex | javaString := ''.]
        do:[
            javaString := Java 
                        as_ST_String:(self perform:#'toString()Ljava/lang/String;').
        ].
    ^ super printString , ' (' , javaString , ')'.

    "Modified: / 04-11-1998 / 18:35:00 / cg"
    "Created: / 20-12-2010 / 23:10:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-01-2011 / 15:10:17 / Marcel Hlopko <hlopik@gmail.com>"
! !

!JavaObject methodsFor:'queries'!

isJavaClassRef
    ^ false

    "Created: / 9.11.1999 / 17:13:37 / cg"
!

isJavaMethodRef
    ^ false

    "Created: / 9.11.1999 / 15:43:21 / cg"
!

isJavaObject
    ^ true

    "Created: 26.3.1997 / 13:34:17 / cg"
! !

!JavaObject methodsFor:'smalltalk interface'!

lookupMethod:selector numArgs:nargs
    "lookup a method"

    |method cls sel|

    sel := selector.
    (sel includes:$:) ifTrue:[
	sel := sel copyTo:(sel indexOf:$:)-1    
    ].

    sel := sel asSymbolIfInterned.
    sel notNil ifTrue:[
	cls := self class.
	[cls notNil and:[cls ~~ JavaObject]] whileTrue:[
	    cls methodDictionary keysAndValuesDo:[:jSel :aMethod |
		(jSel == sel ) ifTrue:[
		    ^ aMethod
		]
	    ].
	    cls methodDictionary keysAndValuesDo:[:jSel :aMethod |
		(aMethod name = sel 
		or:[aMethod signatureNameWithoutReturnType = sel]) ifTrue:[
		    aMethod numArgs == nargs ifTrue:[
			^ aMethod
		    ]
		]
	    ].
	    cls := cls superclass.
	].
    ].

    ^ nil

    "
     |stack|

     stack := (Java at:'java.util.Stack') basicNew.
     stack lookupMethod:#'<init>' numArgs:0. 
    "
    "
     |stack|

     stack := (Java at:'java.util.Stack') new.
     stack lookupMethod:#isEmpty numArgs:0. 
    "
    "
     |frame|

     frame := (Java at:'java.awt.Frame') new.
     frame lookupMethod:#'<init> (String)' numArgs:1. 
    "

    "Modified: 22.3.1997 / 00:56:54 / cg"
! !

!JavaObject class methodsFor:'documentation'!

version
    ^ '$Id: JavaObject.st,v 1.54 2011-08-18 18:42:48 vrany Exp $'
!

version_SVN
    ^ '$Id: JavaObject.st,v 1.54 2011-08-18 18:42:48 vrany Exp $'
! !