java: cast arguments passed to JNI
[nit.git] / src / common_ffi / java.nit
index a03f67d..a43cab6 100644 (file)
@@ -66,7 +66,7 @@ class JavaLanguage
        // retrieve the implementation Java class
        java_class = Sys_load_jclass(sys, "{{{mmodule.impl_java_class_name}}}");
        if (java_class == NULL) {
-               fprintf(stderr, "Nit FFI with Java error: failed to load class.\\n");
+               PRINT_ERROR("Nit FFI with Java error: failed to load class.\\n");
                (*nit_ffi_jni_env)->ExceptionDescribe(nit_ffi_jni_env);
                exit(1);
        }
@@ -82,7 +82,7 @@ class JavaLanguage
        // retreive the implementation static function
        java_meth_id = (*nit_ffi_jni_env)->GetStaticMethodID(nit_ffi_jni_env, java_class, "{{{java_fun_name}}}", "{{{jni_format}}}");
        if (java_meth_id == NULL) {
-               fprintf(stderr, "Nit FFI with Java error: Java implementation not found.\\n");
+               PRINT_ERROR("Nit FFI with Java error: Java implementation not found.\\n");
                (*nit_ffi_jni_env)->ExceptionDescribe(nit_ffi_jni_env);
                exit(1);
        }
@@ -119,7 +119,11 @@ class JavaLanguage
                        end
                end
 
-               for p in signature.mparameters do params.add(p.name)
+               for p in signature.mparameters do
+                       var param_mtype = p.mtype
+                       param_mtype = param_mtype.resolve_for(mclass_type, mclass_type, mmodule, true)
+                       params.add(to_java_call_context.cast_to(param_mtype, p.name))
+               end
 
                var cname = "(*nit_ffi_jni_env)->CallStatic{jni_signature_alt}Method"
                var ccall
@@ -131,7 +135,7 @@ class JavaLanguage
        // execute implementation code
        {{{ccall}}}
        if ((*nit_ffi_jni_env)->ExceptionCheck(nit_ffi_jni_env)) {
-               fprintf(stderr, "Nit FFI with Java error: Exception after call.\\n");
+               PRINT_ERROR("Nit FFI with Java error: Exception after call.\\n");
                (*nit_ffi_jni_env)->ExceptionDescribe(nit_ffi_jni_env);
                exit(1);
        }
@@ -225,7 +229,7 @@ redef class MModule
        };
        jint res = (*env)->RegisterNatives(env, jclazz, methods, n_methods);
        if (res != JNI_OK) {
-               fprintf(stderr, "RegisterNatives failed\\n");
+               PRINT_ERROR("RegisterNatives failed\\n");
                (*env)->ExceptionDescribe(env);
                exit(1);
        }
@@ -290,8 +294,10 @@ redef class AExternPropdef
                var sys_class = modelbuilder.try_get_mclass_by_name(self, mmodule, "Sys")
                assert sys_class != null
                var sys_jni_env_meth = modelbuilder.try_get_mproperty_by_name2(self, mmodule, sys_class.mclass_type, "jni_env")
-               assert sys_jni_env_meth != null
-               assert sys_jni_env_meth isa MMethod
+               if sys_jni_env_meth == null or not sys_jni_env_meth isa MMethod then
+                       toolcontext.error(self.location, "Java FFI error: you must import the `java` module when using the FFI with Java")
+                       return
+               end
 
                explicit_call = new MExplicitCall(sys_class.mclass_type, sys_jni_env_meth, mmodule)
                fcc.callbacks.add(explicit_call)
@@ -349,7 +355,7 @@ class JavaFile
        super ExternFile
 
        redef fun makefile_rule_name do return "{filename.basename(".java")}.class"
-       redef fun makefile_rule_content do return "javac {filename} -d ."
+       redef fun makefile_rule_content do return "javac {filename.basename("")} -d ."
        redef fun add_to_jar do return true
 end
 
@@ -463,10 +469,10 @@ redef class MClassType
        do
                var ftype = mclass.ftype
                if ftype isa ForeignJavaType then return ftype.java_type.
-                       replace('/', ".").replace('$', ".").replace(' ', "")
+                       replace('/', ".").replace('$', ".").replace(' ', "").replace('\n',"")
                if mclass.name == "Bool" then return "boolean"
                if mclass.name == "Char" then return "char"
-               if mclass.name == "Int" then return "int"
+               if mclass.name == "Int" then return "long"
                if mclass.name == "Float" then return "double"
                return super
        end
@@ -477,7 +483,7 @@ redef class MClassType
                if ftype isa ForeignJavaType then return "jobject"
                if mclass.name == "Bool" then return "jboolean"
                if mclass.name == "Char" then return "jchar"
-               if mclass.name == "Int" then return "jint"
+               if mclass.name == "Int" then return "jlong"
                if mclass.name == "Float" then return "jdouble"
                return super
        end
@@ -485,10 +491,30 @@ redef class MClassType
        redef fun jni_format
        do
                var ftype = mclass.ftype
-               if ftype isa ForeignJavaType then return "L{ftype.java_type.replace('.', "/").replace(' ', "")};"
+               if ftype isa ForeignJavaType then
+                       var ori_jni_type = jni_type
+                       var jni_type = ftype.java_type.
+                               replace('.', "/").replace(' ', "").replace('\n', "")
+
+                       # Remove parameters of generic types
+                       loop
+                               var i = jni_type.last_index_of('<')
+                               if i >= 0 then
+                                       var j = jni_type.index_of_from('>', i)
+                                       if j == -1 then
+                                               print "Error: missing closing '>' in extern Java type of \"{mclass.name}\""
+                                               exit 1
+                                       end
+                                       jni_type = jni_type.substring(0, i) +
+                                               jni_type.substring(j+1, jni_type.length)
+                               else break
+                       end
+
+                       return "L{jni_type};"
+               end
                if mclass.name == "Bool" then return "Z"
                if mclass.name == "Char" then return "C"
-               if mclass.name == "Int" then return "I"
+               if mclass.name == "Int" then return "J"
                if mclass.name == "Float" then return "D"
                return super
        end
@@ -499,7 +525,7 @@ redef class MClassType
                if ftype isa ForeignJavaType then return "Object"
                if mclass.name == "Bool" then return "Boolean"
                if mclass.name == "Char" then return "Char"
-               if mclass.name == "Int" then return "Int"
+               if mclass.name == "Int" then return "Long"
                if mclass.name == "Float" then return "Double"
                return super
        end