- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathjni_util.h
478 lines (406 loc) · 16.1 KB
/
jni_util.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#ifndefJNI_UTIL_H
#defineJNI_UTIL_H
#include"jni.h"
#include"jni_util_md.h"
#include"jlong.h"
#ifdef__cplusplus
extern"C" {
#endif
/*
* This file contains utility functions that can be implemented in pure JNI.
*
* Caution: Callers of functions declared in this file should be
* particularly aware of the fact that these functions are convenience
* functions, and as such are often compound operations, each one of
* which may throw an exception. Therefore, the functions this file
* will often return silently if an exception has occurred, and callers
* must check for exception themselves.
*/
/* Throw a Java exception by name. Similar to SignalError. */
JNIEXPORTvoidJNICALL
JNU_ThrowByName(JNIEnv*env, constchar*name, constchar*msg);
/* Throw common exceptions */
JNIEXPORTvoidJNICALL
JNU_ThrowNullPointerException(JNIEnv*env, constchar*msg);
JNIEXPORTvoidJNICALL
JNU_ThrowArrayIndexOutOfBoundsException(JNIEnv*env, constchar*msg);
JNIEXPORTvoidJNICALL
JNU_ThrowOutOfMemoryError(JNIEnv*env, constchar*msg);
JNIEXPORTvoidJNICALL
JNU_ThrowIllegalArgumentException(JNIEnv*env, constchar*msg);
JNIEXPORTvoidJNICALL
JNU_ThrowInternalError(JNIEnv*env, constchar*msg);
JNIEXPORTvoidJNICALL
JNU_ThrowIOException(JNIEnv*env, constchar*msg);
JNIEXPORTvoidJNICALL
JNU_ThrowClassNotFoundException(JNIEnv*env, constchar*msg);
/* Throw an exception by name, using the string returned by
* getLastErrorString for the detail string. If the last-error
* string is NULL, use the given default detail string.
*/
JNIEXPORTvoidJNICALL
JNU_ThrowByNameWithLastError(JNIEnv*env, constchar*name,
constchar*defaultDetail);
/* Throw an exception by name, using a given message and the string
* returned by getLastErrorString to construct the detail string.
*/
JNIEXPORTvoidJNICALL
JNU_ThrowByNameWithMessageAndLastError
(JNIEnv*env, constchar*name, constchar*message);
/* Throw an IOException, using the last-error string for the detail
* string. If the last-error string is NULL, use the given default
* detail string.
*/
JNIEXPORTvoidJNICALL
JNU_ThrowIOExceptionWithLastError(JNIEnv*env, constchar*defaultDetail);
JNIEXPORTvoidJNICALL
JNU_ThrowIOExceptionWithMessageAndLastError(JNIEnv*env, constchar*message);
/* Convert between Java strings and i18n C strings */
JNIEXPORTconstchar*
GetStringPlatformChars(JNIEnv*env, jstringjstr, jboolean*isCopy);
/* Convert between Java strings and i18n C strings
* Performs additional sanity checks on converted string
* such as presence of null characters which are not allowed.
* NULL may be returned with IllegalArgumentException pending
*/
JNIEXPORTconstchar*
GetStringPlatformCharsStrict(JNIEnv*env, jstringjstr, jboolean*isCopy);
JNIEXPORTjstringJNICALL
JNU_NewStringPlatform(JNIEnv*env, constchar*str);
/* Convert between Java strings and i18n C strings
* Performs additional sanity checks on converted string
* such as presence of null characters which are not allowed.
* NULL may be returned with IllegalArgumentException pending
*/
JNIEXPORTconstchar*JNICALL
JNU_GetStringPlatformCharsStrict(JNIEnv*env, jstringjstr, jboolean*isCopy);
/* Convert between Java strings and i18n C strings
* Deprecated: Use JNU_GetStringPlatformCharsStrict
*/
JNIEXPORTconstchar*JNICALL
JNU_GetStringPlatformChars(JNIEnv*env, jstringjstr, jboolean*isCopy);
JNIEXPORTvoidJNICALL
JNU_ReleaseStringPlatformChars(JNIEnv*env, jstringjstr, constchar*str);
/* Class constants */
JNIEXPORTjclassJNICALL
JNU_ClassString(JNIEnv*env);
/* Copy count number of arguments from src to dst. Array bounds
* and ArrayStoreException are checked.
*/
JNIEXPORTjintJNICALL
JNU_CopyObjectArray(JNIEnv*env, jobjectArraydst, jobjectArraysrc,
jintcount);
/* Invoke a object-returning static method, based on class name,
* method name, and signature string.
*
* The caller should check for exceptions by setting hasException
* argument. If the caller is not interested in whether an exception
* has occurred, pass in NULL.
*/
JNIEXPORTjvalueJNICALL
JNU_CallStaticMethodByName(JNIEnv*env,
jboolean*hasException,
constchar*class_name,
constchar*name,
constchar*signature,
...);
/* Invoke an instance method by name.
*/
JNIEXPORTjvalueJNICALL
JNU_CallMethodByName(JNIEnv*env,
jboolean*hasException,
jobjectobj,
constchar*name,
constchar*signature,
...);
JNIEXPORTjvalueJNICALL
JNU_CallMethodByNameV(JNIEnv*env,
jboolean*hasException,
jobjectobj,
constchar*name,
constchar*signature,
va_listargs);
/* Construct a new object of class, specifying the class by name,
* and specififying which constructor to run and what arguments to
* pass to it.
*
* The method will return an initialized instance if successful.
* It will return NULL if an error has occurred (for example if
* it ran out of memory) and the appropriate Java exception will
* have been thrown.
*/
JNIEXPORTjobjectJNICALL
JNU_NewObjectByName(JNIEnv*env, constchar*class_name,
constchar*constructor_sig, ...);
/* returns:
* 0: object is not an instance of the class named by classname.
* 1: object is an instance of the class named by classname.
* -1: the class named by classname cannot be found. An exception
* has been thrown.
*/
JNIEXPORTjintJNICALL
JNU_IsInstanceOfByName(JNIEnv*env, jobjectobject, constchar*classname);
/* Get or set class and instance fields.
* Note that set functions take a variable number of arguments,
* but only one argument of the appropriate type can be passed.
* For example, to set an integer field i to 100:
*
* JNU_SetFieldByName(env, &exc, obj, "i", "I", 100);
*
* To set a float field f to 12.3:
*
* JNU_SetFieldByName(env, &exc, obj, "f", "F", 12.3);
*
* The caller should check for exceptions by setting hasException
* argument. If the caller is not interested in whether an exception
* has occurred, pass in NULL.
*/
JNIEXPORTjvalueJNICALL
JNU_GetFieldByName(JNIEnv*env,
jboolean*hasException,
jobjectobj,
constchar*name,
constchar*sig);
JNIEXPORTvoidJNICALL
JNU_SetFieldByName(JNIEnv*env,
jboolean*hasException,
jobjectobj,
constchar*name,
constchar*sig,
...);
JNIEXPORTjvalueJNICALL
JNU_GetStaticFieldByName(JNIEnv*env,
jboolean*hasException,
constchar*classname,
constchar*name,
constchar*sig);
/************************************************************************
* Miscellaneous utilities used by the class libraries
*/
#defineIS_NULL(obj) ((obj) == NULL)
#defineJNU_IsNull(env,obj) ((obj) == NULL)
/************************************************************************
* Miscellaneous utilities used by the class libraries to return from
* a function if a value is NULL or an exception is pending.
*/
#defineCHECK_NULL(x) \
do { \
if ((x) == NULL) { \
return; \
} \
} while (0) \
#defineCHECK_NULL_THROW_NPE(env, x, msg) \
do { \
if ((x) == NULL) { \
JNU_ThrowNullPointerException((env), (msg));\
return; \
} \
} while(0) \
#defineCHECK_NULL_THROW_NPE_RETURN(env, x, msg, z)\
do { \
if ((x) == NULL) { \
JNU_ThrowNullPointerException((env), (msg));\
return (z); \
} \
} while(0) \
#defineCHECK_NULL_RETURN(x, y) \
do { \
if ((x) == NULL) { \
return (y); \
} \
} while (0) \
#ifdef__cplusplus
#defineJNU_CHECK_EXCEPTION(env) \
do { \
if ((env)->ExceptionCheck()) { \
return; \
} \
} while (0) \
#defineJNU_CHECK_EXCEPTION_RETURN(env, y) \
do { \
if ((env)->ExceptionCheck()) { \
return (y); \
} \
} while (0)
#else
#defineJNU_CHECK_EXCEPTION(env) \
do { \
if ((*env)->ExceptionCheck(env)) { \
return; \
} \
} while (0) \
#defineJNU_CHECK_EXCEPTION_RETURN(env, y) \
do { \
if ((*env)->ExceptionCheck(env)) { \
return (y); \
} \
} while (0)
#endif/* __cplusplus */
/************************************************************************
* Debugging utilities
*/
JNIEXPORTjstringJNICALL
JNU_ToString(JNIEnv*env, jobjectobject);
/*
* Package shorthand for use by native libraries
*/
#defineJNU_JAVAPKG "java/lang/"
#defineJNU_JAVAIOPKG "java/io/"
#defineJNU_JAVANETPKG "java/net/"
/*
* Check if the current thread is attached to the VM, and returns
* the JNIEnv of the specified version if the thread is attached.
*
* If the current thread is not attached, this function returns 0.
*
* If the current thread is attached, this function returns the
* JNI environment, or returns (void *)JNI_ERR if the specified
* version is not supported.
*/
JNIEXPORTvoid*JNICALL
JNU_GetEnv(JavaVM*vm, jintversion);
/*
* Warning free access to pointers stored in Java long fields.
*/
#defineJNU_GetLongFieldAsPtr(env,obj,id) \
(jlong_to_ptr((*(env))->GetLongField((env),(obj),(id))))
#defineJNU_SetLongFieldFromPtr(env,obj,id,val) \
(*(env))->SetLongField((env),(obj),(id),ptr_to_jlong(val))
/*
* Internal use only.
*/
enum {
NO_ENCODING_YET=0, /* "sun.jnu.encoding" not yet set */
NO_FAST_ENCODING, /* Platform encoding is not fast */
FAST_8859_1, /* ISO-8859-1 */
FAST_CP1252, /* MS-DOS Cp1252 */
FAST_646_US, /* US-ASCII : ISO646-US */
FAST_UTF_8
};
JNIEXPORTvoidInitializeEncoding(JNIEnv*env, constchar*name);
void*getProcessHandle();
jstringgetLastErrorString(JNIEnv*env);
JNIEXPORTintJNICALL
getErrorString(interr, char*buf, size_tlen);
#ifdefSTATIC_BUILD
/* Macros for handling declaration of static/dynamic
* JNI library Load/Unload functions
*
* Use DEF_JNI_On{Un}Load when you want a static and non-static entry points.
* Use DEF_STATIC_JNI_On{Un}Load when you only want a static one.
*
* LIBRARY_NAME must be set to the name of the library
*/
/* These three macros are needed to get proper concatenation of
* the LIBRARY_NAME
*
* NOTE: LIBRARY_NAME must be set for static builds.
*/
#defineADD_LIB_NAME3(name, lib) name ## lib
#defineADD_LIB_NAME2(name, lib) ADD_LIB_NAME3(name, lib)
#defineADD_LIB_NAME(entry) ADD_LIB_NAME2(entry, LIBRARY_NAME)
#defineDEF_JNI_OnLoad \
ADD_LIB_NAME(JNI_OnLoad_)(JavaVM *vm, void *reserved) \
{ \
jint JNICALL ADD_LIB_NAME(JNI_OnLoad_dynamic_)(JavaVM *vm, void *reserved); \
ADD_LIB_NAME(JNI_OnLoad_dynamic_)(vm, reserved); \
return JNI_VERSION_1_8; \
} \
jint JNICALL ADD_LIB_NAME(JNI_OnLoad_dynamic_)
#defineDEF_STATIC_JNI_OnLoad \
JNIEXPORT jint JNICALL ADD_LIB_NAME(JNI_OnLoad_)(JavaVM *vm, void *reserved) { \
return JNI_VERSION_1_8; \
}
#defineDEF_JNI_OnUnload \
ADD_LIB_NAME(JNI_OnUnload_)(JavaVM *vm, void *reserved) \
{ \
void JNICALL ADD_LIB_NAME(JNI_OnUnload_dynamic_)(JavaVM *vm, void *reserved); \
ADD_LIB_NAME(JNI_OnUnload_dynamic_)(vm, reserved); \
} \
void JNICALL ADD_LIB_NAME(JNI_OnUnload_dynamic_)
#defineDEF_STATIC_JNI_OnUnload \
ADD_LIB_NAME(JNI_OnUnload_)
#else
#defineDEF_JNI_OnLoad JNI_OnLoad
#defineDEF_STATIC_JNI_OnLoad
#defineDEF_JNI_OnUnload JNI_OnUnload
#defineDEF_STATIC_JNI_OnUnload
#endif
#ifdefSTATIC_BUILD
/* Macros for handling declaration of static/dynamic
* Agent library Load/Attach/Unload functions
*
* Use DEF_Agent_OnLoad, DEF_Agent_OnAttach or DEF_Agent_OnUnload
* when you want both static and non-static entry points.
* Use DEF_STATIC_Agent_OnLoad, DEF_STATIC_Agent_OnAttach or
* DEF_STATIC_Agent_OnUnload when you only want a static one.
*
* LIBRARY_NAME must be set to the name of the library for static builds.
*/
#defineDEF_Agent_OnLoad \
ADD_LIB_NAME(Agent_OnLoad_)(JavaVM *vm, char *options, void *reserved) \
{ \
jint JNICALL ADD_LIB_NAME(Agent_OnLoad_dynamic_)(JavaVM *vm, char *options, void *reserved); \
return ADD_LIB_NAME(Agent_OnLoad_dynamic_)(vm, options, reserved); \
} \
jint JNICALL ADD_LIB_NAME(Agent_OnLoad_dynamic_)
#defineDEF_STATIC_Agent_OnLoad \
JNIEXPORT jint JNICALL ADD_LIB_NAME(Agent_OnLoad_)(JavaVM *vm, char *options, void *reserved) { \
return JNI_FALSE; \
}
#defineDEF_Agent_OnAttach \
ADD_LIB_NAME(Agent_OnAttach_)(JavaVM *vm, char *options, void *reserved) \
{ \
jint JNICALL ADD_LIB_NAME(Agent_OnAttach_dynamic_)(JavaVM *vm, char *options, void *reserved); \
return ADD_LIB_NAME(Agent_OnAttach_dynamic_)(vm, options, reserved); \
} \
jint JNICALL ADD_LIB_NAME(Agent_OnAttach_dynamic_)
#defineDEF_STATIC_Agent_OnAttach \
JNIEXPORT jint JNICALL ADD_LIB_NAME(Agent_OnLoad_)(JavaVM *vm, char *options, void *reserved) { \
return JNI_FALSE; \
}
#defineDEF_Agent_OnUnload \
ADD_LIB_NAME(Agent_OnUnload_)(JavaVM *vm) \
{ \
void JNICALL ADD_LIB_NAME(Agent_OnUnload_dynamic_)(JavaVM *vm); \
ADD_LIB_NAME(Agent_OnUnload_dynamic_)(vm); \
} \
void JNICALL ADD_LIB_NAME(Agent_OnUnload_dynamic_)
#defineDEF_STATIC_Agent_OnUnload \
ADD_LIB_NAME(Agent_OnUnload_)
#else
#defineDEF_Agent_OnLoad Agent_OnLoad
#defineDEF_Agent_OnAttach Agent_OnAttach
#defineDEF_Agent_OnUnload Agent_OnUnload
#defineDEF_STATIC_Agent_OnLoad
#defineDEF_STATIC_Agent_OnAttach
#defineDEF_STATIC_Agent_OnUnload
#endif
#ifdef__cplusplus
} /* extern "C" */
#endif/* __cplusplus */
#endif/* JNI_UTIL_H */