📄 native_dpr.html
字号:
WriteLn(str);
<font color="#003399"><i>// Create a JString and return it</i></font>
Result := JVM.NewStringUTF(PAnsiChar(Str));
JVM.Free;
<b>end</b>;
<font color="#003399"><i>(****************************************************************************
* Java declaration:
* native public void printObjectArray(Object[] array);
*
* Given an array of Objects, each element is printed using the
* 'toString' method of the Object.
*
* Class: Native
* Method: printObjectArray
* Signature: ([Ljava/lang/Object;Z)V
*)</i></font>
<b>procedure</b> Java_Native_printObjectArray(PEnv: PJNIEnv; Obj: JObject; ObjArray: JObjectArray; Print: JBoolean); <b>stdcall</b>;
<b>var</b>
Cls: JClass;
Mid: JMethodID;
Ob: JObject;
Element: JObject;
S: JString;
Str: <b>string</b>;
Len, I: Integer;
JVM: TJNIEnv;
<b>begin</b>
<font color="#003399"><i>// Create an instance of the Java environment</i></font>
JVM := TJNIEnv.Create(PEnv);
<font color="#003399"><i>// Get length of the array</i></font>
len := JVM.GetArrayLength(ObjArray);
<font color="#003399"><i>// Make sure we have at least one object in the array</i></font>
<b>if</b> Len < 1 <b>then</b>
exit;
<font color="#003399"><i>// Get an element of the array so we can get the right 'toString' method</i></font>
Ob := JVM.GetObjectArrayElement(ObjArray, 0);
<font color="#003399"><i>// Get the class associated with this object</i></font>
Cls := JVM.GetObjectClass(Ob);
<b>if</b> Cls = <b>nil</b> <b>then</b>
<b>begin</b>
WriteLn(<font color="#9933CC">'Can'</font><font color="#9933CC">'t GetObjectClass'</font>);
exit;
<b>end</b>;
<font color="#003399"><i>// Get method ID for the 'toString' method of this object's class</i></font>
Mid := JVM.GetMethodID(Cls, <font color="#9933CC">'toString'</font>, <font color="#9933CC">'()Ljava/lang/String;'</font>);
<font color="#003399"><i>// We will check this also</i></font>
<b>if</b> Mid = <b>nil</b> <b>then</b>
<b>begin</b>
WriteLn(<font color="#9933CC">'Can'</font><font color="#9933CC">'t GetMethodID for toString'</font>);
exit;
<b>end</b>;
<font color="#003399"><i>// Loop the the array of objects and print out each one using</i></font>
<font color="#003399"><i>// the 'toString' method of its ancestor class Object</i></font>
<b>for</b> I := 0 <b>to</b> Len - 1 <b>do</b>
<b>begin</b>
<font color="#003399"><i>// Get the next element from the array</i></font>
Element := JVM.GetObjectArrayElement(ObjArray, i);
<font color="#003399"><i>// Call the 'toString' method, which returns a String representation</i></font>
<font color="#003399"><i>// of Rectangle object.</i></font>
S := JVM.CallObjectMethodA(Element, Mid, <b>nil</b>);
<font color="#003399"><i>// The actual printing can be turned on/off. This was useful during</i></font>
<font color="#003399"><i>// debugging when passing thousands of elements into the procedure.</i></font>
<b>if</b> Print <> False <b>then</b>
<b>begin</b>
Str := JVM.JStringToString(S);
WriteLn(Str);
<b>end</b>;
<b>end</b>;
JVM.Free;
<b>end</b>;
<font color="#003399"><i>(****************************************************************************
* Java declaration:
* native public jobjectarray returnRectArray(int size);
*
* This function creates an array of 'size' Rectangles and returns them
* to the Java caller. First, the array is created, then each element
* is assigned a Rectangle object.
*
* Class: Native
* Method: returnRectArray
* Signature: (I)[Ljava/awt/Rectangle;
*)</i></font>
<b>function</b> Java_Native_returnRectArray(PEnv: PJNIEnv; Obj: JObject; Size: JInt): JObjectArray; <b>stdcall</b>;
<b>var</b>
Cls: JClass;
Mid: JMethodID;
Element: JObject;
JOArray: JObjectArray;
I: Integer;
Args: <b>array</b>[0..3] <b>of</b> JValue;
JVM: TJNIEnv;
<b>begin</b>
<font color="#003399"><i>// Create an instance of the Java environment</i></font>
JVM := TJNIEnv.Create(PEnv);
<font color="#003399"><i>// Find the Rectangle class</i></font>
Cls := JVM.FindClass(<font color="#9933CC">'java/awt/Rectangle'</font>);
<b>if</b> Cls = <b>nil</b> <b>then</b>
<b>begin</b>
WriteLn(<font color="#9933CC">'Can'</font><font color="#9933CC">'t FindClass(java/awt/Rectangle'</font>);
Result := <b>nil</b>;
exit;
<b>end</b>;
<font color="#003399"><i>// Get its constructor (the one that takes 4 integers)</i></font>
Mid := JVM.GetMethodID(Cls, <font color="#9933CC">'<init>'</font>, <font color="#9933CC">'(IIII)V'</font>);
<b>if</b> Mid = <b>nil</b> <b>then</b>
<b>begin</b>
WriteLn(<font color="#9933CC">'Can'</font><font color="#9933CC">'t get MethodID for Rectangle constructor'</font>);
Result := <b>nil</b>;
exit;
<b>end</b>;
<font color="#003399"><i>// Allocate the array of Rectangles</i></font>
JOArray := JVM.NewObjectArray(Size, Cls, <b>nil</b>);
<font color="#003399"><i>// Now initialize each one to a Rectangle</i></font>
<b>for</b> I := 0 <b>to</b> Size - 1 <b>do</b>
<b>begin</b>
<font color="#003399"><i>// Create a new Rectangle object</i></font>
Args[0].i := 0;
Args[1].i := 0;
Args[2].i := 5 * I;
Args[3].i := 10 * I;
Element := JVM.NewObjectA(Cls, Mid, @args);
<font color="#003399"><i>// Assign the Rectangle to an element of the array</i></font>
JVM.SetObjectArrayElement(JOArray, I, Element);
<b>end</b>;
<font color="#003399"><i>// Return the array back to the Java client</i></font>
Result := JOArray;
JVM.Free;
<b>end</b>;
<font color="#003399"><i>(****************************************************************************
* Java declaration:
* native public void handleException();
*
* Causes an exception in the JVM, but detects it and supresses it
*
* Class: Native
* Method: handleException
* Signature: ()V
*)</i></font>
<b>procedure</b> Java_Native_handleException(PEnv: PJNIEnv; Obj: JObject); <b>stdcall</b>;
<b>var</b>
Cls: JClass;
AException: JThrowable;
JVM: TJNIEnv;
<b>begin</b>
<font color="#003399"><i>// Create an instance of the Java environment</i></font>
JVM := TJNIEnv.Create(PEnv);
<font color="#003399"><i>// Get the class to which this object belongs</i></font>
Cls := JVM.GetObjectClass(Obj);
<font color="#003399"><i>// Attempt to get the ID of the 'nonexistent' member, which, not by</i></font>
<font color="#003399"><i>// chance, doesn't exist!</i></font>
JVM.GetFieldID(Cls, <font color="#9933CC">'nonexistent'</font>, <font color="#9933CC">'Ljava/lang/String;'</font>);
<font color="#003399"><i>// Check for exception</i></font>
AException := JVM.ExceptionOccurred;
<font color="#003399"><i>// exception is non-zero if an exception occurred</i></font>
<b>if</b> (AException <> <b>nil</b>) <b>then</b>
<b>begin</b>
<font color="#003399"><i>//WriteLn('Exception occurred in Native code and was handled. This was the exception:');</i></font>
Writeln(Format(<font color="#9933CC">'Exception handled in Main.cpp: %d'</font>, [DWORD(AException)]));
<font color="#003399"><i>// This call will print out a description of the exception</i></font>
<font color="#003399"><i>//JVM.ExceptionDescribe(PEnv);</i></font>
<font color="#003399"><i>// Clear the exception so the JVM doesn't react to it after we handled it</i></font>
JVM.ExceptionClear;
<b>end</b>;
JVM.Free;
<b>end</b>;
<font color="#003399"><i>(****************************************************************************
* Java declaration:
* native public void causeException();
*
* Causes an exception in the JVM, but fails to catch it. Thus, it is
* propagated back to the JVM.
*
* Class: Native
* Method: causeException
* Signature: ()V
*)</i></font>
<b>procedure</b> Java_Native_causeException(PEnv: PJNIEnv; Obj: JObject); <b>stdcall</b>;
<b>var</b>
Cls: JClass;
JVM: TJNIEnv;
<b>begin</b>
<font color="#003399"><i>// Create an instance of the Java environment</i></font>
JVM := TJNIEnv.Create(PEnv);
<font color="#003399"><i>// Get the class to which this object belongs</i></font>
Cls := JVM.GetObjectClass(Obj);
<font color="#003399"><i>// Attempt to get the ID of the 'nonexistent' member, which, not by</i></font>
<font color="#003399"><i>// chance, doesn't exist!</i></font>
JVM.GetFieldID(Cls, <font color="#9933CC">'nonexistent'</font>, <font color="#9933CC">'Ljava/lang/String;'</font>);
<font color="#003399"><i>// An exception has occurred, but it has not been suppressed.</i></font>
<font color="#003399"><i>// The JVM will detect it and catch it.</i></font>
<font color="#003399"><i>// Because we don't call this: JVM.ExceptionClear(PEnv),</i></font>
<font color="#003399"><i>// the JVM will react.</i></font>
JVM.Free;
<b>end</b>;
<font color="#003399"><i>(****************************************************************************
* Java declaration:
* native public void pass2DByteArray(byte[][] array);
*
* Pass a 2-D array of Bytes from Java. This method will retrieve each
* element from the arrays, print it, then multiply it by 10 and store
* the new Value back into the array. This is to show access/updating
* of 2-D arrays within native code. The process would be similar for
* 3-D arrays.
*
* Class: Native
* Method: pass2DByteArray
* Signature: ([[B)V
*)</i></font>
<b>procedure</b> Java_Native_pass2DByteArray(PEnv: PJNIEnv; Obj: JObject; Array2D: JObjectArray); <b>stdcall</b>;
<b>var</b>
NumArrays: UINT;
Len: UINT;
I, J: Integer;
JBArray: JByteArray;
Elements: PJByte;
IsCopy: JBoolean;
JVM: TJNIEnv;
<b>begin</b>
<font color="#003399"><i>// Create an instance of the Java environment</i></font>
JVM := TJNIEnv.Create(PEnv);
<font color="#003399"><i>// Get length of the array (number of arrays)</i></font>
NumArrays := JVM.GetArrayLength(Array2D);
WriteLn(<font color="#9933CC">'In native method printing 2-D byte array.'</font>);
WriteLn(<font color="#9933CC">'Each element is then multiplied by 10 and updated.'</font>);
<font color="#003399"><i>// Loop over each array</i></font>
<b>for</b> I := 0 <b>to</b> NumArrays - 1 <b>do</b>
<b>begin</b>
<font color="#003399"><i>// Get the object at the i'th position (it's an array of Bytes)</i></font>
JBArray := JVM.GetObjectArrayElement(Array2D, i);
<font color="#003399"><i>// Get the length of this array of Bytes</i></font>
Len := JVM.GetArrayLength(JBArray);
<font color="#003399"><i>// Get the elements from the Byte array</i></font>
Elements := JVM.GetByteArrayElements(JBArray, IsCopy);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -