public static void MonitorExit(JniObjectReference instance) { int r = _MonitorExit(instance); if (r != 0) { var e = JniEnvironment.GetExceptionForLastThrowable(); if (e != null) { ExceptionDispatchInfo.Capture(e).Throw(); } throw new InvalidOperationException(string.Format("Could not exit monitor; JNIEnv::MonitorExit() returned {0}.", r)); } }
public static void Throw(JniObjectReference toThrow) { if (!toThrow.IsValid) { throw new ArgumentException(nameof(toThrow)); } int r = _Throw(toThrow); if (r != 0) { throw new InvalidOperationException(string.Format("Could not raise an exception; JNIEnv::Throw() returned {0}.", r)); } }
public JniArgumentValue(JniObjectReference value) { this = new JniArgumentValue(); var sh = value.SafeHandle; if (sh != null) { l = value.SafeHandle.DangerousGetHandle(); } else { l = value.Handle; } }
/// <summary> /// Try to garbage collect <paramref name="value"/>. /// </summary> /// <returns> /// <c>true</c>, if <paramref name="value"/> was collected and /// <paramref name="handle"/> is invalid; otherwise <c>false</c>. /// </returns> /// <param name="value"> /// The <see cref="T:Java.Interop.IJavaPeerable"/> instance to collect. /// </param> /// <param name="handle"> /// The <see cref="T:Java.Interop.JniObjectReference"/> of <paramref name="value"/>. /// This value may be updated, and <see cref="P:Java.Interop.IJavaObject.PeerReference"/> /// will be updated with this value. /// </param> internal protected virtual bool TryGC(IJavaPeerable value, ref JniObjectReference handle) { if (!handle.IsValid) { return(true); } var wgref = handle.NewWeakGlobalRef(); JniObjectReference.Dispose(ref handle); JniGC.Collect(); handle = wgref.NewGlobalRef(); JniObjectReference.Dispose(ref wgref); return(!handle.IsValid); }
public virtual void DeleteWeakGlobalReference(ref JniObjectReference reference) { if (!reference.IsValid) { return; } AssertReferenceType(ref reference, JniObjectReferenceType.WeakGlobal); if (WeakGlobalReferenceCount < 0) { AssertCount(WeakGlobalReferenceCount, "WGREF", reference.ToString()); } JniEnvironment.References.DeleteWeakGlobalRef(reference.Handle); reference.Invalidate(); }
public virtual JniObjectReference CreateWeakGlobalReference(JniObjectReference reference) { if (!reference.IsValid) { return(reference); } var n = JniEnvironment.References.NewWeakGlobalRef(reference); if (WeakGlobalReferenceCount < 0) { AssertCount(WeakGlobalReferenceCount, "WGREF", reference.ToString()); } return(n); }
object PeekBoxedObject(JniObjectReference reference) { var t = PeekPeer(reference); if (t == null) { return(null); } object r; return(TryUnboxObject(t, out r) ? r : null); }
public override void DeleteWeakGlobalReference(ref JniObjectReference reference) { if (!reference.IsValid) { return; } NativeMethods.java_interop_gc_bridge_weak_gref_log_delete(bridge, reference.Handle, ToByte(reference.Type), GetCurrentManagedThreadName(LogGlobalReferenceMessages), Environment.CurrentManagedThreadId, GetCurrentManagedThreadStack(LogGlobalReferenceMessages)); base.DeleteWeakGlobalReference(ref reference); }
public override void DestroyGenericArgumentState(object value, ref JniValueMarshalerState state, ParameterAttributes synchronize) { var vm = state.Extra as JniValueMarshaler; if (vm != null) { vm.DestroyArgumentState(value, ref state, synchronize); return; } var r = state.ReferenceValue; JniObjectReference.Dispose(ref r); state = new JniValueMarshalerState(); }
static void DeletedReference(TextWriter?writer, string kind, int count, JniObjectReference reference, JniRuntime runtime) { if (writer == null) { return; } string message = $"{kind} {count} handle {reference.ToString ()} " + $"from thread '{runtime.GetCurrentManagedThreadName ()}'({Environment.CurrentManagedThreadId})" + Environment.NewLine + runtime.GetCurrentManagedThreadStackTrace(skipFrames: 2, fNeedFileInfo: true); writer.WriteLine(message); writer.Flush(); }
public virtual void DeleteLocalReference(ref JniObjectReference reference, ref int localReferenceCount) { if (!reference.IsValid) { return; } AssertReferenceType(ref reference, JniObjectReferenceType.Local); localReferenceCount--; if (localReferenceCount < 0) { AssertCount(localReferenceCount, "LREF", reference.ToString()); } JniEnvironment.References.DeleteLocalRef(reference.Handle); reference.Invalidate(); }
public override IntPtr ReleaseLocalReference(ref JniObjectReference reference, ref int localReferenceCount) { if (!reference.IsValid) { return(IntPtr.Zero); } NativeMethods.java_interop_gc_bridge_lref_log_delete(bridge, localReferenceCount, reference.Handle, ToByte(reference.Type), GetCurrentManagedThreadName(LogLocalReferenceMessages), Environment.CurrentManagedThreadId, GetCurrentManagedThreadStack(LogLocalReferenceMessages)); return(base.ReleaseLocalReference(ref reference, ref localReferenceCount)); }
public static JniType?GetTypeFromInstance(JniObjectReference instance) { if (!instance.IsValid) { return(null); } var lref = JniEnvironment.Types.GetObjectClass(instance); if (lref.IsValid) { return(new JniType(ref lref, JniObjectReferenceOptions.CopyAndDispose)); } return(null); }
public static unsafe JniType DefineClass(string name, JniObjectReference loader, byte[] classFileData) { if (classFileData == null) { return(null); fixed(byte *buf = classFileData) { var lref = JniEnvironment.Types.DefineClass(name, loader, (IntPtr)buf, classFileData.Length); return(new JniType(ref lref, JniObjectReferenceOptions.CopyAndDispose)); } } bool registered; JniObjectReference peerReference;
public virtual IntPtr ReleaseLocalReference(ref JniObjectReference reference, ref int localReferenceCount) { if (!reference.IsValid) { return(IntPtr.Zero); } localReferenceCount--; if (localReferenceCount < 0) { AssertCount(localReferenceCount, "LREF", reference.ToString()); } var h = reference.Handle; reference.Invalidate(); return(h); }
public static void CreatedReference(JniObjectReference value) { if (!value.IsValid) { return; } switch (value.Type) { case JniObjectReferenceType.Local: Runtime.ObjectReferenceManager.CreatedLocalReference(CurrentInfo, value); break; default: throw new ArgumentException("Only JNI Local References are currently supported.", nameof(value)); } }
public void Dispose() { if (!PeerReference.IsValid) { return; } if (registered) { JniEnvironment.Runtime.UnTrack(PeerReference.Handle); } if (methods != null) { UnregisterNativeMethods(); } JniObjectReference.Dispose(ref peerReference); }
public static string?GetJniTypeNameFromInstance(JniObjectReference instance) { if (!instance.IsValid) { return(null); } var lref = GetObjectClass(instance); try { return(GetJniTypeNameFromClass(lref)); } finally { JniObjectReference.Dispose(ref lref, JniObjectReferenceOptions.CopyAndDispose); } }
public unsafe JavaException(string message, Exception innerException) : base(message, innerException) { const string signature = "(Ljava/lang/String;)V"; var native_message = JniEnvironment.Strings.NewString(message); try { var args = stackalloc JniArgumentValue [1]; args [0] = new JniArgumentValue(native_message); var peer = JniPeerMembers.InstanceMethods.StartCreateInstance(signature, GetType(), args); Construct(ref peer, JniObjectReferenceOptions.CopyAndDispose); JniPeerMembers.InstanceMethods.FinishCreateInstance(signature, this, args); } finally { JniObjectReference.Dispose(ref native_message, JniObjectReferenceOptions.CopyAndDispose); } javaStackTrace = _GetJavaStack(PeerReference); }
public override void CreatedLocalReference(JniObjectReference reference, ref int localReferenceCount) { if (!reference.IsValid) { return; } base.CreatedLocalReference(reference, ref localReferenceCount); NativeMethods.java_interop_gc_bridge_lref_log_new(bridge, localReferenceCount, reference.Handle, ToByte(reference.Type), IntPtr.Zero, (byte)0, GetCurrentManagedThreadName(LogLocalReferenceMessages), Environment.CurrentManagedThreadId, GetCurrentManagedThreadStack(LogLocalReferenceMessages)); }
protected void SetPeerReference(ref JniObjectReference reference, JniObjectReferenceOptions options) { if (options == JniObjectReferenceOptions.None) { ((IJavaPeerable)this).SetPeerReference(new JniObjectReference()); return; } #if FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES this.reference = reference; #endif // FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES #if FEATURE_JNIOBJECTREFERENCE_INTPTRS this.handle = reference.Handle; this.handle_type = reference.Type; #endif // FEATURE_JNIOBJECTREFERENCE_INTPTRS JniObjectReference.Dispose(ref reference, options); }
public override JniObjectReference CreateWeakGlobalReference(JniObjectReference reference) { if (!reference.IsValid) { return(reference); } var n = base.CreateWeakGlobalReference(reference); NativeMethods.java_interop_gc_bridge_weak_gref_log_new(bridge, reference.Handle, ToByte(reference.Type), n.Handle, ToByte(n.Type), GetCurrentManagedThreadName(LogGlobalReferenceMessages), Environment.CurrentManagedThreadId, GetCurrentManagedThreadStack(LogGlobalReferenceMessages)); return(n); }
public override void ActivatePeer(IJavaPeerable?self, JniObjectReference reference, ConstructorInfo cinfo, object?[]?argumentValues) { var runtime = JniEnvironment.Runtime; try { var f = runtime.MarshalMemberBuilder.CreateConstructActivationPeerFunc(cinfo); f(cinfo, reference, argumentValues); } catch (Exception e) { var m = string.Format("Could not activate {{ PeerReference={0} IdentityHashCode=0x{1} Java.Type={2} }} for managed type '{3}'.", reference, runtime.ValueManager.GetJniIdentityHashCode(reference).ToString("x"), JniEnvironment.Types.GetJniTypeNameFromInstance(reference), cinfo.DeclaringType.FullName); Debug.WriteLine(m); throw new NotSupportedException(m, e); } }
public static void ThrowNew(JniObjectReference klass, string message) { if (!klass.IsValid) { throw new ArgumentException(nameof(klass)); } if (message == null) { throw new ArgumentNullException(nameof(message)); } int r = _ThrowNew(klass, message); if (r != 0) { throw new InvalidOperationException(string.Format("Could not raise an exception; JNIEnv::ThrowNew() returned {0}.", r)); } }
public override IJavaPeerable?PeekPeer(JniObjectReference reference) { if (RegisteredInstances == null) { throw new ObjectDisposedException(nameof(MonoRuntimeValueManager)); } if (!reference.IsValid) { return(null); } int key = GetJniIdentityHashCode(reference); lock (RegisteredInstances) { List <WeakReference <IJavaPeerable> > peers; if (!RegisteredInstances.TryGetValue(key, out peers)) { return(null); } for (int i = peers.Count - 1; i >= 0; i--) { var wp = peers [i]; IJavaPeerable p; if (!wp.TryGetTarget(out p)) { // Peer was collected peers.RemoveAt(i); continue; } if (JniEnvironment.Types.IsSameObject(reference, p.PeerReference)) { return(p); } } if (peers.Count == 0) { RegisteredInstances.Remove(key); } } return(null); }
static bool Equals(IntPtr jnienv, IntPtr n_self, IntPtr n_value) { var envp = new JniTransition(jnienv); try { var self = (JavaProxyObject?)JniEnvironment.Runtime.ValueManager.PeekPeer(new JniObjectReference(n_self)); var r_value = new JniObjectReference(n_value); var value = JniEnvironment.Runtime.ValueManager.GetValue(ref r_value, JniObjectReferenceOptions.Copy); return(self?.Equals(value) ?? false); } catch (Exception e) when(JniEnvironment.Runtime.ExceptionShouldTransitionToJni(e)) { envp.SetPendingException(e); return(false); } finally { envp.Dispose(); } }
internal static void DeleteLocalReference(JniLocalReference value, IntPtr handle) { var localRefs = Info.Value.LocalReferences; var c = localRefs.FirstOrDefault(r => r.Contains(value)); if (c == null) { Runtime.ObjectReferenceManager.WriteLocalReferenceLine( "Deleting JNI local reference handle 0x{0} from wrong thread {1}! Ignoring...", handle.ToString("x"), Runtime.GetCurrentThreadDescription()); Runtime.ObjectReferenceManager.WriteLocalReferenceLine("{0}", System.Activator.CreateInstance(Type.GetType("System.Diagnostics.StackTrace"))); return; } c.Remove(value); var lref = new JniObjectReference(value, JniObjectReferenceType.Local); Runtime.ObjectReferenceManager.DeleteLocalReference(Info.Value, ref lref); value.SetHandleAsInvalid(); }
public object PeekValue(JniObjectReference reference) { if (!reference.IsValid) { return(null); } var t = PeekPeer(reference); if (t == null) { return(t); } object r; return(TryUnboxObject(t, out r) ? r : t); }
public override JniObjectReference CreateLocalReference(JniObjectReference reference, ref int localReferenceCount) { if (!reference.IsValid) { return(reference); } var r = base.CreateLocalReference(reference, ref localReferenceCount); NativeMethods.java_interop_gc_bridge_lref_log_new(bridge, localReferenceCount, reference.Handle, ToByte(reference.Type), r.Handle, ToByte(r.Type), GetCurrentManagedThreadName(LogLocalReferenceMessages), Environment.CurrentManagedThreadId, GetCurrentManagedThreadStack(LogLocalReferenceMessages)); return(r); }
public T CreateValue <T> (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null) { if (disposed) { throw new ObjectDisposedException(GetType().Name); } if (!reference.IsValid) { return(default(T)); } if (targetType != null && !typeof(T).GetTypeInfo().IsAssignableFrom(targetType.GetTypeInfo())) { throw new ArgumentException( string.Format("Requested runtime '{0}' value of '{1}' is not compatible with requested compile-time type T of '{2}'.", nameof(targetType), targetType, typeof(T)), nameof(targetType)); } var boxed = PeekBoxedObject(reference); if (boxed != null) { JniObjectReference.Dispose(ref reference, options); return((T)Convert.ChangeType(boxed, targetType ?? typeof(T))); } targetType = targetType ?? typeof(T); if (typeof(IJavaPeerable).GetTypeInfo().IsAssignableFrom(targetType.GetTypeInfo())) { return((T)JavaPeerableValueMarshaler.Instance.CreateGenericValue(ref reference, options, targetType)); } var marshaler = GetValueMarshaler <T> (); return(marshaler.CreateGenericValue(ref reference, options, targetType)); }