Skip to content

Commit 2ad6aa9

Browse files
jdduketensorflower-gardener
authored andcommitted
Properly namespace JNI exception strings
This avoids collisions for clients who use both TF and TFLite JNI libraries PiperOrigin-RevId: 343765942 Change-Id: Ibdc17b95bdaebac88fe23b1482cba7c8b611db11
1 parent 9367ca6 commit 2ad6aa9

File tree

4 files changed

+46
-47
lines changed

4 files changed

+46
-47
lines changed

tensorflow/lite/java/src/main/native/jni_utils.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ limitations under the License.
1919
#include <stdio.h>
2020
#include <stdlib.h>
2121

22+
namespace tflite {
23+
namespace jni {
24+
2225
const char kIllegalArgumentException[] = "java/lang/IllegalArgumentException";
2326
const char kIllegalStateException[] = "java/lang/IllegalStateException";
2427
const char kNullPointerException[] = "java/lang/NullPointerException";
25-
const char kIndexOutOfBoundsException[] = "java/lang/IndexOutOfBoundsException";
2628
const char kUnsupportedOperationException[] =
2729
"java/lang/UnsupportedOperationException";
2830

29-
namespace tflite {
30-
namespace jni {
31-
3231
void ThrowException(JNIEnv* env, const char* clazz, const char* fmt, ...) {
3332
va_list args;
3433
va_start(args, fmt);

tensorflow/lite/java/src/main/native/jni_utils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ limitations under the License.
2020

2121
#include "tensorflow/lite/error_reporter.h"
2222

23+
namespace tflite {
24+
namespace jni {
25+
2326
extern const char kIllegalArgumentException[];
2427
extern const char kIllegalStateException[];
2528
extern const char kNullPointerException[];
26-
extern const char kIndexOutOfBoundsException[];
2729
extern const char kUnsupportedOperationException[];
2830

29-
namespace tflite {
30-
namespace jni {
31-
3231
void ThrowException(JNIEnv* env, const char* clazz, const char* fmt, ...);
3332

3433
class BufferErrorReporter : public ErrorReporter {

tensorflow/lite/java/src/main/native/nativeinterpreterwrapper_jni.cc

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace {
4040
tflite_api_dispatcher::Interpreter* convertLongToInterpreter(JNIEnv* env,
4141
jlong handle) {
4242
if (handle == 0) {
43-
ThrowException(env, kIllegalArgumentException,
43+
ThrowException(env, tflite::jni::kIllegalArgumentException,
4444
"Internal error: Invalid handle to Interpreter.");
4545
return nullptr;
4646
}
@@ -50,7 +50,7 @@ tflite_api_dispatcher::Interpreter* convertLongToInterpreter(JNIEnv* env,
5050
tflite_api_dispatcher::TfLiteModel* convertLongToModel(JNIEnv* env,
5151
jlong handle) {
5252
if (handle == 0) {
53-
ThrowException(env, kIllegalArgumentException,
53+
ThrowException(env, tflite::jni::kIllegalArgumentException,
5454
"Internal error: Invalid handle to model.");
5555
return nullptr;
5656
}
@@ -59,7 +59,7 @@ tflite_api_dispatcher::TfLiteModel* convertLongToModel(JNIEnv* env,
5959

6060
BufferErrorReporter* convertLongToErrorReporter(JNIEnv* env, jlong handle) {
6161
if (handle == 0) {
62-
ThrowException(env, kIllegalArgumentException,
62+
ThrowException(env, tflite::jni::kIllegalArgumentException,
6363
"Internal error: Invalid handle to ErrorReporter.");
6464
return nullptr;
6565
}
@@ -68,7 +68,7 @@ BufferErrorReporter* convertLongToErrorReporter(JNIEnv* env, jlong handle) {
6868

6969
TfLiteDelegate* convertLongToDelegate(JNIEnv* env, jlong handle) {
7070
if (handle == 0) {
71-
ThrowException(env, kIllegalArgumentException,
71+
ThrowException(env, tflite::jni::kIllegalArgumentException,
7272
"Internal error: Invalid handle to delegate.");
7373
return nullptr;
7474
}
@@ -80,7 +80,7 @@ std::vector<int> convertJIntArrayToVector(JNIEnv* env, jintArray inputs) {
8080
std::vector<int> outputs(size, 0);
8181
jint* ptr = env->GetIntArrayElements(inputs, nullptr);
8282
if (ptr == nullptr) {
83-
ThrowException(env, kIllegalArgumentException,
83+
ThrowException(env, tflite::jni::kIllegalArgumentException,
8484
"Array has empty dimensions.");
8585
return {};
8686
}
@@ -130,7 +130,7 @@ bool AreDimsDifferent(JNIEnv* env, TfLiteTensor* tensor, jintArray dims) {
130130
int num_dims = static_cast<int>(env->GetArrayLength(dims));
131131
jint* ptr = env->GetIntArrayElements(dims, nullptr);
132132
if (ptr == nullptr) {
133-
ThrowException(env, kIllegalArgumentException,
133+
ThrowException(env, tflite::jni::kIllegalArgumentException,
134134
"Empty dimensions of input array.");
135135
return true;
136136
}
@@ -170,7 +170,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_getInputNames(JNIEnv* env,
170170
if (interpreter == nullptr) return nullptr;
171171
jclass string_class = env->FindClass("java/lang/String");
172172
if (string_class == nullptr) {
173-
ThrowException(env, kUnsupportedOperationException,
173+
ThrowException(env, tflite::jni::kUnsupportedOperationException,
174174
"Internal error: Can not find java/lang/String class to get "
175175
"input names.");
176176
return nullptr;
@@ -197,7 +197,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_allocateTensors(
197197

198198
if (interpreter->AllocateTensors() != kTfLiteOk) {
199199
ThrowException(
200-
env, kIllegalStateException,
200+
env, tflite::jni::kIllegalStateException,
201201
"Internal error: Unexpected failure when preparing tensor allocations:"
202202
" %s",
203203
error_reporter->CachedErrorMessage());
@@ -284,7 +284,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_getOutputNames(JNIEnv* env,
284284
if (interpreter == nullptr) return nullptr;
285285
jclass string_class = env->FindClass("java/lang/String");
286286
if (string_class == nullptr) {
287-
ThrowException(env, kUnsupportedOperationException,
287+
ThrowException(env, tflite::jni::kUnsupportedOperationException,
288288
"Internal error: Can not find java/lang/String class to get "
289289
"output names.");
290290
return nullptr;
@@ -365,12 +365,12 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_useXNNPACK(
365365
// TODO(b/166483905): Add support for multiple delegates when model allows.
366366
if (delegation_status != kTfLiteOk &&
367367
delegation_status != kTfLiteApplicationError) {
368-
ThrowException(env, kIllegalArgumentException,
368+
ThrowException(env, tflite::jni::kIllegalArgumentException,
369369
"Internal error: Failed to apply XNNPACK delegate: %s",
370370
error_reporter->CachedErrorMessage());
371371
}
372372
} else {
373-
ThrowException(env, kIllegalArgumentException,
373+
ThrowException(env, tflite::jni::kIllegalArgumentException,
374374
"Failed to load XNNPACK delegate from current runtime. "
375375
"Have you added the necessary dependencies?");
376376
}
@@ -422,7 +422,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_createModel(
422422
auto model = tflite_api_dispatcher::TfLiteModel::VerifyAndBuildFromFile(
423423
path, verifier.get(), error_reporter);
424424
if (!model) {
425-
ThrowException(env, kIllegalArgumentException,
425+
ThrowException(env, tflite::jni::kIllegalArgumentException,
426426
"Contents of %s does not encode a valid "
427427
"TensorFlow Lite model: %s",
428428
path, error_reporter->CachedErrorMessage());
@@ -443,15 +443,15 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_createModelWithBuffer(
443443
static_cast<char*>(env->GetDirectBufferAddress(model_buffer));
444444
jlong capacity = env->GetDirectBufferCapacity(model_buffer);
445445
if (!VerifyModel(buf, capacity)) {
446-
ThrowException(env, kIllegalArgumentException,
446+
ThrowException(env, tflite::jni::kIllegalArgumentException,
447447
"ByteBuffer is not a valid flatbuffer model");
448448
return 0;
449449
}
450450

451451
auto model = tflite_api_dispatcher::TfLiteModel::BuildFromBuffer(
452452
buf, static_cast<size_t>(capacity), error_reporter);
453453
if (!model) {
454-
ThrowException(env, kIllegalArgumentException,
454+
ThrowException(env, tflite::jni::kIllegalArgumentException,
455455
"ByteBuffer does not encode a valid model: %s",
456456
error_reporter->CachedErrorMessage());
457457
return 0;
@@ -474,7 +474,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_createInterpreter(
474474
TfLiteStatus status = tflite_api_dispatcher::InterpreterBuilder(
475475
*model, *(resolver.get()))(&interpreter, static_cast<int>(num_threads));
476476
if (status != kTfLiteOk) {
477-
ThrowException(env, kIllegalArgumentException,
477+
ThrowException(env, tflite::jni::kIllegalArgumentException,
478478
"Internal error: Cannot create interpreter: %s",
479479
error_reporter->CachedErrorMessage());
480480
return 0;
@@ -496,7 +496,7 @@ JNIEXPORT void JNICALL Java_org_tensorflow_lite_NativeInterpreterWrapper_run(
496496

497497
if (interpreter->Invoke() != kTfLiteOk) {
498498
// TODO(b/168266570): Return InterruptedException.
499-
ThrowException(env, kIllegalArgumentException,
499+
ThrowException(env, tflite::jni::kIllegalArgumentException,
500500
"Internal error: Failed to run on the given Interpreter: %s",
501501
error_reporter->CachedErrorMessage());
502502
return;
@@ -511,7 +511,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_getOutputDataType(
511511
if (interpreter == nullptr) return -1;
512512
const int idx = static_cast<int>(output_idx);
513513
if (output_idx < 0 || output_idx >= interpreter->outputs().size()) {
514-
ThrowException(env, kIllegalArgumentException,
514+
ThrowException(env, tflite::jni::kIllegalArgumentException,
515515
"Failed to get %d-th output out of %d outputs", output_idx,
516516
interpreter->outputs().size());
517517
return -1;
@@ -532,7 +532,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_resizeInput(
532532
convertLongToInterpreter(env, interpreter_handle);
533533
if (interpreter == nullptr) return JNI_FALSE;
534534
if (input_idx < 0 || input_idx >= interpreter->inputs().size()) {
535-
ThrowException(env, kIllegalArgumentException,
535+
ThrowException(env, tflite::jni::kIllegalArgumentException,
536536
"Input error: Can not resize %d-th input for a model having "
537537
"%d inputs.",
538538
input_idx, interpreter->inputs().size());
@@ -552,7 +552,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_resizeInput(
552552
tensor_idx, convertJIntArrayToVector(env, dims));
553553
}
554554
if (status != kTfLiteOk) {
555-
ThrowException(env, kIllegalArgumentException,
555+
ThrowException(env, tflite::jni::kIllegalArgumentException,
556556
"Internal error: Failed to resize %d-th input: %s",
557557
input_idx, error_reporter->CachedErrorMessage());
558558
return JNI_FALSE;
@@ -578,7 +578,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_applyDelegate(
578578

579579
TfLiteStatus status = interpreter->ModifyGraphWithDelegate(delegate);
580580
if (status != kTfLiteOk) {
581-
ThrowException(env, kIllegalArgumentException,
581+
ThrowException(env, tflite::jni::kIllegalArgumentException,
582582
"Internal error: Failed to apply delegate: %s",
583583
error_reporter->CachedErrorMessage());
584584
}
@@ -597,7 +597,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_resetVariableTensors(
597597

598598
TfLiteStatus status = interpreter->ResetVariableTensors();
599599
if (status != kTfLiteOk) {
600-
ThrowException(env, kIllegalArgumentException,
600+
ThrowException(env, tflite::jni::kIllegalArgumentException,
601601
"Internal error: Failed to reset variable tensors: %s",
602602
error_reporter->CachedErrorMessage());
603603
}
@@ -609,7 +609,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_createCancellationFlag(
609609
tflite_api_dispatcher::Interpreter* interpreter =
610610
convertLongToInterpreter(env, interpreter_handle);
611611
if (interpreter == nullptr) {
612-
ThrowException(env, kIllegalArgumentException,
612+
ThrowException(env, tflite::jni::kIllegalArgumentException,
613613
"Internal error: Invalid handle to interpreter.");
614614
}
615615
std::atomic_bool* cancellation_flag = new std::atomic_bool(false);

tensorflow/lite/java/src/main/native/tensor_jni.cc

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class TensorHandle {
5353

5454
TfLiteTensor* GetTensorFromHandle(JNIEnv* env, jlong handle) {
5555
if (handle == 0) {
56-
ThrowException(env, kIllegalArgumentException,
56+
ThrowException(env, tflite::jni::kIllegalArgumentException,
5757
"Internal error: Invalid handle to TfLiteTensor.");
5858
return nullptr;
5959
}
@@ -62,7 +62,7 @@ TfLiteTensor* GetTensorFromHandle(JNIEnv* env, jlong handle) {
6262

6363
int GetTensorIndexFromHandle(JNIEnv* env, jlong handle) {
6464
if (handle == 0) {
65-
ThrowException(env, kIllegalArgumentException,
65+
ThrowException(env, tflite::jni::kIllegalArgumentException,
6666
"Internal error: Invalid handle to TfLiteTensor.");
6767
return -1;
6868
}
@@ -110,7 +110,7 @@ size_t WriteOneDimensionalArray(JNIEnv* env, jobject object, TfLiteType type,
110110
const int num_elements = env->GetArrayLength(array);
111111
size_t to_copy = num_elements * ElementByteSize(type);
112112
if (to_copy > dst_size) {
113-
ThrowException(env, kIllegalStateException,
113+
ThrowException(env, tflite::jni::kIllegalStateException,
114114
"Internal error: cannot write Java array of %d bytes to "
115115
"Tensor of %d bytes",
116116
to_copy, dst_size);
@@ -150,7 +150,7 @@ size_t WriteOneDimensionalArray(JNIEnv* env, jobject object, TfLiteType type,
150150
}
151151
default: {
152152
ThrowException(
153-
env, kUnsupportedOperationException,
153+
env, tflite::jni::kUnsupportedOperationException,
154154
"DataType error: TensorFlowLite currently supports float "
155155
"(32 bits), int (32 bits), byte (8 bits), bool (8 bits), and long "
156156
"(64 bits), support for other types (DataType %d in this "
@@ -167,7 +167,7 @@ size_t ReadOneDimensionalArray(JNIEnv* env, TfLiteType data_type,
167167
const size_t size = len * ElementByteSize(data_type);
168168
if (size > src_size) {
169169
ThrowException(
170-
env, kIllegalStateException,
170+
env, tflite::jni::kIllegalStateException,
171171
"Internal error: cannot fill a Java array of %d bytes with a Tensor of "
172172
"%d bytes",
173173
size, src_size);
@@ -205,7 +205,7 @@ size_t ReadOneDimensionalArray(JNIEnv* env, TfLiteType data_type,
205205
return size;
206206
}
207207
default: {
208-
ThrowException(env, kIllegalStateException,
208+
ThrowException(env, tflite::jni::kIllegalStateException,
209209
"DataType error: invalid DataType(%d)", data_type);
210210
}
211211
}
@@ -345,7 +345,7 @@ void WriteScalar(JNIEnv* env, jobject src, TfLiteType type, void* dst,
345345
size_t src_size = ElementByteSize(type);
346346
if (src_size != dst_size) {
347347
ThrowException(
348-
env, kIllegalStateException,
348+
env, tflite::jni::kIllegalStateException,
349349
"Scalar (%d bytes) not compatible with allocated tensor (%d bytes)",
350350
src_size, dst_size);
351351
return;
@@ -377,7 +377,8 @@ void WriteScalar(JNIEnv* env, jobject src, TfLiteType type, void* dst,
377377
return;
378378
}
379379
default:
380-
ThrowException(env, kIllegalStateException, "Invalid DataType(%d)", type);
380+
ThrowException(env, tflite::jni::kIllegalStateException,
381+
"Invalid DataType(%d)", type);
381382
return;
382383
}
383384
}
@@ -415,7 +416,7 @@ JNIEXPORT jobject JNICALL Java_org_tensorflow_lite_Tensor_buffer(JNIEnv* env,
415416
TfLiteTensor* tensor = GetTensorFromHandle(env, handle);
416417
if (tensor == nullptr) return nullptr;
417418
if (tensor->data.raw == nullptr) {
418-
ThrowException(env, kIllegalArgumentException,
419+
ThrowException(env, tflite::jni::kIllegalArgumentException,
419420
"Internal error: Tensor hasn't been allocated.");
420421
return nullptr;
421422
}
@@ -430,13 +431,13 @@ JNIEXPORT void JNICALL Java_org_tensorflow_lite_Tensor_writeDirectBuffer(
430431

431432
void* src_data_raw = env->GetDirectBufferAddress(src);
432433
if (!src_data_raw) {
433-
ThrowException(env, kIllegalArgumentException,
434+
ThrowException(env, tflite::jni::kIllegalArgumentException,
434435
"Input ByteBuffer is not a direct buffer");
435436
return;
436437
}
437438

438439
if (!tensor->data.data) {
439-
ThrowException(env, kIllegalArgumentException,
440+
ThrowException(env, tflite::jni::kIllegalArgumentException,
440441
"Internal error: Tensor hasn't been allocated.");
441442
return;
442443
}
@@ -459,7 +460,7 @@ Java_org_tensorflow_lite_Tensor_readMultiDimensionalArray(JNIEnv* env,
459460
if (tensor == nullptr) return;
460461
int num_dims = tensor->dims->size;
461462
if (num_dims == 0) {
462-
ThrowException(env, kIllegalArgumentException,
463+
ThrowException(env, tflite::jni::kIllegalArgumentException,
463464
"Internal error: Cannot copy empty/scalar Tensors.");
464465
return;
465466
}
@@ -481,12 +482,12 @@ Java_org_tensorflow_lite_Tensor_writeMultiDimensionalArray(JNIEnv* env,
481482
TfLiteTensor* tensor = GetTensorFromHandle(env, handle);
482483
if (tensor == nullptr) return;
483484
if (tensor->type != kTfLiteString && tensor->data.raw == nullptr) {
484-
ThrowException(env, kIllegalArgumentException,
485+
ThrowException(env, tflite::jni::kIllegalArgumentException,
485486
"Internal error: Target Tensor hasn't been allocated.");
486487
return;
487488
}
488489
if (tensor->dims->size == 0) {
489-
ThrowException(env, kIllegalArgumentException,
490+
ThrowException(env, tflite::jni::kIllegalArgumentException,
490491
"Internal error: Cannot copy empty/scalar Tensors.");
491492
return;
492493
}
@@ -503,12 +504,12 @@ JNIEXPORT void JNICALL Java_org_tensorflow_lite_Tensor_writeScalar(
503504
TfLiteTensor* tensor = GetTensorFromHandle(env, handle);
504505
if (tensor == nullptr) return;
505506
if ((tensor->type != kTfLiteString) && (tensor->data.raw == nullptr)) {
506-
ThrowException(env, kIllegalArgumentException,
507+
ThrowException(env, tflite::jni::kIllegalArgumentException,
507508
"Internal error: Target Tensor hasn't been allocated.");
508509
return;
509510
}
510511
if ((tensor->dims->size != 0) && (tensor->dims->data[0] != 1)) {
511-
ThrowException(env, kIllegalArgumentException,
512+
ThrowException(env, tflite::jni::kIllegalArgumentException,
512513
"Internal error: Cannot write Java scalar to non-scalar "
513514
"Tensor.");
514515
return;
@@ -533,7 +534,7 @@ JNIEXPORT jstring JNICALL Java_org_tensorflow_lite_Tensor_name(JNIEnv* env,
533534
jlong handle) {
534535
TfLiteTensor* tensor = GetTensorFromHandle(env, handle);
535536
if (tensor == nullptr) {
536-
ThrowException(env, kIllegalArgumentException,
537+
ThrowException(env, tflite::jni::kIllegalArgumentException,
537538
"Target Tensor doesn't exist.");
538539
return nullptr;
539540
}

0 commit comments

Comments
 (0)