예제 #1
0
        public void GenerateAbstractDeclaration(StreamWriter sw, string indent, CodeGenerationOptions opt, InterfaceGen gen, GenBase impl)
        {
            if (RetVal.IsGeneric && gen != null)
            {
                GenerateCustomAttributes(sw, indent);
                sw.WriteLine("{0}{1} {2}.{3} ({4})", indent, opt.GetOutputName(RetVal.FullName), opt.GetOutputName(gen.FullName), Name, GenBase.GetSignature(this, opt));
                sw.WriteLine("{0}{{", indent);
                sw.WriteLine("{0}\tthrow new NotImplementedException ();", indent);
                sw.WriteLine("{0}}}", indent);
                sw.WriteLine();
            }
            else
            {
                bool   gen_as_formatted = IsReturnCharSequence;
                string name             = AdjustedName;
                GenerateCallback(sw, indent, opt, impl, null, gen_as_formatted);
                sw.WriteLine("{0}// Metadata.xml XPath method reference: path=\"{1}\"", indent, GetMetadataXPathReference(this.DeclaringType));
                sw.WriteLine("{0}[Register (\"{1}\", \"{2}\", \"{3}\"{4})]", indent, JavaName, JniSignature, ConnectorName, this.AdditionalAttributeString());
                GenerateCustomAttributes(sw, indent);
                sw.WriteLine("{0}{1} abstract {2} {3} ({4});", indent, Visibility, opt.GetOutputName(RetVal.FullName), name, GenBase.GetSignature(this, opt));
                sw.WriteLine();

                if (gen_as_formatted || Parameters.HasCharSequence)
                {
                    GenerateStringOverload(sw, indent, opt);
                }
            }

            GenerateAsyncWrapper(sw, indent, opt);
        }
예제 #2
0
        void GenerateEventArgs(Method m, StreamWriter sw, string indent, CodeGenerationOptions opt)
        {
            string args_name = GetArgsName(m);

            if (m.RetVal.IsVoid || m.IsEventHandlerWithHandledProperty)
            {
                if (!m.IsSimpleEventHandler || m.IsEventHandlerWithHandledProperty)
                {
                    sw.WriteLine("{0}public partial class {1} : global::System.EventArgs {{", indent, args_name);
                    sw.WriteLine();
                    var signature = m.Parameters.GetSignatureDropSender(opt);
                    sw.WriteLine("{0}\tpublic {1} ({2}{3}{4})", indent, args_name,
                                 m.IsEventHandlerWithHandledProperty ? "bool handled" : "",
                                 (m.IsEventHandlerWithHandledProperty && signature.Length != 0) ? ", " : "",
                                 signature);
                    sw.WriteLine("{0}\t{{", indent);
                    if (m.IsEventHandlerWithHandledProperty)
                    {
                        sw.WriteLine("{0}\t\tthis.handled = handled;", indent);
                    }
                    foreach (Parameter p in m.Parameters)
                    {
                        if (!p.IsSender)
                        {
                            sw.WriteLine("{0}\t\tthis.{1} = {1};", indent, opt.GetSafeIdentifier(p.Name));
                        }
                    }
                    sw.WriteLine("{0}\t}}", indent);
                    if (m.IsEventHandlerWithHandledProperty)
                    {
                        sw.WriteLine();
                        sw.WriteLine("{0}\tbool handled;", indent);
                        sw.WriteLine("{0}\tpublic bool Handled {{", indent);
                        sw.WriteLine("{0}\t\tget {{ return handled; }}", indent);
                        sw.WriteLine("{0}\t\tset {{ handled = value; }}", indent);
                        sw.WriteLine("{0}\t}}", indent);
                    }
                    foreach (Parameter p in m.Parameters)
                    {
                        if (p.IsSender)
                        {
                            continue;
                        }
                        sw.WriteLine();
                        sw.WriteLine("{0}\t{1} {2};", indent, opt.GetOutputName(p.Type), opt.GetSafeIdentifier(p.Name));
                        // AbsListView.IMultiChoiceModeListener.onItemCheckedStateChanged() hit this strict name check, at parameter "@checked".
                        sw.WriteLine("{0}\tpublic {1} {2} {{", indent, opt.GetOutputName(p.Type), p.PropertyName);
                        sw.WriteLine("{0}\t\tget {{ return {1}; }}", indent, opt.GetSafeIdentifier(p.Name));
                        sw.WriteLine("{0}\t}}", indent);
                    }
                    sw.WriteLine("{0}}}", indent);
                    sw.WriteLine();
                }
            }
            else
            {
                sw.WriteLine("{0}public delegate {1} {2} ({3});", indent, opt.GetOutputName(m.RetVal.FullName), GetEventDelegateName(m), GenBase.GetSignature(m, opt));
                sw.WriteLine();
            }
        }
