2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * JNI helper functions.
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.
23 #ifndef _NATIVEHELPER_JNIHELP_H
24 #define _NATIVEHELPER_JNIHELP_H
27 #include "utils/Log.h"
30 # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
38 * Register one or more native methods with a particular class.
40 int jniRegisterNativeMethods(C_JNIEnv
* env
, const char* className
,
41 const JNINativeMethod
* gMethods
, int numMethods
);
44 * Throw an exception with the specified class and an optional message.
45 * The "className" argument will be passed directly to FindClass, which
46 * takes strings with slashes (e.g. "java/lang/Object").
48 * Returns 0 on success, nonzero if something failed (e.g. the exception
49 * class couldn't be found).
51 * Currently aborts the VM if it can't throw the exception.
53 int jniThrowException(C_JNIEnv
* env
, const char* className
, const char* msg
);
56 * Throw a java.lang.RuntimeException, with an optional message.
58 int jniThrowRuntimeException(JNIEnv
* env
, const char* msg
);
61 * Throw a java.io.IOException, generating the message from errno.
63 int jniThrowIOException(C_JNIEnv
* env
, int errnum
);
66 * Create a java.io.FileDescriptor given an integer fd
68 jobject
jniCreateFileDescriptor(C_JNIEnv
* env
, int fd
);
71 * Get an int file descriptor from a java.io.FileDescriptor
73 int jniGetFDFromFileDescriptor(C_JNIEnv
* env
, jobject fileDescriptor
);
76 * Set an int file descriptor to a java.io.FileDescriptor
78 void jniSetFileDescriptorOfFD(C_JNIEnv
* env
, jobject fileDescriptor
, int value
);
86 * For C++ code, we provide inlines that map to the C functions. g++ always
87 * inlines these, even on non-optimized builds.
89 #if defined(__cplusplus) && !defined(JNI_FORCE_C)
90 inline int jniRegisterNativeMethods(JNIEnv
* env
, const char* className
,
91 const JNINativeMethod
* gMethods
, int numMethods
)
93 return jniRegisterNativeMethods(&env
->functions
, className
, gMethods
,
96 inline int jniThrowException(JNIEnv
* env
, const char* className
,
99 return jniThrowException(&env
->functions
, className
, msg
);
101 inline int jniThrowIOException(JNIEnv
* env
, int errnum
)
103 return jniThrowIOException(&env
->functions
, errnum
);
105 inline jobject
jniCreateFileDescriptor(JNIEnv
* env
, int fd
)
107 return jniCreateFileDescriptor(&env
->functions
, fd
);
109 inline int jniGetFDFromFileDescriptor(JNIEnv
* env
, jobject fileDescriptor
)
111 return jniGetFDFromFileDescriptor(&env
->functions
, fileDescriptor
);
113 inline void jniSetFileDescriptorOfFD(JNIEnv
* env
, jobject fileDescriptor
,
116 return jniSetFileDescriptorOfFD(&env
->functions
, fileDescriptor
, value
);
120 #endif /*_NATIVEHELPER_JNIHELP_H*/