Back to home page

Android Cross Reference

 
 

    


0001 /*
0002  * Copyright (C) 2007 The Android Open Source Project
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *      http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  */
0016 
0017 /*
0018  * JNI helper functions.
0019  *
0020  * This file may be included by C or C++ code, which is trouble because jni.h
0021  * uses different typedefs for JNIEnv in each language.
0022  */
0023 #ifndef NATIVEHELPER_JNIHELP_H_
0024 #define NATIVEHELPER_JNIHELP_H_
0025 
0026 #include "jni.h"
0027 #include "cutils/log.h"
0028 #include <unistd.h>
0029 
0030 #ifndef NELEM
0031 # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
0032 #endif
0033 
0034 #ifdef __cplusplus
0035 extern "C" {
0036 #endif
0037 
0038 /*
0039  * Register one or more native methods with a particular class.
0040  * "className" looks like "java/lang/String". Aborts on failure.
0041  * TODO: fix all callers and change the return type to void.
0042  */
0043 int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods);
0044 
0045 /*
0046  * Throw an exception with the specified class and an optional message.
0047  *
0048  * The "className" argument will be passed directly to FindClass, which
0049  * takes strings with slashes (e.g. "java/lang/Object").
0050  *
0051  * If an exception is currently pending, we log a warning message and
0052  * clear it.
0053  *
0054  * Returns 0 on success, nonzero if something failed (e.g. the exception
0055  * class couldn't be found, so *an* exception will still be pending).
0056  *
0057  * Currently aborts the VM if it can't throw the exception.
0058  */
0059 int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
0060 
0061 /*
0062  * Throw a java.lang.NullPointerException, with an optional message.
0063  */
0064 int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
0065 
0066 /*
0067  * Throw a java.lang.RuntimeException, with an optional message.
0068  */
0069 int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
0070 
0071 /*
0072  * Throw a java.io.IOException, generating the message from errno.
0073  */
0074 int jniThrowIOException(C_JNIEnv* env, int errnum);
0075 
0076 /*
0077  * Return a pointer to a locale-dependent error string explaining errno
0078  * value 'errnum'. The returned pointer may or may not be equal to 'buf'.
0079  * This function is thread-safe (unlike strerror) and portable (unlike
0080  * strerror_r).
0081  */
0082 const char* jniStrError(int errnum, char* buf, size_t buflen);
0083 
0084 /*
0085  * Returns a new java.io.FileDescriptor for the given int fd.
0086  */
0087 jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
0088 
0089 /*
0090  * Returns the int fd from a java.io.FileDescriptor.
0091  */
0092 int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
0093 
0094 /*
0095  * Sets the int fd in a java.io.FileDescriptor.
0096  */
0097 void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
0098 
0099 /*
0100  * Log a message and an exception.
0101  * If exception is NULL, logs the current exception in the JNI environment.
0102  */
0103 void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
0104 
0105 #ifdef __cplusplus
0106 }
0107 #endif
0108 
0109 
0110 /*
0111  * For C++ code, we provide inlines that map to the C functions.  g++ always
0112  * inlines these, even on non-optimized builds.
0113  */
0114 #if defined(__cplusplus)
0115 inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
0116     return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods);
0117 }
0118 
0119 inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) {
0120     return jniThrowException(&env->functions, className, msg);
0121 }
0122 
0123 extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
0124 
0125 /*
0126  * Equivalent to jniThrowException but with a printf-like format string and
0127  * variable-length argument list. This is only available in C++.
0128  */
0129 inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) {
0130     va_list args;
0131     va_start(args, fmt);
0132     return jniThrowExceptionFmt(&env->functions, className, fmt, args);
0133     va_end(args);
0134 }
0135 
0136 inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) {
0137     return jniThrowNullPointerException(&env->functions, msg);
0138 }
0139 
0140 inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) {
0141     return jniThrowRuntimeException(&env->functions, msg);
0142 }
0143 
0144 inline int jniThrowIOException(JNIEnv* env, int errnum) {
0145     return jniThrowIOException(&env->functions, errnum);
0146 }
0147 
0148 inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
0149     return jniCreateFileDescriptor(&env->functions, fd);
0150 }
0151 
0152 inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
0153     return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
0154 }
0155 
0156 inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
0157     jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
0158 }
0159 
0160 inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) {
0161     jniLogException(&env->functions, priority, tag, exception);
0162 }
0163 #endif
0164 
0165 /* Logging macros.
0166  *
0167  * Logs an exception.  If the exception is omitted or NULL, logs the current exception
0168  * from the JNI environment, if any.
0169  */
0170 #define LOG_EX(env, priority, tag, ...) \
0171     IF_ALOG(priority, tag) jniLogException(env, ANDROID_##priority, tag, ##__VA_ARGS__)
0172 #define LOGV_EX(env, ...) LOG_EX(env, LOG_VERBOSE, LOG_TAG, ##__VA_ARGS__)
0173 #define LOGD_EX(env, ...) LOG_EX(env, LOG_DEBUG, LOG_TAG, ##__VA_ARGS__)
0174 #define LOGI_EX(env, ...) LOG_EX(env, LOG_INFO, LOG_TAG, ##__VA_ARGS__)
0175 #define LOGW_EX(env, ...) LOG_EX(env, LOG_WARN, LOG_TAG, ##__VA_ARGS__)
0176 #define LOGE_EX(env, ...) LOG_EX(env, LOG_ERROR, LOG_TAG, ##__VA_ARGS__)
0177 
0178 /*
0179  * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
0180  * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
0181  * not already defined, then define it here.
0182  */
0183 #ifndef TEMP_FAILURE_RETRY
0184 /* Used to retry syscalls that can return EINTR. */
0185 #define TEMP_FAILURE_RETRY(exp) ({         \
0186     typeof (exp) _rc;                      \
0187     do {                                   \
0188         _rc = (exp);                       \
0189     } while (_rc == -1 && errno == EINTR); \
0190     _rc; })
0191 #endif
0192 
0193 #endif  /* NATIVEHELPER_JNIHELP_H_ */