1 /*
2 * Copyright (C) 2018 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 #ifndef LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_BASE_H_
18 #define LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_BASE_H_
19
20 #include <jni.h>
21 #include <string>
22
23 // When we use a macro as an argument for a macro, an additional level of
24 // indirection is needed, if the macro argument is used with # or ##.
25 #define TC3_ADD_QUOTES_HELPER(TOKEN) #TOKEN
26 #define TC3_ADD_QUOTES(TOKEN) TC3_ADD_QUOTES_HELPER(TOKEN)
27
28 #ifndef TC3_PACKAGE_NAME
29 #define TC3_PACKAGE_NAME com_google_android_textclassifier
30 #endif
31
32 #ifndef TC3_PACKAGE_PATH
33 #define TC3_PACKAGE_PATH \
34 "com/google/android/textclassifier/"
35 #endif
36
37 #define TC3_JNI_METHOD_NAME_INTERNAL(package_name, class_name, method_name) \
38 Java_##package_name##_##class_name##_##method_name
39
40 #define TC3_JNI_METHOD_PRIMITIVE(return_type, package_name, class_name, \
41 method_name) \
42 JNIEXPORT return_type JNICALL TC3_JNI_METHOD_NAME_INTERNAL( \
43 package_name, class_name, method_name)
44
45 // The indirection is needed to correctly expand the TC3_PACKAGE_NAME macro.
46 // See the explanation near TC3_ADD_QUOTES macro.
47 #define TC3_JNI_METHOD2(return_type, package_name, class_name, method_name) \
48 TC3_JNI_METHOD_PRIMITIVE(return_type, package_name, class_name, method_name)
49
50 #define TC3_JNI_METHOD(return_type, class_name, method_name) \
51 TC3_JNI_METHOD2(return_type, TC3_PACKAGE_NAME, class_name, method_name)
52
53 #define TC3_JNI_METHOD_NAME2(package_name, class_name, method_name) \
54 TC3_JNI_METHOD_NAME_INTERNAL(package_name, class_name, method_name)
55
56 #define TC3_JNI_METHOD_NAME(class_name, method_name) \
57 TC3_JNI_METHOD_NAME2(TC3_PACKAGE_NAME, class_name, method_name)
58
59 namespace libtextclassifier3 {
60
61 template <typename T, typename F>
CallJniMethod0(JNIEnv * env,jobject object,jclass class_object,F function,const std::string & method_name,const std::string & return_java_type)62 std::pair<bool, T> CallJniMethod0(JNIEnv* env, jobject object,
63 jclass class_object, F function,
64 const std::string& method_name,
65 const std::string& return_java_type) {
66 const jmethodID method = env->GetMethodID(class_object, method_name.c_str(),
67 ("()" + return_java_type).c_str());
68 if (!method) {
69 return std::make_pair(false, T());
70 }
71 return std::make_pair(true, (env->*function)(object, method));
72 }
73
74 std::string ToStlString(JNIEnv* env, const jstring& str);
75
76 // Get system-level file descriptor from AssetFileDescriptor.
77 jint GetFdFromAssetFileDescriptor(JNIEnv* env, jobject afd);
78
79 // Get system-level file descriptor from FileDescriptor.
80 jint GetFdFromFileDescriptor(JNIEnv* env, jobject fd);
81 } // namespace libtextclassifier3
82
83 #endif // LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_BASE_H_
84