Ejemplo n.º 1
0
        static bool IsParameterCompatibleWith(TypeReference a, TypeReference b, TypeDefinitionCache?cache)
        {
            if (a is TypeSpecification || b is TypeSpecification)
            {
                if (a.GetType() != b.GetType())
                {
                    return(false);
                }

                return(IsParameterCompatibleWith((TypeSpecification)a, (TypeSpecification)b, cache));
            }

            if (a.IsGenericParameter)
            {
                if (b.IsGenericParameter && a.Name == b.Name)
                {
                    return(true);
                }
                var gpa = (GenericParameter)a;
                foreach (var c in gpa.Constraints)
                {
                    if (!c.ConstraintType.IsAssignableFrom(b, cache))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            return(a.FullName == b.FullName);
        }
Ejemplo n.º 2
0
        static bool IsParameterCompatibleWith(IModifierType a, IModifierType b, TypeDefinitionCache?cache)
        {
            if (!IsParameterCompatibleWith(a.ModifierType, b.ModifierType, cache))
            {
                return(false);
            }

            return(IsParameterCompatibleWith(a.ElementType, b.ElementType, cache));
        }
Ejemplo n.º 3
0
        public static string GetAssemblyQualifiedName(this TypeReference type, TypeDefinitionCache?cache)
        {
            TypeDefinition def = cache != null?cache.Resolve(type) : type.Resolve();

            return(string.Format("{0}, {1}",
                                 // Cecil likes to use '/' as the nested type separator, while
                                 // Reflection uses '+' as the nested type separator. Use Reflection.
                                 type.FullName.Replace('/', '+'),
                                 (def ?? type).Module.Assembly.Name.FullName));
        }
Ejemplo n.º 4
0
 public static string?GetJniSignature(MethodDefinition method, TypeDefinitionCache?cache)
 {
     return(GetJniSignature <TypeReference, ParameterDefinition> (
                method.Parameters,
                p => p.ParameterType,
                p => GetExportKind(p),
                method.ReturnType,
                GetExportKind(method.MethodReturnType),
                (t, k) => GetJniTypeName(t, k, cache),
                method.IsConstructor));
 }
Ejemplo n.º 5
0
 public static bool IsSubclassOf(this TypeDefinition type, string typeName, TypeDefinitionCache?cache)
 {
     foreach (var t in type.GetTypeAndBaseTypes(cache))
     {
         if (t.FullName == typeName)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 6
0
        static bool ShouldSkipGeneric(TypeDefinition?a, TypeDefinition?b, TypeDefinitionCache?cache)
        {
            if (a == null || b == null)
            {
                return(false);
            }
            if (!a.ImplementsInterface("Android.Runtime.IJavaObject", cache) || !b.ImplementsInterface("Android.Runtime.IJavaObject", cache))
            {
                return(false);
            }

            return(GetRegisteredJavaTypeName(a) == GetRegisteredJavaTypeName(b));
        }
Ejemplo n.º 7
0
        public static TypeDefinition?GetBaseType(this TypeDefinition type, TypeDefinitionCache?cache)
        {
            var bt = type.BaseType;

            if (bt == null)
            {
                return(null);
            }
            if (cache != null)
            {
                return(cache.Resolve(bt));
            }
            return(bt.Resolve());
        }
Ejemplo n.º 8
0
        internal static bool IsNonStaticInnerClass(TypeDefinition?type, TypeDefinitionCache?cache)
        {
            if (type == null)
            {
                return(false);
            }
            if (!type.IsNested)
            {
                return(false);
            }

            if (!type.DeclaringType.IsSubclassOf("Java.Lang.Object", cache))
            {
                return(false);
            }

            return(GetBaseConstructors(type, cache)
                   .Any(ctor => ctor.Parameters.Any(p => p.Name == "__self")));
        }
Ejemplo n.º 9
0
        public static string GetPackageName(TypeDefinition type, TypeDefinitionCache?cache)
        {
            if (IsPackageNamePreservedForAssembly(type.GetPartialAssemblyName(cache)))
            {
                return(type.Namespace.ToLowerInvariant());
            }
            switch (PackageNamingPolicy)
            {
            case PackageNamingPolicy.Lowercase:
                return(type.Namespace.ToLowerInvariant());

            case PackageNamingPolicy.LowercaseWithAssemblyName:
                return("assembly_" + (type.GetPartialAssemblyName(cache).Replace('.', '_') + "." + type.Namespace).ToLowerInvariant());

            case PackageNamingPolicy.LowercaseCrc64:
                return(CRC_PREFIX + ToCrc64(type.Namespace + ":" + type.GetPartialAssemblyName(cache)));

            default:
                throw new NotSupportedException($"PackageNamingPolicy.{PackageNamingPolicy} is no longer supported.");
            }
        }
Ejemplo n.º 10
0
        static string?ToJniName(TypeDefinition type, ExportParameterKind exportKind, TypeDefinitionCache?cache)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (type.IsValueType)
            {
                return(GetPrimitiveClass(type));
            }

            if (type.FullName == "System.String")
            {
                return("java/lang/String");
            }

            if (!type.ImplementsInterface("Android.Runtime.IJavaObject", cache))
            {
                return(GetSpecialExportJniType(type.FullName, exportKind));
            }

            return(ToJniName(type, t => t.DeclaringType, t => t.Name, t => GetPackageName(t, cache), ToJniNameFromAttributes, t => IsNonStaticInnerClass(t as TypeDefinition, cache)));
        }
Ejemplo n.º 11
0
        static bool IsParameterCompatibleWith(TypeSpecification a, TypeSpecification b, TypeDefinitionCache?cache)
        {
            if (a is GenericInstanceType)
            {
                return(IsParameterCompatibleWith((GenericInstanceType)a, (GenericInstanceType)b, cache));
            }

            if (a is IModifierType)
            {
                return(IsParameterCompatibleWith((IModifierType)a, (IModifierType)b, cache));
            }

            return(IsParameterCompatibleWith(a.ElementType, b.ElementType, cache));
        }
Ejemplo n.º 12
0
        public static IEnumerable <MethodDefinition> GetOverriddenMethods(MethodDefinition method, bool inherit, TypeDefinitionCache?cache)
        {
            yield return(method);

            if (inherit)
            {
                MethodDefinition baseMethod = method;
                while ((baseMethod = method.GetBaseDefinition(cache)) != null && baseMethod != method)
                {
                    yield return(method);

                    method = baseMethod;
                }
            }
        }
Ejemplo n.º 13
0
        public static bool AreParametersCompatibleWith(this Collection <ParameterDefinition> a, Collection <ParameterDefinition> b, TypeDefinitionCache?cache)
        {
            if (a.Count != b.Count)
            {
                return(false);
            }

            if (a.Count == 0)
            {
                return(true);
            }

            for (int i = 0; i < a.Count; i++)
            {
                if (!IsParameterCompatibleWith(a [i].ParameterType, b [i].ParameterType, cache))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 14
0
        static IEnumerable <MethodDefinition> GetBaseConstructors(TypeDefinition type, TypeDefinitionCache?cache)
        {
            var baseType = type.GetBaseTypes(cache).FirstOrDefault(t => t.GetCustomAttributes(typeof(RegisterAttribute)).Any());

            if (baseType != null)
            {
                return(baseType.Methods.Where(m => m.IsConstructor && !m.IsStatic));
            }
            return(Enumerable.Empty <MethodDefinition> ());
        }
Ejemplo n.º 15
0
        public static MethodDefinition GetBaseDefinition(this MethodDefinition method, TypeDefinitionCache?cache)
        {
            if (method.IsStatic || method.IsNewSlot || !method.IsVirtual)
            {
                return(method);
            }

            foreach (var baseType in method.DeclaringType.GetBaseTypes(cache))
            {
                foreach (var m in baseType.Methods)
                {
                    if (!m.IsConstructor &&
                        m.Name == method.Name &&
                        (m.IsVirtual || m.IsAbstract) &&
                        AreParametersCompatibleWith(m.Parameters, method.Parameters, cache))
                    {
                        return(m);
                    }
                }
            }
            return(method);
        }
Ejemplo n.º 16
0
 public static MethodDefinition GetBaseDefinition(this MethodDefinition method, TypeDefinitionCache?cache) =>
 GetBaseDefinition(method, (IMetadataResolver?)cache);
Ejemplo n.º 17
0
 public static bool ImplementsInterface(this TypeDefinition type, string interfaceName, TypeDefinitionCache?cache)
 {
     foreach (var t in type.GetTypeAndBaseTypes(cache))
     {
         foreach (var i in t.Interfaces)
         {
             if (i.InterfaceType.FullName == interfaceName)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
 public static IEnumerable <TypeDefinition> GetBaseTypes(this TypeDefinition type, TypeDefinitionCache?cache) =>
 GetBaseTypes(type, (IMetadataResolver?)cache);
Ejemplo n.º 19
0
 public static IEnumerable <MethodDefinition> GetOverriddenMethods(MethodDefinition method, bool inherit, TypeDefinitionCache?cache) =>
 GetOverriddenMethods(method, inherit, (IMetadataResolver?)cache);
 public static bool ImplementsInterface(this TypeDefinition type, string interfaceName, TypeDefinitionCache?cache) =>
 ImplementsInterface(type, interfaceName, (IMetadataResolver?)cache);
Ejemplo n.º 21
0
 public static string GetPartialAssemblyQualifiedName(this TypeReference type, TypeDefinitionCache?cache)
 {
     return(string.Format("{0}, {1}",
                          // Cecil likes to use '/' as the nested type separator, while
                          // Reflection uses '+' as the nested type separator. Use Reflection.
                          type.FullName.Replace('/', '+'),
                          type.GetPartialAssemblyName(cache)));
 }
Ejemplo n.º 22
0
        public static string GetPartialAssemblyName(this TypeReference type, TypeDefinitionCache?cache)
        {
            TypeDefinition def = cache != null?cache.Resolve(type) : type.Resolve();

            return((def ?? type).Module.Assembly.Name.Name);
        }
 public static bool IsSubclassOf(this TypeDefinition type, string typeName, TypeDefinitionCache?cache) =>
 IsSubclassOf(type, typeName, (IMetadataResolver?)cache);
Ejemplo n.º 24
0
        static bool IsParameterCompatibleWith(GenericInstanceType a, GenericInstanceType b, TypeDefinitionCache?cache)
        {
            if (!IsParameterCompatibleWith(a.ElementType, b.ElementType, cache))
            {
                return(false);
            }

            if (a.GenericArguments.Count != b.GenericArguments.Count)
            {
                return(false);
            }

            if (a.GenericArguments.Count == 0)
            {
                return(true);
            }

            for (int i = 0; i < a.GenericArguments.Count; i++)
            {
                if (!IsParameterCompatibleWith(a.GenericArguments [i], b.GenericArguments [i], cache))
                {
                    return(false);
                }
            }

            return(true);
        }
 public static TypeDefinition?GetBaseType(this TypeDefinition type, TypeDefinitionCache?cache) =>
 GetBaseType(type, (IMetadataResolver?)cache);
Ejemplo n.º 26
0
 public static bool AreParametersCompatibleWith(this Collection <ParameterDefinition> a, Collection <ParameterDefinition> b, TypeDefinitionCache?cache) =>
 AreParametersCompatibleWith(a, b, (IMetadataResolver?)cache);
 public static string GetAssemblyQualifiedName(this TypeReference type, TypeDefinitionCache?cache) =>
 GetAssemblyQualifiedName(type, (IMetadataResolver?)cache);
Ejemplo n.º 28
0
        public static IEnumerable <TypeDefinition> GetBaseTypes(this TypeDefinition type, TypeDefinitionCache?cache)
        {
            TypeDefinition?t = type;

            while ((t = t.GetBaseType(cache)) != null)
            {
                yield return(t);
            }
        }
 public static bool IsAssignableFrom(this TypeReference type, TypeReference c, TypeDefinitionCache?cache) =>
 IsAssignableFrom(type, c, (IMetadataResolver?)cache);
Ejemplo n.º 30
0
        public static bool IsAssignableFrom(this TypeReference type, TypeReference c, TypeDefinitionCache?cache)
        {
            if (type.FullName == c.FullName)
            {
                return(true);
            }
            var d = c.Resolve();

            if (d == null)
            {
                return(false);
            }
            foreach (var t in d.GetTypeAndBaseTypes(cache))
            {
                if (type.FullName == t.FullName)
                {
                    return(true);
                }
                foreach (var ifaceImpl in t.Interfaces)
                {
                    var i = ifaceImpl.InterfaceType;
                    if (IsAssignableFrom(type, i, cache))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }