Contents Previous Next

Chapter   5

KNI Functions


This chapter specifies the KNI functions. For each KNI function, we provide the following information:

Note that since KNI is an implementation-level interface, the interface places some important restrictions and expectations on the programmer.

We use the adverb must to describe restrictions and conventions that the KNI programmer is expected to adhere to. For instance, when some KNI function must receive a non-null object as a parameter, it is the programmer’s responsibility not to pass a null value to that function. Programmer-level restrictions like this allow the KNI implementation to be more efficient, as the KNI implementation does not check for null pointers or other similar conditions. In general, KNI is a “shoot yourself in the foot” interface that performs only a minimum number of checks at runtime to ensure the validity of the parameters.

5.1 Version Information

5.1.1 KNI_GetVersion

TABLE 6  –  KNI_GetVersion
Function
Description
Prototype
jint KNI_GetVersion();
Description
Returns the version number of the KNI interface.
Parameters
None.
Return Values
Returns the version number of the KNI interface. Hexadecimal number “0x00010000” identifies KNI version 1.0.
Exceptions
None.

5.2 Class and Interface Operations

5.2.1 KNI_FindClass

TABLE 7  –  KNI_FindClass
Function
Description
Prototype
void KNI_FindClass(const char* name, jclass classHandle);
Description
Initializes a handle with a reference to the named class or interface. The function assumes that the class has already been loaded to the system and properly initialized. If this is not the case, or if no class is found, the handle will be initialized to NULL.
 
The name argument is a class descriptor (See Section 4.3.2 “Class Descriptors”). For example, the descriptor for the java.lang.String class is:
 
java/lang/String
 
The descriptor of the array class java.lang.object[] is:
 
