JavaObject.st
author cg
Thu, 02 May 1996 16:54:10 +0000
changeset 33 bef781ce106e
parent 29 eb3367f8fb9b
child 34 edb4800567d5
permissions -rw-r--r--
checkin from browser

Object subclass:#JavaObject
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Java-Classes'
!


!JavaObject class methodsFor:'misc'!

resolveClassRefs
! !

!JavaObject methodsFor:'initialization'!

initializeToZero
    1 to:self class instSize do:[:i |
        self instVarAt:i put:0
    ]
! !

!JavaObject methodsFor:'message sending'!

invokeJava:selector
    "send javaSelector (name+sig) message, without arguments"

    |method cls|

    cls := self class.
    [cls notNil] whileTrue:[
        method := cls compiledMethodAt:selector.
        method notNil ifTrue:[
            ^ self invokeJavaMethod:method
        ].
        cls := cls superclass.
    ].

    ^ self doesNotUnderstand:(Message selector:selector)

!

invokeJava:selector with:arg
    "send javaSelector (name+sig) message, with 1 argument"

    |method cls|

    cls := self class.
    [cls notNil] whileTrue:[
        method := cls compiledMethodAt:selector.
        method notNil ifTrue:[
            ^ self invokeJavaMethod:method with:arg
        ].
        cls := cls superclass.
    ].

    ^ self doesNotUnderstand:(Message selector:selector)
!

invokeJavaMethod:aJavaMethod
    "invoke java method, without arguments"

    |i val|

    aJavaMethod numArgs ~~ 0 ifTrue:[
        self halt:'need arguments'
    ].

    i := JavaInterpreter new.
    i push:self.

    val := i interpret:aJavaMethod sender:thisContext sender.

    ^ self convertJavaObject:val signature:(aJavaMethod retValSignature)
!

invokeJavaMethod:aJavaMethod with:arg
    "invoke java method, with one argument"

    |i val|

    aJavaMethod numArgs ~~ 1 ifTrue:[
        self halt:'argument count'
    ].


    i := JavaInterpreter new.
    i push:self.
    i push:arg.

    val := i interpret:aJavaMethod sender:thisContext sender.

    ^ self convertJavaObject:val signature:(aJavaMethod retValSignature)

! !

!JavaObject methodsFor:'printing & storing'!

displayString
    ^ 'a JavaObject(' , self class name , ')'

! !

!JavaObject methodsFor:'smalltalk interface'!

convertJavaObject:val signature:retValSignature
    retValSignature = 'void' ifTrue:[
        ^ #void
    ].
    retValSignature = 'boolean' ifTrue:[
        val == 0 ifTrue:[^ false].
        ^ true
    ].
    retValSignature = 'int' ifTrue:[
        val isInteger ifFalse:[
            self halt
        ].
        ^ val
    ].
    retValSignature = 'Object' ifTrue:[
        ^ val
    ].

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

invoke:selector
    "send a message, without args"

    |method cls|

    cls := self class.
    [cls notNil and:[cls ~~ JavaObject]] whileTrue:[
        cls methodArray do:[:aMethod |
"/ aMethod name printNL.
            aMethod name == selector ifTrue:[
                aMethod numArgs == 0 ifTrue:[
                    ^ self invokeJavaMethod:aMethod
                ]
            ]
        ].
        cls := cls superclass.
    ].

    ^ self doesNotUnderstand:(Message selector:selector)

    "
     |stack|

     stack := (Java at:'java/util/Stack') basicNew.
     stack invoke:#'<init>'. 
    "
    "
     |stack|

     stack := (Java at:'java/util/Stack') basicNew.
     stack invoke:#isEmpty. 
    "
    "
     |stack|

     stack := (Java at:'java/util/Stack') basicNew.
     stack invoke:#size. 
    "
!

invoke:selector with:argument
    "send a message, with 1 argument. This method needs more work."

    |method cls argClass jSel|

    "/
    "/ hard to do - must find a matching method probably
    "/
    (argument isKindOf:JavaObject) ifTrue:[
        argClass := argument class.
    ] ifFalse:[
        "/
        "/ map to Java:
        "/   String -> [c
        "/
        (argument isMemberOf:String) ifTrue:[
            jSel := (selector , '([C)V') asSymbolIfInterned.
            jSel notNil ifTrue:[
                ^ self invokeJava:jSel with:argument
            ]
        ]
    ].

    cls := self class.
    [cls notNil and:[cls ~~ JavaObject]] whileTrue:[
        cls methodArray do:[:aMethod |
            aMethod name == selector ifTrue:[
                aMethod numArgs == 1 ifTrue:[
                    "/
                    "/ this is not completely correct:
                    "/ must look for the best type-match,
                    "/ (especially: have to look for best match
                    "/  over whole superclass chain ...)
                    "/ for now take the first with matching number of args
                    "/
                    ^ self invokeJavaMethod:aMethod with:argument
                ]
            ]
        ].
        cls := cls superclass.
    ].

    ^ self doesNotUnderstand:(Message selector:selector)

    "
     |stack|

     stack := (Java at:'java/util/Stack') new.
     stack invoke:#push with:1. 
    "
! !

!JavaObject class methodsFor:'documentation'!

version
    ^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaObject.st,v 1.3 1996/05/02 16:54:10 cg Exp $'
! !