Exemplo n.º 1
0
        protected override void WriteGetterBody(CodeWriter writer)
        {
            writer.WriteLine($"const string __id = \"{field.JavaName}.{field.Symbol.JniName}\";");
            writer.WriteLine();

            var invokeType = SourceWriterExtensions.GetInvokeType(field.GetMethodPrefix);
            var indirect   = field.IsStatic ? "StaticFields" : "InstanceFields";
            var invoke     = "Get{0}Value";

            invoke = string.Format(invoke, invokeType);

            writer.WriteLine($"var __v = {field.Symbol.ReturnCast}_members.{indirect}.{invoke} (__id{(field.IsStatic ? "" : ", this")});");

            if (field.Symbol.IsArray)
            {
                writer.WriteLine($"return global::Android.Runtime.JavaArray<{opt.GetOutputName (field.Symbol.ElementType)}>.FromJniHandle (__v.Handle, JniHandleOwnership.TransferLocalRef);");
            }
            else if (field.Symbol.NativeType != field.Symbol.FullName)
            {
                writer.WriteLine($"return {field.Symbol.ReturnCast}{(field.Symbol.FromNative (opt, invokeType != "Object" ? "__v" : "__v.Handle", true) + opt.GetNullForgiveness (field))};");
            }
            else
            {
                writer.WriteLine("return __v;");
            }
        }
Exemplo n.º 2
0
        protected override void WriteSetterBody(CodeWriter writer)
        {
            writer.WriteLine($"const string __id = \"{field.JavaName}.{field.Symbol.JniName}\";");
            writer.WriteLine();

            var invokeType = SourceWriterExtensions.GetInvokeType(field.GetMethodPrefix);
            var indirect   = field.IsStatic ? "StaticFields" : "InstanceFields";

            string arg;
            bool   have_prep = false;

            if (field.Symbol.IsArray)
            {
                arg = opt.GetSafeIdentifier(TypeNameUtilities.GetNativeName("value"));
                writer.WriteLine($"IntPtr {arg} = global::Android.Runtime.JavaArray<{opt.GetOutputName (field.Symbol.ElementType)}>.ToLocalJniHandle (value);");
            }
            else
            {
                foreach (var prep in field.SetParameters.GetCallPrep(opt))
                {
                    have_prep = true;
                    writer.WriteLine(prep);
                }

                arg = field.SetParameters [0].ToNative(opt);

                if (field.SetParameters.HasCleanup && !have_prep)
                {
                    arg = opt.GetSafeIdentifier(TypeNameUtilities.GetNativeName("value"));
                    writer.WriteLine($"IntPtr {arg} = global::Android.Runtime.JNIEnv.ToLocalJniHandle (value);");
                }
            }

            writer.WriteLine("try {");

            writer.WriteLine($"\t_members.{indirect}.SetValue (__id{(field.IsStatic ? "" : ", this")}, {(invokeType != "Object" ? arg : "new JniObjectReference (" + arg + ")")});");

            writer.WriteLine("} finally {");
            writer.Indent();

            if (field.Symbol.IsArray)
            {
                writer.WriteLine($"global::Android.Runtime.JNIEnv.DeleteLocalRef ({arg});");
            }
            else
            {
                foreach (var cleanup in field.SetParameters.GetCallCleanup(opt))
                {
                    writer.WriteLine(cleanup);
                }
                if (field.SetParameters.HasCleanup && !have_prep)
                {
                    writer.WriteLine($"global::Android.Runtime.JNIEnv.DeleteLocalRef ({arg});");
                }
            }

            writer.Unindent();
            writer.WriteLine("}");
        }