src/JavaVM.st
branchjk_new_structure
changeset 1202 7d9f24940ed5
parent 1201 326987885b9b
child 1204 6c2b887399e8
--- a/src/JavaVM.st	Thu Dec 01 17:47:44 2011 +0000
+++ b/src/JavaVM.st	Thu Dec 01 22:50:55 2011 +0000
@@ -2904,6 +2904,69 @@
     "Modified: / 21-10-2011 / 13:50:36 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 !
 
+invoke: method receiver: obj arguments: args
+
+    "Common helper for invoking methods and ctors. Performs
+     (type) checks and unboxing. Return (possibly boxed) return value."
+
+    | descriptor adescriptors bargs bargss retval retcls bi |
+
+    (obj isNil and:[method isStatic not]) ifTrue:[
+        self throwNullPointerException.
+        ^nil.            
+    ].
+
+    descriptor := method descriptor.
+
+    "Possibly box/unbox arguments"
+    args notEmptyOrNil ifTrue:[
+        adescriptors := descriptor parameters.
+        bargs := Array new: (bargss := method numArgs).
+        bi := 1.
+        1 to: args size do:[:i|
+            | cls |
+            bi > bargss ifTrue:[
+                 self throwExceptionClassName: 'java.lang.InvocationTargetException'
+                                  withMessage: 'passed more arguments than expected'
+            ].
+
+            cls := (adescriptors at: i) javaClass.
+            cls isJavaPrimitiveType ifTrue:[
+                bargs at: bi put: (cls javaUnbox: (args at:i) onError:[
+                    self throwIllegalArgumentException:'illegal unbox'. ^nil
+                ]).
+                (cls == LargeInteger or:[cls == Float]) ifTrue:[bi := bi + 1].
+            ] ifFalse:[
+                (self canCast: (args at:i) class to: cls) ifFalse:[
+                    self throwIllegalArgumentException:'incomplatible argument types'. ^nil.
+                ].
+                bargs at: bi put: (args at:i).
+            ].
+            bi := bi + 1.
+       ].
+        bi <= bargss ifTrue:[
+             self throwExceptionClassName: 'java.lang.InvocationTargetException'
+                              withMessage: 'not enough arguments'
+        ].
+    ] ifFalse:[
+        bargs := #()
+    ].
+
+    "Fire the method"
+    method isStatic ifTrue:[
+        retval := method valueWithReceiver: method javaClass arguments: bargs.
+    ] ifFalse:[
+        retval := obj perform: method selector withArguments: bargs.
+    ].
+    retcls := descriptor return.
+    retcls notNil ifTrue:[
+        retval := retcls javaClass javaBox: retval.
+    ].
+    ^retval.
+
+    "Created: / 01-12-2011 / 23:20:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 javaArrayClassFor:aClass 
     ^ self reflection javaArrayClassObjectForClass:aClass
         ifAbsentPut:[JavaArray javaArrayClassFor:aClass]
@@ -16056,43 +16119,14 @@
     <javanative: 'sun/reflect/NativeConstructorAccessorImpl' name: 'newInstance0'>
 
     
-    | ctor args descriptor adescriptors bargs method instance |
+    | ctor args method instance |
     ctor := aJavaContext argAt: 1.
     args := aJavaContext argAt: 2.
     method := self reflection methodForJavaConstructorObject: ctor.
 
-    method numArgs ~~ args size ifTrue:[
-        self throwExceptionClassName: 'java.lang.InvocationTargetException'
-                         withMessage: 'invalid number of parameters '
-    ].
-
-    descriptor := method descriptor.
-
-    "Possibly box/unbox arguments"
-    args notEmptyOrNil ifTrue:[
-        adescriptors := descriptor parameters.        
-        bargs := Array new: args size.
-        1 to: args size do:[:i|
-            | cls |
-
-            cls := (adescriptors at: i) javaClass.
-            cls isJavaPrimitiveType ifTrue:[
-                bargs at: i put: (cls
-                    javaUnbox: (args at:i)
-                    onError:[self throwIllegalArgumentException:'illegal unbox'. ^nil])                
-            ] ifFalse:[
-                (self canCast: (args at:i) class to: cls) ifFalse:[
-                    self throwIllegalArgumentException:'illegal unbox'. ^nil.
-                ]
-            ]
-        ]
-    ] ifFalse:[
-        bargs := #()
-    ].
-
     instance := method javaClass new.
     [
-        method valueWithReceiver:instance arguments:bargs.
+        self invoke: method receiver: instance arguments: args
     ] on: JavaUnhandledExceptionError do:[:ex|
         self throwExceptionClassName: 'java.lang.InvocationTargetException'
                          withMessage: 'exception when calling constructor: ', ex description
@@ -16101,7 +16135,7 @@
 
     "Created: / 26-11-2010 / 11:41:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 09-02-2011 / 01:12:10 / Marcel Hlopko <hlopik@gmail.com>"
-    "Modified: / 25-11-2011 / 19:21:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 01-12-2011 / 23:32:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 _sun_reflect_NativeMethodAccessorImpl_invoke0: nativeContext
@@ -16110,58 +16144,18 @@
     "
     private static native Object invoke0(Method m, Object obj, Object[] args);
     "
-    | m obj args method descriptor adescriptors bargs retval retcls |
+    | m obj args method |
     m := nativeContext argAt: 1.
     obj := nativeContext argAt: 2.
     args := nativeContext argAt: 3.
-
     method := self reflection methodForJavaMethodObject: m.
 
-
-    (obj isNil and:[method isStatic not]) ifTrue:[
-        self throwNullPointerException.
-        ^nil.            
-    ].
-
-    descriptor := method descriptor.
-
-    "Possibly box/unbox arguments"
-    args notEmptyOrNil ifTrue:[
-        adescriptors := descriptor parameters.
-        bargs := Array new: args size.
-        1 to: args size do:[:i|
-            | cls |
-
-            cls := (adescriptors at: i) javaClass.
-            cls isJavaPrimitiveType ifTrue:[
-                bargs at: i put: (cls
-                    javaUnbox: (args at:i)
-                    onError:[self throwIllegalArgumentException:'illegal unbox'. ^nil])                
-            ] ifFalse:[
-                (self canCast: (args at:i) class to: cls) ifFalse:[
-                    self throwIllegalArgumentException:'illegal unbox'. ^nil.
-                ]
-            ]
-       ].
-    ] ifFalse:[
-        bargs := #()
-    ].
-    "Fire the method"
-    method isStatic ifTrue:[
-        retval := method valueWithReceiver: method javaClass arguments: bargs.
-    ] ifFalse:[
-        retval := obj perform: method selector withArguments: bargs.
-    ].
-    retcls := descriptor return.
-    retcls notNil ifTrue:[
-        retval := retcls javaClass javaBox: retval.
-    ].
-    ^retval.
+    ^ self invoke: method receiver: obj arguments: args
 
     "Created: / 06-02-2011 / 00:00:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 28-02-2011 / 16:57:31 / Marcel Hlopko <hlopik@gmail.com>"
     "Modified: / 19-08-2011 / 15:06:00 / cg"
-    "Modified: / 22-11-2011 / 14:32:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 01-12-2011 / 23:33:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 _sun_reflect_Reflection_getCallerClass: aJavaContext