コード例 #1
0
        internal static Type?TypeRegistrationFallback(string class_name)
        {
            __TypeRegistrations.RegisterPackages();

            Type?type    = null;
            int  ls      = class_name.LastIndexOf('/');
            var  package = ls >= 0 ? class_name.Substring(0, ls) : "";

            if (packageLookup.TryGetValue(package, out var mappers))
            {
                foreach (Converter <string, Type?> c in mappers)
                {
                    type = c(class_name);
                    if (type == null)
                    {
                        continue;
                    }
                    return(type);
                }
            }
            if ((type = Type.GetType(JavaNativeTypeManager.ToCliType(class_name))) != null)
            {
                return(type);
            }
            return(null);
        }
コード例 #2
0
        internal static Type GetJavaToManagedType(string class_name)
        {
            var t = monodroid_typemap_java_to_managed(class_name);

            if (t != IntPtr.Zero)
            {
                return(Type.GetType(Marshal.PtrToStringAnsi(t)));
            }

            var type    = (Type)null;
            int ls      = class_name.LastIndexOf('/');
            var package = ls >= 0 ? class_name.Substring(0, ls) : "";
            List <Converter <string, Type> > mappers;

            if (packageLookup.TryGetValue(package, out mappers))
            {
                foreach (Converter <string, Type> c in mappers)
                {
                    type = c(class_name);
                    if (type == null)
                    {
                        continue;
                    }
                    return(type);
                }
            }
            if ((type = Type.GetType(JavaNativeTypeManager.ToCliType(class_name))) != null)
            {
                return(type);
            }
            return(null);
        }
コード例 #3
0
        internal static Type GetJavaToManagedType(string class_name)
        {
            Type type = monodroid_typemap_java_to_managed(class_name);

            if (type != null)
            {
                return(type);
            }

            if (!JNIEnv.IsRunningOnDesktop)
            {
                // Miss message is logged in the native runtime
                if (JNIEnv.LogTypemapMissStackTrace)
                {
                    JNIEnv.LogTypemapTrace(new System.Diagnostics.StackTrace(true));
                }
                return(null);
            }

            __TypeRegistrations.RegisterPackages();

            type = null;
            int ls      = class_name.LastIndexOf('/');
            var package = ls >= 0 ? class_name.Substring(0, ls) : "";
            List <Converter <string, Type> > mappers;

            if (packageLookup.TryGetValue(package, out mappers))
            {
                foreach (Converter <string, Type> c in mappers)
                {
                    type = c(class_name);
                    if (type == null)
                    {
                        continue;
                    }
                    return(type);
                }
            }
            if ((type = Type.GetType(JavaNativeTypeManager.ToCliType(class_name))) != null)
            {
                return(type);
            }
            return(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 (JavaNativeTypeManager.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;
        }