blob: d70fdad02c0691b3360f97a8a22c9a718c88c8f5 [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.
Brian Carlstromdd8af232012-05-13 23:56:07 -070022 *
23 * TODO: remove C support.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080024 */
Carl Shapiro1ff7a0d2011-06-14 20:31:24 -070025#ifndef NATIVEHELPER_JNIHELP_H_
26#define NATIVEHELPER_JNIHELP_H_
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080027
28#include "jni.h"
Dan Albert099d5212014-05-21 13:25:01 -070029#include <errno.h>
Dan Bornstein6edaa472009-10-26 13:33:22 -070030#include <unistd.h>
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080031
32#ifndef NELEM
33# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
34#endif
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/*
41 * Register one or more native methods with a particular class.
Elliott Hughes73d3c2e2012-05-03 17:06:04 -070042 * "className" looks like "java/lang/String". Aborts on failure.
43 * TODO: fix all callers and change the return type to void.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080044 */
Elliott Hughes87f62a82011-04-22 19:22:54 -070045int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080046
47/*
48 * Throw an exception with the specified class and an optional message.
Elliott Hughes87f62a82011-04-22 19:22:54 -070049 *
Andy McFadden0b9d26c2009-06-04 14:34:14 -070050 * The "className" argument will be passed directly to FindClass, which
51 * takes strings with slashes (e.g. "java/lang/Object").
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080052 *
Elliott Hughes87f62a82011-04-22 19:22:54 -070053 * If an exception is currently pending, we log a warning message and
54 * clear it.
55 *
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080056 * Returns 0 on success, nonzero if something failed (e.g. the exception
Elliott Hughes87f62a82011-04-22 19:22:54 -070057 * class couldn't be found, so *an* exception will still be pending).
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080058 *
59 * Currently aborts the VM if it can't throw the exception.
60 */
61int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
62
63/*
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080064 * Throw a java.lang.NullPointerException, with an optional message.
65 */
66int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
67
68/*
Dan Bornsteinf84f4c62009-12-07 15:46:23 -080069 * Throw a java.lang.RuntimeException, with an optional message.
70 */
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080071int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
Dan Bornsteinf84f4c62009-12-07 15:46:23 -080072
73/*
74 * Throw a java.io.IOException, generating the message from errno.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080075 */
76int jniThrowIOException(C_JNIEnv* env, int errnum);
77
78/*
Elliott Hughese4239ae2009-10-20 16:59:01 -070079 * Return a pointer to a locale-dependent error string explaining errno
80 * value 'errnum'. The returned pointer may or may not be equal to 'buf'.
81 * This function is thread-safe (unlike strerror) and portable (unlike
82 * strerror_r).
83 */
84const char* jniStrError(int errnum, char* buf, size_t buflen);
85
86/*
Elliott Hughes87f62a82011-04-22 19:22:54 -070087 * Returns a new java.io.FileDescriptor for the given int fd.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080088 */
89jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
90
Carl Shapiroabb4eff2010-06-08 16:37:12 -070091/*
Elliott Hughes87f62a82011-04-22 19:22:54 -070092 * Returns the int fd from a java.io.FileDescriptor.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080093 */
94int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
95
96/*
Pete Bentley0505b222018-07-20 18:18:44 +010097 * Sets the int fd in a java.io.FileDescriptor. Throws java.lang.NullPointerException
98 * if fileDescriptor is null.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080099 */
100void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
101
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700102/*
Josh Gao669bc9e2018-06-25 16:22:11 -0700103 * Returns the long ownerId from a java.io.FileDescriptor.
104 */
105jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
106
107/*
Jeff Browna7de6782013-04-02 18:09:29 -0700108 * Returns the reference from a java.lang.ref.Reference.
109 */
110jobject jniGetReferent(C_JNIEnv* env, jobject ref);
111
112/*
Fredrik Roubert2e312802017-07-04 21:53:08 +0200113 * Returns a Java String object created from UTF-16 data either from jchar or,
114 * if called from C++11, char16_t (a bitwise identical distinct type).
115 */
116jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len);
117
118/*
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700119 * Log a message and an exception.
120 * If exception is NULL, logs the current exception in the JNI environment.
121 */
122void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
123
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800124#ifdef __cplusplus
125}
126#endif
127
128
129/*
130 * For C++ code, we provide inlines that map to the C functions. g++ always
131 * inlines these, even on non-optimized builds.
132 */
Andy McFaddenb53ec152011-02-08 16:12:33 -0800133#if defined(__cplusplus)
Elliott Hughes87f62a82011-04-22 19:22:54 -0700134inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
135 return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800136}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700137
138inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800139 return jniThrowException(&env->functions, className, msg);
140}
Elliott Hughes2f82af12011-04-08 17:15:16 -0700141
Elliott Hughes87f62a82011-04-22 19:22:54 -0700142extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
Elliott Hughes2f82af12011-04-08 17:15:16 -0700143
144/*
145 * Equivalent to jniThrowException but with a printf-like format string and
146 * variable-length argument list. This is only available in C++.
147 */
Elliott Hughes87f62a82011-04-22 19:22:54 -0700148inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) {
Elliott Hughes2f82af12011-04-08 17:15:16 -0700149 va_list args;
150 va_start(args, fmt);
151 return jniThrowExceptionFmt(&env->functions, className, fmt, args);
152 va_end(args);
153}
154
Elliott Hughes87f62a82011-04-22 19:22:54 -0700155inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800156 return jniThrowNullPointerException(&env->functions, msg);
157}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700158
159inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800160 return jniThrowRuntimeException(&env->functions, msg);
161}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700162
163inline int jniThrowIOException(JNIEnv* env, int errnum) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800164 return jniThrowIOException(&env->functions, errnum);
165}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700166
167inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800168 return jniCreateFileDescriptor(&env->functions, fd);
169}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700170
171inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800172 return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
173}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700174
175inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700176 jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
177}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700178
Josh Gao669bc9e2018-06-25 16:22:11 -0700179inline jlong jniGetOwnerIdFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
180 return jniGetOwnerIdFromFileDescriptor(&env->functions, fileDescriptor);
181}
182
Jeff Browna7de6782013-04-02 18:09:29 -0700183inline jobject jniGetReferent(JNIEnv* env, jobject ref) {
184 return jniGetReferent(&env->functions, ref);
185}
186
Fredrik Roubert2e312802017-07-04 21:53:08 +0200187inline jstring jniCreateString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
188 return jniCreateString(&env->functions, unicodeChars, len);
189}
190
Fredrik Roubert2e312802017-07-04 21:53:08 +0200191inline jstring jniCreateString(JNIEnv* env, const char16_t* unicodeChars, jsize len) {
192 return jniCreateString(&env->functions, reinterpret_cast<const jchar*>(unicodeChars), len);
193}
Fredrik Roubert2e312802017-07-04 21:53:08 +0200194
Elliott Hughes87f62a82011-04-22 19:22:54 -0700195inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700196 jniLogException(&env->functions, priority, tag, exception);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800197}
Brian Carlstromdd8af232012-05-13 23:56:07 -0700198
Ian Rogers8288dde2014-11-04 11:42:02 -0800199#if !defined(DISALLOW_COPY_AND_ASSIGN)
200// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private:
201// declarations in a class.
Ian Rogers8288dde2014-11-04 11:42:02 -0800202#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
203 TypeName(const TypeName&) = delete; \
204 void operator=(const TypeName&) = delete
Ian Rogers8288dde2014-11-04 11:42:02 -0800205#endif // !defined(DISALLOW_COPY_AND_ASSIGN)
206
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800207#endif
208
Dan Bornstein6edaa472009-10-26 13:33:22 -0700209/*
210 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
211 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
212 * not already defined, then define it here.
213 */
214#ifndef TEMP_FAILURE_RETRY
215/* Used to retry syscalls that can return EINTR. */
216#define TEMP_FAILURE_RETRY(exp) ({ \
217 typeof (exp) _rc; \
218 do { \
219 _rc = (exp); \
220 } while (_rc == -1 && errno == EINTR); \
221 _rc; })
222#endif
223
Carl Shapiro1ff7a0d2011-06-14 20:31:24 -0700224#endif /* NATIVEHELPER_JNIHELP_H_ */