[Ljava/lang/Object;”.
Parameters
name: the descriptor of the class or interface to be returned. The name is represented as a UTF-8 string.
classHandle: a handle in which the class pointer will be returned.
Return Values
Returns nothing directly, but returns the class pointer in the ‘classHandle’ handle. If the class cannot be found, the returned handle will be set to NULL.
Exceptions
None.

5.2.2 KNI_GetSuperClass

TABLE 8  –  KNI_GetSuperClass
Function
Description
Prototype
void KNI_GetSuperClass(jclass classHandle, jclass superclassHandle);
Description
Initializes the superclassHandle handle to contain a pointer to the superclass of the given class (represented by classHandle). If classHandle represents the class java.lang.Object, then the function sets the superclassHandle handle to NULL.
Parameters
classHandle: a handle initialized with a reference to the class object whose superclass is to be determined.
superclassHandle: a handle which, upon return from this function, will contain a reference to the superclass object.
Return Values
Returns nothing directly, but parameter superclassHandle is used as a result value.
Exceptions
None.

5.2.3 KNI_IsAssignableFrom

TABLE 9  –  KNI_IsAssignableFrom
Function
Description
Prototype
jboolean KNI_IsAssignableFrom(jclass classHandle1, jclass classHandle2);
Description
Determines whether an object of class or interface classHandle1 can be safely cast to a class or interface classHandle2.
Parameters
classHandle1: handle initialized with a reference to the first class or interface argument
classHandle2: handle initialized with a reference to the second class or interface argument
Return Values
Returns KNI_TRUE if any of the following is true:
 
The first and second argument refer to the same class or interface
The first argument refers to a subclass of the second argument
The first argument refers to a class that has the second argument as one of its interfaces
The first and second arguments both refer to array of classes with element types X and Y, and KNI_IsAssignableFrom(X, Y) is KNI_TRUE; otherwise it returns KNI_FALSE
Exceptions
None.

5.3 Exceptions

5.3.1 KNI_ThrowNew

TABLE 10  –  KNI_ThrowNew
Function
Description
Prototype
jint KNI_ThrowNew(const char* name, const char* message);
Description
Instantiates an exception object with the message specified by message, and causes that exception to be thrown. A thrown exception will be pending in the current thread, but does not immediately disrupt native code execution. The actual exception will be thrown when the program control returns from the native function back to the virtual machine. Note that KNI_ThrowNew does not run the constructor on the new exception object.
Parameters
name: the name of a java.lang.Throwable class as a UTF-8 string.
message: the message used to construct the java.lang.Throwable object; represented as a UTF-8 string.
Return Values
KNI_OK on success; otherwise KNI_ERR.
Exceptions
None.


Note – Since KNI does not allow Java code to be run from native methods, KNI_ThrowNew does not run the constructor of the exception object when it creates a new exception object. If you use KNI_ThrowNew to throw exceptions that have other fields than those defined in class java.lang.Throwable, the additional fields will not be initialized.

5.3.2 KNI_FatalError

TABLE 11  –  KNI_FatalError
Function
Description
Prototype
void KNI_FatalError(const char* message);
Description
Prints an error message to the system’s standard error output, and terminates the execution of the virtual machine immediately.
Parameters
message: the error message as a UTF-8 string.
Return Values
This function does not return.
Exceptions
None.

5.4 Object Operations

5.4.1 KNI_GetObjectClass

TABLE 12  –  KNI_GetObjectClass
Function
Description
Prototype
void KNI_GetObjectClass(jobject objectHandle, jclass classHandle);
Description
Sets the handle classHandle to point to the class of the object represented by objectHandle.
Parameters
objectHandle: a handle pointing to an object whose class is being sought.
classHandle: a handle which, upon return of this function, will contain a reference to the class of this object.
Return Values
Returns nothing directly, but parameter classHandle is used as a return value.
Exceptions
None.

5.4.2 KNI_IsInstanceOf

TABLE 13  –  KNI_IsInstanceOf
Function
Description
Prototype
jboolean KNI_IsInstanceOf(jobject objectHandle, jclass classHandle);
Description
Tests whether an object represented by objectHandle is an instance of a class or interface represented by classHandle.
Parameters
objectHandle: a handle pointing to an object.
clazz: a handle pointing to a class or interface.
Return Values
Returns KNI_TRUE if the given object is an instance of given class, KNI_FALSE otherwise.
Exceptions
None.

5.5 Instance Field Access

5.5.1 KNI_GetFieldID

TABLE 14  –  KNI_GetFieldID
Function
Description
Prototype
jfieldID KNI_GetFieldID(jclass classHandle, const char* name, const char* signature);
Description
Returns the field ID of an instance field of the class represented by classHandle. The field is specified by its name and signature. The Get<Type>Field and Set<Type>Field families of accessor functions use field IDs to retrieve the value of instance fields. The field must be accessible from the class referred to by classHandle.
Parameters
classHandle: a handle pointing to a class whose field ID will be retrieved.
name: the field name as a UTF-8 string.
signature: the field descriptor as a UTF-8 string.
Return Values
Returns a field ID or NULL if the lookup fails for any reason.
Exceptions
None.

5.5.2 KNI_Get<Type>Field

TABLE 15  –  KNI_Get<Type>Field
Function
Description
Prototype
<NativeType> KNI_Get<Type>Field(jobject objectHandle, jfieldID fieldID);
Description
Returns the value of an instance field of a primitive type. The field to access is denoted by fieldID.
Parameters
objectHandle: a handle pointing to an object whose field is to be accessed.
fieldID: the field ID of the given instance field.
Return Values
Returns the value of a primitive instance field.
Exceptions
None.

This family of functions consists of eight members that are listed in the table below. Note that those functions that manipulate jfloat or jdouble data types are assumed to be available only if the underlying virtual machine or J2ME configuration supports floating point data types.

TABLE 16  –  KNI_Get<Type>Field Functions
KNI_Get<Type>Field
<NativeType>
KNI_GetBooleanField
jboolean
KNI_GetByteField
jbyte
KNI_GetCharField
jchar
KNI_GetShortField
jshort
KNI_GetIntField
jint
KNI_GetLongField
jlong
KNI_GetFloatField
jfloat
KNI_GetDoubleField
jdouble

5.5.3 KNI_Set<Type>Field

TABLE 17  –  KNI_Set<Type>Field
Function
Description
Prototype
void KNI_Set<Type>Field(jobject objectHandle, jfieldID fieldID, <NativeType> value);
Description
Sets the value of an instance field of a primitive type. The field to modify is denoted by fieldID.
Parameters
objectHandle: a handle pointing to an object whose field is to be modified.
fieldID: the field ID of the given instance field.
value: the value to set to the instance field.
Return Values
void.
Exceptions
None.

This family of functions consists of nine members that are listed in the table below. Note that those functions that manipulate jfloat or jdouble data types are assumed to be available only if the underlying virtual machine or J2ME configuration supports floating point data types.

TABLE 18  –  KNI_Set<Type>Field Functions
KNI_Set<Type>Field
<NativeType>
KNI_SetBooleanField
jboolean
KNI_SetByteField
jbyte
KNI_SetCharField
jchar
KNI_SetShortField
jshort
KNI_SetIntField
jint
KNI_SetLongField
jlong
KNI_SetFloatField
jfloat
KNI_SetDoubleField
jdouble

5.5.4 KNI_GetObjectField

TABLE 19  –  KNI_GetObjectField
Function
Description
Prototype
void KNI_GetObjectField(jobject objectHandle, jfieldID fieldID, jobject toHandle);
Description
Returns the value of a field of a reference type, and assigns it to the handle toHandle. The field to access is denoted by fieldID.
Parameters
objectHandle: a handle pointing to an object whose whose field is to be accessed.
fieldID: the field ID of the given instance field.
toHandle: a handle to which the return value will be assigned.
Return Values
Returns nothing directly, but handle toHandle will contain the return value.
Exceptions
None.

5.5.5 KNI_SetObjectField

TABLE 20  –  KNI_SetObjectField
Function
Description
Prototype
void KNI_SetObjectField(jobject objectHandle, jfieldID fieldID, jobject fromHandle);
Description
Sets the value of an instance field of a reference type. The field to modify is denoted by fieldID.
Parameters
objectHandle: a handle pointing to an object whose field is to be modified.
fieldID: the field ID of the given instance field.
fromHandle: a handle pointing to an object that will be assigned to the field.
Return Values
void.
Exceptions
None.

5.6 Static Field Access

5.6.1 KNI_GetStaticFieldID

TABLE 21  –  KNI_GetStaticField
Function
Description
Prototype
jfieldID KNI_GetStaticFieldID(jclass classHandle, const char* name, const char* signature);
Description
Returns the field ID of a static field of the class represented by classHandle. The field is specified by its name and signature. The GetStatic<Type>Field and SetStatic<Type>Field families of accessor functions use field IDs to retrieve the value of static fields. The field must be accessible from the class referred to by classHandle.
Parameters
classHandle: a handle pointing to a class whose field ID will be retrieved.
name: the field name as a UTF-8 string
signature: the field descriptor as a UTF-8 string.
Return Values
Returns a field ID or NULL if the lookup fails for any reason.
Exceptions
Lookup of field IDs in interfaces is not supported.

5.6.2 KNI_GetStatic<Type>Field

TABLE 22  –  KNI_GetStatic<Type>Field
Function
Description
Prototype
<NativeType> KNI_GetStatic<Type>Field(jclass classHandle, jfield fieldID);
Description
Returns the value of a static field of a primitive type. The field to access is denoted by fieldID.
Parameters
classHandle: a handle pointing to a class or interface whose static field is to be accessed.
fieldID: a field ID of the given class.
Return Values
Returns the value of a static field of a primitive type.
Exceptions
None.

This family of functions consists of eight members that are listed in the table below. Note that those functions that manipulate jfloat or jdouble data types are assumed to be available only if the underlying virtual machine or J2ME configuration supports floating point data types:

TABLE 23  –  KNI_GetStatic<Type>Field Functions
KNI_GetStatic<Type>Field
<NativeType>
KNI_GetStaticBooleanField
jboolean
KNI_GetStaticByteField
jbyte
KNI_GetStaticCharField
jchar
KNI_GetStaticShortField
jshort
KNI_GetStaticIntField
jint
KNI_GetStaticLongField
jlong
KNI_GetStaticFloatField
jfloat
KNI_GetStaticDoubleField
jdouble

5.6.3 KNI_SetStatic<Type>Field

TABLE 24  –  KNI_SetStatic<Type>Field
Function
Description
Prototype
void KNI_SetStatic<Type>Field(jclass classHandle, jfieldID fieldID, <NativeType> value);
Description
Sets the value of a static field of a primitive type. The field to modify is denoted by fieldID.
Parameters
classHandle: a handle pointing to the class or interface whose static field is to be modified.
fieldID: a field ID of the given class.
value: the value to set to the static field.
Return Values
void.
Exceptions
None.

This family of functions consists of eight members that are listed in the table below. Note that those functions that manipulate jfloat or jdouble data types are assumed to be available only if the underlying virtual machine or J2ME configuration supports floating point data types:

TABLE 25  –  KNI_SetStatic<Type>Field Functions
KNI_SetStatic<Type>Field
<NativeType>
KNI_SetStaticBooleanField
jboolean
KNI_SetStaticByteField
jbyte
KNI_SetStaticCharField
jchar
KNI_SetStaticShortField
jshort
KNI_SetStaticIntField
jint
KNI_SetStaticLongField
jlong
KNI_SetStaticFloatField
jfloat
KNI_SetStaticDoubleField
jdouble

5.6.4 KNI_GetStaticObjectField

TABLE 26  –  KNI_GetStaticObjectField
Function
Description
Prototype
void KNI_GetStaticObjectField(jclass classHandle, jfield fieldID, jobject toHandle);
Description
Returns the value of a static field of a reference type, and assigns it to the handle toHandle. The field to access is denoted by fieldID.
Parameters
classHandle: a handle pointing to a class or interface whose static field is to be accessed.
fieldID: a field ID of the given class.
toHandle: a handle to which the return value will be assigned.
Return Values
Nothing directly, but handle toHandle will contain the return value.
Exceptions
None.

5.6.5 KNI_SetStaticObjectField

TABLE 27  –  KNI_SetStaticObjectField
Function
Description
Prototype
void KNI_SetStaticObjectField(jclass classHandle, jfield fieldID, jobject fromHandle);
Description
Sets the value of a static field of a reference type. The field to modify is denoted by fieldID.
Parameters
classHandle: a handle pointing to the class or interface whose whose static field is to be modified.
fieldID: a field ID of the given class.
fromHandle: a handle pointing to an object that will assigned to the field.
Return Values
void.
Exceptions
None.

5.7 String Operations

5.7.1 KNI_GetStringLength

TABLE 28  –  KNI_GetStringLength
Function
Description
Prototype
jsize KNI_GetStringLength(jstring stringHandle);
Description
Returns the number of Unicode characters in a java.lang.String object. If the given string handle is NULL, the function returns -1.
Parameters
stringHandle: a handle pointing to a java.lang.String string object whose length is to be determined.
Return Values
Returns the length of the string, or -1 if the given string handle is NULL.
Exceptions
None.

5.7.2 KNI_GetStringRegion

TABLE 29  –  KNI_GetStringRegion
Function
Description
Prototype
void KNI_GetStringRegion(jstring stringHandle, jsize offset, jsize n, jchar* jcharbuf);
Description
Copies a number of Unicode characters from a java.lang.String object (denoted by stringHandle), beginning at offset, to the given buffer jcharbuf. No range checking is performed. It is the responsibility of the native function programmer to allocate the return buffer, and to make sure that it is large enough.
Parameters
stringHandle: a handle pointing to a java.lang.String object whose contents are to be copied.
offset: the offset within the string object at which to start copying (offset 0: first character in the Java string.)
n: the number of 16-bit Unicode characters to copy.
jcharbuf: a pointer to a buffer to hold the Unicode characters.
Return Values
Data is returned in user-allocated buffer jcharbuf. Each character in the returned buffer is 16 bits wide.
Exceptions
None.

5.7.3 KNI_NewString

TABLE 30  –  KNI_NewString
Function
Description
Prototype
void KNI_NewString(const jchar* uchars, jsize length, jstring stringHandle);
Description
Creates a java.lang.String object from the given Unicode sequence. The resulting object is returned to the caller via the stringHandle handle.
Parameters
uchars: a Unicode sequence that will make up the contents of the new string object.
stringHandle: a handle to hold the reference to the new java.lang.String object..
Return Values
Returns nothing directly, but handle stringHandle will contain a reference to the new string object.
Exceptions
OutOfMemoryError if the virtual machine runs out of memory.

5.7.4 KNI_NewStringUTF

TABLE 31  –  KNI_NewStringUTF
Function
Description
Prototype
void KNI_NewStringUTF(const char* utf8chars, jstring stringHandle);
Description
Creates a java.lang.String object from the given UTF-8 string. The resulting object is returned to the caller via the stringHandle handle.
Parameters
utf8chars: a UTF-8 string that will make up the contents of the new string object.
lenght: length of the Unicode string.
stringHandle: a handle to hold the reference to the new java.lang.String object..
Return Values
Returns nothing directly, but handle stringHandle will contain a reference to the new string object.
Exceptions
OutOfMemoryError if the virtual machine runs out of memory.

5.8 Array Operations

5.8.1 KNI_GetArrayLength

TABLE 32  –  KNI_GetArrayLength
Function
Description
Prototype
jsize KNI_GetArrayLength(jarray arrayHandle);
Description
Returns the number of elements in a given Java array object. The array argument may denote an array of any element type, including primitive types or reference types. If the given array handle is NULL, the function returns -1.
Parameters
arrayHandle: a handle pointing to an array object whose length is to be determined.
Return Values
Returns the number of elements in the array, or -1 if the given array handle is NULL.
Exceptions
None.

5.8.2 KNI_Get<Type>ArrayElement

TABLE 33  –  KNI_Get<Type>ArrayElement
Function
Description
Prototype
<NativeType> KNI_Get<Type>ArrayElement(<ArrayType> arrayHandle, jint index);
Description
Returns an element of an array of <Type> type. The given array reference must not be NULL. No array type checking or index range checking is performed.
Parameters
arrayHandle: a handle pointing to an array object.
index: the index of the element in the array. Index 0 denotes the first element in the array.
Return Values
Returns the element at position index of an array of <Type> type.
Exceptions
None.

This family of functions consists of eight members that are listed in the table below. Note that those functions that manipulate jfloat or jdouble data types are assumed to be available only if the underlying virtual machine or J2ME configuration supports floating point data types.

TABLE 34  –  KNI_Get<Type>ArrayElement Functions
KNI_Get<Type>ArrayELement
<ArrayType>
<NativeType>
KNI_GetBooleanArrayElement
jbooleanArray
jboolean
KNI_GetByteArrayElement
jbyteArray
jbyte
KNI_GetCharArrayElement
jcharArray
jchar
KNI_GetShortArrayElement
jshortArray
jshort
KNI_GetIntArrayElement
jintArray
jint
KNI_GetLongArrayElement
jlongArray
jlong
KNI_GetFloatArrayElement
jfloatArray
jfloat
KNI_GetDoubleArrayElement
jdoubleArray
jdouble

5.8.3 KNI_Set<Type>ArrayElement

TABLE 35  –  KNI_Set<Type>ArrayElement
Function
Description
Prototype
void KNI_Set<Type>ArrayElement(<ArrayType> arrayHandle, jint index, <NativeType> value);
Description
Sets an element of an array of <Type> type. The given array handle must not be NULL. No array type checking or index range checking is performed.
Parameters
arrayHandle: a handle pointing to an array object.
index: the index of the element in the array. Index 0 denotes the first element in the array.
value: the value that will be stored in the array.
Return Values
void.
Exceptions
None.

This family of functions consists of eight members that are listed in the table below. Note that those functions that manipulate jfloat or jdouble data types are assumed to be available only if the underlying virtual machine or J2ME configuration supports floating point data types.

TABLE 36  –  KNI_Set<Type>ArrayElement Functions
KNI_Set<Type>ArrayElement
<ArrayType>
<NativeType>
KNI_SetBooleanArrayElement
jbooleanArray
jboolean
KNI_SetByteArrayElement
jbyteArray
jbyte
KNI_SetCharArrayElement
jcharArray
jchar
KNI_SetShortArrayElement
jshortArray
jshort
KNI_SetIntArrayElement
jintArray
jint
KNI_SetLongArrayElement
jlongArray
jlong
KNI_SetFloatArrayElement
jfloatArray
jfloat
KNI_SetDoubleArrayElement
jdoubleArray
jdouble

5.8.4 KNI_GetObjectArrayElement

TABLE 37  –  KNI_GetObjectArrayElement
Function
Description
Prototype
void KNI_GetObjectArrayElement(jobjectArray arrayHandle, jint index, jobject toHandle);
Description
Returns an element of an array of a reference type. The given array handle must not be NULL. No array type checking or index range checking is performed.
Parameters
arrayHandle: a handle pointing to an array object.
index: the index of the element in the array. Index 0 denotes the first element in the array.
toHandle: a handle to which the return value will be assigned.
Return Values
Returns nothing directly, but handle toHandle will contain the return value.
Exceptions
None.

5.8.5 KNI_SetObjectArrayElement

TABLE 38  –  KNI_SetObjectArrayElement
Function
Description
Prototype
void KNI_SetObjectArrayElement(jobjectArray arrayHandle, jint index, jobject fromHandle);
Description
Sets an element of an array of a reference type. The given array handle must not be NULL. No array type checking or index range checking is performed.
Parameters
arrayHandle: a handle pointing to an array object.
index: the index of the element in the array. Index 0 denotes the first element in the array.
fromHandle: a handle from which value will be read.
Return Values
void.
Exceptions
None.

5.8.6 KNI_GetRawArrayRegion

TABLE 39  –  KNI_GetRawArrayRegion
Function
Description
Prototype
void KNI_GetRawArrayRegion(jarray arrayHandle, jsize offset, jsize n, jbyte* dstBuffer);
Description
Gets a region of n bytes of an array of a primitive type. The given array handle must not be NULL. No array type checking or range checking is performed.
Parameters
arrayHandle: a handle initialized with the array reference.
offset: a byte offset within the array.
n: the number of bytes of raw data to get.
dstBuffer: the destination of the data as bytes.
Return Values
void.
Exceptions
None.

5.8.7 KNI_SetRawArrayRegion

TABLE 40  –  KNI_SetRawArrayRegion
Function
Description
Prototype
void KNI_SetRawArrayRegion(jarray arrayHandle, jsize offset, jsize n, const jbyte* srcBuffer);
Description
Sets a region of n bytes of an array of a primitive type. The given array handle must not be NULL. No array type checking or range checking is performed.
Parameters
arrayHandle: a handle initialized with the array reference.
offset: a byte offset within the array.
n: the number of bytes of raw data to change.
srcBuffer: the source of the data as bytes.
Return Values
void.
Exceptions
None.

5.9 Parameter (Operand Stack) Access

KNI introduces a new set of functions for reading the parameters (local variables) that a Java method has passed on to a native method, as well as a set of functions for returning values back from native methods to Java methods. These functions are typically used in conjunction of the handle operations introduced in Section 5.10 “Handle Operations.”

5.9.1 KNI_GetParameterAs<Type>

TABLE 41  –  KNI_GetParameterAs<Type>
Function
Description
Prototype
<ReturnType> KNI_GetParameterAs<Type>(jint index);
Description
Returns the value of a parameter of a primitive type <Type> at physical location specified by index. Parameter indices are mapped from left to right. Index value 1 refers to the leftmost parameter that has been passed on to the native method. Remember that parameters of type long or double take up two entries on the operand stack. No type checking or index range checking is performed.
Parameters
index: the index of a parameter of a primitive type to the native method. Index value 1 refers to the leftmost parameter in the Java method.
Return Values
Returns the argument of a primitive type at position index in the list of arguments to the native method.
Exceptions
None.


Note – It is important to note that parameters of type long or double take up two entries on the operand stack. For example, when calling the following function

native void foo(int a, long b, int c);

the index of parameter ‘a’ would be 1, index of parameter ‘b’ would be 2, and index of parameter ‘c’ would 4.

This family of functions consists of eight members that are listed in the table below. Note that those functions that manipulate jfloat or jdouble data types are assumed to be available only if the underlying virtual machine or J2ME configuration supports floating point data types.

TABLE 42  –  Support for accessing a native method actual parameters
KNI_GetParameterAs<Type>
<Type>
<ReturnType>
KNI_GetParameterAsBoolean
Boolean
jboolean
KNI_GetParameterAsByte
Byte
jbyte
KNI_GetParameterAsChar
Char
jchar
KNI_GetParameterAsShort
Short
jshort
KNI_GetParameterAsInt
Int
jint
KNI_GetParameterAsLong
Long
jlong
KNI_GetParameterAsFloat
Float
jfloat
KNI_GetParameterAsDouble
Double
jdouble

5.9.2 KNI_GetParameterAsObject

TABLE 43  –  KNI_GetParameterAsObject
Function
Description
Prototype
void KNI_GetParameterAsObject(jint index, jobject toHandle);
Description
Reads the value (object reference) of the parameter specified by index, and stores it in the handle toHandle. Parameter indices are mapped from left to right. Index value 1 refers to the leftmost parameter that has been passed on to the native method. No type checking or index range checking is performed.
Parameters
index: the index of a parameter of a reference type to the native method. Index value 1 refers to the leftmost parameter in the Java method.
toHandle: a handle to to which the return value will be assigned.
Return Values
Returns nothing directly, but handle toHandle will contain the return value.
Exceptions
None.

5.9.3 KNI_GetThisPointer

TABLE 44  –  KNI_GetThisPointer
Function
Description
Prototype
void KNI_GetThisPointer(jobject toHandle);
Description
Reads the value of the ‘this’ pointer in the current stack frame of an instance (non-static) native method, and stores the value in the handle toHandle.
Parameters
toHandle: a handle to contain the ‘this’ pointer.
Return Values
Returns nothing directly, but handle toHandle will contain the return value. The return value is unspecified if this method is called inside a static native method.
Exceptions
None.


Note – The function KNI_GetThisPointer should only be used only inside instance (non-static) native methods. The return value of this function is unspecified if the function is called from a static native method.

5.9.4 KNI_GetClassPointer

TABLE 45  –  KNI_GetClassPointer
Function
Description
Prototype
void KNI_GetClassPointer(jclass toHandle);
Description
Reads the value of the class pointer in the current stack frame of a static native method, and stores the value in the handle toHandle.
Parameters
toHandle: a handle to contain the class pointer.
Return Values
Returns nothing directly, but handle toHandle will contain the return value.
Exceptions
None.

5.9.5 KNI_ReturnVoid

TABLE 46  –  KNI_ReturnVoid
Function
Description
Prototype
void KNI_ReturnVoid();
Description
Returns a void value from a native function. Calling this function terminates the execution of the native function immediately.
Parameters
None.
Return Values
None
Exceptions
None.


Note – IMPORTANT: The KNI_ReturnVoid function MUST BE CALLED at the end of a native function when a native function function does not want to return a value back to the calling Java function. Otherwise the operand stack of the Java virtual machine may become corrupted.

5.9.6 KNI_Return<Type>

TABLE 47  –  KNI_Return<Type>
Function
Description
Prototype
void KNI_Return<Type>(<NativeType> value);
Description
Returns a primitive value from a native function. Calling any of the KNI_Return<Type> functions will terminate the execution of the native function immediately.
Parameters
value: the result value of the native method.
Return Values
Returns a primitive value back to the function that called the native function containing the KNI_Return<Type> call.
Exceptions
None.


Note – The semantics of the KNI_Return<Type> functions are similar to the standard C/C++ return statement. The execution of the function is terminated immediately, and the control is returned back to the caller.

This family of functions consists of eight members that are listed in the table below. Note that those functions that manipulate jfloat or jdouble data types are assumed to be available only if the underlying virtual machine or J2ME configuration supports floating point data types.

TABLE 48  –  Support for setting a native result value
KNI_Return<Type>
<Type>
<NativeType>
KNI_ReturnBoolean
Boolean
jboolean
KNI_ReturnByte
Byte
jbyte
KNI_ReturnChar
Char
jchar
KNI_ReturnShort
Short
jshort
KNI_ReturnInt
Int
jint
KNI_ReturnLong
Long
jlong
KNI_ReturnFloat
Float
jfloat
KNI_ReturnDouble
Double
jdouble

5.10 Handle Operations

To avoid garbage collection problems , all the object references used by KNI are handles to objects rather than direct object references. Many KNI functions (such as KNI_GetParameterAsObject and KNI_GetObjectField) use object handles as input or output parameters. It is the programmer’s responsibility to allocate, declare and unallocate the handles that are needed inside a native function.

Seven handle functions are provided, as listed below.


Note – It is important to notice that the implementation of KNI handle operations may vary significantly from one virtual machine to another. The native function programmer should not assume anything about the internal structure or behavior of the handles. In order to write portable code, the programmer must only use the public KNI handle operations defined below.

5.10.1 KNI_StartHandles

TABLE 49  –  KNI_StartHandles
Function
Description
Prototype
void KNI_StartHandles(n)
Description
Allocates enough space for n handles for the current native method.
Parameters
n: a positive integer specifying the number of handles to allocate.
Return Values
Nothing directly, but allocates space for a number of handles in an implementation-dependent fashion.
Exceptions
None.


Note – Every call to KNI_StartHandles must be matched with a corresponding KNI_EndHandles or KNI_EndHandlesAndReturnObject call. Nested KNI_StartHandles and KNI_EndHandles / KNI_EndHandlesAndReturnObject calls are not allowed inside the same C/C++ code block ({ ... }).

5.10.2 KNI_DeclareHandle

TABLE 50  –  KNI_DeclareHandle
Function
Description
Prototype
void KNI_DeclareHandle(handle)
Description
Declares a handle that can be used as an input, output, or in/out parameter to an appropriate KNI function. The initial value of the handle is unspecified. If the programmer has not allocated enough space for the handle in the preceding call to KNI_StartHandles, the behavior of this function is unspecified.
Parameters
handle: the name by which the handle will be identified between the calls to KNI_StartHandles and KNI_EndHandles / KNI_EndHandlesAndReturnObject.
Return Values
Nothing directly, but declares a handle in an implementation-dependent fashion.
Exceptions
None.

5.10.3 KNI_IsNullHandle

TABLE 51  –  KNI_IsNullHandle
Function
Description
Prototype
jboolean KNI_IsNullHandle(handle)
Description
Checks if the given handle is NULL.
Parameters
handle: the handle whose contents will be examined.
Return Values
KNI_TRUE if the handle is NULL, KNI_FALSE otherwise.
Exceptions
None.

5.10.4 KNI_IsSameObject

TABLE 52  –  KNI_IsSameObject
Function
Description
Prototype
jboolean KNI_IsSameObject(handle1, handle2)
Description
Checks if the given two handles refer to the same object. No parameter type checking is performed.
Parameters
handle1: a handle pointing to an object.
handle2: a handle pointing to an object.
Return Values
KNI_TRUE if the handles refer to the same object, KNI_FALSE otherwise.
Exceptions
None.

5.10.5 KNI_ReleaseHandle

TABLE 53  –  KNI_ReleaseHandle
Function
Description
Prototype
void KNI_ReleaseHandle(handle)
Description
Sets the value of the given handle to NULL.
Parameters
handle: the handle whose value will be set to NULL.
Return Values
None.
Exceptions
None.

5.10.6 KNI_EndHandles

TABLE 54  –  KNI_EndHandles
Function
Description
Prototype
void KNI_EndHandles()
Description
Undeclares and unallocates the handles defined after the preceding KNI_StartHandles call.
Parameters
None.
Return Values
None.
Exceptions
None.


Note – Every call to KNI_StartHandles must be matched with a corresponding KNI_EndHandles or KNI_EndHandlesAndReturnObject call. Nested KNI_StartHandles and KNI_EndHandles/KNI_EndHandlesAndReturnObject calls are not allowed inside the same C/C++ code block ({ ... }).

5.10.7 KNI_EndHandlesAndReturnObject

TABLE 55  –  KNI_EndHandlesAndReturnObject
Function
Description
Prototype
void KNI_EndHandlesAndReturnObject(jobject objectHandle);
Description
Undeclares and unallocates the handles defined after the preceding KNI_StartHandles call. At the same time, this function will terminate the execution of the native function immediately and return an object reference back to the caller.
Parameters
objectHandle: the handle from which the result value will be read.
Return Values
Returns an object value back to the function that called the native function containing the KNI_EndHandlesAndReturnObject call.
Exceptions
None.

 


Contents Previous Next KNI Specification
K Native Interface (KNI), 1.0