blob: d95670c7a2a938a91f5ae5f1b640ec3492e78741 [file] [log] [blame]
The Android Open Source Project7f844dd2009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * JNI helper functions.
19 *
20 * This file may be included by C or C++ code, which is trouble because jni.h
21 * uses different typedefs for JNIEnv in each language.
22 */
Orion Hodsonce487362020-04-22 19:41:46 +010023#pragma once
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080024
Orion Hodson511dfd52020-06-02 12:18:23 +010025#include <sys/cdefs.h>
26
Dan Albert099d5212014-05-21 13:25:01 -070027#include <errno.h>
Dan Bornstein6edaa472009-10-26 13:33:22 -070028#include <unistd.h>
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080029
Orion Hodson511dfd52020-06-02 12:18:23 +010030#include <jni.h>
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000031
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080032#ifndef NELEM
Orion Hodson85cdde82019-10-24 15:48:11 +010033#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080034#endif
35
Orion Hodson511dfd52020-06-02 12:18:23 +010036__BEGIN_DECLS
37
38
39/*
40 * Register one or more native methods with a particular class. "className" looks like
41 * "java/lang/String". Aborts on failure, returns 0 on success.
42 */
43int jniRegisterNativeMethods(C_JNIEnv* env,
44 const char* className,
45 const JNINativeMethod* gMethods,
46 int numMethods);
47
48/*
49 * Throw an exception with the specified class and an optional message.
50 *
51 * The "className" argument will be passed directly to FindClass, which
52 * takes strings with slashes (e.g. "java/lang/Object").
53 *
54 * If an exception is currently pending, we log a warning message and
55 * clear it.
56 *
57 * Returns 0 on success, nonzero if something failed (e.g. the exception
58 * class couldn't be found, so *an* exception will still be pending).
59 *
60 * Currently aborts the VM if it can't throw the exception.
61 */
62int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
63
64/*
65 * Throw an exception with the specified class and formatted error message.
66 *
67 * The "className" argument will be passed directly to FindClass, which
68 * takes strings with slashes (e.g. "java/lang/Object").
69 *
70 * If an exception is currently pending, we log a warning message and
71 * clear it.
72 *
73 * Returns 0 on success, nonzero if something failed (e.g. the exception
74 * class couldn't be found, so *an* exception will still be pending).
75 *
76 * Currently aborts the VM if it can't throw the exception.
77 */
78int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
79
80/*
81 * Throw a java.lang.NullPointerException, with an optional message.
82 */
83int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
84
85/*
86 * Throw a java.lang.RuntimeException, with an optional message.
87 */
88int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
89
90/*
91 * Throw a java.io.IOException, generating the message from errno.
92 */
93int jniThrowIOException(C_JNIEnv* env, int errnum);
94
95/*
Orion Hodson511dfd52020-06-02 12:18:23 +010096 * Returns a Java String object created from UTF-16 data either from jchar or,
97 * if called from C++11, char16_t (a bitwise identical distinct type).
98 */
99jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len);
100
101/*
Orion Hodson511dfd52020-06-02 12:18:23 +0100102 * Log a message and an exception.
103 * If exception is NULL, logs the current exception in the JNI environment.
104 */
105void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
106
107__END_DECLS
108
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800109/*
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800110 * For C++ code, we provide inlines that map to the C functions. g++ always
111 * inlines these, even on non-optimized builds.
112 */
Andy McFaddenb53ec152011-02-08 16:12:33 -0800113#if defined(__cplusplus)
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000114
Elliott Hughes87f62a82011-04-22 19:22:54 -0700115inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
116 return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800117}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700118
119inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800120 return jniThrowException(&env->functions, className, msg);
121}
Elliott Hughes2f82af12011-04-08 17:15:16 -0700122
Elliott Hughes2f82af12011-04-08 17:15:16 -0700123/*
124 * Equivalent to jniThrowException but with a printf-like format string and
125 * variable-length argument list. This is only available in C++.
126 */
Elliott Hughes87f62a82011-04-22 19:22:54 -0700127inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) {
Elliott Hughes2f82af12011-04-08 17:15:16 -0700128 va_list args;
129 va_start(args, fmt);
130 return jniThrowExceptionFmt(&env->functions, className, fmt, args);
131 va_end(args);
132}
133
Elliott Hughes87f62a82011-04-22 19:22:54 -0700134inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800135 return jniThrowNullPointerException(&env->functions, msg);
136}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700137
138inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800139 return jniThrowRuntimeException(&env->functions, msg);
140}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700141
142inline int jniThrowIOException(JNIEnv* env, int errnum) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800143 return jniThrowIOException(&env->functions, errnum);
144}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700145
Fredrik Roubert2e312802017-07-04 21:53:08 +0200146inline jstring jniCreateString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
147 return jniCreateString(&env->functions, unicodeChars, len);
148}
149
Fredrik Roubert2e312802017-07-04 21:53:08 +0200150inline jstring jniCreateString(JNIEnv* env, const char16_t* unicodeChars, jsize len) {
151 return jniCreateString(&env->functions, reinterpret_cast<const jchar*>(unicodeChars), len);
152}
Fredrik Roubert2e312802017-07-04 21:53:08 +0200153
Elliott Hughes87f62a82011-04-22 19:22:54 -0700154inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700155 jniLogException(&env->functions, priority, tag, exception);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800156}
Brian Carlstromdd8af232012-05-13 23:56:07 -0700157
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000158#endif // defined(__cplusplus)
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800159