예제 #3
0
 public void GenerateExplicitInterfaceInvoker(StreamWriter sw, string indent, CodeGenerationOptions opt, GenBase iface)
 {
     //sw.WriteLine ("\t\t// explicitly implemented invoker method from " + iface.FullName);
     GenerateIdField(sw, indent, opt);
     sw.WriteLine("{0}unsafe {1} {2}.{3} ({4})",
                  indent, opt.GetOutputName(RetVal.FullName), opt.GetOutputName(iface.FullName), Name, GenBase.GetSignature(this, opt));
     sw.WriteLine("{0}{{", indent);
     GenerateBody(sw, indent + "\t", opt);
     sw.WriteLine("{0}}}", indent);
     sw.WriteLine();
 }
예제 #4
0
        public void GenerateExplicitIface(StreamWriter sw, string indent, CodeGenerationOptions opt, GenericSymbol gen, string adapter)
        {
            Dictionary <string, string> mappings = new Dictionary <string, string> ();

            for (int i = 0; i < gen.TypeParams.Length; i++)
            {
                mappings [gen.Gen.TypeParameters [i].Name] = gen.TypeParams [i].FullName;
            }

            //If the property type is Java.Lang.Object, we don't need to generate an explicit implementation
            if (Getter?.RetVal.GetGenericType(mappings) == "Java.Lang.Object")
            {
                return;
            }
            if (Setter?.Parameters[0].GetGenericType(mappings) == "Java.Lang.Object")
            {
                return;
            }

            sw.WriteLine("{0}// This method is explicitly implemented as a member of an instantiated {1}", indent, gen.FullName);
            sw.WriteLine("{0}{1} {2}.{3} {{", indent, opt.GetOutputName(Type), opt.GetOutputName(gen.Gen.FullName), AdjustedName);
            if (Getter != null)
            {
                if (gen.Gen.IsGeneratable)
                {
                    sw.WriteLine("{0}\t// Metadata.xml XPath method reference: path=\"{1}/method[@name='{2}'{3}]\"", indent, gen.Gen.MetadataXPathReference, Getter.JavaName, Getter.Parameters.GetMethodXPathPredicate());
                }
                if (Getter.GenericArguments != null && Getter.GenericArguments.Any())
                {
                    sw.WriteLine("{0}{1}", indent, Getter.GenericArguments.ToGeneratedAttributeString());
                }
                sw.WriteLine("{0}\t[Register (\"{1}\", \"{2}\", \"{3}:{4}\"{5})] get {{", indent, Getter.JavaName, Getter.JniSignature, Getter.ConnectorName, Getter.GetAdapterName(opt, adapter), Getter.AdditionalAttributeString());
                sw.WriteLine("{0}\t\treturn {1};", indent, Name);
                sw.WriteLine("{0}\t}}", indent);
            }
            if (Setter != null)
            {
                if (gen.Gen.IsGeneratable)
                {
                    sw.WriteLine("{0}\t// Metadata.xml XPath method reference: path=\"{1}/method[@name='{2}'{3}]\"", indent, gen.Gen.MetadataXPathReference, Setter.JavaName, Setter.Parameters.GetMethodXPathPredicate());
                }
                if (Setter.GenericArguments != null && Setter.GenericArguments.Any())
                {
                    sw.WriteLine("{0}{1}", indent, Setter.GenericArguments.ToGeneratedAttributeString());
                }
                sw.WriteLine("{0}\t[Register (\"{1}\", \"{2}\", \"{3}:{4}\"{5})] set {{", indent, Setter.JavaName, Setter.JniSignature, Setter.ConnectorName, Setter.GetAdapterName(opt, adapter), Setter.AdditionalAttributeString());
                sw.WriteLine("{0}\t\t{1} = {2};", indent, Name, Setter.Parameters.GetGenericCall(opt, mappings));
                sw.WriteLine("{0}\t}}", indent);
            }
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();
        }
예제 #5
0
        public string[] PreCallback(CodeGenerationOptions opt, string var_name, bool owned)
        {
            var rgm = this as IRequireGenericMarshal;

            return(new string[] {
                string.Format("{0} {1} = {5}global::Java.Lang.Object.GetObject<{4}> ({2}, {3});",
                              opt.GetOutputName(FullName),
                              opt.GetSafeIdentifier(var_name),
                              opt.GetSafeIdentifier(SymbolTable.GetNativeName(var_name)),
                              owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer",
                              opt.GetOutputName(rgm != null ? (rgm.GetGenericJavaObjectTypeOverride() ?? FullName) : FullName),
                              rgm != null ? "(" + opt.GetOutputName(FullName) + ")" : string.Empty)
            });
        }
