Skip to content

Declaring Java Types

gershnik-smartsheet edited this page May 18, 2016 · 3 revisions

Raw JNI provides very little in the way of type safety. There are jobject, jstring and a few other predefined types but most of the time you are on your own. Pretty much everything is jobject and if you mix up OneJavaType with AnotherJavaType because of that - prepare to deal with runtime crash.

SmJNI rectifies this by requiring you to declare all Java types being used, either in JNI methods you implement or in Java methods/fields you access. Fortunately this is a very simple process. You declare a non-array (a.k.a.) Java type like this

DEFINE_JAVA_TYPE(jSomething,       "com.mystuff.Something");

The first argument is the name of the C++ type to create to represent the Java class. The second is the fully qualified name of the Java class. How to name the C++ type is up to you. In this guide we will follow jJavaSimpleName convention. The declaration above should be put in the global namespace and the C++ type is created in global namespace. In the future we might expose a way to declare it in a namespace of your choosing.

Nested classes

The fully qualified name of a nested Java class is simply com.mystuff.Something$Nested. That is use $ sign to separate nested types from their owners.

Arrays

Arrays are first class objects in Java so to use arrays you need to also declare a C++ representation. Don't forget to declare the element type first if you haven't done so!

DEFINE_JAVA_TYPE(jSomething,       "com.mystuff.Something");
DEFINE_ARRAY_JAVA_TYPE(jSomething); //Declares jSomethingArray 

The second line in the code above declares a jSomethingArray type. The Array suffix mimics the built-in JNI's jintArray etc. and currently cannot be changed.

Conversions

Clone this wiki locally