-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathWLJavaProxy.m
More file actions
279 lines (226 loc) · 8.83 KB
/
WLJavaProxy.m
File metadata and controls
279 lines (226 loc) · 8.83 KB
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
//
// WLJavaProxy.m
// libjcocoa
//
// Created by Steve Hannah on 2012-10-21.
// Copyright (c) 2012 Web Lite Solutions. All rights reserved.
//
#import "WLJavaProxy.h"
#include "JavaUtil.h"
static JavaVM *jvm = NULL;
static bool debugMode = false;
unsigned long acceptNSRange(NSRange range) {
return range.length + range.location;
}
@implementation WLJavaProxy
-(WLJavaProxy*)init:(jobject)thePeer
{
JNIEnv *env=0;
@try {
int attach = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
if ( attach == 0 ){
//JNF_COCOA_ENTER(env);
(*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6);
peer = (*env)->NewGlobalRef(env,thePeer);
jclass cls = (*env)->GetObjectClass(env, peer);
peerClass = (*env)->NewGlobalRef(env, cls);
(*env)->DeleteLocalRef(env, cls);
jMethodSignatureForSelector = (*env)->GetMethodID(env, peerClass, "methodSignatureForSelector", "(J)J" );
jForwardInvocation = (*env)->GetMethodID(env, peerClass, "forwardInvocation", "(J)V" );
jRespondsToSelector = (*env)->GetMethodID(env, peerClass, "respondsToSelector", "(J)Z" );
//JNF_COCOA_EXIT(env);
}
//(*jvm)->DetachCurrentThread(jvm);
return self;
} @catch (NSException *e) {
NSLog(@"Exception: %@", e);
[JavaUtil throwJavaException: env withMessage: [[e reason] UTF8String] ];
}
}
-(void)dealloc
{
JNIEnv *env=0;
@try {
int attach = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
if ( attach == 0 ){
//JNF_COCOA_ENTER(env);
(*env)->DeleteGlobalRef(env, peerClass);
(*env)->DeleteGlobalRef(env, peer);
[super dealloc];
//JNF_COCOA_EXIT(env);
}
} @catch (NSException *e) {
NSLog(@"Exception: %@", e);
[JavaUtil throwJavaException: env withMessage: [[e reason] UTF8String] ];
}
}
+(void)setJVM:(JavaVM*)theJvm
{
JNIEnv *env=0;
@try {
int attach = (*theJvm)->AttachCurrentThread(theJvm, (void**)&env, NULL);
if ( attach == 0 ){
jvm = theJvm;
}
} @catch (NSException *e) {
NSLog(@"Exception: %@", e);
[JavaUtil throwJavaException: env withMessage: [[e reason] UTF8String] ];
}
}
-(jobject)javaPeer
{
JNIEnv *env=0;
@try {
int attach = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
if ( attach == 0 ){
return peer;
}
return NULL;
} @catch (NSException *e) {
NSLog(@"Exception: %@", e);
[JavaUtil throwJavaException: env withMessage: [[e reason] UTF8String] ];
}
}
-(NSMethodSignature*)methodSignatureForSelector:(SEL)sel
{
NSMethodSignature* signature;
JNIEnv *env=0;
@try {
int attach = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
if ( attach == 0 ){
//JNF_COCOA_ENTER(env);
signature = (NSMethodSignature*)(*env)->CallLongMethod(env, peer, jMethodSignatureForSelector, sel);
if (!signature) {
signature = [@"" methodSignatureForSelector:sel];
}
//JNF_COCOA_EXIT(env);
}
//(*jvm)->DetachCurrentThread(jvm);
return signature;
} @catch (NSException *e) {
NSLog(@"Exception: %@", e);
[JavaUtil throwJavaException: env withMessage: [[e reason] UTF8String] ];
}
// return [NSMethodSignature methodSignatureForSelector:sel];
}
#ifndef logIfDebug
#define logIfDebug(args...) if(debugMode) { NSLog(args); }
#endif
-(void)forwardInvocation:(NSInvocation *)invocation
{
logIfDebug(@"forwardInvocation: %@", invocation);
JNIEnv *env=0;
SEL aSelector = [invocation selector];
int attach = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
@try {
if ( attach == 0 ) {
logIfDebug(@"Before CallBooleanMethod");
//JNF_COCOA_ENTER(env);
(*env)->CallVoidMethod(env, peer, jForwardInvocation, invocation);
//JNF_COCOA_EXIT(env);
logIfDebug(@"After CallBooleanMethod");
}
//(*jvm)->DetachCurrentThread(jvm);
} @catch (NSException *e) {
NSString *message = [NSString stringWithFormat:@"Selector '%@' caused exception: %@", NSStringFromSelector(aSelector), e.reason];
NSLog(@"%@", message);
[JavaUtil throwJavaException: env withMessage: [message UTF8String] ];
}
}
+ (BOOL)instancesRespondToSelector:(SEL)aSelector {
return YES;
}
-(BOOL)respondsToSelector:(SEL)aSelector
{
BOOL response = FALSE;
JNIEnv *env=0;
@try {
int attach = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
if ( attach == 0 ){
//JNF_COCOA_ENTER(env);
response = (*env)->CallBooleanMethod(env, peer, jRespondsToSelector, aSelector);
//JNF_COCOA_EXIT(env);
}
//(*jvm)->DetachCurrentThread(jvm);
return response==1?TRUE:FALSE;
} @catch (NSException *e) {
NSLog(@"Exception: %@", e);
[JavaUtil throwJavaException: env withMessage: [[e reason] UTF8String] ];
}
// return YES;
}
- (id)valueForKey:(NSString *)key {
return [self forwardInvocationForSelector: _cmd withTarget:self withArguments: [NSArray arrayWithObject: key]];
}
- (id)valueForUndefinedKey:(NSString *)key {
return [self forwardInvocationForSelector: _cmd withTarget:self withArguments: [NSArray arrayWithObject: key]];
}
- (void)setValue:(id)value forKey:(NSString *)key {
[self forwardInvocationForSelector: _cmd withTarget:self withArguments: [NSArray arrayWithObjects: value, key, nil]];
}
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath {
[self forwardInvocationForSelector: _cmd withTarget:self withArguments: [NSArray arrayWithObjects: value, keyPath, nil]];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
[self forwardInvocationForSelector: _cmd withTarget:self withArguments: [NSArray arrayWithObjects: value, key, nil]];
}
+ (NSSet<NSString *> *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSLog(@"Calling keyPathsForValuesAffectingValueForKey: %@", key);
return [super keyPathsForValuesAffectingValueForKey: key];
}
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context {
[self forwardInvocationForSelector: _cmd withTarget:self withArguments: [NSArray arrayWithObjects: observer, keyPath, options, context, nil]];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[self forwardInvocationForSelector: _cmd withTarget:self withArguments: [NSArray arrayWithObjects: keyPath, object, change, context, nil]];
}
- (void)doesNotRecognizeSelector:(SEL)aSelector {
NSString* message = [NSString stringWithFormat:@"Unrecognized selector called: %@", NSStringFromSelector(aSelector)];
NSLog(@"%@", message);
JNIEnv *env=0;
(*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
[JavaUtil throwJavaException: env withMessage: [message UTF8String] ];
}
/**
Forwards an unhandled call to the java proxy.
*/
- (id)forwardInvocationForSelector: (SEL)aSelector withTarget: (id _Nullable)aTarget withArguments: (NSArray*)args {
@try {
NSString* sel = NSStringFromSelector(aSelector);
NSLog(@"Forwarding selector: %@, %@", sel, args);
NSMethodSignature *aSignature = [self methodSignatureForSelector:aSelector];
NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature:aSignature];
[anInvocation setTarget:aTarget];
[anInvocation setSelector:aSelector];
NSLog(@"Settings arguments for selector: %@", sel);
int i = 2;
for (__unsafe_unretained id arg in args) {
NSLog(@"Settings argument %d to: %@", i, arg);
[anInvocation setArgument:&arg atIndex:i];
i++;
}
[anInvocation retainArguments];
NSLog(@"Forwarding selector to java object: %@", sel);
[self forwardInvocation:anInvocation];
NSLog(@"Getting return value for selector: %@", sel);
NSUInteger length = [[anInvocation methodSignature] methodReturnLength];
if (length > 0 ) {
void *result = (void *) malloc(length);
[anInvocation getReturnValue:&result];
return result;
} else {
return nil;
}
} @catch(NSException *e) {
NSLog(@"Exception: %@", e);
JNIEnv *env=0;
(*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
[JavaUtil throwJavaException: env withMessage: [[e reason] UTF8String] ];
}
return nil;
}
+(void)setDebugMode:(bool)_debugMode {
debugMode = _debugMode;
}
@end