The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 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 Hodson | ce48736 | 2020-04-22 19:41:46 +0100 | [diff] [blame] | 23 | #pragma once |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 24 | |
Orion Hodson | 511dfd5 | 2020-06-02 12:18:23 +0100 | [diff] [blame] | 25 | #include <sys/cdefs.h> |
| 26 | |
Dan Albert | 099d521 | 2014-05-21 13:25:01 -0700 | [diff] [blame] | 27 | #include <errno.h> |
Dan Bornstein | 6edaa47 | 2009-10-26 13:33:22 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | |
Orion Hodson | 511dfd5 | 2020-06-02 12:18:23 +0100 | [diff] [blame] | 30 | #include <jni.h> |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame] | 31 | |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | #ifndef NELEM |
Orion Hodson | 85cdde8 | 2019-10-24 15:48:11 +0100 | [diff] [blame] | 33 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | #endif |
| 35 | |
Orion Hodson | 511dfd5 | 2020-06-02 12:18:23 +0100 | [diff] [blame] | 36 | __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 | */ |
| 43 | int 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 | */ |
| 62 | int 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 | */ |
| 78 | int 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 | */ |
| 83 | int jniThrowNullPointerException(C_JNIEnv* env, const char* msg); |
| 84 | |
| 85 | /* |
| 86 | * Throw a java.lang.RuntimeException, with an optional message. |
| 87 | */ |
| 88 | int jniThrowRuntimeException(C_JNIEnv* env, const char* msg); |
| 89 | |
| 90 | /* |
| 91 | * Throw a java.io.IOException, generating the message from errno. |
| 92 | */ |
| 93 | int jniThrowIOException(C_JNIEnv* env, int errnum); |
| 94 | |
| 95 | /* |
Orion Hodson | 511dfd5 | 2020-06-02 12:18:23 +0100 | [diff] [blame] | 96 | * 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 | */ |
| 99 | jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len); |
| 100 | |
| 101 | /* |
Orion Hodson | 511dfd5 | 2020-06-02 12:18:23 +0100 | [diff] [blame] | 102 | * Log a message and an exception. |
| 103 | * If exception is NULL, logs the current exception in the JNI environment. |
| 104 | */ |
| 105 | void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception); |
| 106 | |
| 107 | __END_DECLS |
| 108 | |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 109 | /* |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 110 | * For C++ code, we provide inlines that map to the C functions. g++ always |
| 111 | * inlines these, even on non-optimized builds. |
| 112 | */ |
Andy McFadden | b53ec15 | 2011-02-08 16:12:33 -0800 | [diff] [blame] | 113 | #if defined(__cplusplus) |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame] | 114 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 115 | inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) { |
| 116 | return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 117 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 118 | |
| 119 | inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 120 | return jniThrowException(&env->functions, className, msg); |
| 121 | } |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 122 | |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 123 | /* |
| 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 Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 127 | inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) { |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 128 | va_list args; |
| 129 | va_start(args, fmt); |
| 130 | return jniThrowExceptionFmt(&env->functions, className, fmt, args); |
| 131 | va_end(args); |
| 132 | } |
| 133 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 134 | inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 135 | return jniThrowNullPointerException(&env->functions, msg); |
| 136 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 137 | |
| 138 | inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) { |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 139 | return jniThrowRuntimeException(&env->functions, msg); |
| 140 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 141 | |
| 142 | inline int jniThrowIOException(JNIEnv* env, int errnum) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 143 | return jniThrowIOException(&env->functions, errnum); |
| 144 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 145 | |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 146 | inline jstring jniCreateString(JNIEnv* env, const jchar* unicodeChars, jsize len) { |
| 147 | return jniCreateString(&env->functions, unicodeChars, len); |
| 148 | } |
| 149 | |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 150 | inline jstring jniCreateString(JNIEnv* env, const char16_t* unicodeChars, jsize len) { |
| 151 | return jniCreateString(&env->functions, reinterpret_cast<const jchar*>(unicodeChars), len); |
| 152 | } |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 153 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 154 | inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) { |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 155 | jniLogException(&env->functions, priority, tag, exception); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 156 | } |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 157 | |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame] | 158 | #endif // defined(__cplusplus) |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 159 | |