Skip to content

Commit f60906c

Browse files
committed
[Java.Interop] Move JniRuntime type-related methods to JniTypeManager
In keeping with the principal of commit 6a42bb8 -- reduce API surface area, or at least organize into reasonable sections -- move the 5 JniRuntime methods that deal with JNI type signature :: System.Type mapping and move them into the new (nested) JniRuntime.JniTypeManager type. An instance of JniRuntime.JniTypeManager is available from the new JniRuntime.TypeManager property, permitting use such as: JniTypeSignature signature = runtime.TypeManager.GetTypeSignature ("[I"); Type type = runtime.TypeManager.GetType (signature); Additionally, since type mapping is always "iffy" [0], add additional JniRuntime.JniTypeManager method overloads which permit returning more than one possible mapping: partial class JniTypeManager { public IEnumerable<JniTypeSignature> GetTypeSignatures (Type type); public IEnumerable<Type> GetTypes (JniTypeSignature typeSignature); } Convenience "overloads" which return a single mapping are also present, which merely return .FirstOrDefault() on the above methods. [0]: How should an `int[]` marshal to Java? (a) As an `int[]`, a'la Xamarin.Android (b) As a JavaInt32Array (c) As a JavaPrimitiveArray<int> (d) As a JavaObjectArray<int> All of these are plausible, depending on the situation, and we should arguably support *all* of them, but means that the mapping is inherently ambiguous.
1 parent 652dcda commit f60906c

File tree

19 files changed

+740
-629
lines changed

19 files changed

+740
-629
lines changed

src/Android.Interop/Java.Interop/AndroidVM.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public AndroidVMBuilder ()
2727
NewObjectRequired = ((int) Android.OS.Build.VERSION.SdkInt) <= 10;
2828
InvocationPointer = invocationPointer;
2929
ObjectReferenceManager = Java.InteropTests.LoggingJniObjectReferenceManagerDecorator.GetObjectReferenceManager (new JniObjectReferenceManager ());
30+
TypeManager = new AndroidTypeManager ();
3031
}
3132

3233
public AndroidVM CreateAndroidVM ()
@@ -35,6 +36,18 @@ public AndroidVM CreateAndroidVM ()
3536
}
3637
}
3738

39+
class AndroidTypeManager : JniRuntime.JniTypeManager {
40+
41+
protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpleReference)
42+
{
43+
foreach (var t in base.GetTypesForSimpleReference (jniSimpleReference))
44+
yield return t;
45+
Type target = ((AndroidVM) Runtime).GetTypeMapping (jniSimpleReference);
46+
if (target != null)
47+
yield return target;
48+
}
49+
}
50+
3851
public class AndroidVM : JniRuntime {
3952

4053
internal AndroidVM (AndroidVMBuilder builder)
@@ -69,13 +82,11 @@ public void AddTypeMapping (string jniTypeReference, Type type)
6982
}
7083
}
7184

