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 * 23 * TODO: remove C support. 24 */ 25 #ifndef NATIVEHELPER_JNIHELP_H_ 26 #define NATIVEHELPER_JNIHELP_H_ 27 28 #include "jni.h" 29 #include <errno.h> 30 #include <unistd.h> 31 32 #ifndef NELEM 33 # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) 34 #endif 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 /* 41 * Register one or more native methods with a particular class. 42 * "className" looks like "java/lang/String". Aborts on failure. 43 * TODO: fix all callers and change the return type to void. 44 */ 45 int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods); 46 47 /* 48 * Throw an exception with the specified class and an optional message. 49 * 50 * The "className" argument will be passed directly to FindClass, which 51 * takes strings with slashes (e.g. "java/lang/Object"). 52 * 53 * If an exception is currently pending, we log a warning message and 54 * clear it. 55 * 56 * Returns 0 on success, nonzero if something failed (e.g. the exception 57 * class couldn't be found, so *an* exception will still be pending). 58 * 59 * Currently aborts the VM if it can't throw the exception. 60 */ 61 int jniThrowException(C_JNIEnv* env, const char* className, const char* msg); 62 63 /* 64 * Throw a java.lang.NullPointerException, with an optional message. 65 */ 66 int jniThrowNullPointerException(C_JNIEnv* env, const char* msg); 67 68 /* 69 * Throw a java.lang.RuntimeException, with an optional message. 70 */ 71 int jniThrowRuntimeException(C_JNIEnv* env, const char* msg); 72 73 /* 74 * Throw a java.io.IOException, generating the message from errno. 75 */ 76 int jniThrowIOException(C_JNIEnv* env, int errnum); 77 78 /* 79 * 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 */ 84 const char* jniStrError(int errnum, char* buf, size_t buflen); 85 86 /* 87 * Returns a new java.io.FileDescriptor for the given int fd. 88 */ 89 jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd); 90 91 /* 92 * Returns the int fd from a java.io.FileDescriptor. 93 */ 94 int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); 95 96 /* 97 * Sets the int fd in a java.io.FileDescriptor. 98 */ 99 void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value); 100 101 /* 102 * Returns the reference from a java.lang.ref.Reference. 103 */ 104 jobject jniGetReferent(C_JNIEnv* env, jobject ref); 105 106 /* 107 * Returns a Java String object created from UTF-16 data either from jchar or, 108 * if called from C++11, char16_t (a bitwise identical distinct type). 109 */ 110 jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len); 111 112 /* 113 * Log a message and an exception. 114 * If exception is NULL, logs the current exception in the JNI environment. 115 */ 116 void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception); 117 118 #ifdef __cplusplus 119 } 120 #endif 121 122 123 /* 124 * For C++ code, we provide inlines that map to the C functions. g++ always 125 * inlines these, even on non-optimized builds. 126 */ 127 #if defined(__cplusplus) 128 inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) { 129 return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods); 130 } 131 132 inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) { 133 return jniThrowException(&env->functions, className, msg); 134 } 135 136 extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args); 137 138 /* 139 * Equivalent to jniThrowException but with a printf-like format string and 140 * variable-length argument list. This is only available in C++. 141 */ 142 inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) { 143 va_list args; 144 va_start(args, fmt); 145 return jniThrowExceptionFmt(&env->functions, className, fmt, args); 146 va_end(args); 147 } 148 149 inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { 150 return jniThrowNullPointerException(&env->functions, msg); 151 } 152 153 inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) { 154 return jniThrowRuntimeException(&env->functions, msg); 155 } 156 157 inline int jniThrowIOException(JNIEnv* env, int errnum) { 158 return jniThrowIOException(&env->functions, errnum); 159 } 160 161 inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) { 162 return jniCreateFileDescriptor(&env->functions, fd); 163 } 164 165 inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) { 166 return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor); 167 } 168 169 inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) { 170 jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value); 171 } 172 173 inline jobject jniGetReferent(JNIEnv* env, jobject ref) { 174 return jniGetReferent(&env->functions, ref); 175 } 176 177 inline jstring jniCreateString(JNIEnv* env, const jchar* unicodeChars, jsize len) { 178 return jniCreateString(&env->functions, unicodeChars, len); 179 } 180 181 #if __cplusplus >= 201103L 182 inline jstring jniCreateString(JNIEnv* env, const char16_t* unicodeChars, jsize len) { 183 return jniCreateString(&env->functions, reinterpret_cast<const jchar*>(unicodeChars), len); 184 } 185 #endif // __cplusplus >= 201103L 186 187 inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) { 188 jniLogException(&env->functions, priority, tag, exception); 189 } 190 191 #if !defined(DISALLOW_COPY_AND_ASSIGN) 192 // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private: 193 // declarations in a class. 194 #if __cplusplus >= 201103L 195 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 196 TypeName(const TypeName&) = delete; \ 197 void operator=(const TypeName&) = delete 198 #else 199 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 200 TypeName(const TypeName&); \ 201 void operator=(const TypeName&) 202 #endif // __has_feature(cxx_deleted_functions) 203 #endif // !defined(DISALLOW_COPY_AND_ASSIGN) 204 205 #endif 206 207 /* 208 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of 209 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's 210 * not already defined, then define it here. 211 */ 212 #ifndef TEMP_FAILURE_RETRY 213 /* Used to retry syscalls that can return EINTR. */ 214 #define TEMP_FAILURE_RETRY(exp) ({ \ 215 typeof (exp) _rc; \ 216 do { \ 217 _rc = (exp); \ 218 } while (_rc == -1 && errno == EINTR); \ 219 _rc; }) 220 #endif 221 222 #endif /* NATIVEHELPER_JNIHELP_H_ */ 223