-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathExportTest.cs
279 lines (237 loc) · 8.29 KB
/
ExportTest.cs
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
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Collections.Generic;
using Java.Interop;
using Java.Interop.Expressions;
// For use by `jnimarshalmethod-gen.exe` & `make run-test-jnimarshal`
delegate bool _JniMarshal_PPZBCSIJFDLLLLLDFJ_Z (
IntPtr jnienv,
IntPtr klass,
bool a,
sbyte b,
char c,
short d,
int e,
long f,
float g,
double h,
IntPtr i, // java.lang.Object
IntPtr j, // java.lang.String
IntPtr k, // java.util.ArrayList<String>
IntPtr l, // java.lang.String
IntPtr m, // java.lang.Object
double n,
float o,
long p
);
namespace Java.InteropTests
{
[JniTypeSignature ("com/xamarin/interop/export/ExportType")]
public class ExportTest : JavaObject
{
[JniAddNativeMethodRegistrationAttribute]
static void RegisterNativeMembers (JniNativeMethodRegistrationArguments args)
{
args.AddRegistrations (JniEnvironment.Runtime.MarshalMemberBuilder.GetExportedMemberRegistrations (typeof (ExportTest)));
}
public ExportTest (ref JniObjectReference reference, JniObjectReferenceOptions transfer)
: base (ref reference, transfer)
{
}
public bool HelloCalled;
[JavaCallable ("action", Signature="()V")]
public void InstanceAction ()
{
HelloCalled = true;
}
public static bool StaticHelloCalled;
[JavaCallable ("staticAction", Signature="()V")]
public static void StaticAction ()
{
StaticHelloCalled = true;
}
public static bool StaticActionInt32StringCalled;
[JavaCallable ("staticActionInt32String", Signature = "(ILjava/lang/String;)V")]
public static void StaticActionInt32String (int i, string v)
{
StaticActionInt32StringCalled = i == 1 && v == "2";
}
[JavaCallable ("staticFuncMyLegacyColorMyColor_MyColor")]
public static MyColor StaticFuncMyLegacyColorMyColor_MyColor ([JniValueMarshaler (typeof (MyLegacyColorValueMarshaler))] MyLegacyColor color1, MyColor color2)
{
return new MyColor (color1.Value + color2.Value);
}
[JavaCallable ("funcInt64", Signature = "()J")]
public long FuncInt64 ()
{
return 42;
}
[JavaCallable ("funcIJavaObject", Signature = "()Ljava/lang/Object;")]
public JavaObject FuncIJavaObject ()
{
return this;
}
[JavaCallable ("actionIJavaObject", Signature="(Ljava/lang/Object;)V")]
public void InstanceActionIJavaObject (JavaObject test)
{
}
[JavaCallable ("staticActionIJavaObject", Signature="(Ljava/lang/Object;)V")]
public static void StaticActionIJavaObject (JavaObject test)
{
}
[JavaCallable ("staticActionInt", Signature="(I)V")]
public static void StaticActionInt (int i)
{
}
[JavaCallable ("staticActionFloat", Signature="(F)V")]
public static void StaticActionFloat (float f)
{
}
[JavaCallable ("staticFuncThisMethodTakesLotsOfParameters", Signature="(ZBCSIJFDLjava/lang/Object;Ljava/lang/String;Ljava/util/ArrayList;Ljava/lang/String;Ljava/lang/Object;DFJ)Z")]
public static bool StaticFuncThisMethodTakesLotsOfParameters (
bool a,
sbyte b,
char c,
short d,
int e,
long f,
float g,
double h,
IntPtr i, // java.lang.Object
IntPtr j, // java.lang.String
IntPtr k, // java.util.ArrayList<String>
IntPtr l, // java.lang.String
IntPtr m, // java.lang.Object
double n,
float o,
long p)
{
if (a != false)
return false;
if (b != (byte) 0xb)
return false;
if (c != 'c')
return false;
if (d != (short) 0xd)
return false;
if (e != 0xe)
return false;
if (f != 0xf)
return false;
if (g != 1.0f)
return false;
if (h != 2.0)
return false;
if (i == IntPtr.Zero)
return false;
if (j == IntPtr.Zero)
return false;
if (k == IntPtr.Zero)
return false;
if (l == IntPtr.Zero)
return false;
if (m == IntPtr.Zero)
return false;
if (n != 3.0)
return false;
if (o != 4.0f)
return false;
if (p != 0x70)
return false;
return true;
}
}
[JniValueMarshaler (typeof (MyColorValueMarshaler))]
public struct MyColor {
public readonly int Value;
public MyColor (int value)
{
Value = value;
}
}
// Note: no [JniValueMarshaler] type; we use a parameter custom attribute instead.
public struct MyLegacyColor {
public readonly int Value;
public MyLegacyColor (int value)
{
Value = value;
}
}
public interface MyInterface : IJavaPeerable
{
void MyMethod ();
}
public class MyColorValueMarshaler : JniValueMarshaler<MyColor> {
public override Type MarshalType {
get {return typeof (int);}
}
public override MyColor CreateGenericValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
{
throw new NotImplementedException ();
}
public override JniValueMarshalerState CreateGenericObjectReferenceArgumentState (MyColor value, ParameterAttributes synchronize)
{
throw new NotImplementedException ();
}
public override void DestroyGenericArgumentState (MyColor value, ref JniValueMarshalerState state, ParameterAttributes synchronize)
{
throw new NotImplementedException ();
}
public override Expression CreateParameterToManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type targetType)
{
var c = typeof (MyColor).GetConstructor (new[]{typeof (int)});
var v = Expression.Variable (typeof (MyColor), sourceValue.Name + "_val");
context.LocalVariables.Add (v);
context.CreationStatements.Add (Expression.Assign (v, Expression.New (c, sourceValue)));
return v;
}
public override Expression CreateParameterFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
{
var r = Expression.Variable (MarshalType, sourceValue.Name + "_p");
context.LocalVariables.Add (r);
context.CreationStatements.Add (Expression.Assign (r, Expression.Field (sourceValue, "Value")));
return r;
}
public override Expression CreateReturnValueFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue)
{
return CreateParameterFromManagedExpression (context, sourceValue, 0);
}
}
public class MyLegacyColorValueMarshaler : JniValueMarshaler<MyLegacyColor> {
public override Type MarshalType {
get {return typeof (int);}
}
public override MyLegacyColor CreateGenericValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
{
throw new NotImplementedException ();
}
public override JniValueMarshalerState CreateGenericObjectReferenceArgumentState (MyLegacyColor value, ParameterAttributes synchronize)
{
throw new NotImplementedException ();
}
public override void DestroyGenericArgumentState (MyLegacyColor value, ref JniValueMarshalerState state, ParameterAttributes synchronize)
{
throw new NotImplementedException ();
}
public override Expression CreateParameterToManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type targetType)
{
var c = typeof (MyLegacyColor).GetConstructor (new[]{typeof (int)});
var v = Expression.Variable (typeof (MyLegacyColor), sourceValue.Name + "_val");
context.LocalVariables.Add (v);
context.CreationStatements.Add (Expression.Assign (v, Expression.New (c, sourceValue)));
return v;
}
public override Expression CreateParameterFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
{
var r = Expression.Variable (MarshalType, sourceValue.Name + "_p");
context.LocalVariables.Add (r);
context.CreationStatements.Add (Expression.Assign (r, Expression.Field (sourceValue, "Value")));
return r;
}
public override Expression CreateReturnValueFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue)
{
return CreateParameterFromManagedExpression (context, sourceValue, 0);
}
}
}