-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathMarshalMemberBuilderTest.cs
More file actions
570 lines (517 loc) · 16.7 KB
/
MarshalMemberBuilderTest.cs
File metadata and controls
570 lines (517 loc) · 16.7 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
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
using System;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Java.Interop;
using Mono.Linq.Expressions;
using NUnit.Framework;
namespace Java.InteropTests
{
[TestFixture]
class MarshalMemberBuilderTest : JavaVMFixture
{
[Test]
public void AddExportMethods ()
{
using (var t = CreateExportTestType ()) {
var methods = CreateBuilder ()
.GetExportedMemberRegistrations (typeof (ExportTest))
.ToList ();
Assert.AreEqual (11, methods.Count);
Assert.AreEqual ("n_InstanceAction", methods [0].Name);
Assert.AreEqual ("()V", methods [0].Signature);
Assert.AreEqual ("n_StaticAction", methods [1].Name);
Assert.AreEqual ("()V", methods [1].Signature);
#if NET
Assert.AreEqual ("_JniMarshal_PP_V", methods [0].Marshaler.GetType ().FullName);
Assert.AreEqual ("_JniMarshal_PP_V", methods [1].Marshaler.GetType ().FullName);
#else
Assert.IsTrue (methods [0].Marshaler is Action<IntPtr, IntPtr>);
Assert.IsTrue (methods [1].Marshaler is Action<IntPtr, IntPtr>);
#endif // NET
var m = t.GetStaticMethod ("testStaticMethods", "()V");
JniEnvironment.StaticMethods.CallStaticVoidMethod (t.PeerReference, m);
Assert.IsTrue (ExportTest.StaticHelloCalled);
Assert.IsTrue (ExportTest.StaticActionInt32StringCalled);
using (var o = CreateExportTest (t)) {
var n = t.GetInstanceMethod ("testMethods", "()V");
JniEnvironment.InstanceMethods.CallVoidMethod (o.PeerReference, n);
Assert.IsTrue (o.HelloCalled);
o.Dispose ();
}
}
}
static MarshalMemberBuilder CreateBuilder ()
{
return new MarshalMemberBuilder (JniRuntime.CurrentRuntime);
}
static JniType CreateExportTestType ()
{
return new JniType ("net/dot/jni/test/ExportType");
}
static unsafe ExportTest CreateExportTest (JniType type)
{
var c = type.GetConstructor ("()V");
var p = type.NewObject (c, null);
return new ExportTest (ref p, JniObjectReferenceOptions.CopyAndDispose);
}
[Test]
public void GetExportedMemberRegistrations_NullChecks ()
{
var builder = CreateBuilder ();
Assert.Throws<ArgumentNullException> (() => builder.GetExportedMemberRegistrations (null));
}
[Test]
public void GetJniMethodSignature_NullChecks ()
{
var builder = CreateBuilder ();
Action a = () => {};
Assert.Throws<ArgumentNullException> (() => builder.GetJniMethodSignature (null, a.Method));
Assert.Throws<ArgumentNullException> (() => builder.GetJniMethodSignature (new JavaCallableAttribute (), null));
}
[Test]
public void GetJniMethodSignature ()
{
var builder = CreateBuilder ();
Action a = () => {};
var export = new JavaCallableAttribute () {
Signature = "(I)V",
};
// Note: no validation between actual MethodInfo & existing signature
// Validation would be done by CreateMarshalFromJniMethodRegistration().
Assert.AreEqual ("(I)V", builder.GetJniMethodSignature (export, a.Method));
Assert.AreEqual ("(I)V", export.Signature);
export = new JavaCallableAttribute () {
Signature = null,
};
Assert.AreEqual ("()V", builder.GetJniMethodSignature (export, a.Method));
// Note: export.Signature updated
Assert.AreEqual ("()V", export.Signature);
Action<string> s = v => {};
Assert.AreEqual ("(Ljava/lang/String;)V", builder.GetJniMethodSignature (new JavaCallableAttribute (), s.Method));
Action<IntPtr, IntPtr, string> ds = (e, c, v) => {};
Assert.AreEqual ("(Ljava/lang/String;)V", builder.GetJniMethodSignature (new JavaCallableAttribute (), ds.Method));
Func<string> fs = () => null;
Assert.AreEqual ("()Ljava/lang/String;", builder.GetJniMethodSignature (new JavaCallableAttribute (), fs.Method));
Func<IntPtr, IntPtr, string> dfs = (e, c) => null;
Assert.AreEqual ("()Ljava/lang/String;", builder.GetJniMethodSignature (new JavaCallableAttribute (), dfs.Method));
// Note: AppDomain currently has no builtin marshaling defaults
// TODO: but should it? We could default wrap to JavaProxyObject...?
Action<AppDomain> aad = v => {};
Assert.Throws<NotSupportedException> (() => builder.GetJniMethodSignature (new JavaCallableAttribute (), aad.Method));
Func<AppDomain> fad = () => null;
Assert.Throws<NotSupportedException> (() => builder.GetJniMethodSignature (new JavaCallableAttribute (), fad.Method));
}
[Test]
public void CreateMarshalToManagedExpression_NullChecks ()
{
Action a = ExportTest.StaticAction;
var builder = CreateBuilder ();
Assert.Throws<ArgumentNullException> (() => builder.CreateMarshalToManagedExpression (null));
builder.CreateMarshalToManagedExpression (a.Method, null);
builder.CreateMarshalToManagedExpression (a.Method, null, null);
}
[Test]
public void CreateMarshalToManagedExpression_SignatureMismatch ()
{
Action<int, string> a = ExportTest.StaticActionInt32String;
var builder = CreateBuilder ();
// Parameter count mismatch: 0 != 2
Assert.Throws<ArgumentException>(() => builder.CreateMarshalToManagedExpression (
a.Method, new JavaCallableAttribute () { Signature = "()V" }, a.Method.DeclaringType));
// Parameter count mismatch: 1 != 2
Assert.Throws<ArgumentException>(() => builder.CreateMarshalToManagedExpression (
a.Method, new JavaCallableAttribute () { Signature = "(I)V" }, a.Method.DeclaringType));
// Parameter type mismatch: (int, int) != (int, string)
Assert.Throws<ArgumentException>(() => builder.CreateMarshalToManagedExpression (
a.Method, new JavaCallableAttribute () { Signature = "(II)V" }, a.Method.DeclaringType));
// return type mismatch: int != void
Assert.Throws<ArgumentException>(() => builder.CreateMarshalToManagedExpression (
a.Method, new JavaCallableAttribute () { Signature = "(ILjava/lang/String;)I" }, a.Method.DeclaringType));
// invalid JNI signatures
Assert.Throws<ArgumentException>(() => builder.CreateMarshalToManagedExpression (
a.Method, new JavaCallableAttribute () { Signature = "(IL)I" }, a.Method.DeclaringType));
Assert.Throws<ArgumentException>(() => builder.CreateMarshalToManagedExpression (
a.Method, new JavaCallableAttribute () { Signature = "(I[)I" }, a.Method.DeclaringType));
}
[Test]
public void CreateMarshalFromJniMethodExpression_InstanceAction ()
{
var t = typeof (ExportTest);
var m = t.GetMethod ("InstanceAction");
CheckCreateMarshalToManagedExpression (null, t, m, typeof (Action<IntPtr, IntPtr>),
@"void (IntPtr __jnienv, IntPtr __this)
{
JniTransition __envp;
JniRuntime __jvm;
JniValueManager __vm;
ExportTest __this_val;
__envp = new JniTransition(__jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__vm = __jvm.ValueManager;
__vm.WaitForGCBridgeProcessing();
__this_val = __vm.GetValue<ExportTest>(__this);
__this_val.InstanceAction();
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
}
finally
{
__envp.Dispose();
}
}");
}
static void CheckCreateMarshalToManagedExpression (JavaCallableAttribute export, Type type, MethodInfo method, Type expectedDelegateType, string expectedBody)
{
export = export ?? new JavaCallableAttribute ();
var b = CreateBuilder ();
var l = b.CreateMarshalToManagedExpression (method, export, type);
CheckExpression (l, method.Name, expectedDelegateType, expectedBody);
}
static void CheckExpression (LambdaExpression expression, string memberName, Type expressionType, string expectedBody)
{
Console.WriteLine ("## member: {0}", memberName);
Console.WriteLine (expression.ToCSharpCode ());
Assert.AreEqual (expectedBody, expression.ToCSharpCode ());
#if NET
// TODO: Use src/Java.Interop.Tools.Expressions to compile `expression`
// and use the "IL decompiler" in tests/Java.Interop.Tools.Expressions-Tests
// to verify the expected IL
#else
var da = AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName("dyn"), // call it whatever you want
System.Reflection.Emit.AssemblyBuilderAccess.Save,
Path.GetDirectoryName (typeof (MarshalMemberBuilderTest).Assembly.Location));
var _name = "dyn-" + memberName + ".dll";
var dm = da.DefineDynamicModule("dyn_mod", _name);
var dt = dm.DefineType ("dyn_type", TypeAttributes.Public);
var mb = dt.DefineMethod(
memberName,
MethodAttributes.Public | MethodAttributes.Static);
expression.CompileToMethod (mb);
dt.CreateType();
Assert.AreEqual (expressionType, expression.Type);
#if !__ANDROID__
da.Save (_name);
#endif // !__ANDROID__
#endif // !NET
}
[Test]
public void CreateMarshalFromJniMethodExpression_StaticAction ()
{
var t = typeof (ExportTest);
Action a = ExportTest.StaticAction;
CheckCreateMarshalToManagedExpression (null, t, a.Method, typeof(Action<IntPtr, IntPtr>),
@"void (IntPtr __jnienv, IntPtr __class)
{
JniTransition __envp;
JniRuntime __jvm;
__envp = new JniTransition(__jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__jvm.ValueManager.WaitForGCBridgeProcessing();
ExportTest.StaticAction();
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
}
finally
{
__envp.Dispose();
}
}");
}
[Test]
public void CreateMarshalFromJniMethodExpression_StaticActionIJavaLangObject ()
{
var t = typeof (ExportTest);
var m = t.GetMethod ("StaticActionIJavaObject");
CheckCreateMarshalToManagedExpression (null, t, m, typeof(Action<IntPtr, IntPtr, IntPtr>),
@"void (IntPtr __jnienv, IntPtr __class, IntPtr test)
{
JniTransition __envp;
JniRuntime __jvm;
JniValueManager __vm;
JavaObject test_val;
__envp = new JniTransition(__jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__vm = __jvm.ValueManager;
__vm.WaitForGCBridgeProcessing();
test_val = __vm.GetValue<JavaObject>(test);
ExportTest.StaticActionIJavaObject(test_val);
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
}
finally
{
__envp.Dispose();
}
}");
}
[Test]
public void CreateMarshalFromJniMethodExpression_InstanceActionIJavaLangObject ()
{
var t = typeof (ExportTest);
var m = t.GetMethod ("InstanceActionIJavaObject");
CheckCreateMarshalToManagedExpression (null, t, m, typeof(Action<IntPtr, IntPtr, IntPtr>),
@"void (IntPtr __jnienv, IntPtr __this, IntPtr test)
{
JniTransition __envp;
JniRuntime __jvm;
JniValueManager __vm;
ExportTest __this_val;
JavaObject test_val;
__envp = new JniTransition(__jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__vm = __jvm.ValueManager;
__vm.WaitForGCBridgeProcessing();
__this_val = __vm.GetValue<ExportTest>(__this);
test_val = __vm.GetValue<JavaObject>(test);
__this_val.InstanceActionIJavaObject(test_val);
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
}
finally
{
__envp.Dispose();
}
}");
}
[Test]
public void CreateMarshalFromJniMethodExpression_StaticActionInt32String ()
{
var t = typeof (ExportTest);
var m = ((Action<int, string>) ExportTest.StaticActionInt32String);
var e = new JavaCallableAttribute () {
Signature = "(ILjava/lang/String;)V",
};
CheckCreateMarshalToManagedExpression (e, t, m.Method, typeof (Action<IntPtr, IntPtr, int, IntPtr>),
@"void (IntPtr __jnienv, IntPtr __class, int i, IntPtr v)
{
JniTransition __envp;
JniRuntime __jvm;
string v_val;
__envp = new JniTransition(__jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__jvm.ValueManager.WaitForGCBridgeProcessing();
v_val = Strings.ToString(v);
ExportTest.StaticActionInt32String(i, v_val);
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
}
finally
{
__envp.Dispose();
}
}");
}
[Test]
public void CreateMarshalFromJniMethodExpression_StaticFuncMyLegacyColorMyColor_MyColor ()
{
var t = typeof (ExportTest);
var m = ((Func<MyLegacyColor, MyColor, MyColor>) ExportTest.StaticFuncMyLegacyColorMyColor_MyColor);
var e = new JavaCallableAttribute () {
Signature = "(II)I",
};
CheckCreateMarshalToManagedExpression (e, t, m.Method, typeof (Func<IntPtr, IntPtr, int, int, int>),
@"int (IntPtr __jnienv, IntPtr __class, int color1, int color2)
{
JniTransition __envp;
JniRuntime __jvm;
MyColor __mret;
MyLegacyColor color1_val;
MyColor color2_val;
int __mret_p;
__envp = new JniTransition(__jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__jvm.ValueManager.WaitForGCBridgeProcessing();
color1_val = new MyLegacyColor(color1);
color2_val = new MyColor(color2);
__mret = ExportTest.StaticFuncMyLegacyColorMyColor_MyColor(color1_val, color2_val);
__mret_p = __mret.Value;
return __mret_p;
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
return default(int);
}
finally
{
__envp.Dispose();
}
}");
}
[Test]
public void CreateMarshalFromJniMethodExpression_FuncInt64 ()
{
var t = typeof (ExportTest);
var m = t.GetMethod ("FuncInt64");
var e = new JavaCallableAttribute () {
Signature = "()J",
};
CheckCreateMarshalToManagedExpression (e, t, m, typeof (Func<IntPtr, IntPtr, long>),
@"long (IntPtr __jnienv, IntPtr __this)
{
JniTransition __envp;
JniRuntime __jvm;
JniValueManager __vm;
long __mret;
ExportTest __this_val;
__envp = new JniTransition(__jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__vm = __jvm.ValueManager;
__vm.WaitForGCBridgeProcessing();
__this_val = __vm.GetValue<ExportTest>(__this);
__mret = __this_val.FuncInt64();
return __mret;
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
return default(long);
}
finally
{
__envp.Dispose();
}
}");
}
[Test]
public void CreateMarshalFromJniMethodExpression_FuncIJavaObject ()
{
var t = typeof (ExportTest);
var m = t.GetMethod ("FuncIJavaObject");
var e = new JavaCallableAttribute () {
Signature = "()Ljava/lang/Object;",
};
CheckCreateMarshalToManagedExpression (e, t, m, typeof (Func<IntPtr, IntPtr, IntPtr>),
@"IntPtr (IntPtr __jnienv, IntPtr __this)
{
JniTransition __envp;
JniRuntime __jvm;
JniValueManager __vm;
JavaObject __mret;
ExportTest __this_val;
JniObjectReference __mret_ref;
IntPtr __mret_rtn;
__envp = new JniTransition(__jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__vm = __jvm.ValueManager;
__vm.WaitForGCBridgeProcessing();
__this_val = __vm.GetValue<ExportTest>(__this);
__mret = __this_val.FuncIJavaObject();
if (null == __mret)
{
return __mret_ref = new JniObjectReference();
}
else
{
return __mret_ref = (IJavaPeerable)__mret.PeerReference;
}
__mret_rtn = References.NewReturnToJniRef(__mret_ref);
return __mret_rtn;
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
return default(IntPtr);
}
finally
{
__envp.Dispose();
}
}");
}
static void DirectInvocation (IntPtr jnienv, IntPtr context)
{
}
[Test]
public void CreateMarshalToManagedExpression_DirectMethod ()
{
Action<IntPtr, IntPtr> a = DirectInvocation;
var e = new JavaCallableAttribute () {
Signature = "()V",
};
CheckCreateMarshalToManagedExpression (e, a.Method.DeclaringType, a.Method, typeof (Action<IntPtr, IntPtr>),
@"void (IntPtr jnienv, IntPtr context)
{
JniTransition __envp;
JniRuntime __jvm;
__envp = new JniTransition(jnienv);
try
{
__jvm = JniEnvironment.Runtime;
__jvm.ValueManager.WaitForGCBridgeProcessing();
MarshalMemberBuilderTest.DirectInvocation(jnienv, context);
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e))
{
__envp.SetPendingException(__e);
}
finally
{
__envp.Dispose();
}
}");
}
[Test]
public void CreateConstructActivationPeerExpression_Exceptions ()
{
var b = CreateBuilder ();
Assert.Throws<ArgumentNullException> (() => b.CreateConstructActivationPeerExpression (null));
}
[Test]
public void CreateConstructActivationPeerExpression ()
{
var b = CreateBuilder ();
var c = typeof (MarshalMemberBuilderTest).GetConstructor (new Type [0]);
var e = b.CreateConstructActivationPeerExpression (c);
const string GetUninitializedObject_decltype =
#if NETCOREAPP
"RuntimeHelpers"
#else // !NETCOREAPP
"FormatterServices"
#endif // !NETCOREAPP
;
CheckExpression (e,
"ExportedMemberBuilderTest_ctor",
typeof(Func<ConstructorInfo, JniObjectReference, object[], object>),
$@"object (ConstructorInfo constructor, JniObjectReference reference, object[] parameters)
{{
Type type;
object self;
type = constructor.DeclaringType;
self = {GetUninitializedObject_decltype}.GetUninitializedObject(type);
(IJavaPeerable)self.SetPeerReference(reference);
constructor.Invoke(self, parameters);
return self;
}}");
}
}
}