-
Notifications
You must be signed in to change notification settings - Fork 0
/
OJNIEnv.m
executable file
·661 lines (483 loc) · 21.9 KB
/
OJNIEnv.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
* Copyright 2016 Alexander Shitikov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "OJNIEnv.h"
#import "OJNIJavaException.h"
#import "OJNIEnvironmentException.h"
#import "OJNIJavaObject.h"
@interface OJNIEnv () {
JNIEnv *env;
}
@end
@implementation OJNIEnv
- (instancetype)initWithJNIEnv:(JNIEnv *)jniEnv {
self = [super init];
if (self) {
env = jniEnv;
[[OJNIJavaVM sharedVM] associatePointer:env withObject:self];
}
return self;
}
- (JNIEnv *)jniEnv {
return env;
}
+ (instancetype)currentEnv {
return [[OJNIJavaVM sharedVM] getEnv];
}
- (void)releaseObject:(jobject)obj {
(*env)->DeleteLocalRef(env, obj);
(*env)->DeleteGlobalRef(env, obj);
}
CALL_METHOD_IMPLEMENTATION(jboolean, Boolean)
CALL_METHOD_IMPLEMENTATION(jbyte, Byte)
CALL_METHOD_IMPLEMENTATION(jchar, Char)
CALL_METHOD_IMPLEMENTATION(jshort, Short)
CALL_METHOD_IMPLEMENTATION(jint, Int)
CALL_METHOD_IMPLEMENTATION(jlong, Long)
CALL_METHOD_IMPLEMENTATION(jfloat, Float)
CALL_METHOD_IMPLEMENTATION(jdouble, Double)
CALL_STATIC_METHOD_IMPLEMENTATION(jboolean, Boolean)
CALL_STATIC_METHOD_IMPLEMENTATION(jbyte, Byte)
CALL_STATIC_METHOD_IMPLEMENTATION(jchar, Char)
CALL_STATIC_METHOD_IMPLEMENTATION(jshort, Short)
CALL_STATIC_METHOD_IMPLEMENTATION(jint, Int)
CALL_STATIC_METHOD_IMPLEMENTATION(jlong, Long)
CALL_STATIC_METHOD_IMPLEMENTATION(jfloat, Float)
CALL_STATIC_METHOD_IMPLEMENTATION(jdouble, Double)
//GET_FIELD_IMPLEMENTATION(jobject, Object)
GET_FIELD_IMPLEMENTATION(jboolean, Boolean)
GET_FIELD_IMPLEMENTATION(jbyte, Byte)
GET_FIELD_IMPLEMENTATION(jchar, Char)
GET_FIELD_IMPLEMENTATION(jshort, Short)
GET_FIELD_IMPLEMENTATION(jint, Int)
GET_FIELD_IMPLEMENTATION(jlong, Long)
GET_FIELD_IMPLEMENTATION(jfloat, Float)
GET_FIELD_IMPLEMENTATION(jdouble, Double)
SET_FIELD_IMPLEMENTATION(jobject, Object)
SET_FIELD_IMPLEMENTATION(jboolean, Boolean)
SET_FIELD_IMPLEMENTATION(jbyte, Byte)
SET_FIELD_IMPLEMENTATION(jchar, Char)
SET_FIELD_IMPLEMENTATION(jshort, Short)
SET_FIELD_IMPLEMENTATION(jint, Int)
SET_FIELD_IMPLEMENTATION(jlong, Long)
SET_FIELD_IMPLEMENTATION(jfloat, Float)
SET_FIELD_IMPLEMENTATION(jdouble, Double)
//GET_STATIC_FIELD_IMPLEMENTATION(jobject, Object)
GET_STATIC_FIELD_IMPLEMENTATION(jboolean, Boolean)
GET_STATIC_FIELD_IMPLEMENTATION(jbyte, Byte)
GET_STATIC_FIELD_IMPLEMENTATION(jchar, Char)
GET_STATIC_FIELD_IMPLEMENTATION(jshort, Short)
GET_STATIC_FIELD_IMPLEMENTATION(jint, Int)
GET_STATIC_FIELD_IMPLEMENTATION(jlong, Long)
GET_STATIC_FIELD_IMPLEMENTATION(jfloat, Float)
GET_STATIC_FIELD_IMPLEMENTATION(jdouble, Double)
SET_STATIC_FIELD_IMPLEMENTATION(jobject, Object)
SET_STATIC_FIELD_IMPLEMENTATION(jboolean, Boolean)
SET_STATIC_FIELD_IMPLEMENTATION(jbyte, Byte)
SET_STATIC_FIELD_IMPLEMENTATION(jchar, Char)
SET_STATIC_FIELD_IMPLEMENTATION(jshort, Short)
SET_STATIC_FIELD_IMPLEMENTATION(jint, Int)
SET_STATIC_FIELD_IMPLEMENTATION(jlong, Long)
SET_STATIC_FIELD_IMPLEMENTATION(jfloat, Float)
SET_STATIC_FIELD_IMPLEMENTATION(jdouble, Double)
- (jobject)getObjectField:(jobject)obj field:(jfieldID)fid {
if (obj == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get field of NULL object"];
else if (fid == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get NULL field"];
jobject result = (*env)->GetObjectField(env, obj, fid);
[self reportException];
GLOBALIZE(result)
return __global;
}
- (jobject)getStaticObjectField:(jobject)obj field:(jfieldID)fid {
if (obj == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get static field of NULL object"];
else if (fid == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get static NULL field"];
jobject result = (*env)->GetStaticObjectField(env, obj, fid);
[self reportException];
GLOBALIZE(result)
return __global;
}
- (void)callVoidMethodOnObject:(jobject)obj method:(jmethodID)mid, ... {
if (obj == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call method on NULL object"];
else if (mid == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call NULL method"];
va_list ap;
va_start(ap, mid);
(*env)->CallVoidMethodV(env, obj, mid, ap);
va_end(ap);
[self reportException];
}
- (jobject)callObjectMethodOnObject:(jobject)obj method:(jmethodID)mid, ... {
if (obj == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call method on NULL object"];
else if (mid == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call NULL method"];
va_list ap;
jobject result;
va_start(ap, mid);
result = (*env)->CallObjectMethodV(env, obj, mid, ap);
va_end(ap);
[self reportException];
GLOBALIZE(result)
return __global;
}
- (jobject)callStaticObjectMethodOnClass:(jclass)cls method:(jmethodID)mid, ... {
if (cls == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call statoc method on NULL class"];
else if (mid == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call NULL method"];
va_list ap;
jobject result;
va_start(ap, mid);
result = (*env)->CallStaticObjectMethodV(env, cls, mid, ap);
va_end(ap);
[self reportException];
GLOBALIZE(result)
return __global;
}
- (void)callStaticVoidMethodOnClass:(jclass)cls method:(jmethodID)mid, ... {
if (cls == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call static method on NULL class"];
else if (mid == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call NULL method"];
va_list ap;
va_start(ap, mid);
(*env)->CallStaticVoidMethodV(env, cls, mid, ap);
va_end(ap);
[self reportException];
}
- (jobject)newObject:(jclass)cls method:(jmethodID)mid, ... {
if (cls == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot create object with NULL class"];
else if (mid == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot call NULL constructor"];
va_list ap;
jobject result;
va_start(ap, mid);
result = (*env)->NewObjectV(env, cls, mid, ap);
[self reportException];
if (result == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Object with class %p and method %p creation failed", cls, mid];
va_end(ap);
GLOBALIZE(result)
return __global;
}
- (jclass)findClass:(NSString *)cls {
if (cls == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot find NULL class"];
jclass result;
result = (*env)->FindClass(env, [cls UTF8String]);
[self reportException];
if (result == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Failed to find class %@", cls];
// TODO check release object
GLOBALIZE(result)
return __global;
}
- (jclass)getObjectClass:(jobject)obj {
if (obj == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get class of NULL object"];
jclass result;
result = (*env)->GetObjectClass(env, obj);
[self reportException];
GLOBALIZE(result)
return __global;
}
- (jobjectArray)innerJavaObjectArrayFromArray:(NSArray *)array
baseClass:(id)baseClass
isPrimitive:(BOOL)isPrimitive
currentDimension:(int)currentDimension {
int length = (int)[array count];
jclass resultBaseClass = NULL;
if ([baseClass isKindOfClass:[NSString class]]) {
// probably interface
resultBaseClass = [[OJNIMidManager sharedManager] javaClassForClassName:baseClass];
} else if (isPrimitive) {
NSMutableString *classIdentifier = [[NSMutableString alloc] init];
for (int i = 0; i < currentDimension; i++) {
[classIdentifier appendString:@"["];
}
[classIdentifier appendString:[baseClass javaArrayIdentifier]];
resultBaseClass = [[OJNIMidManager sharedManager] javaClassForClassName:classIdentifier];
} else {
resultBaseClass = [baseClass OJNIClass];
}
jobjectArray result = (*env)->NewObjectArray(env, length, resultBaseClass, NULL);
for (int i = 0; i < length; i++) {
id object = [array objectAtIndex:i];
if (currentDimension > 1) {
(*env)->SetObjectArrayElement(env, result, i, [self innerJavaObjectArrayFromArray:array[i]
baseClass:baseClass
isPrimitive:isPrimitive
currentDimension:currentDimension - 1]);
}
else if (isPrimitive) {
(*env)->SetObjectArrayElement(env, result, i, [object javaArray]);
}
else {
(*env)->SetObjectArrayElement(env, result, i, [object javaObject]);
}
}
return result;
}
- (jobjectArray)newJavaObjectArrayFromArray:(NSArray *)array
baseClass:(id)baseClass
dimensions:(int)dimensions {
jobjectArray result;
BOOL primitive = (![baseClass isKindOfClass:[NSString class]] &&
[baseClass isSubclassOfClass:[OJNIPrimitiveArray class]]);
if (primitive)
dimensions--;
result = [self innerJavaObjectArrayFromArray:array
baseClass:baseClass
isPrimitive:primitive
currentDimension:dimensions];
return result;
}
- (NSArray *)innerArrayFromJavaObjectArray:(jobjectArray)array
baseClass:(Class)baseClass
classPrefix:(NSString *)classPrefix
isPrimitive:(BOOL)isPrimitive
currentDimension:(int)currentDimension {
int length = [self lengthOfArray:array];
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:length];
for (int i = 0; i < length; i++) {
jobject object = (*env)->GetObjectArrayElement(env, array, i);
if (!object)
{
[result addObject:[NSNull null]];
}
else if (currentDimension > 1) {
[result addObject:[self innerArrayFromJavaObjectArray:object
baseClass:baseClass
classPrefix:classPrefix
isPrimitive:isPrimitive
currentDimension:currentDimension - 1]];
}
else if (isPrimitive) {
[result addObject:[[baseClass alloc] initWithJavaArray:object]];
}
else {
[result addObject:[OJNIJavaObject retrieveFromJavaObject:object classPrefix:classPrefix]];
}
// check, needed?
(*env)->DeleteLocalRef(env, object);
}
return result;
}
- (NSArray *)newArrayFromJavaObjectArray:(jobjectArray)array
baseClass:(Class)baseClass
classPrefix:(NSString *)classPrefix
dimensions:(int)dimensions {
NSArray *result;
BOOL primitive = ([baseClass isSubclassOfClass:[OJNIPrimitiveArray class]]);
if (primitive)
dimensions--;
result = array ? [self innerArrayFromJavaObjectArray:array
baseClass:baseClass
classPrefix:classPrefix
isPrimitive:primitive
currentDimension:dimensions] : [[NSArray alloc] init];
return result;
}
NEW_ARRAY_METHOD_IMPLEMENTATION(jintArray, Int)
NEW_ARRAY_METHOD_IMPLEMENTATION(jcharArray, Char)
NEW_ARRAY_METHOD_IMPLEMENTATION(jshortArray, Short)
NEW_ARRAY_METHOD_IMPLEMENTATION(jlongArray, Long)
NEW_ARRAY_METHOD_IMPLEMENTATION(jfloatArray, Float)
NEW_ARRAY_METHOD_IMPLEMENTATION(jdoubleArray, Double)
NEW_ARRAY_METHOD_IMPLEMENTATION(jbooleanArray, Boolean)
NEW_ARRAY_METHOD_IMPLEMENTATION(jbyteArray, Byte)
- (jfieldID)getFieldID:(jclass)cls name:(NSString *)name signature:(NSString *)signature isStatic:(BOOL)isStatic {
if (cls == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get field ID of NULL class"];
else if (name == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get field ID with NULL name"];
else if (signature == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get field ID with NULL signature"];
jfieldID result;
if (isStatic)
result = (*env)->GetStaticFieldID(env, cls, [name UTF8String], [signature UTF8String]);
else
result = (*env)->GetFieldID(env, cls, [name UTF8String], [signature UTF8String]);
[self reportException];
if (result == NULL) {
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"%@Field ID with class %p, name %@ and signature %@ not found", isStatic ? @"Static " : @"", cls, name, signature];
}
return result;
}
- (jfieldID)getFieldID:(jclass)cls name:(NSString *)name signature:(NSString *)signature {
return [self getFieldID:cls name:name signature:signature isStatic:NO];
}
- (jfieldID)getStaticFieldID:(jclass)cls name:(NSString *)name signature:(NSString *)signature {
return [self getFieldID:cls name:name signature:signature isStatic:YES];
}
- (jmethodID)getMethodID:(jclass)cls name:(NSString *)name signature:(NSString *)signature isStatic:(BOOL)isStatic {
if (cls == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get method ID of NULL class"];
else if (name == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get method ID with NULL name"];
else if (signature == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get method ID with NULL signature"];
jmethodID result;
if (isStatic)
result = (*env)->GetStaticMethodID(env, cls, [name UTF8String], [signature UTF8String]);
else
result = (*env)->GetMethodID(env, cls, [name UTF8String], [signature UTF8String]);
[self reportException];
if (result == NULL) {
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"%@Method ID with class %p, name %@ and signature %@ not found", isStatic ? @"Static " : @"", cls, name, signature];
}
return result;
}
- (jmethodID)getMethodID:(jclass)cls name:(NSString *)name signature:(NSString *)signature {
return [self getMethodID:cls name:name signature:signature isStatic:NO];
}
- (jmethodID)getStaticMethodID:(jclass)cls name:(NSString *)name signature:(NSString *)signature {
return [self getMethodID:cls name:name signature:signature isStatic:YES];
}
- (jsize)lengthOfArray:(jarray)array {
if (array == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get length of NULL array"];
jsize result;
result = (*env)->GetArrayLength(env, array);
return result;
}
GET_PRIMITIVE_ARRAY_METHOD_IMPLEMENTATION(jintArray, int, Int)
GET_PRIMITIVE_ARRAY_METHOD_IMPLEMENTATION(jcharArray, char, Char)
GET_PRIMITIVE_ARRAY_METHOD_IMPLEMENTATION(jshortArray, short, Short)
GET_PRIMITIVE_ARRAY_METHOD_IMPLEMENTATION(jlongArray, long, Long)
GET_PRIMITIVE_ARRAY_METHOD_IMPLEMENTATION(jfloatArray, float, Float)
GET_PRIMITIVE_ARRAY_METHOD_IMPLEMENTATION(jdoubleArray, double, Double)
GET_PRIMITIVE_ARRAY_METHOD_IMPLEMENTATION(jbooleanArray, bool, Boolean)
GET_PRIMITIVE_ARRAY_METHOD_IMPLEMENTATION(jbyteArray, char, Byte)
- (NSString *)newStringFromJavaString:(jstring)javaString utf8Encoding:(BOOL)utf8Encoding {
if (javaString == NULL)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get NSString of NULL java string"];
NSString *result = nil;
if (utf8Encoding) {
const char *chars = (*env)->GetStringUTFChars(env, javaString, NULL);
[self reportException];
result = [NSString stringWithUTF8String:chars];
(*env)->ReleaseStringUTFChars(env, javaString, chars);
} else {
const unichar *chars = (*env)->GetStringChars(env, javaString, NULL);
[self reportException];
jsize length = (*env)->GetStringLength(env, javaString);
result = [NSString stringWithCharacters:chars length:length];
(*env)->ReleaseStringChars(env, javaString, chars);
}
return result;
}
- (jstring)newJavaStringFromString:(NSString *)string utf8Encoding:(BOOL)utf8Encoding {
jstring result = NULL;
if (utf8Encoding) {
const char *chars = [string cStringUsingEncoding:NSUTF8StringEncoding];
result = (*env)->NewStringUTF(env, chars);
} else {
int length = (int)string.length;
unichar *chars = malloc(length * sizeof(unichar));
[string getCharacters:chars range:NSMakeRange(0, length)];
result = (*env)->NewString(env, chars, length);
free(chars);
}
// TODO fix string references
[self reportException];
GLOBALIZE(result)
return __global;
}
- (BOOL)isJavaObject:(jobject)obj1 equalToObject:(jobject)obj2 {
return ((*env)->IsSameObject(env, obj1, obj2) == JNI_TRUE);
}
- (NSString *)getComponentTypeClassNameOfArray:(jarray)javaArray {
static jmethodID mid = NULL;
jclass javaClass = [self getObjectClass:javaArray];
if (mid == NULL) {
jclass jniClass = [self getObjectClass:javaClass];
mid = [self getMethodID:jniClass name:@"getComponentType" signature:@"()Ljava/lang/Class;"];
[self releaseObject:jniClass];
}
jclass componentClass = [self callObjectMethodOnObject:javaClass method:mid];
NSString *result = [self getClassNameOfJavaClass:componentClass removePackage:NO];
[self releaseObject:componentClass];
return result;
}
// TODO: REWORK!!!
- (NSString *)getClassNameOfJavaClass:(jclass)javaClass removePackage:(BOOL)removePackage {
static jmethodID mid = NULL;
if (mid == NULL) {
jclass jniClass = [self getObjectClass:javaClass];
mid = [self getMethodID:jniClass name:@"getName" signature:@"()Ljava/lang/String;"];
[self releaseObject:jniClass];
}
jobject simpleName = [self callObjectMethodOnObject:javaClass method:mid];
[self releaseObject:javaClass];
NSString *result = [self newStringFromJavaString:simpleName utf8Encoding:YES];
if (removePackage)
result = [self classNameWithoutPackage:result];
return result;
}
- (NSString *)getClassNameOfJavaObject:(jobject)javaObject removePackage:(BOOL)removePackage {
jclass javaClass = [self getObjectClass:javaObject];
return [self getClassNameOfJavaClass:javaClass removePackage:removePackage];
}
- (NSString *)classNameWithoutPackage:(NSString *)className {
NSUInteger location = [className rangeOfString:@"." options:NSBackwardsSearch].location;
if (location == NSNotFound) {
return className;
}
// remove package name
return [className substringFromIndex:location + 1];
}
- (BOOL)isJavaObjectArray:(jobject)javaObject {
static jmethodID mid = NULL;
jclass javaClass = [self getObjectClass:javaObject];
if (mid == NULL) {
jclass jniClass = [self getObjectClass:javaClass];
mid = [self getMethodID:jniClass name:@"isArray" signature:@"()Z"];
[self releaseObject:jniClass];
}
BOOL result = ([self callBooleanMethodOnObject:javaClass method:mid] == JNI_TRUE);
[self releaseObject:javaClass];
return result;
}
- (Class)runtimeClassFromJavaObject:(jobject)javaObject prefix:(NSString *)prefix {
NSString *className = [self getClassNameOfJavaObject:javaObject removePackage:YES];
if (className == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get class name of java object %p", javaObject];
Class resultClass = [self runtimeClassFromJavaClassName:className prefix:prefix];
if (resultClass == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Runtime class getting failed for object %p with java object's classname %@", javaObject, className];
return resultClass;
}
- (Class)runtimeClassFromJavaClassName:(NSString *)className prefix:(NSString *)prefix {
NSString *result = [prefix stringByAppendingString:className];
Class resultClass = NSClassFromString(result);
return resultClass;
}
- (void)reportException {
if ((*env)->ExceptionCheck(env)) {
jthrowable throwable = (*env)->ExceptionOccurred(env);
(*env)->ExceptionClear(env);
// throw translated obj-c exception
OJNIJavaException *exception = [OJNIJavaException exceptionWithThrowable:throwable];
@throw exception;
}
}
@end