Exemplo n.º 1
0
        static void RegisterNativeMembers(
            IntPtr jnienv,
            IntPtr klass,
            IntPtr n_nativeClass,
            IntPtr n_assemblyQualifiedName,
            IntPtr n_methods)
        {
            var envp = new JniTransition(jnienv);

            try {
                var r_nativeClass = new JniObjectReference(n_nativeClass);
                var nativeClass   = new JniType(ref r_nativeClass, JniObjectReferenceOptions.Copy);

                var assemblyQualifiedName = JniEnvironment.Strings.ToString(new JniObjectReference(n_assemblyQualifiedName));
                var methods = JniEnvironment.Strings.ToString(new JniObjectReference(n_methods));

                var type = Type.GetType(assemblyQualifiedName, throwOnError: true);

                JniEnvironment.Runtime.TypeManager.RegisterNativeMembers(nativeClass, type, methods);
            }
            catch (Exception e) when(JniEnvironment.Runtime.ExceptionShouldTransitionToJni(e))
            {
                Debug.WriteLine(e.ToString());
                envp.SetPendingException(e);
            }
            finally {
                envp.Dispose();
            }
        }
Exemplo n.º 2
0
        static int GetHashCode(IntPtr jnienv, IntPtr n_self)
        {
            var envp = new JniTransition(jnienv);

            try {
                var self = (JavaProxyObject?)JniEnvironment.Runtime.ValueManager.PeekPeer(new JniObjectReference(n_self));
                return(self?.GetHashCode() ?? 0);
            }
            catch (Exception e) when(JniEnvironment.Runtime.ExceptionShouldTransitionToJni(e))
            {
                envp.SetPendingException(e);
                return(0);
            }
            finally {
                envp.Dispose();
            }
        }
Exemplo n.º 3
0
        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();
            }
        }
Exemplo n.º 4
0
        static IntPtr ToString(IntPtr jnienv, IntPtr n_self)
        {
            var envp = new JniTransition(jnienv);

            try {
                var self = (JavaProxyObject?)JniEnvironment.Runtime.ValueManager.PeekPeer(new JniObjectReference(n_self));
                var s    = self?.ToString();
                var r    = JniEnvironment.Strings.NewString(s);
                try {
                    return(JniEnvironment.References.NewReturnToJniRef(r));
                } finally {
                    JniObjectReference.Dispose(ref r);
                }
            }
            catch (Exception e) when(JniEnvironment.Runtime.ExceptionShouldTransitionToJni(e))
            {
                envp.SetPendingException(e);
                return(IntPtr.Zero);
            }
            finally {
                envp.Dispose();
            }
        }
Exemplo n.º 5
0
        // TODO: Keep in sync with the code generated by ExportedMemberBuilder
        static void Construct(
            IntPtr jnienv,
            IntPtr klass,
            IntPtr n_self,
            IntPtr n_assemblyQualifiedName,
            IntPtr n_constructorSignature,
            IntPtr n_constructorArguments)
        {
            var envp = new JniTransition(jnienv);

            try {
                var runtime = JniEnvironment.Runtime;
                var r_self  = new JniObjectReference(n_self);
                var self    = runtime.ValueManager.PeekPeer(r_self);
                if (self != null)
                {
                    var state = self.JniManagedPeerState;
                    if ((state & JniManagedPeerStates.Activatable) != JniManagedPeerStates.Activatable &&
                        (state & JniManagedPeerStates.Replaceable) != JniManagedPeerStates.Replaceable)
                    {
                        return;
                    }
                }

                if (JniEnvironment.WithinNewObjectScope)
                {
                    if (runtime.ObjectReferenceManager.LogGlobalReferenceMessages)
                    {
                        runtime.ObjectReferenceManager.WriteGlobalReferenceLine(
                            "Warning: Skipping managed constructor invocation for PeerReference={0} IdentityHashCode=0x{1} Java.Type={2}. " +
                            "Please use JniPeerMembers.InstanceMethods.StartCreateInstance() + JniPeerMembers.InstanceMethods.FinishCreateInstance() instead of " +
                            "JniEnvironment.Object.NewObject().",
                            r_self,
                            runtime.ValueManager.GetJniIdentityHashCode(r_self).ToString("x"),
                            JniEnvironment.Types.GetJniTypeNameFromInstance(r_self));
                    }
                    return;
                }

                var type = Type.GetType(JniEnvironment.Strings.ToString(n_assemblyQualifiedName), throwOnError: true);
                var info = type.GetTypeInfo();
                if (info.IsGenericTypeDefinition)
                {
                    throw new NotSupportedException(
                              "Constructing instances of generic types from Java is not supported, as the type parameters cannot be determined.",
                              CreateJniLocationException());
                }

                var ptypes  = GetParameterTypes(JniEnvironment.Strings.ToString(n_constructorSignature));
                var pvalues = GetValues(runtime, new JniObjectReference(n_constructorArguments), ptypes);
                var ctor    = info.DeclaredConstructors
                              .FirstOrDefault(c => !c.IsStatic &&
                                              c.GetParameters().Select(p => p.ParameterType).SequenceEqual(ptypes));
                if (ctor == null)
                {
                    throw CreateMissingConstructorException(type, ptypes);
                }
                if (self != null)
                {
                    ctor.Invoke(self, pvalues);
                    return;
                }

                try {
                    var f = JniEnvironment.Runtime.MarshalMemberBuilder.CreateConstructActivationPeerFunc(ctor);
                    f(ctor, new JniObjectReference(n_self), pvalues);
                }
                catch (Exception e) {
                    var m = string.Format("Could not activate {{ PeerReference={0} IdentityHashCode=0x{1} Java.Type={2} }} for managed type '{3}'.",
                                          r_self,
                                          runtime.ValueManager.GetJniIdentityHashCode(r_self).ToString("x"),
                                          JniEnvironment.Types.GetJniTypeNameFromInstance(r_self),
                                          type.FullName);
                    Debug.WriteLine(m);

                    throw new NotSupportedException(m, e);
                }
            }
            catch (Exception e) when(JniEnvironment.Runtime.ExceptionShouldTransitionToJni(e))
            {
                envp.SetPendingException(e);
            }
            finally {
                envp.Dispose();
            }
        }