rename `NativeString` to `CString`
[nit.git] / lib / java / ffi_support.nit
index 5c8ce27..5fafd18 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Supporting services for the FFI with Java
+# Core supporting services for the FFI with Java
 #
-# This modules relies on `Sys::jvm`, `Sys::jni_env` and
-# `Sys::create_default_jvm` to get a handle on a JVM. You can adapt the
-# behavior of the FFI and services in this module by redefing
-# `Sys::create_default_jvm` and supply your own JVM object. You can manage
-# multiple java thread by switching the current environment in a redef
-# of `Sys::jni_env`, and multiple JVM using `Sys::jvm`.
+# This module *must* be imported by modules using the Java FFI.
+# Some might prefer to import the whole `java` package as it provides
+# other useful services.
 module ffi_support is
        cflags "-I $(JAVA_HOME)/include/ -I $(JAVA_HOME)/include/linux/"
        ldflags "-L $(JNI_LIB_PATH) -ljvm"
@@ -32,8 +29,9 @@ redef class Sys
        private var jvm_cache: nullable JavaVM = null
        private var jni_env_cache: nullable JniEnv = null
 
-       # Default Java Virtual Machine to use (will be instantiated using
-       # `create_default_jvm` if not already set)
+       # Default Java Virtual Machine to use
+       #
+       # Instantiated using `create_default_jvm` if not already set.
        fun jvm: JavaVM
        do
                if jvm_cache == null then create_default_jvm
@@ -72,7 +70,7 @@ redef class Sys
        end
 
        # Get a Java class by its name from the current `jni_env`
-       fun load_jclass(name: NativeString): JClass import jni_env `{
+       fun load_jclass(name: CString): JClass import jni_env `{
                JNIEnv *nit_ffi_jni_env = Sys_jni_env(self);
 
                // retrieve the implementation Java class
@@ -90,12 +88,12 @@ end
 # A standard Java string `java.lang.String`
 #
 # Converted to a Nit string using `to_s`, or to a C string with `to_cstring`.
-# Created using `String::to_java_string` or `NativeString::to_java_string`.
+# Created using `String::to_java_string` or `CString::to_java_string`.
 extern class JavaString in "Java" `{ java.lang.String `}
        super JavaObject
 
        # Get the string from Java and copy it to Nit memory
-       fun to_cstring: NativeString import sys, Sys.jni_env `{
+       fun to_cstring: CString import sys, Sys.jni_env `{
                Sys sys = JavaString_sys(self);
                JNIEnv *env = Sys_jni_env(sys);
 
@@ -113,10 +111,14 @@ extern class JavaString in "Java" `{ java.lang.String `}
                return nit_cstr;
        `}
 
-       redef fun to_s do return to_cstring.to_s
+       redef fun to_s
+       do
+               if is_java_null then return "<{inspect_head}:null>"
+               return to_cstring.to_s
+       end
 end
 
-redef class NativeString
+redef class CString
        # Get a Java string from this C string
        #
        # This instance is only valid until the next execution of Java code.
@@ -191,7 +193,7 @@ redef extern class JavaObject
        # Use Java's `toString` for any `JavaObject`
        redef fun to_s
        do
-               if is_java_null then return super
+               if is_java_null then return "<{inspect_head}:null>"
                return to_java_string.to_s
        end
 end