예제 #6
0
 public string FromNative(CodeGenerationOptions opt, string var_name, bool owned)
 {
     if (!string.IsNullOrEmpty(managed_type) && (sym is ClassGen || sym is InterfaceGen))
     {
         if (opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.JavaInterop1)
         {
             return("global::Java.Interop.JniEnvironment.Runtime.ValueManager.GetValue<" +
                    opt.GetOutputName(managed_type) +
                    $"> (ref __rm, JniObjectReferenceOptions.Copy)");
         }
         return(string.Format("global::Java.Lang.Object.GetObject<{0}> ({1}, {2})",
                              opt.GetOutputName(managed_type), var_name, owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer"));
     }
     return(sym.FromNative(opt, var_name, owned));
 }
예제 #7
0
        public void GenerateInvokerBody(StreamWriter sw, string indent, CodeGenerationOptions opt)
        {
            sw.WriteLine("{0}if ({1} == IntPtr.Zero)", indent, EscapedIdName);
            sw.WriteLine("{0}\t{1} = JNIEnv.GetMethodID (class_ref, \"{2}\", \"{3}\");", indent, EscapedIdName, JavaName, JniSignature);
            foreach (string prep in Parameters.GetCallPrep(opt))
            {
                sw.WriteLine("{0}{1}", indent, prep);
            }
            Parameters.WriteCallArgs(sw, indent, opt, invoker: true);
            string env_method = "Call" + RetVal.CallMethodPrefix + "Method";
            string call       = "JNIEnv." + env_method + " (" +
                                opt.ContextType.GetObjectHandleProperty("this") + ", " + EscapedIdName + Parameters.GetCallArgs(opt, invoker: true) + ")";

            if (IsVoid)
            {
                sw.WriteLine("{0}{1};", indent, call);
            }
            else
            {
                sw.WriteLine("{0}{1}{2};", indent, Parameters.HasCleanup ? opt.GetOutputName(RetVal.FullName) + " __ret = " : "return ", RetVal.FromNative(opt, call, true));
            }

            foreach (string cleanup in Parameters.GetCallCleanup(opt))
            {
                sw.WriteLine("{0}{1}", indent, cleanup);
            }

            if (!IsVoid && Parameters.HasCleanup)
            {
                sw.WriteLine("{0}return __ret;", indent);
            }
        }
예제 #8
0
        public void GenerateAsyncWrapper(StreamWriter sw, string indent, CodeGenerationOptions opt)
        {
            if (!Asyncify)
            {
                return;
            }

            string static_arg = IsStatic ? " static" : String.Empty;
            string ret;

            if (IsVoid)
            {
                ret = "global::System.Threading.Tasks.Task";
            }
            else
            {
                ret = "global::System.Threading.Tasks.Task<" + opt.GetOutputName(RetVal.FullName) + ">";
            }

            sw.WriteLine("{0}{1}{2} {3} {4}Async ({5})", indent, Visibility, static_arg, ret, AdjustedName, GenBase.GetSignature(this, opt));
            sw.WriteLine("{0}{{", indent);
            sw.WriteLine("{0}\treturn global::System.Threading.Tasks.Task.Run (() => {1} ({2}));", indent, AdjustedName, Parameters.GetCall(opt));
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();
        }
예제 #9
0
 string GetManagedTypeName(CodeGenerationOptions opt)
 {
     return(opt.GetOutputName(marshaler +
                              (parms != null && parms.IsConcrete
                                          ? parms.ToString()
                                          : string.Empty)));
 }
예제 #10
0
 public void GenerateDeclaration(StreamWriter sw, string indent, CodeGenerationOptions opt, GenBase gen, string adapter)
 {
     sw.WriteLine("{0}{1} {2} {{", indent, opt.GetOutputName(Type), AdjustedName);
     if (Getter != null)
     {
         if (gen.IsGeneratable)
         {
             sw.WriteLine("{0}\t// Metadata.xml XPath method reference: path=\"{1}/method[@name='{2}'{3}]\"", indent, gen.MetadataXPathReference, Getter.JavaName, Getter.Parameters.GetMethodXPathPredicate());
         }
         if (Getter.GenericArguments != null && Getter.GenericArguments.Any())
         {
             sw.WriteLine("{0}{1}", indent, Getter.GenericArguments.ToGeneratedAttributeString());
         }
         sw.WriteLine("{0}\t[Register (\"{1}\", \"{2}\", \"{3}:{4}\"{5})] get;", indent, Getter.JavaName, Getter.JniSignature, Getter.ConnectorName, Getter.GetAdapterName(opt, adapter), Getter.AdditionalAttributeString());
     }
     if (Setter != null)
     {
         if (gen.IsGeneratable)
         {
             sw.WriteLine("{0}\t// Metadata.xml XPath method reference: path=\"{1}/method[@name='{2}'{3}]\"", indent, gen.MetadataXPathReference, Setter.JavaName, Setter.Parameters.GetMethodXPathPredicate());
         }
         if (Setter.GenericArguments != null && Setter.GenericArguments.Any())
         {
             sw.WriteLine("{0}{1}", indent, Setter.GenericArguments.ToGeneratedAttributeString());
         }
         sw.WriteLine("{0}\t[Register (\"{1}\", \"{2}\", \"{3}:{4}\"{5})] set;", indent, Setter.JavaName, Setter.JniSignature, Setter.ConnectorName, Setter.GetAdapterName(opt, adapter), Setter.AdditionalAttributeString());
     }
     sw.WriteLine("{0}}}", indent);
     sw.WriteLine();
 }
예제 #11
0
        protected void GenerateAnnotationAttribute(CodeGenerationOptions opt, GenerationInfo gen_info)
        {
            if (ShouldGenerateAnnotationAttribute)
            {
                var baseName          = Namespace.Length > 0 ? FullName.Substring(Namespace.Length + 1) : FullName;
                var attrClassNameBase = baseName.Substring(TypeNamePrefix.Length) + "Attribute";
                var localFullName     = Namespace + (Namespace.Length > 0 ? "." : string.Empty) + attrClassNameBase;
                gen_info.CurrentType = localFullName;
                StreamWriter sw = gen_info.Writer = gen_info.OpenStream(opt.GetFileName(localFullName));
                sw.WriteLine("using System;");
                sw.WriteLine();
                sw.WriteLine("namespace {0} {{", Namespace);
                sw.WriteLine();
                sw.WriteLine("\t[global::Android.Runtime.Annotation (\"{0}\")]", JavaName);
                sw.WriteLine("\t{0} partial class {1} : Attribute", this.Visibility, attrClassNameBase);
                sw.WriteLine("\t{");

                // An Annotation attribute property is generated for each applicable annotation method,
                // where *applicable* means java annotation compatible types. See IsTypeCommensurate().
                foreach (var method in Methods.Where(m => m.Parameters.Count == 0 &&
                                                     IsTypeCommensurate(SymbolTable.Lookup(m.RetVal.JavaName))))
                {
                    sw.WriteLine("\t\t[global::Android.Runtime.Register (\"{0}\"{1})]", method.JavaName, method.AdditionalAttributeString());
                    sw.WriteLine("\t\tpublic {0} {1} {{ get; set; }}", opt.GetOutputName(method.RetVal.FullName), method.Name);
                    sw.WriteLine();
                }
                sw.WriteLine("\t}");
                sw.WriteLine("}");
                sw.Close();
                gen_info.Writer = null;
            }
        }
예제 #12
0
        public void GenerateAbstractDeclaration(StreamWriter sw, string indent, CodeGenerationOptions opt, GenBase gen)
        {
            bool overrides = false;
            var  baseProp  = gen.BaseSymbol != null?gen.BaseSymbol.GetPropertyByName(name, true) : null;

            if (baseProp != null)
            {
                if (baseProp.Type != Getter.Return)
                {
                    // This may not be required if we can change generic parameter support to return constrained type (not just J.L.Object).
                    sw.WriteLine("{0}// skipped generating property {1} because its Java method declaration is variant that we cannot represent in C#", indent, name);
                    return;
                }
                overrides = true;
            }

            bool   requiresNew   = false;
            string abstract_name = AdjustedName;
            string visibility    = Getter.RetVal.IsGeneric ? "protected" : Getter.Visibility;

            if (!overrides)
            {
                requiresNew = gen.RequiresNew(abstract_name);
                GenerateCallbacks(sw, indent, opt, gen, abstract_name);
            }
            sw.WriteLine("{0}{1}{2} abstract{3} {4} {5} {{",
                         indent,
                         visibility,
                         requiresNew ? " new" : "",
                         overrides ? " override" : "",
                         opt.GetOutputName(Getter.ReturnType),
                         abstract_name);
            if (gen.IsGeneratable)
            {
                sw.WriteLine("{0}\t// Metadata.xml XPath method reference: path=\"{1}/method[@name='{2}'{3}]\"", indent, gen.MetadataXPathReference, Getter.JavaName, Getter.Parameters.GetMethodXPathPredicate());
            }
            if (Getter.IsReturnEnumified)
            {
                sw.WriteLine("{0}[return:global::Android.Runtime.GeneratedEnum]", indent);
            }
            opt.CodeGenerator.WriteMethodCustomAttributes(Getter, sw, indent);
            sw.WriteLine("{0}\t[Register (\"{1}\", \"{2}\", \"{3}\"{4})] get;", indent, Getter.JavaName, Getter.JniSignature, Getter.ConnectorName, Getter.AdditionalAttributeString());
            if (Setter != null)
            {
                if (gen.IsGeneratable)
                {
                    sw.WriteLine("{0}\t// Metadata.xml XPath method reference: path=\"{1}/method[@name='{2}'{3}]\"", indent, gen.MetadataXPathReference, Setter.JavaName, Setter.Parameters.GetMethodXPathPredicate());
                }
                opt.CodeGenerator.WriteMethodCustomAttributes(Setter, sw, indent);
                sw.WriteLine("{0}\t[Register (\"{1}\", \"{2}\", \"{3}\"{4})] set;", indent, Setter.JavaName, Setter.JniSignature, Setter.ConnectorName, Setter.AdditionalAttributeString());
            }
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();
            if (Type.StartsWith("Java.Lang.ICharSequence"))
            {
                GenerateStringVariant(sw, indent);
            }
        }
예제 #13
0
 public string FromNative(CodeGenerationOptions opt, string var_name, bool owned)
 {
     if (!string.IsNullOrEmpty(managed_type) && (sym is ClassGen || sym is InterfaceGen))
     {
         return(string.Format("global::Java.Lang.Object.GetObject<{0}> ({1}, {2})",
                              opt.GetOutputName(managed_type), var_name, owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer"));
     }
     return(sym.FromNative(opt, var_name, owned));
 }
예제 #14
0
 public string FromNative(CodeGenerationOptions opt, string var_name, bool owned)
 {
     if (!string.IsNullOrEmpty(managed_type) && (sym is ClassGen || sym is InterfaceGen))
     {
         return(string.Format("global::Java.Lang.Object.GetObject<{0}> (ref {1}, {2})",
                              opt.GetOutputName(managed_type), var_name, owned ? "JniObjectReferenceOptions.CopyAndDispose" : "JniObjectReferenceOptions.None"));
     }
     return(sym.FromNative(opt, var_name, owned));
 }
예제 #15
0
        public void Generate(StreamWriter sw, string indent, CodeGenerationOptions opt, GenBase type, bool generate_callbacks)
        {
            if (!IsValid)
            {
                return;
            }

            bool gen_as_formatted = IsReturnCharSequence;

            if (generate_callbacks && IsVirtual)
            {
                GenerateCallback(sw, indent, opt, type, null, gen_as_formatted);
            }

            string name_and_jnisig     = JavaName + JniSignature.Replace("java/lang/CharSequence", "java/lang/String");
            bool   gen_string_overload = !IsOverride && Parameters.HasCharSequence && !type.ContainsMethod(name_and_jnisig);

            string static_arg = IsStatic ? " static" : String.Empty;
            string virt_ov    = IsOverride ? " override" : IsVirtual ? " virtual" : String.Empty;

            if ((string.IsNullOrEmpty(virt_ov) || virt_ov == " virtual") && type.RequiresNew(AdjustedName))
            {
                virt_ov = " new" + virt_ov;
            }
            string seal = IsOverride && IsFinal ? " sealed" : null;
            string ret  = opt.GetOutputName(RetVal.FullName);

            GenerateIdField(sw, indent, opt);
            if (DeclaringType.IsGeneratable)
            {
                sw.WriteLine("{0}// Metadata.xml XPath method reference: path=\"{1}\"", indent, GetMetadataXPathReference(this.DeclaringType));
            }
            if (Deprecated != null)
            {
                sw.WriteLine("{0}[Obsolete (@\"{1}\")]", indent, Deprecated.Replace("\"", "\"\""));
            }
            if (IsReturnEnumified)
            {
                sw.WriteLine("{0}[return:global::Android.Runtime.GeneratedEnum]", indent);
            }
            sw.WriteLine("{0}[Register (\"{1}\", \"{2}\", \"{3}\"{4})]",
                         indent, JavaName, JniSignature, IsVirtual ? ConnectorName : String.Empty, this.AdditionalAttributeString());
            GenerateCustomAttributes(sw, indent);
            sw.WriteLine("{0}{1}{2}{3}{4} unsafe {5} {6} ({7})", indent, Visibility, static_arg, virt_ov, seal, ret, AdjustedName, GenBase.GetSignature(this, opt));
            sw.WriteLine("{0}{{", indent);
            GenerateBody(sw, indent + "\t", opt);
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();

            //NOTE: Invokers are the only place false is passed for generate_callbacks, they do not need string overloads
            if (generate_callbacks && (gen_string_overload || gen_as_formatted))
            {
                GenerateStringOverload(sw, indent, opt);
            }

            GenerateAsyncWrapper(sw, indent, opt);
        }
 public string[] PreCallback(CodeGenerationOptions opt, string var_name, bool owned)
 {
     return(new string[] {
         string.Format("var {1} = global::Java.Lang.Object.GetObject<{0}> ({2}, {3});",
                       opt.GetOutputName(FullName),
                       var_name,
                       opt.GetSafeIdentifier(TypeNameUtilities.GetNativeName(var_name)),
                       owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer"),
     });
 }
예제 #17
0
        public void GenerateExplicitInterfaceImplementation(StreamWriter sw, string indent, CodeGenerationOptions opt, GenBase iface)
        {
//			sw.WriteLine ("// explicitly implemented method from " + iface.FullName);
            GenerateCustomAttributes(sw, indent);
            sw.WriteLine("{0}{1} {2}.{3} ({4})", indent, opt.GetOutputName(RetVal.FullName), opt.GetOutputName(iface.FullName), Name, GenBase.GetSignature(this, opt));
            sw.WriteLine("{0}{{", indent);
            sw.WriteLine("{0}\treturn {1} ({2});", indent, Name, Parameters.GetCall(opt));
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();
        }
예제 #18
0
        void GenerateEventOrProperty(Method m, StreamWriter sw, string indent, ClassGen target, CodeGenerationOptions opt, string name, string connector_fmt, string add, string remove)
        {
            if (m.EventName == string.Empty)
            {
                return;
            }
            string nameSpec           = Methods.Count > 1 ? m.AdjustedName : String.Empty;
            int    idx                = FullName.LastIndexOf(".");
            int    start              = Name.StartsWith("IOn") ? 3 : 1;
            string full_delegate_name = FullName.Substring(0, idx + 1) + Name.Substring(start, Name.Length - start - 8) + nameSpec;

            if (m.IsSimpleEventHandler)
            {
                full_delegate_name = "EventHandler";
            }
            else if (m.RetVal.IsVoid || m.IsEventHandlerWithHandledProperty)
            {
                full_delegate_name = "EventHandler<" + FullName.Substring(0, idx + 1) + GetArgsName(m) + ">";
            }
            else
            {
                full_delegate_name += "Handler";
            }
            if (m.RetVal.IsVoid || m.IsEventHandlerWithHandledProperty)
            {
                if (opt.GetSafeIdentifier(name) != name)
                {
                    Report.Warning(0, Report.WarningInterfaceGen + 5, "event name for {0}.{1} is invalid. `eventName' or `argsType` can be used to assign a valid member name.", FullName, name);
                    return;
                }
                else
                {
                    GenerateEvent(sw, indent, opt, name, nameSpec, m.AdjustedName, full_delegate_name, !m.Parameters.HasSender, connector_fmt, add, remove);
                }
            }
            else
            {
                if (opt.GetSafeIdentifier(name) != name)
                {
                    Report.Warning(0, Report.WarningInterfaceGen + 6, "event property name for {0}.{1} is invalid. `eventName' or `argsType` can be used to assign a valid member name.", FullName, name);
                    return;
                }
                sw.WriteLine("{0}WeakReference weak_implementor_{1};", indent, name);
                sw.WriteLine("{0}{1}Implementor Impl{2} {{", indent, opt.GetOutputName(FullName), name);
                sw.WriteLine("{0}\tget {{", indent);
                sw.WriteLine("{0}\t\tif (weak_implementor_{1} == null || !weak_implementor_{1}.IsAlive)", indent, name);
                sw.WriteLine("{0}\t\t\treturn null;", indent);
                sw.WriteLine("{0}\t\treturn weak_implementor_{1}.Target as {2}Implementor;", indent, name, opt.GetOutputName(FullName));
                sw.WriteLine("{0}\t}}", indent);
                sw.WriteLine("{0}\tset {{ weak_implementor_{1} = new WeakReference (value, true); }}", indent, name);
                sw.WriteLine("{0}}}", indent);
                sw.WriteLine();
                GenerateProperty(sw, indent, opt, name, nameSpec, m.AdjustedName, connector_fmt, full_delegate_name);
            }
        }
예제 #19
0
 public string FromNative(CodeGenerationOptions opt, string var_name, bool owned)
 {
     if (IsArray)
     {
         return(String.Format("({0}[]) JNIEnv.GetArray ({1}, {2}, typeof ({0}))", opt.GetOutputName(enum_type), var_name, owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer"));
     }
     else
     {
         return(String.Format("({0}) {1}", opt.GetOutputName(enum_type), var_name));
     }
 }
예제 #20
0
 public void GenerateInvoker(StreamWriter sw, string indent, CodeGenerationOptions opt, GenBase type)
 {
     GenerateCallback(sw, indent, opt, type, null, IsReturnCharSequence);
     GenerateIdField(sw, indent, opt, invoker: true);
     sw.WriteLine("{0}public unsafe {1}{2} {3} ({4})",
                  indent, IsStatic ? "static " : string.Empty, opt.GetOutputName(RetVal.FullName), AdjustedName, GenBase.GetSignature(this, opt));
     sw.WriteLine("{0}{{", indent);
     GenerateInvokerBody(sw, indent + "\t", opt);
     sw.WriteLine("{0}}}", indent);
     sw.WriteLine();
 }
예제 #21
0
        void GenerateProperty(StreamWriter sw, string indent, CodeGenerationOptions opt, string name, string nameSpec, string methodName, string connector_fmt, string full_delegate_name)
        {
            string handlerPrefix = Methods.Count > 1 ? methodName : string.Empty;

            sw.WriteLine("{0}public {1} {2} {{", indent, opt.GetOutputName(full_delegate_name), name);
            sw.WriteLine("{0}\tget {{", indent);
            sw.WriteLine("{0}\t\t{1}Implementor impl = Impl{2};", indent, opt.GetOutputName(FullName), name);
            sw.WriteLine("{0}\t\treturn impl == null ? null : impl.{1}Handler;", indent, handlerPrefix);
            sw.WriteLine("{0}\t}}", indent);
            sw.WriteLine("{0}\tset {{", indent);
            sw.WriteLine("{0}\t\t{1}Implementor impl = Impl{2};", indent, opt.GetOutputName(FullName), name);
            sw.WriteLine("{0}\t\tif (impl == null) {{", indent);
            sw.WriteLine("{0}\t\t\timpl = new {1}Implementor ({2});", indent, opt.GetOutputName(FullName), NeedsSender ? "this" : string.Empty);
            sw.WriteLine("{0}\t\t\tImpl{1} = impl;", indent, name);
            sw.WriteLine("{0}\t\t}} else", indent);
            sw.WriteLine("{0}\t\t\timpl.{1}Handler = value;", indent, nameSpec);
            sw.WriteLine("{0}\t}}", indent);
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();
        }
        public string ToNative(CodeGenerationOptions opt, string varname, Dictionary <string, string> mappings = null)
        {
            var    mapped     = mappings != null && mappings.ContainsKey(parm.Name) ? mappings [parm.Name] : null;
            string targetType = opt.GetOutputName(mappings == null ? parm.Name : mapped);

            if (targetType == "string")
            {
                return(string.Format("new global::Java.Lang.String ({0})", varname));
            }
            return(varname);
        }
        internal override void WriteMethodBody(Method method, StreamWriter sw, string indent, CodeGenerationOptions opt)
        {
            sw.WriteLine("{0}if ({1} == IntPtr.Zero)", indent, method.EscapedIdName);
            sw.WriteLine("{0}\t{1} = JNIEnv.Get{2}MethodID (class_ref, \"{3}\", \"{4}\");", indent, method.EscapedIdName, method.IsStatic ? "Static" : String.Empty, method.JavaName, method.JniSignature);
            bool use_non_virtual = method.IsVirtual && !method.IsAbstract;

            foreach (string prep in method.Parameters.GetCallPrep(opt))
            {
                sw.WriteLine("{0}{1}", indent, prep);
            }
            sw.WriteLine("{0}try {{", indent);
            var oldindent = indent;

            indent += "\t";
            method.Parameters.WriteCallArgs(sw, indent, opt, invoker: false);
            if (method.IsStatic)
            {
                GenerateJNICall(method, sw, indent, opt, "JNIEnv.CallStatic" + method.RetVal.CallMethodPrefix + "Method  (class_ref, " + method.EscapedIdName + method.Parameters.GetCallArgs(opt, invoker: false) + ")", true);
            }
            else if (use_non_virtual)
            {
                sw.WriteLine();
                if (!method.IsVoid && method.Parameters.HasCleanup)
                {
                    sw.WriteLine("{0}{1} __ret;", indent, opt.GetOutputName(method.RetVal.FullName));
                }
                sw.WriteLine("{0}if (GetType () == ThresholdType)", indent);
                GenerateJNICall(method, sw, indent + "\t", opt, "JNIEnv.Call" + method.RetVal.CallMethodPrefix + "Method  (Handle, " + method.EscapedIdName + method.Parameters.GetCallArgs(opt, invoker: false) + ")", false);
                sw.WriteLine("{0}else", indent);
                GenerateJNICall(method, sw, indent + "\t", opt,
                                "JNIEnv.CallNonvirtual" + method.RetVal.CallMethodPrefix + "Method  (Handle, ThresholdClass, " +
                                string.Format("JNIEnv.GetMethodID (ThresholdClass, \"{0}\", \"{1}\")", method.JavaName, method.JniSignature) +
                                method.Parameters.GetCallArgs(opt, invoker: false) + ")",
                                false);
            }
            else
            {
                GenerateJNICall(method, sw, indent, opt, "JNIEnv.Call" + method.RetVal.CallMethodPrefix + "Method  (Handle, " + method.EscapedIdName + method.Parameters.GetCallArgs(opt, invoker: false) + ")", true);
            }

            if (!method.IsVoid && method.Parameters.HasCleanup)
            {
                sw.WriteLine("{0}return __ret;", indent);
            }
            indent = oldindent;
            sw.WriteLine("{0}}} finally {{", indent);
            foreach (string cleanup in method.Parameters.GetCallCleanup(opt))
            {
                sw.WriteLine("{0}\t{1}", indent, cleanup);
            }
            sw.WriteLine("{0}}}", indent);
        }
예제 #24
0
 void GenerateEvent(StreamWriter sw, string indent, CodeGenerationOptions opt, string name, string nameSpec, string methodName, string full_delegate_name, bool needs_sender, string wrefSuffix, string add, string remove)
 {
     sw.WriteLine("{0}public event {1} {2} {{", indent, opt.GetOutputName(full_delegate_name), name);
     sw.WriteLine("{0}\tadd {{", indent);
     sw.WriteLine("{0}\t\tglobal::Java.Interop.EventHelper.AddEventHandler<{1}, {1}Implementor>(",
                  indent, opt.GetOutputName(FullName));
     sw.WriteLine("{0}\t\t\t\tref weak_implementor_{1},", indent, wrefSuffix);
     sw.WriteLine("{0}\t\t\t\t__Create{1}Implementor,", indent, Name);
     sw.WriteLine("{0}\t\t\t\t{1},", indent, add);
     sw.WriteLine("{0}\t\t\t\t__h => __h.{1}Handler += value);", indent, nameSpec);
     sw.WriteLine("{0}\t}}", indent);
     sw.WriteLine("{0}\tremove {{", indent);
     sw.WriteLine("{0}\t\tglobal::Java.Interop.EventHelper.RemoveEventHandler<{1}, {1}Implementor>(",
                  indent, opt.GetOutputName(FullName));
     sw.WriteLine("{0}\t\t\t\tref weak_implementor_{1},", indent, wrefSuffix);
     sw.WriteLine("{0}\t\t\t\t{1}Implementor.__IsEmpty,", indent, opt.GetOutputName(FullName));
     sw.WriteLine("{0}\t\t\t\t{1},", indent, remove);
     sw.WriteLine("{0}\t\t\t\t__h => __h.{1}Handler -= value);", indent, nameSpec);
     sw.WriteLine("{0}\t}}", indent);
     sw.WriteLine("{0}}}", indent);
     sw.WriteLine();
 }
 void GenerateJNICall(Method method, StreamWriter sw, string indent, CodeGenerationOptions opt, string call, bool declare_ret)
 {
     if (method.IsVoid)
     {
         sw.WriteLine("{0}{1};", indent, call);
     }
     else if (method.Parameters.HasCleanup)
     {
         sw.WriteLine("{0}{1}__ret = {2};", indent, declare_ret ? opt.GetOutputName(method.RetVal.FullName) + " " : String.Empty, method.RetVal.FromNative(opt, call, true));
     }
     else
     {
         sw.WriteLine("{0}return {1};", indent, method.RetVal.FromNative(opt, call, true));
     }
 }
예제 #26
0
 public string[] GetPreCallback(CodeGenerationOptions opt)
 {
     if (Type == NativeType)
     {
         return(new string [0]);
     }
     else if (NeedsPrep)
     {
         return(sym.PreCallback(opt, Name, false));
     }
     else
     {
         return new string[] { opt.GetOutputName(Type) + " " + opt.GetSafeIdentifier(Name) + " = " + FromNative(opt, false) + ";" }
     };
 }
예제 #27
0
        void GenerateStringOverload(StreamWriter sw, string indent, CodeGenerationOptions opt)
        {
            string static_arg = IsStatic ? " static" : String.Empty;
            string ret        = opt.GetOutputName(RetVal.FullName.Replace("Java.Lang.ICharSequence", "string"));

            if (Deprecated != null)
            {
                sw.WriteLine("{0}[Obsolete (@\"{1}\")]", indent, Deprecated.Replace("\"", "\"\"").Trim());
            }
            sw.WriteLine("{0}{1}{2} {3} {4} ({5})", indent, Visibility, static_arg, ret, Name, GenBase.GetSignature(this, opt).Replace("Java.Lang.ICharSequence", "string").Replace("global::string", "string"));
            sw.WriteLine("{0}{{", indent);
            GenerateStringOverloadBody(sw, indent + "\t", opt, false);
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();
        }
예제 #28
0
        // This is supposed to generate instantiated generic method output, but I don't think it is done yet.
        public void GenerateExplicitIface(StreamWriter sw, string indent, CodeGenerationOptions opt, GenericSymbol gen)
        {
            sw.WriteLine("{0}// This method is explicitly implemented as a member of an instantiated {1}", indent, gen.FullName);
            GenerateCustomAttributes(sw, indent);
            sw.WriteLine("{0}{1} {2}.{3} ({4})", indent, opt.GetOutputName(RetVal.FullName), opt.GetOutputName(gen.Gen.FullName), Name, GenBase.GetSignature(this, opt));
            sw.WriteLine("{0}{{", indent);
            Dictionary <string, string> mappings = new Dictionary <string, string> ();

            for (int i = 0; i < gen.TypeParams.Length; i++)
            {
                mappings [gen.Gen.TypeParameters[i].Name] = gen.TypeParams [i].FullName;
            }
            GenerateGenericBody(sw, indent + "\t", opt, null, String.Empty, mappings);
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();
        }
예제 #29
0
        public override string FromNative(CodeGenerationOptions opt, string varname, bool owned)
        {
            if (opt.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1)
            {
                return("global::Java.Interop.JniEnvironment.Runtime.ValueManager.GetValue<" +
                       opt.GetOutputName(FullName) +
                       $"> (ref {varname}, JniObjectReferenceOptions.{(owned ? "CopyAndDispose" : "Copy")})");
            }
            return(string.Format("global::Java.Lang.Object.GetObject<{0}> ({1}, {2})", opt.GetOutputName(FullName), varname, owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer"));

            /*
             * if (String.IsNullOrEmpty (Marshaler))
             *      return String.Format ("global::Java.Lang.Object.GetObject<{0}> ({1}, {2})", opt.GetOutputName (FullName), varname, owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer");
             * else
             *      return String.Format ("new {1} ({0})", varname, Marshaler);
             */
        }
예제 #30
0
        public void GenerateExtensionOverload(StreamWriter sw, string indent, CodeGenerationOptions opt, string selfType)
        {
            if (!CanHaveStringOverload)
            {
                return;
            }

            string ret = opt.GetOutputName(RetVal.FullName.Replace("Java.Lang.ICharSequence", "string"));

            sw.WriteLine();
            sw.WriteLine("{0}public static {1} {2} (this {3} self, {4})",
                         indent, ret, Name, selfType,
                         GenBase.GetSignature(this, opt).Replace("Java.Lang.ICharSequence", "string").Replace("global::string", "string"));
            sw.WriteLine("{0}{{", indent);
            GenerateStringOverloadBody(sw, indent + "\t", opt, true);
            sw.WriteLine("{0}}}", indent);
        }