Exemplo n.º 1
0
        internal static GType RegisterType(Type type, TypeRegistration registration)
        {
            Type original = type;
            if (knownTypes.ContainsKey(type))
            {
                GType known = knownTypes[type];
                if (registration != null)
                {
                    known.Registration = registration;
                }
                return known;
            }
            var res = new GType();
            if(typeof(Delegate).IsAssignableFrom(type))
            {
                int i = 0;
            }
            if (type.IsPointer)
            {
                //it's out really
                res.IsOut = true;
                type = type.GetElementType();
            }
            if (type.IsByRef)
            {
                //it's ref really
                res.IsRef = true;
                type = type.GetElementType();
            }
            if (type.IsArray)
            {
                res.ArrayElement = RegisterType(type.GetElementType());
                res.IsArray = true;
            }

            res.LowerName = original.FullName.ToLowerInvariant();
            if (res.IsOut)
            {
                res.LowerName += "!";
            }
            if (knownNames.ContainsKey(res.LowerName))
            {
                res = knownNames[res.LowerName];
            }

            if (type.IsAbstract)
            {
                res.IsAbstract = true;
            }
            if (type.IsSealed)
            {
                res.IsFinal = true;
            }
            if (typeof(Delegate).IsAssignableFrom(type) && typeof(Delegate) != type && typeof(MulticastDelegate) != type)
            {
                res.IsDelegate = true;
            }

            if (res.Registration == null && registration != null)
            {
                res.Registration = registration;
            }
            res.IsPrimitive = type.IsPrimitive;
            res.IsException = typeof(Exception).IsAssignableFrom(type);
            res.CLRType = original;
            if (res.IsArray)
            {
                res.CLRFullName = type.GetElementType().FullName + "[]";
            }
            else
            {
                res.CLRFullName = type.FullName;
            }
            res.Attributes = type.Attributes;
            res.IsCLRType = true;
            res.IsInterface = type.IsInterface;
            res.IsJVMProxy = jvmProxyType.IsAssignableFrom(type);
            if (!res.IsJVMProxy)
            {
                res.IsCLRRealType = true;
            }
            if (type.BaseType != null && res.Base == null
                && type != typeof (object)
                && type != typeof (Exception)
                && type.FullName != "java.lang.Throwable"
                && type.FullName != "java.lang.Object"
                )
            {
                res.Base = RegisterType(type.BaseType);
            }
            foreach (Type ifc in type.GetInterfaces())
            {
                if (!ifc.IsAssignableFrom(type.BaseType))
                {
                    if (!TestCLRTypeStrong(ifc))
                    {
                        GType gifc = RegisterType(ifc);
                        if (!res.Interfaces.Contains(gifc))
                        {

                            if (res.IsInterface && res.Base == null)
                            {
                                res.Base = gifc;
                            }
                            res.Interfaces.Add(gifc);
                            res.AllInterfaces.Add(gifc);
                        }
                    }
                }
            }
            Register(res);

            if (type.IsGenericType)
            {
                Type sub;
                Type supGeneric = type.GetGenericTypeDefinition();
                if (typeof (IComparable<>).IsAssignableFrom(supGeneric))
                {
                    sub = typeof (IComparable);
                    res.LowerName =
                        ("System.IComparable<" + type.GetGenericArguments()[0].Name + ">").ToLowerInvariant();
                }
                else if (typeof (IEnumerable<>).IsAssignableFrom(supGeneric))
                {
                    sub = typeof (IEnumerable);
                    res.LowerName =
                        ("System.Collections.Generic.IEnumerable<" + type.GetGenericArguments()[0].Name + ">").
                            ToLowerInvariant();
                }
                else if (typeof (IEnumerator<>).IsAssignableFrom(supGeneric))
                {
                    sub = typeof (IEnumerator);
                    res.LowerName =
                        ("System.Collections.Generic.IEnumerator<" + type.GetGenericArguments()[0].Name + ">").
                            ToLowerInvariant();
                }
                else if (typeof (IEquatable<>).IsAssignableFrom(supGeneric))
                {
                    sub = typeof (object);
                    res.LowerName =
                        ("System.Collections.Generic.IEquatable<" + type.GetGenericArguments()[0].Name + ">").
                            ToLowerInvariant();
                }
                else if (typeof (ICollection<>).IsAssignableFrom(supGeneric))
                {
                    sub = typeof (ICollection);
                    res.LowerName =
                        ("System.Collections.Generic.ICollection<" + type.GetGenericArguments()[0].Name + ">").
                            ToLowerInvariant();
                }
                else if (typeof (IList<>).IsAssignableFrom(supGeneric))
                {
                    sub = typeof (IList);
                    res.LowerName =
                        ("System.Collections.Generic.IList<" + type.GetGenericArguments()[0].Name + ">").
                            ToLowerInvariant();
                }
                else
                {
                    sub = type.BaseType;
                    if (sub == null)
                    {
                        sub = typeof (object);
                    }
                }
                res.JVMSubst = RegisterType(sub);
            }
            if (res.IsOut)
            {
                Type sub;
                if (type.IsArray)
                {
                    sub = typeof(Out<>).MakeGenericType(typeof(object));
                }
                else
                {
                    sub = typeof(Out<>).MakeGenericType(type);
                }
                res.JVMSubst = RegisterType(sub);
                res.CLRSubst = res;
            }
            if (res.IsRef)
            {
                Type sub;
                if (type.IsArray)
                {
                    sub = typeof(Ref<>).MakeGenericType(typeof(object));
                }
                else
                {
                    sub = typeof(Ref<>).MakeGenericType(type);
                }
                res.JVMSubst = RegisterType(sub);
                res.CLRSubst = res;
            }

            return res;
        }