72-
public override Type GetTypeForJniSimplifiedTypeReference (string jniTypeReference)
85+
internal Type GetTypeMapping (string jniSimpleReference)
7386
{
74-
Type target = base.GetTypeForJniSimplifiedTypeReference (jniTypeReference);
75-
if (target != null)
76-
return target;
87+
Type target;
7788
lock (typeMappings) {
78-
if (typeMappings != null && typeMappings.TryGetValue (jniTypeReference, out target))
89+
if (typeMappings.TryGetValue (jniSimpleReference, out target))
7990
return target;
8091
}
8192
return null;

src/Java.Interop.Dynamic/Java.Interop.Dynamic/JavaClassInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Dictionary<string, List<JavaFieldInfo>> LookupFields ()
227227

228228
var n_type = Field_getType.InvokeVirtualObjectMethod (field);
229229
using (var type = new JniType (ref n_type, JniObjectReferenceOptions.DisposeSourceReference)) {
230-
var info = JniEnvironment.Runtime.GetJniTypeInfoForJniTypeReference (type.Name);
230+
var info = JniEnvironment.Runtime.TypeManager.GetTypeSignature (type.Name);
231231
overloads.Add (new JavaFieldInfo (Members, name + "\u0000" + info.QualifiedReference, isStatic));
232232
}
233233

@@ -324,7 +324,7 @@ static List<JniType> GetJniTypes (DynamicMetaObject[] args)
324324
var vm = JniEnvironment.Runtime;
325325
foreach (var a in args) {
326326
try {
327-
var at = new JniType (vm.GetJniTypeInfoForType (a.LimitType).QualifiedReference);
327+
var at = new JniType (vm.TypeManager.GetTypeSignature (a.LimitType).QualifiedReference);
328328
r.Add (at);
329329
} catch (JavaException e) {
330330
e.Dispose ();

src/Java.Interop.Dynamic/Java.Interop.Dynamic/JavaMethodBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public bool CompatibleWith (List<JniType> args, DynamicMetaObject[] dargs)
116116
for (int i = 0; i < arguments.Count; ++i) {
117117
if (args [i] == null) {
118118
// Builtin type -- JNIEnv.FindClass("I") throws!
119-
if (JniEnvironment.Types.GetJniTypeNameFromClass (arguments [i]) != vm.GetJniTypeInfoForType (dargs [i].LimitType).QualifiedReference)
119+
if (JniEnvironment.Types.GetJniTypeNameFromClass (arguments [i]) != vm.TypeManager.GetTypeSignature (dargs [i].LimitType).QualifiedReference)
120120
return false;
121121
}
122122
else if (!JniEnvironment.Types.IsAssignableFrom (arguments [i], args [i].PeerReference))

src/Java.Interop.Dynamic/Java.Interop.Dynamic/JavaMethodInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected override string JniReturnType {
5454
get {
5555
if (ReturnType == null)
5656
return "V";
57-
return JniEnvironment.Runtime.GetJniTypeInfoForJniTypeReference (ReturnType.Name).QualifiedReference;
57+
return JniEnvironment.Runtime.TypeManager.GetTypeSignature (ReturnType.Name).QualifiedReference;
5858
}
5959
}
6060

src/Java.Interop.Dynamic/Java.Interop.Dynamic/JniMetaObject.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public override DynamicMetaObject BindConvert (ConvertBinder binder)
4646
{
4747
var vm = JniEnvironment.Runtime;
4848
if (binder.Type == typeof (Type)) {
49-
var type = vm.GetTypeForJniTypeRefererence (info.JniClassName);
49+
var sig = vm.TypeManager.GetTypeSignature (info.JniClassName);
50+
var type = vm.TypeManager.GetType (sig);
5051
var typeE = Expression.Convert (Expression.Constant (type), binder.Type);
5152
return new DynamicMetaObject (typeE, BindingRestrictions.GetTypeRestriction (typeE, binder.Type), type);
5253
}

src/Java.Interop.Export/Java.Interop/ExportedMemberBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ public virtual string GetJniMethodSignature (JavaCallableAttribute export, Metho
7272

7373
var signature = new StringBuilder ().Append ("(");
7474
foreach (var p in method.GetParameters ()) {
75-
var info = Runtime.GetJniTypeInfoForType (p.ParameterType);
75+
var info = Runtime.TypeManager.GetTypeSignature (p.ParameterType);
7676
if (info.SimpleReference == null)
7777
throw new NotSupportedException ("Don't know how to determine JNI signature for parameter type: " + p.ParameterType.FullName + ".");
7878
signature.Append (info.QualifiedReference);
7979
}
8080
signature.Append (")");
81-
var ret = Runtime.GetJniTypeInfoForType (method.ReturnType);
81+
var ret = Runtime.TypeManager.GetTypeSignature (method.ReturnType);
8282
if (ret.SimpleReference == null)
8383
throw new NotSupportedException ("Don't know how to determine JNI signature for return type: " + method.ReturnType.FullName + ".");
8484
signature.Append (ret.QualifiedReference);

src/Java.Interop/Java.Interop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Java.Interop\JniMethodInfo.cs" />
6262
<Compile Include="Java.Interop\JniPeerMembers.cs" />
6363
<Compile Include="Java.Interop\JniRuntime.cs" />
64+
<Compile Include="Java.Interop\JniRuntime.JniTypeManager.cs" />
6465
<Compile Include="Java.Interop\JniStaticMethodInfo.cs" />
6566
<Compile Include="Java.Interop\JniStaticFieldInfo.cs" />
6667
<Compile Include="Java.Interop\JniTransition.cs" />

src/Java.Interop/Java.Interop/JavaObjectArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public JavaObjectArray (ref JniObjectReference handle, JniObjectReferenceOptions
1212

1313
static JniObjectReference _NewArray (int length)
1414
{
15-
var info = JniEnvironment.Runtime.GetJniTypeInfoForType (typeof (T));
15+
var info = JniEnvironment.Runtime.TypeManager.GetTypeSignature (typeof (T));
1616
if (info.SimpleReference == null)
1717
info = new JniTypeSignature ("java/lang/Object", info.ArrayRank);
1818
if (info.IsKeyword && info.ArrayRank == 0) {

src/Java.Interop/Java.Interop/JniMarshal.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ internal static T GetValue<T> (ref JniObjectReference reference, JniObjectRefere
6161
return (T) info.GetValueFromJni (ref reference, transfer, typeof (T));
6262
}
6363

64-
var targetType = jvm.GetTypeForJniTypeRefererence (JniEnvironment.Types.GetJniTypeNameFromInstance (reference));
64+
var signature = jvm.TypeManager.GetTypeSignature (JniEnvironment.Types.GetJniTypeNameFromInstance (reference));
65+
var targetType = jvm.TypeManager.GetType (signature);
6566
if (targetType != null &&
6667
typeof (T).IsAssignableFrom (targetType) &&
6768
(info = jvm.GetJniMarshalInfoForType (targetType)).GetValueFromJni != null) {

src/Java.Interop/Java.Interop/JniObjectReferenceManager.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99

1010
namespace Java.Interop {
1111

12-
public class JniObjectReferenceManager : IDisposable {
12+
public class JniObjectReferenceManager : IDisposable, JniRuntime.ISetRuntime {
13+
14+
15+
protected JniRuntime Runtime { get; private set; }
16+
17+
void JniRuntime.ISetRuntime.SetRuntime (JniRuntime runtime)
18+
{
19+
Runtime = runtime;
20+
}
1321

1422
int grefc;
1523
public virtual int GlobalReferenceCount {

0 commit comments

Comments
 (0)