예제 #1
0
        public override void Generate(CodeGenerationOptions opt, GenerationInfo gen_info)
        {
            using (var sw = gen_info.OpenStream(opt.GetFileName(FullName))) {
                sw.WriteLine("using System;");
                sw.WriteLine("using System.Collections.Generic;");
                sw.WriteLine("using Android.Runtime;");
                if (opt.CodeGenerationTarget != CodeGenerationTarget.XamarinAndroid)
                {
                    sw.WriteLine("using Java.Interop;");
                }
                sw.WriteLine();
                sw.WriteLine("namespace {0} {{", Namespace);
                sw.WriteLine();

                var generator = opt.CreateCodeGenerator(sw);
                generator.WriteInterface(this, "\t", gen_info);

                sw.WriteLine("}");
            }

            GenerateAnnotationAttribute(opt, gen_info);
        }
예제 #2
0
        public void Generate(CodeGenerationOptions opt, GenerationInfo gen_info)
        {
            using (var sw = gen_info.OpenStream(opt.GetFileName("__NamespaceMapping__"))) {
                sw.WriteLine("using System;");
                sw.WriteLine();

                if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
                {
                    foreach (var p in mappings)
                    {
                        sw.WriteLine($"[assembly:global::Android.Runtime.NamespaceMapping (Java = \"{p.Key}\", Managed=\"{p.Value}\")]");
                    }
                    sw.WriteLine();
                }

                // delegate bool _JniMarshal_PPL_Z (IntPtr jnienv, IntPtr klass, IntPtr a);
                foreach (var jni in opt.GetJniMarshalDelegates())
                {
                    sw.WriteLine($"delegate {FromJniType (jni[jni.Length - 1])} {jni} (IntPtr jnienv, IntPtr klass{GetDelegateParameters (jni)});");
                }

                // [SupportedOSPlatform] only exists in .NET 5.0+, so we need to generate a
                // dummy one so earlier frameworks can compile.
                if (opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1)
                {
                    sw.WriteLine("#if !NET");
                    sw.WriteLine("namespace System.Runtime.Versioning {");
                    sw.WriteLine("    [System.Diagnostics.Conditional(\"NEVER\")]");
                    sw.WriteLine("    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]");
                    sw.WriteLine("    internal sealed class SupportedOSPlatformAttribute : Attribute {");
                    sw.WriteLine("        public SupportedOSPlatformAttribute (string platformName) { }");
                    sw.WriteLine("    }");
                    sw.WriteLine("}");
                    sw.WriteLine("#endif");
                    sw.WriteLine("");
                }
            }
        }
예제 #3
0
        public override void Generate(CodeGenerationOptions opt, GenerationInfo gen_info)
        {
            gen_info.CurrentType = FullName;

            StreamWriter sw = gen_info.Writer = gen_info.OpenStream(opt.GetFileName(FullName));

            sw.WriteLine("using System;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using Android.Runtime;");
            if (opt.CodeGenerationTarget != CodeGenerationTarget.XamarinAndroid)
            {
                sw.WriteLine("using Java.Interop;");
            }
            sw.WriteLine();
            sw.WriteLine("namespace {0} {{", Namespace);
            sw.WriteLine();

            Generate(sw, "\t", opt, gen_info);

            sw.WriteLine("}");
            sw.Close();
            gen_info.Writer = null;
        }
