-
Notifications
You must be signed in to change notification settings - Fork 0
/
OJNIArrayInfo.m
88 lines (64 loc) · 2.43 KB
/
OJNIArrayInfo.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
//
// OJNIArrayInfo.m
// Zdravcity-iOS
//
// Created by Alexander Shitikov on 16.03.16.
// Copyright © 2016 A. All rights reserved.
//
#import "OJNIArrayInfo.h"
#import "OJNIEnvironmentException.h"
@implementation OJNIArrayInfo
+ (instancetype)arrayInfoFromJavaArray:(jarray)array environment:(OJNIEnv *)env prefix:(NSString *)prefix {
OJNIArrayInfo *info = [[self alloc] init];
NSString *identifier = [env getClassNameOfJavaObject:array removePackage:NO];
NSUInteger location = [identifier rangeOfString:@"[" options:NSBackwardsSearch].location;
if (location == NSNotFound)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Cannot get information from not array type %@", identifier];
info.dimensions = location + 1;
info.isPrimitive = (identifier.length - info.dimensions == 1);
if (info.isPrimitive) {
unichar arrId = [identifier characterAtIndex:info.dimensions];
info.componentType = [self primitiveArrayClassFromId:arrId];
if (info.componentType == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Unable to get primitive array type with signature %c", arrId];
}
else {
NSString *className = [env getComponentTypeClassNameOfArray:array];
info.componentType = [env runtimeClassFromJavaClassName:className prefix:prefix];
if (info.componentType == nil)
@throw [OJNIEnvironmentException pointerExceptionWithReason:@"Unable to get array type with component's class name %@", className];
}
return info;
}
+ (Class)primitiveArrayClassFromId:(unichar)arrId {
switch (arrId) {
case 'Z':
return [OJNIPrimitiveBooleanArray class];
break;
case 'B':
return [OJNIPrimitiveByteArray class];
break;
case 'C':
return [OJNIPrimitiveCharArray class];
break;
case 'S':
return [OJNIPrimitiveShortArray class];
break;
case 'I':
return [OJNIPrimitiveIntArray class];
break;
case 'J':
return [OJNIPrimitiveLongArray class];
break;
case 'F':
return [OJNIPrimitiveFloatArray class];
break;
case 'D':
return [OJNIPrimitiveDoubleArray class];
break;
default:
return nil;
break;
}
}
@end