Example #1
0
        static void n_Activate(IntPtr jnienv, IntPtr jclass, IntPtr typename_ptr, IntPtr signature_ptr, IntPtr jobject, IntPtr parameters_ptr)
        {
            var o  = Java.Lang.Object.PeekObject(jobject);
            var ex = o as IJavaObjectEx;

            if (ex != null)
            {
                if (!ex.NeedsActivation && !ex.IsProxy)
                {
                    return;
                }
            }
            if (!ActivationEnabled)
            {
                if (Logger.LogGlobalRef)
                {
                    Logger.Log(LogLevel.Info, "monodroid-gref",
                               string.Format("warning: Skipping managed constructor invocation for handle 0x{0} (key_handle 0x{1}). " +
                                             "Please use JNIEnv.StartCreateInstance() + JNIEnv.FinishCreateInstance() instead of " +
                                             "JNIEnv.NewObject() and/or JNIEnv.CreateInstance().",
                                             jobject.ToString("x"), JNIEnv.IdentityHash(jobject).ToString("x")));
                }
                return;
            }

            Type type = Type.GetType(JNIEnv.GetString(typename_ptr, JniHandleOwnership.DoNotTransfer), throwOnError: true);

            if (type.IsGenericTypeDefinition)
            {
                throw new NotSupportedException(
                          "Constructing instances of generic types from Java is not supported, as the type parameters cannot be determined.",
                          CreateJavaLocationException());
            }
            Type[]          ptypes = GetParameterTypes(JNIEnv.GetString(signature_ptr, JniHandleOwnership.DoNotTransfer));
            object[]        parms  = JNIEnv.GetObjectArray(parameters_ptr, ptypes);
            ConstructorInfo cinfo  = type.GetConstructor(ptypes);

            if (cinfo == null)
            {
                throw CreateMissingConstructorException(type, ptypes);
            }
            if (o != null)
            {
                cinfo.Invoke(o, parms);
                return;
            }
            try {
                var activator = ConstructorBuilder.CreateDelegate(type, cinfo, ptypes);
                activator(jobject, parms);
            } catch (Exception e) {
                var m = string.Format("Could not activate JNI Handle 0x{0} (key_handle 0x{1}) of Java type '{2}' as managed type '{3}'.",
                                      jobject.ToString("x"), JNIEnv.IdentityHash(jobject).ToString("x"), JNIEnv.GetClassNameFromInstance(jobject), type.FullName);
                Logger.Log(LogLevel.Warn, "monodroid", m);
                Logger.Log(LogLevel.Warn, "monodroid", CreateJavaLocationException().ToString());

                throw new NotSupportedException(m, e);
            }
        }
Example #2
0
        internal static void Activate(IJavaPeerable?o, IntPtr jobject, ConstructorInfo cinfo, object? []?parms)
        {
            try {
                var activator = ConstructorBuilder.CreateDelegate(cinfo);
                activator(jobject, parms);
            } catch (Exception e) {
                var m = string.Format("Could not activate JNI Handle 0x{0} (key_handle 0x{1}) of Java type '{2}' as managed type '{3}'.",
                                      jobject.ToString("x"), JNIEnv.IdentityHash !(jobject).ToString("x"), JNIEnv.GetClassNameFromInstance(jobject), cinfo.DeclaringType.FullName);
                Logger.Log(LogLevel.Warn, "monodroid", m);
                Logger.Log(LogLevel.Warn, "monodroid", CreateJavaLocationException().ToString());

                throw new NotSupportedException(m, e);
            }
        }