Merge: SharedPreferences: Nit API wrapping android SharedPreferences class
[nit.git] / src / common_ffi / java.nit
index 6172e47..f6e44f7 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);
        }
@@ -131,7 +131,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);
        }
@@ -168,7 +168,7 @@ class JavaLanguage
                mmodule.insert_compiler_options
 
                # Enable linking C callbacks to java native methods
-               mmodule.ensure_linking_callback_methods(ffi_ccu, mmodule.ffi_callbacks[self])
+               mmodule.ensure_linking_callback_methods(ffi_ccu.as(not null), mmodule.ffi_callbacks[self])
 
                # Java implementation code
                var java_file = mmodule.java_file
@@ -177,7 +177,7 @@ class JavaLanguage
                mmodule.ffi_files.add(extern_java_file)
        end
 
-       var ffi_ccu: CCompilationUnit # HACK
+       var ffi_ccu: nullable CCompilationUnit = null # HACK
 
        redef fun compile_callback(callback, mmodule, mainmodule, ccu)
        do
@@ -225,7 +225,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 +290,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 +351,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
 
@@ -462,7 +464,8 @@ redef class MClassType
        redef fun java_type
        do
                var ftype = mclass.ftype
-               if ftype isa ForeignJavaType then return ftype.java_type
+               if ftype isa ForeignJavaType then return ftype.java_type.
+                       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"
@@ -484,7 +487,7 @@ 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 return "L{ftype.java_type.replace('.', "/").replace(' ', "").replace('\n', "")};"
                if mclass.name == "Bool" then return "Z"
                if mclass.name == "Char" then return "C"
                if mclass.name == "Int" then return "I"