xref: /aosp_15_r20/external/tflite-support/tensorflow_lite_support/cc/utils/jni_utils.h (revision b16991f985baa50654c05c5adbb3c8bbcfb40082)
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_LITE_SUPPORT_CC_UTILS_JNI_UTILS_H_
17 #define TENSORFLOW_LITE_SUPPORT_CC_UTILS_JNI_UTILS_H_
18 
19 #include <jni.h>
20 
21 #include <functional>
22 #include <string>
23 #include <vector>
24 
25 #include "absl/strings/string_view.h"
26 
27 namespace tflite {
28 namespace support {
29 namespace utils {
30 
31 const char kIllegalArgumentException[] = "java/lang/IllegalArgumentException";
32 const char kIllegalStateException[] = "java/lang/IllegalStateException";
33 const char kNullPointerException[] = "java/lang/NullPointerException";
34 const char kIndexOutOfBoundsException[] = "java/lang/IndexOutOfBoundsException";
35 const char kUnsupportedOperationException[] =
36     "java/lang/UnsupportedOperationException";
37 const char kAssertionError[] = "java/lang/AssertionError";
38 
39 constexpr int kInvalidPointer = 0;
40 
41 // Check if t is nullptr, throw IllegalStateException if it is.
42 // Used to verify different types of jobjects are correctly created from jni.
43 template <typename T>
CheckNotNull(JNIEnv * env,T && t)44 T CheckNotNull(JNIEnv* env, T&& t) {
45   if (t == nullptr) {
46     env->ThrowNew(env->FindClass(kIllegalStateException), "");
47     return nullptr;
48   }
49   return std::forward<T>(t);
50 }
51 
52 // Converts a std::vector<T> into a Java ArrayList using a converter, which
53 // processes a single element in the vector before adding it to the ArrayList.
54 template <typename T>
ConvertVectorToArrayList(JNIEnv * env,const std::vector<T> & results,std::function<jobject (T)> converter)55 jobject ConvertVectorToArrayList(JNIEnv* env, const std::vector<T>& results,
56                                  std::function<jobject(T)> converter) {
57   jclass array_list_class = env->FindClass("java/util/ArrayList");
58   jmethodID array_list_ctor =
59       env->GetMethodID(array_list_class, "<init>", "(I)V");
60   jint initial_capacity = static_cast<jint>(results.size());
61   jobject array_list_object =
62       env->NewObject(array_list_class, array_list_ctor, initial_capacity);
63   jmethodID array_list_add_method =
64       env->GetMethodID(array_list_class, "add", "(Ljava/lang/Object;)Z");
65 
66   for (const auto& ans : results) {
67     env->CallBooleanMethod(array_list_object, array_list_add_method,
68                            converter(ans));
69   }
70   return array_list_object;
71 }
72 
73 std::string JStringToString(JNIEnv* env, jstring jstr);
74 
75 std::vector<std::string> StringListToVector(JNIEnv* env, jobject list_object);
76 
77 // Gets a mapped file buffer from a java object representing a file.
78 absl::string_view GetMappedFileBuffer(JNIEnv* env, const jobject& file_buffer);
79 
80 // Creates a Java byte array object based on the input data.
81 jbyteArray CreateByteArray(JNIEnv* env, const jbyte* data, int num_bytes);
82 
83 void ThrowException(JNIEnv* env, const char* clazz, const char* fmt, ...);
84 
85 void ThrowExceptionWithMessage(JNIEnv* env, const char* clazz,
86                                const char* message);
87 
88 }  // namespace utils
89 }  // namespace support
90 }  // namespace tflite
91 #endif  // TENSORFLOW_LITE_SUPPORT_CC_UTILS_JNI_UTILS_H_
92