예제 #4
0
        public static void GenerateTypeRegistrations(CodeGenerationOptions opt, GenerationInfo gen_info)
        {
            StreamWriter sw = gen_info.Writer = gen_info.OpenStream(opt.GetFileName("Java.Interop.__TypeRegistrations"));

            Dictionary <string, List <KeyValuePair <string, string> > > mapping = new Dictionary <string, List <KeyValuePair <string, string> > >();

            foreach (KeyValuePair <string, string> reg in gen_info.TypeRegistrations)
            {
                int    ls      = reg.Key.LastIndexOf('/');
                string package = ls >= 0 ? reg.Key.Substring(0, ls) : "";

                if (JniType.ToCliType(reg.Key) == reg.Value)
                {
                    continue;
                }
                List <KeyValuePair <string, string> > v;
                if (!mapping.TryGetValue(package, out v))
                {
                    mapping.Add(package, v = new List <KeyValuePair <string, string> >());
                }
                v.Add(new KeyValuePair <string, string>(reg.Key, reg.Value));
            }

            sw.WriteLine("using System;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using Android.Runtime;");
            sw.WriteLine();
            sw.WriteLine("namespace Java.Interop {");
            sw.WriteLine();
            sw.WriteLine("\tpartial class __TypeRegistrations {");
            sw.WriteLine();
            sw.WriteLine("\t\tpublic static void RegisterPackages ()");
            sw.WriteLine("\t\t{");
            sw.WriteLine("#if MONODROID_TIMING");
            sw.WriteLine("\t\t\tvar start = DateTime.Now;");
            sw.WriteLine("\t\t\tAndroid.Util.Log.Info (\"MonoDroid-Timing\", \"RegisterPackages start: \" + (start - new DateTime (1970, 1, 1)).TotalMilliseconds);");
            sw.WriteLine("#endif // def MONODROID_TIMING");
            sw.WriteLine("\t\t\tJava.Interop.TypeManager.RegisterPackages (");
            sw.WriteLine("\t\t\t\t\tnew string[]{");
            foreach (KeyValuePair <string, List <KeyValuePair <string, string> > > e in mapping)
            {
                sw.WriteLine("\t\t\t\t\t\t\"{0}\",", e.Key);
            }
            sw.WriteLine("\t\t\t\t\t},");
            sw.WriteLine("\t\t\t\t\tnew Converter<string, Type>[]{");
            foreach (KeyValuePair <string, List <KeyValuePair <string, string> > > e in mapping)
            {
                sw.WriteLine("\t\t\t\t\t\tlookup_{0}_package,", e.Key.Replace('/', '_'));
            }
            sw.WriteLine("\t\t\t\t\t});");
            sw.WriteLine("#if MONODROID_TIMING");
            sw.WriteLine("\t\t\tvar end = DateTime.Now;");
            sw.WriteLine("\t\t\tAndroid.Util.Log.Info (\"MonoDroid-Timing\", \"RegisterPackages time: \" + (end - new DateTime (1970, 1, 1)).TotalMilliseconds + \" [elapsed: \" + (end - start).TotalMilliseconds + \" ms]\");");
            sw.WriteLine("#endif // def MONODROID_TIMING");
            sw.WriteLine("\t\t}");
            sw.WriteLine();
            sw.WriteLine("\t\tstatic Type Lookup (string[] mappings, string javaType)");
            sw.WriteLine("\t\t{");
            sw.WriteLine("\t\t\tstring managedType = Java.Interop.TypeManager.LookupTypeMapping (mappings, javaType);");
            sw.WriteLine("\t\t\tif (managedType == null)");
            sw.WriteLine("\t\t\t\treturn null;");
            sw.WriteLine("\t\t\treturn Type.GetType (managedType);");
            sw.WriteLine("\t\t}");
            foreach (KeyValuePair <string, List <KeyValuePair <string, string> > > map in mapping)
            {
                sw.WriteLine();
                string package = map.Key.Replace('/', '_');
                sw.WriteLine("\t\tstatic string[] package_{0}_mappings;", package);
                sw.WriteLine("\t\tstatic Type lookup_{0}_package (string klass)", package);
                sw.WriteLine("\t\t{");
                sw.WriteLine("\t\t\tif (package_{0}_mappings == null) {{", package);
                sw.WriteLine("\t\t\t\tpackage_{0}_mappings = new string[]{{", package);
                map.Value.Sort((a, b) => a.Key.CompareTo(b.Key));
                foreach (KeyValuePair <string, string> t in map.Value)
                {
                    sw.WriteLine("\t\t\t\t\t\"{0}:{1}\",", t.Key, t.Value);
                }
                sw.WriteLine("\t\t\t\t};");
                sw.WriteLine("\t\t\t}");
                sw.WriteLine("");
                sw.WriteLine("\t\t\treturn Lookup (package_{0}_mappings, klass);", package);
                sw.WriteLine("\t\t}");
            }
            sw.WriteLine("\t}");
            sw.WriteLine("}");
            sw.Close();
            gen_info.Writer = null;
        }