-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java.Interop] Remove IJavaObject constraint on JavaObjectArray.
(Work in progress!) Allow e.g. JavaObjectArray<object> (i.e. a non-IJavaObject type). This requires the introduction of JavaProxyObject, which allows the JVM to "hold onto" a non-IJavaObject type. This in turn requires the generation of the new "java-interop.jar" file, which MUST be included in the $CLASSPATH of all Java.Interop-using JVMs. TODO: * Support builtin types (JavaObjectArray<int> causes things to blow up, as JNIEnv::FindClass("I") throws), * "Full" collection marshaling (JavaObjectArray<int[]> should deep-marshal the array instead of using JavaProxyObject) Note: Xamarin.Android's JavaProxyObject equivalent -- Android.Runtime.JavaObject -- overrides Java.Lang.Object.Clone(). JavaProxyObject does not, because ICloneable isn't part of the PCL profile that Java.Interop is targeting, and thus there's no way to reference the ICloneable.Clone() method w/o resorting to Reflection.
- Loading branch information
Showing
7 changed files
with
172 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Java.Interop { | ||
|
||
[JniTypeInfo (JavaProxyObject.JniTypeName)] | ||
sealed class JavaProxyObject : JavaObject | ||
{ | ||
internal const string JniTypeName = "com/xamarin/android/internal/JavaProxyObject"; | ||
|
||
static readonly JniType TypeRef; | ||
static readonly ConditionalWeakTable<object, JavaProxyObject> CachedValues; | ||
|
||
static JavaProxyObject () | ||
{ | ||
TypeRef = new JniType (JniTypeName); | ||
TypeRef.RegisterWithVM (); | ||
TypeRef.RegisterNativeMethods ( | ||
new JniNativeMethodRegistration ("equals", "(Ljava/lang/Object;)Z", (Func<IntPtr, IntPtr, IntPtr, bool>) _Equals), | ||
new JniNativeMethodRegistration ("hashCode", "()I", (Func<IntPtr, IntPtr, int>) _GetHashCode), | ||
new JniNativeMethodRegistration ("toString", "()Ljava/lang/String;", (Func<IntPtr, IntPtr, IntPtr>) _ToString)); | ||
CachedValues = new ConditionalWeakTable<object, JavaProxyObject> (); | ||
} | ||
|
||
JavaProxyObject (object value) | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException ("value"); | ||
Value = value; | ||
} | ||
|
||
public object Value {get; private set;} | ||
|
||
public override int GetHashCode () | ||
{ | ||
return Value.GetHashCode (); | ||
} | ||
|
||
public override bool Equals (object obj) | ||
{ | ||
var other = obj as JavaProxyObject; | ||
if (other != null) | ||
return object.Equals (Value, other.Value); | ||
return object.Equals (Value, obj); | ||
} | ||
|
||
public override string ToString () | ||
{ | ||
return Value.ToString (); | ||
} | ||
|
||
public static JavaProxyObject GetProxy (object value) | ||
{ | ||
if (value == null) | ||
return null; | ||
|
||
lock (CachedValues) { | ||
JavaProxyObject proxy; | ||
if (CachedValues.TryGetValue (value, out proxy)) | ||
return proxy; | ||
proxy = new JavaProxyObject (value); | ||
proxy.RegisterWithVM (); | ||
CachedValues.Add (value, proxy); | ||
return proxy; | ||
} | ||
} | ||
|
||
static bool _Equals (IntPtr jnienv, IntPtr n_self, IntPtr n_value) | ||
{ | ||
JniEnvironment.CheckCurrent (jnienv); | ||
var self = JniEnvironment.Current.JavaVM.GetObject<JavaProxyObject> (n_self); | ||
var value = JniEnvironment.Current.JavaVM.GetObject (n_value); | ||
return self.Equals (value); | ||
} | ||
|
||
static int _GetHashCode (IntPtr jnienv, IntPtr n_self) | ||
{ | ||
JniEnvironment.CheckCurrent (jnienv); | ||
var self = JniEnvironment.Current.JavaVM.GetObject<JavaProxyObject> (n_self); | ||
return self.GetHashCode (); | ||
} | ||
|
||
static IntPtr _ToString (IntPtr jnienv, IntPtr n_self) | ||
{ | ||
JniEnvironment.CheckCurrent (jnienv); | ||
var self = JniEnvironment.Current.JavaVM.GetObject<JavaProxyObject> (n_self); | ||
var s = self.ToString (); | ||
using (var r = JniEnvironment.Strings.NewString (s)) | ||
return JniEnvironment.Handles.NewReturnToJniRef (r); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
|
||
namespace Java.Interop { | ||
|
||
static class JniMarshal { | ||
|
||
public static T GetValue<T> (JniReferenceSafeHandle handle, JniHandleOwnership transfer) | ||
{ | ||
var target = JniEnvironment.Current.JavaVM.GetObject (handle, transfer, typeof (T)); | ||
var proxy = target as JavaProxyObject; | ||
if (proxy != null) | ||
return (T) proxy.Value; | ||
return (T) target; | ||
} | ||
|
||
public static JniLocalReference CreateLocalRef<T> (T value) | ||
{ | ||
var o = (value as IJavaObject) ?? | ||
JavaProxyObject.GetProxy (value); | ||
if (o == null || o.SafeHandle.IsInvalid) | ||
return new JniLocalReference (); | ||
return o.SafeHandle.NewLocalRef (); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Java.Interop/java/com/xamarin/android/internal/JavaProxyObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.xamarin.android.internal; | ||
|
||
/* package */ class JavaProxyObject extends java.lang.Object { | ||
|
||
@Override | ||
public native boolean equals(Object obj); | ||
|
||
@Override | ||
public native int hashCode(); | ||
|
||
@Override | ||
public native String toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters