Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | #define LOG_TAG "JNIHelp" |
| 18 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 19 | #include "JniConstants.h" |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 20 | #include "JNIHelp.h" |
Elliott Hughes | a6f951c | 2011-06-08 15:54:05 -0700 | [diff] [blame] | 21 | #include "cutils/log.h" |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 27 | #include <string> |
| 28 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Equivalent to ScopedLocalRef, but for C_JNIEnv instead. (And slightly more powerful.) |
| 31 | */ |
| 32 | template<typename T> |
| 33 | class scoped_local_ref { |
| 34 | public: |
| 35 | scoped_local_ref(C_JNIEnv* env, T localRef = NULL) |
| 36 | : mEnv(env), mLocalRef(localRef) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | ~scoped_local_ref() { |
| 41 | reset(); |
| 42 | } |
| 43 | |
| 44 | void reset(T localRef = NULL) { |
| 45 | if (mLocalRef != NULL) { |
| 46 | (*mEnv)->DeleteLocalRef(reinterpret_cast<JNIEnv*>(mEnv), mLocalRef); |
| 47 | mLocalRef = localRef; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | T get() const { |
| 52 | return mLocalRef; |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | C_JNIEnv* mEnv; |
| 57 | T mLocalRef; |
| 58 | |
| 59 | // Disallow copy and assignment. |
| 60 | scoped_local_ref(const scoped_local_ref&); |
| 61 | void operator=(const scoped_local_ref&); |
| 62 | }; |
| 63 | |
| 64 | static jclass findClass(C_JNIEnv* env, const char* className) { |
| 65 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 66 | return (*env)->FindClass(e, className); |
| 67 | } |
| 68 | |
| 69 | extern "C" int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, |
| 70 | const JNINativeMethod* gMethods, int numMethods) |
| 71 | { |
| 72 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 73 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 74 | ALOGV("Registering %s's %d native methods...", className, numMethods); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 75 | |
| 76 | scoped_local_ref<jclass> c(env, findClass(env, className)); |
| 77 | if (c.get() == NULL) { |
Elliott Hughes | a3b5700 | 2013-01-22 09:35:09 -0800 | [diff] [blame] | 78 | char* msg; |
Brian Carlstrom | deb6d5d | 2013-05-10 09:50:08 -0700 | [diff] [blame] | 79 | asprintf(&msg, "Native registration unable to find class '%s'; aborting...", className); |
Elliott Hughes | a3b5700 | 2013-01-22 09:35:09 -0800 | [diff] [blame] | 80 | e->FatalError(msg); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if ((*env)->RegisterNatives(e, c.get(), gMethods, numMethods) < 0) { |
Elliott Hughes | a3b5700 | 2013-01-22 09:35:09 -0800 | [diff] [blame] | 84 | char* msg; |
Brian Carlstrom | deb6d5d | 2013-05-10 09:50:08 -0700 | [diff] [blame] | 85 | asprintf(&msg, "RegisterNatives failed for '%s'; aborting...", className); |
Elliott Hughes | a3b5700 | 2013-01-22 09:35:09 -0800 | [diff] [blame] | 86 | e->FatalError(msg); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Returns a human-readable summary of an exception object. The buffer will |
| 94 | * be populated with the "binary" class name and, if present, the |
| 95 | * exception message. |
| 96 | */ |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 97 | static bool getExceptionSummary(C_JNIEnv* env, jthrowable exception, std::string& result) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 98 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 99 | |
| 100 | /* get the name of the exception's class */ |
| 101 | scoped_local_ref<jclass> exceptionClass(env, (*env)->GetObjectClass(e, exception)); // can't fail |
| 102 | scoped_local_ref<jclass> classClass(env, |
| 103 | (*env)->GetObjectClass(e, exceptionClass.get())); // java.lang.Class, can't fail |
| 104 | jmethodID classGetNameMethod = |
| 105 | (*env)->GetMethodID(e, classClass.get(), "getName", "()Ljava/lang/String;"); |
| 106 | scoped_local_ref<jstring> classNameStr(env, |
| 107 | (jstring) (*env)->CallObjectMethod(e, exceptionClass.get(), classGetNameMethod)); |
| 108 | if (classNameStr.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 109 | (*env)->ExceptionClear(e); |
| 110 | result = "<error getting class name>"; |
| 111 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 112 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 113 | const char* classNameChars = (*env)->GetStringUTFChars(e, classNameStr.get(), NULL); |
| 114 | if (classNameChars == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 115 | (*env)->ExceptionClear(e); |
| 116 | result = "<error getting class name UTF-8>"; |
| 117 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 118 | } |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 119 | result += classNameChars; |
| 120 | (*env)->ReleaseStringUTFChars(e, classNameStr.get(), classNameChars); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 121 | |
| 122 | /* if the exception has a detail message, get that */ |
| 123 | jmethodID getMessage = |
| 124 | (*env)->GetMethodID(e, exceptionClass.get(), "getMessage", "()Ljava/lang/String;"); |
| 125 | scoped_local_ref<jstring> messageStr(env, |
| 126 | (jstring) (*env)->CallObjectMethod(e, exception, getMessage)); |
| 127 | if (messageStr.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 128 | return true; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 131 | result += ": "; |
| 132 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 133 | const char* messageChars = (*env)->GetStringUTFChars(e, messageStr.get(), NULL); |
| 134 | if (messageChars != NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 135 | result += messageChars; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 136 | (*env)->ReleaseStringUTFChars(e, messageStr.get(), messageChars); |
| 137 | } else { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 138 | result += "<error getting message>"; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 139 | (*env)->ExceptionClear(e); // clear OOM |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 142 | return true; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Returns an exception (with stack trace) as a string. |
| 147 | */ |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 148 | static bool getStackTrace(C_JNIEnv* env, jthrowable exception, std::string& result) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 149 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 150 | |
| 151 | scoped_local_ref<jclass> stringWriterClass(env, findClass(env, "java/io/StringWriter")); |
| 152 | if (stringWriterClass.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 153 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | jmethodID stringWriterCtor = (*env)->GetMethodID(e, stringWriterClass.get(), "<init>", "()V"); |
| 157 | jmethodID stringWriterToStringMethod = |
| 158 | (*env)->GetMethodID(e, stringWriterClass.get(), "toString", "()Ljava/lang/String;"); |
| 159 | |
| 160 | scoped_local_ref<jclass> printWriterClass(env, findClass(env, "java/io/PrintWriter")); |
| 161 | if (printWriterClass.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 162 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | jmethodID printWriterCtor = |
| 166 | (*env)->GetMethodID(e, printWriterClass.get(), "<init>", "(Ljava/io/Writer;)V"); |
| 167 | |
| 168 | scoped_local_ref<jobject> stringWriter(env, |
| 169 | (*env)->NewObject(e, stringWriterClass.get(), stringWriterCtor)); |
| 170 | if (stringWriter.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 171 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | jobject printWriter = |
| 175 | (*env)->NewObject(e, printWriterClass.get(), printWriterCtor, stringWriter.get()); |
| 176 | if (printWriter == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 177 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | scoped_local_ref<jclass> exceptionClass(env, (*env)->GetObjectClass(e, exception)); // can't fail |
| 181 | jmethodID printStackTraceMethod = |
| 182 | (*env)->GetMethodID(e, exceptionClass.get(), "printStackTrace", "(Ljava/io/PrintWriter;)V"); |
| 183 | (*env)->CallVoidMethod(e, exception, printStackTraceMethod, printWriter); |
| 184 | |
| 185 | if ((*env)->ExceptionCheck(e)) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 186 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | scoped_local_ref<jstring> messageStr(env, |
| 190 | (jstring) (*env)->CallObjectMethod(e, stringWriter.get(), stringWriterToStringMethod)); |
| 191 | if (messageStr.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 192 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | const char* utfChars = (*env)->GetStringUTFChars(e, messageStr.get(), NULL); |
| 196 | if (utfChars == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 197 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 200 | result = utfChars; |
| 201 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 202 | (*env)->ReleaseStringUTFChars(e, messageStr.get(), utfChars); |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 203 | return true; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | extern "C" int jniThrowException(C_JNIEnv* env, const char* className, const char* msg) { |
| 207 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 208 | |
| 209 | if ((*env)->ExceptionCheck(e)) { |
| 210 | /* TODO: consider creating the new exception with this as "cause" */ |
| 211 | scoped_local_ref<jthrowable> exception(env, (*env)->ExceptionOccurred(e)); |
| 212 | (*env)->ExceptionClear(e); |
| 213 | |
| 214 | if (exception.get() != NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 215 | std::string text; |
| 216 | getExceptionSummary(env, exception.get(), text); |
| 217 | ALOGW("Discarding pending exception (%s) to throw %s", text.c_str(), className); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | scoped_local_ref<jclass> exceptionClass(env, findClass(env, className)); |
| 222 | if (exceptionClass.get() == NULL) { |
Steve Block | 79a0d9c | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 223 | ALOGE("Unable to find exception class %s", className); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 224 | /* ClassNotFoundException now pending */ |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | if ((*env)->ThrowNew(e, exceptionClass.get(), msg) != JNI_OK) { |
Steve Block | 79a0d9c | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 229 | ALOGE("Failed throwing '%s' '%s'", className, msg); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 230 | /* an exception, most likely OOM, will now be pending */ |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args) { |
| 238 | char msgBuf[512]; |
| 239 | vsnprintf(msgBuf, sizeof(msgBuf), fmt, args); |
| 240 | return jniThrowException(env, className, msgBuf); |
| 241 | } |
| 242 | |
| 243 | int jniThrowNullPointerException(C_JNIEnv* env, const char* msg) { |
| 244 | return jniThrowException(env, "java/lang/NullPointerException", msg); |
| 245 | } |
| 246 | |
| 247 | int jniThrowRuntimeException(C_JNIEnv* env, const char* msg) { |
| 248 | return jniThrowException(env, "java/lang/RuntimeException", msg); |
| 249 | } |
| 250 | |
| 251 | int jniThrowIOException(C_JNIEnv* env, int errnum) { |
| 252 | char buffer[80]; |
| 253 | const char* message = jniStrError(errnum, buffer, sizeof(buffer)); |
| 254 | return jniThrowException(env, "java/io/IOException", message); |
| 255 | } |
| 256 | |
Elliott Hughes | a4e4e74 | 2013-09-03 17:47:18 -0700 | [diff] [blame] | 257 | static std::string jniGetStackTrace(C_JNIEnv* env, jthrowable exception) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 258 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 259 | |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 260 | scoped_local_ref<jthrowable> currentException(env, (*env)->ExceptionOccurred(e)); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 261 | if (exception == NULL) { |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 262 | exception = currentException.get(); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 263 | if (exception == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 264 | return "<no pending exception>"; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 265 | } |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 266 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 267 | |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 268 | if (currentException.get() != NULL) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 269 | (*env)->ExceptionClear(e); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 272 | std::string trace; |
| 273 | if (!getStackTrace(env, exception, trace)) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 274 | (*env)->ExceptionClear(e); |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 275 | getExceptionSummary(env, exception, trace); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 278 | if (currentException.get() != NULL) { |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 279 | (*env)->Throw(e, currentException.get()); // rethrow |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 280 | } |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 281 | |
| 282 | return trace; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Elliott Hughes | a4e4e74 | 2013-09-03 17:47:18 -0700 | [diff] [blame] | 285 | void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception) { |
| 286 | std::string trace(jniGetStackTrace(env, exception)); |
| 287 | __android_log_write(priority, tag, trace.c_str()); |
| 288 | } |
| 289 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 290 | const char* jniStrError(int errnum, char* buf, size_t buflen) { |
Elliott Hughes | d60512c | 2013-10-08 14:15:18 -0700 | [diff] [blame] | 291 | #if __GLIBC__ |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 292 | // Note: glibc has a nonstandard strerror_r that returns char* rather than POSIX's int. |
| 293 | // char *strerror_r(int errnum, char *buf, size_t n); |
Elliott Hughes | d60512c | 2013-10-08 14:15:18 -0700 | [diff] [blame] | 294 | return strerror_r(errnum, buf, buflen); |
| 295 | #else |
| 296 | int rc = strerror_r(errnum, buf, buflen); |
| 297 | if (rc != 0) { |
| 298 | // (POSIX only guarantees a value other than 0. The safest |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 299 | // way to implement this function is to use C++ and overload on the |
Elliott Hughes | d60512c | 2013-10-08 14:15:18 -0700 | [diff] [blame] | 300 | // type of strerror_r to accurately distinguish GNU from POSIX.) |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 301 | snprintf(buf, buflen, "errno %d", errnum); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 302 | } |
Elliott Hughes | d60512c | 2013-10-08 14:15:18 -0700 | [diff] [blame] | 303 | return buf; |
| 304 | #endif |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 307 | jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd) { |
| 308 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 309 | static jmethodID ctor = e->GetMethodID(JniConstants::fileDescriptorClass, "<init>", "()V"); |
| 310 | jobject fileDescriptor = (*env)->NewObject(e, JniConstants::fileDescriptorClass, ctor); |
Narayan Kamath | e29dbf5 | 2013-10-31 17:17:10 +0000 | [diff] [blame^] | 311 | // NOTE: NewObject ensures that an OutOfMemoryError will be seen by the Java |
| 312 | // caller if the alloc fails, so we just return NULL when that happens. |
| 313 | if (fileDescriptor != NULL) { |
| 314 | jniSetFileDescriptorOfFD(env, fileDescriptor, fd); |
| 315 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 316 | return fileDescriptor; |
| 317 | } |
| 318 | |
| 319 | int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor) { |
| 320 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 321 | static jfieldID fid = e->GetFieldID(JniConstants::fileDescriptorClass, "descriptor", "I"); |
Narayan Kamath | e29dbf5 | 2013-10-31 17:17:10 +0000 | [diff] [blame^] | 322 | if (fileDescriptor != NULL) { |
| 323 | return (*env)->GetIntField(e, fileDescriptor, fid); |
| 324 | } else { |
| 325 | return -1; |
| 326 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value) { |
| 330 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 331 | static jfieldID fid = e->GetFieldID(JniConstants::fileDescriptorClass, "descriptor", "I"); |
| 332 | (*env)->SetIntField(e, fileDescriptor, fid, value); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jeff Brown | dc176c5 | 2013-04-02 18:09:29 -0700 | [diff] [blame] | 335 | jobject jniGetReferent(C_JNIEnv* env, jobject ref) { |
| 336 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 337 | static jmethodID get = e->GetMethodID(JniConstants::referenceClass, "get", "()Ljava/lang/Object;"); |
| 338 | return (*env)->CallObjectMethod(e, ref, get); |
| 339 | } |
| 340 | |