Exemplo n.º 2
0
        internal static GType RegisterClass(Class clazz, TypeRegistration registration)
        {
            if (knownClasses.ContainsKey(clazz))
            {
                GType known = knownClasses[clazz];
                if (registration != null)
                {
                    known.Registration = registration;
                }
                return known;
            }
            var res = new GType();
            if (clazz.isArray())
            {
                res.ArrayElement = RegisterClass(clazz.getComponentType(), null);
                res.IsArray = true;
                string array = "[]";
                Class comp = clazz.getComponentType();
                while (comp.isArray())
                {
                    array += "[]";
                    comp = comp.getComponentType();
                }
                res.LowerName = ((string) comp.getName()).ToLowerInvariant() + array;
            }
            else
            {
                res.LowerName = ((string) clazz.getName()).ToLowerInvariant();
            }

            res.Attributes = 0;
            var classModifiers = (ModifierFlags) clazz.getModifiers();
            if ((classModifiers & ModifierFlags.Abstract) != 0)
            {
                res.IsAbstract = true;
                res.Attributes |= TypeAttributes.Abstract;
            }
            if ((classModifiers & ModifierFlags.Final) != 0)
            {
                res.IsFinal = true;
            }
            if ((classModifiers & ModifierFlags.Public) != 0)
            {
                res.Attributes |= TypeAttributes.Public;
            }
            else if ((classModifiers & ModifierFlags.Private) != 0)
            {
                res.Attributes |= TypeAttributes.NotPublic;
            }
            //TODO internal ?
            if (knownNames.ContainsKey(res.LowerName))
            {
                res = knownNames[res.LowerName];
            }
            if (res.Registration == null && registration != null)
            {
                res.Registration = registration;
            }
            res.JVMType = clazz;
            res.JVMFullName = clazz.getName();
            if (res.IsArray)
            {
                string array = "[]";
                Class comp = clazz.getComponentType();
                while (comp.isArray())
                {
                    array += "[]";
                    comp = comp.getComponentType();
                }
                res.JVMFullName = comp.getName() + array;
            }
            else
            {
                res.JVMFullName = clazz.getName();
            }
            res.IsJVMType = true;
            res.IsPrimitive = clazz.isPrimitive();
            res.IsException = Throwable._class.isAssignableFrom(clazz);
            res.IsInterface = clazz.isInterface();
            res.IsCLRProxy = clrProxyClass != null && clrProxyClass.isAssignableFrom(clazz);
            if (!res.IsCLRProxy)
            {
                res.IsJVMRealType = true;
            }
            Class superclass = clazz.getSuperclass();
            var isBaseClassPublic = superclass == null || ((ModifierFlags)superclass.getModifiers() & ModifierFlags.Public) != 0;
            if (superclass != null && res.Base == null
                && clazz != Object._class
                && clazz != Throwable._class
                && res.JVMFullName != "system.Object"
                && res.JVMFullName != "system.Exception"
                && isBaseClassPublic)
            {
                res.Base = RegisterClass(superclass);
            }
            List<Class> interfaces = new List<Class>(clazz.getInterfaces());
            if (!isBaseClassPublic)
            {
                interfaces.AddRange(superclass.getInterfaces());
                res.Base = RegisterClass(superclass.getSuperclass());
            }
            foreach (Class ifc in interfaces)
            {
                GType gifc = RegisterClass(ifc);
                if (!res.Interfaces.Contains(gifc))
                {
                    if (res.IsInterface && res.Base == null)
                    {
                        res.Base = gifc;
                    }
                    res.Interfaces.Add(gifc);
                    res.AllInterfaces.Add(gifc);
                }
            }
            Register(res);
            return res;
        }
