📄 main_java.html
字号:
<html>
<BODY BGCOLOR="#e0e0e0">
<pre class="sourcecode"><code><font color="#003399"><i>/****************************************************************************
*
* Main.java
*
* This code is used as a driver program to demonstrate the native methods
* declared in Native.java.
*
* The countPrimes algorithm was taken from Microsoft's documentation of
* their Raw Native Interface (RNI).
*
*
*/</i></font>
<b>public</b> <b>class</b> Main {
<b>static</b> <b>int</b> a, b;
<b>static</b> <b>final</b> <b>int</b> size = 1000000;
<b>static</b> <b>final</b> <b>int</b> array_size = 10000;
<b>static</b> <b>final</b> <b>int</b> func_calls = 1000000;
<b>static</b> <b>final</b> <b>int</b> rectangles = 500;
<b>static</b> <b>byte</b>[] c = <b>new</b> <b>byte</b>[size];
<b>static</b> <b>byte</b>[] d = <b>new</b> <b>byte</b>[array_size];
<b>public</b> <b>static</b> <b>void</b> main(String[] args)
{
String str;
<b>long</b> start, stop, elapsed;
<b>int</b> t;
<b>int</b> x;
<b>byte</b> value = 123;
<font color="#003399"><i>// static member</i></font>
<b>Native</b>.z = 47;
<font color="#003399"><i>// Create an object from Native.java</i></font>
<b>Native</b> nat = <b>new</b> <b>Native</b>();
<font color="#003399"><i>// Print version</i></font>
nat.printVersion();
<font color="#003399"><i>// Test all data types and show that Delphi floats won't work correctly with JNI methods</i></font>
<font color="#003399"><i>// that can accept a variable number of arguments.</i></font>
System.out.println(<font color="#9933CC">"This should print out the following (but the float field will be incorrect):"</font>);
System.out.print(<font color="#9933CC">"boolean = true, byte = 11, char = J, int = 123456, "</font>);
System.out.println(<font color="#9933CC">"long = 987654321, float = 47.56, double = 898.87678 "</font>);
nat.testAllTypesD(<b>true</b>, (<b>byte</b>)11, <font color="#9933CC">'J'</font>, 123456, 987654321L, 47.56F, 898.87678D);
System.out.println(<font color="#9933CC">"\n******* Test A: Native.multiplyIntegers(3, 7)"</font>);
System.out.println(<font color="#9933CC">"Multiply: 3 x 7 = "</font> + nat.multiplyIntegers(3, 7));
<font color="#003399"><i>// Simple test</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 1: Native.displayHelloWorld"</font>);
nat.displayHelloWorld();
<font color="#003399"><i>// Prints out all members of the Java Object by calling</i></font>
<font color="#003399"><i>// back into the nat object's 'toString' method</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 2: Native.toStringWithPrint"</font>);
nat.toStringWithPrint();
<font color="#003399"><i>// Prints a Java string passed in and also returns a Java string</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 3: Native.printLine"</font>);
str = <font color="#9933CC">"This is from Java"</font>;
str = nat.printLine(str);
System.out.println(<font color="#9933CC">"In Java code, string is: "</font> + str);
<font color="#003399"><i>// Prints out the w, x, y, and z members of the nat object</i></font>
<font color="#003399"><i>// all from native code</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 4: Native.printWXYZ"</font>);
nat.printWXYZ();
<font color="#003399"><i>// An array of Rectangles is created and passed to the native code</i></font>
<font color="#003399"><i>// to be printed</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 5: Native.printObjectArray(Rectangle[3])"</font>);
java.awt.Rectangle[] rects = <b>new</b> java.awt.Rectangle[3];
<b>for</b> (<b>int</b> i = 0; i < rects.length; i++)
rects[i] = <b>new</b> java.awt.Rectangle(0, 0, 4 * i, 3 * i);
nat.printObjectArray(rects, <b>true</b>);
<font color="#003399"><i>// An array of Native objects are passed to Delphi for printing</i></font>
<font color="#003399"><i>// Demonstrates how the native method works on any 'thing' derived</i></font>
<font color="#003399"><i>// from a Java Object.</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 6: Native.printObjectArray(Native[2])"</font>);
<b>Native</b>[] nats = <b>new</b> <b>Native</b>[2];
<b>for</b> (<b>int</b> i = 0; i < nats.length; i++)
nats[i] = <b>new</b> <b>Native</b>();
nat.printObjectArray(nats, <b>true</b>);
<font color="#003399"><i>// The Delphi code allocates and initializes an array of Rectangles</i></font>
<font color="#003399"><i>// that get passed back to Java and printed here.</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 7: Native.returnRectArray(3)"</font>);
java.awt.Rectangle[] rects2 = nat.returnRectArray(3);
<b>for</b> (<b>int</b> i = 0; i < rects2.length; i++)
System.out.println(rects2[i].toString());
<font color="#003399"><i>// Some exception handling examples</i></font>
<font color="#003399"><i>// Notice that we must catch 'Throwable' objects and not</i></font>
<font color="#003399"><i>// Exception objects. It turns out that these tests are</i></font>
<font color="#003399"><i>// actually returning Error objects, but both Error and</i></font>
<font color="#003399"><i>// Exception derive from Throwable.</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 8: Native.handleException"</font>);
<b>try</b>
{
nat.handleException();
}
<b>catch</b> (Throwable e)
{
System.out.println(<font color="#9933CC">"Exception in Native code not handled: "</font> + e);
}
System.out.println(<font color="#9933CC">"\n******* Test 9: Native.causeException"</font>);
<b>try</b>
{
nat.causeException();
}
<b>catch</b> (Throwable e)
{
System.out.println(<font color="#9933CC">"Exception in Native code not handled: "</font> + e);
}
<font color="#003399"><i>// 12-18-98</i></font>
System.out.println(<font color="#9933CC">"\n******* Test 10: Native.pass2DByteArray(array2D)"</font>);
<b>int</b> dim1 = 2, dim2 = 3;
<b>byte</b> count = 0;
<b>byte</b>[][] array2D = <b>new</b> <b>byte</b>[dim1][dim2];
<b>for</b> (<b>int</b> i = 0; i < dim1; i++)
<b>for</b> (<b>int</b> j = 0; j < dim2; j++)
array2D[i][j] = ++count;
<font color="#003399"><i>// Call Native code to initialize an array to a specific value many times</i></font>
nat.pass2DByteArray(array2D);
System.out.println(<font color="#9933CC">"\nIn Java printing array that was changed by native method."</font>);
<b>for</b> (<b>int</b> i = 0; i < dim1; i++)
<b>for</b> (<b>int</b> j = 0; j < dim2; j++)
System.out.println(i + <font color="#9933CC">","</font> + j + <font color="#9933CC">" = "</font> + array2D[i][j]);
System.out.println(<font color="#9933CC">"\n\n********** Timings **********\n"</font>);
System.out.print(<font color="#9933CC">"Native countPrimes up to "</font> + size + <font color="#9933CC">" ...."</font>);
start = System.currentTimeMillis();
nat.countPrimes(c);
stop = System.currentTimeMillis();
elapsed = stop - start;
System.out.println(<font color="#9933CC">" "</font> + elapsed + <font color="#9933CC">" milliseconds"</font>);
System.out.print(<font color="#9933CC">" Java countPrimes up to "</font> + size + <font color="#9933CC">" ...."</font>);
start = System.currentTimeMillis();
countPrimes(c);
stop = System.currentTimeMillis();
elapsed = stop - start;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -