-
Notifications
You must be signed in to change notification settings - Fork 0
/
JNITest.java
155 lines (121 loc) · 4.95 KB
/
JNITest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Java Native Interface/NASM example
* Copyright (C) Matthijs Laan <[email protected]>, February 10th 2002
*/
/*
* Output should be something like this:
*
* Java Native Interface/NASM example
* Copyright (C) Matthijs Laan <[email protected]>, February 10th 2002
*
* add(23, 53): 76
* callInstanceMethod(this):
* ...instanceCallback(123) called
* callStaticMethod(this.getClass()):
* ...staticCallback(13) called
* sumIntArray({-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}): 44
* reverseASCIIString("Hello, World!"): !dlroW ,olleH
* reverseASCIIString("HOll), W(rld!"):
* ...exception:
* java.lang.IllegalArgumentException: String passed to reverseASCIIString() is not ASCII
* at JNITest.reverseASCIIString(Native Method)
* at JNITest.<init>(JNITest.java:123)
* at JNITest.main(JNITest.java:153)
* staticField = 123
* changeStaticField():
* staticField = 124
* instanceField = 456
* changeInstanceField():
* instanceField = 457
* pi(): 3.141592653589793
* sqrt(2): 1.4142135623730951
* CPUID available
* Vendor ID: AuthenticAMD
* Brand string: AMD Athlon(tm) Processor
*
*/
public class JNITest {
static {
System.loadLibrary("JNITest");
}
/* Add two ints */
public static native int add(int a, int b);
/* Call an instance method of an object */
public static native void callInstanceMethod(Object obj);
/* Call a static method of a class */
public static native void callStaticMethod(Class clazz);
/* Add all integers in an array */
public static native int sumIntArray(int[] array);
/* Reverse an ASCII string, and possibly throw an exception */
public static native String reverseASCIIString(String str) throws IllegalArgumentException;
/* Change a static field of the class of the "this" object */
public native void changeStaticField();
/* Change an instance field of the "this" object */
public native void changeInstanceField();
/* Return pi */
public static native double pi();
/* Return the square root of a double */
public static native double sqrt(double v);
/* Does the CPU support the CPUID instruction (Pentium or higher) */
public static native boolean haveCPUID();
/* Return the CPU's ID string */
public static native String vendorID();
/* Return the CPU's brand string (only on new CPU's) */
public static native String brandString();
static int staticField = 123;
int instanceField = 456;
public static void staticCallback(int a) {
System.out.println(" ...staticCallback(" + a + ") called");
}
public void instanceCallback(int a) {
System.out.println(" ...instanceCallback(" + a + ") called");
}
private static void printArray(int[] array) {
System.out.print("{");
for(int i=0;i<array.length;i++) {
System.out.print((i>0?", ":"") + array[i]);
}
System.out.print("}");
}
public JNITest() {
System.out.println("Java Native Interface/NASM example");
System.out.println("Copyright (C) Matthijs Laan <[email protected]>, February 10th 2002\n");
System.out.println("add(23, 53): " + add(23, 53));
System.out.println("callInstanceMethod(this):");
callInstanceMethod(this);
System.out.println("callStaticMethod(this.getClass()):");
callStaticMethod(this.getClass());
int[] array = new int[] {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.print("sumIntArray(");
printArray(array);
System.out.println("): " + sumIntArray(array));
System.out.println("reverseASCIIString(\"Hello, World!\"): " + reverseASCIIString("Hello, World!"));
System.out.print("reverseASCIIString(\"Hêllõ, Wôrld!\"): ");
try {
System.out.print(reverseASCIIString("Hêllõ, Wôrld!"));
} catch(IllegalArgumentException e) {
System.out.println("\n ...exception:");
e.printStackTrace();
}
System.out.println("staticField = " + staticField);
System.out.println("changeStaticField():");
changeStaticField();
System.out.println("staticField = " + staticField);
System.out.println("instanceField = " + instanceField);
System.out.println("changeInstanceField():");
changeInstanceField();
System.out.println("instanceField = " + instanceField);
System.out.println("pi(): " + pi());
System.out.println("sqrt(2): " + sqrt(2));
if(haveCPUID()) {
System.out.println("CPUID available");
System.out.println("Vendor ID: " + vendorID());
System.out.println("Brand string: " + brandString());
} else {
System.out.println("CPUID not available");
}
}
public static void main(String[] args) {
new JNITest();
}
}