Exemplo n.º 3
0
        public static void Register()
        {
            if (config.Verbose)
            {
                Console.WriteLine("clr.version         :" + RuntimeEnvironment.GetSystemVersion());
                Console.WriteLine("clr.arch            :" + ((IntPtr.Size == 8) ? "64bit" : "32bit"));
            }
            LoadClasspath();
            if (config.Verbose)
            {
                Console.WriteLine("java.home           :" + Bridge.Setup.JavaHome);
                Console.WriteLine("java.version        :" + java.lang.System.getProperty("java.version"));
                Console.WriteLine("sun.arch.data.model :" + java.lang.System.getProperty("sun.arch.data.model"));
                Console.WriteLine("");
            }

            LoadAssemblies();

            RegisterAssemblies();

            if (config.JavaClass != null)
            {
                RegisterClasses();
            }
            if (config.ClrType != null)
            {
                RegisterTypes();
            }

            foreach (Assembly assembly in generateAssemblies)
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsPublic)
                    {
                        TypeRegistration registration=new TypeRegistration();
                        registration.TypeName = type.FullName;
                        GType reg = RegisterType(type, registration);
                        reg.IsJVMGenerate = true;
                        reg.IsSkipCLRInterface = !registration.SyncInterface;
                        reg.MergeJavaSource = registration.MergeJavaSource;
                    }
                }
            }

            foreach (string classPath in generateCp)
            {
                if (Directory.Exists(classPath))
                {
                    string path = Path.GetFullPath(classPath);
                    foreach (string classFile in Directory.GetFiles(path, "*.class", SearchOption.AllDirectories))
                    {
                        if (!classFile.Contains("$"))
                        {
                            string name = classFile.Substring(path.Length+1);
                            string clazzName = name.Substring(0, name.Length - (".class".Length)).Replace('\\', '/');
                            RegisterClass(clazzName);
                        }
                    }
                }
                else if (File.Exists(classPath) && Path.GetExtension(classPath)==".jar")
                {
                    using (var fis = Adapt.Disposable(new FileInputStream(classPath)))
                    {
                        using (var zis = Adapt.Disposable(new ZipInputStream(fis.Real)))
                        {
                            ZipEntry entry = zis.Real.getNextEntry();
                            while (entry!=null)
                            {
                                string name = entry.getName();
                                if (!entry.isDirectory() && name.EndsWith(".class") && !name.Contains("$"))
                                {
                                    string clazzName = name.Substring(0, name.Length - (".class".Length));
                                    RegisterClass(clazzName);
                                }
                                entry = zis.Real.getNextEntry();
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
 private static void RegisterClass(string clazzName)
 {
     Class clazz = loadClass(clazzName, false);
     if (clazz != null && ((ModifierFlags)clazz.getModifiers() & ModifierFlags.Public) != 0)
     {
         TypeRegistration registration = new TypeRegistration();
         registration.TypeName = clazzName;
         GType reg = RegisterClass(clazz, registration);
         reg.IsCLRGenerate = true;
         reg.IsSkipJVMInterface = !registration.SyncInterface;
         reg.MergeJavaSource = registration.MergeJavaSource;
     }
 }