Пример #1
0
        static string?ToJniNameFromAttributes(TypeDefinition type, IMetadataResolver?resolver)
        {
            #region CustomAttribute alternate name support
            var attrs = type.CustomAttributes.Where(a => Resolve(resolver, a.AttributeType).Interfaces.Any(it => it.InterfaceType.FullName == typeof(IJniNameProviderAttribute).FullName));
            return(attrs.Select(attr => {
                var ap = attr.Properties.FirstOrDefault(p => p.Name == "Name");
                string?name = null;
                if (ap.Name == null)
                {
                    var ca = attr.ConstructorArguments.FirstOrDefault();
                    if (ca.Type == null || ca.Type.FullName != "System.String")
                    {
                        return null;
                    }
                    name = (string)ca.Value;
                }
                else
                {
                    name = (string)ap.Argument.Value;
                }
                if (!string.IsNullOrEmpty(name))
                {
                    return name.Replace('.', '/');
                }
                else
                {
                    return null;
                }
            })
                   .FirstOrDefault(s => s != null));

            #endregion
        }
Пример #2
0
        static bool IsParameterCompatibleWith(TypeReference a, TypeReference b, IMetadataResolver?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);
        }
Пример #3
0
        static string?ToJniName(TypeDefinition type, ExportParameterKind exportKind, IMetadataResolver?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:               type,
                       decl:               t => t.DeclaringType,
                       name:               t => t.Name,
                       ns:                 t => GetPackageName(t, cache),
                       overrideName:       t => ToJniNameFromAttributes(t, cache),
                       shouldUpdateName:   t => IsNonStaticInnerClass(t as TypeDefinition, cache)
                       ));
        }
Пример #4
0
        static bool IsParameterCompatibleWith(IModifierType a, IModifierType b, IMetadataResolver?cache)
        {
            if (!IsParameterCompatibleWith(a.ModifierType, b.ModifierType, cache))
            {
                return(false);
            }

            return(IsParameterCompatibleWith(a.ElementType, b.ElementType, cache));
        }
        public static string GetAssemblyQualifiedName(this TypeReference type, IMetadataResolver?resolver)
        {
            TypeDefinition?def = (resolver?.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));
        }
Пример #6
0
 public static string?GetJniSignature(MethodDefinition method, IMetadataResolver?resolver)
 {
     return(GetJniSignature <TypeReference, ParameterDefinition> (
                method.Parameters,
                p => p.ParameterType,
                p => GetExportKind(p),
                method.ReturnType,
                GetExportKind(method.MethodReturnType),
                (t, k) => GetJniTypeName(t, k, resolver),
                method.IsConstructor));
 }
Пример #7
0
 public static string ToCompatJniName(TypeDefinition type, IMetadataResolver?resolver)
 {
     return(ToJniName(
                type:               type,
                decl:               t => t.DeclaringType,
                name:               t => t.Name,
                ns:                 ToCompatPackageName,
                overrideName:       t => ToJniNameFromAttributes(t, resolver),
                shouldUpdateName:   t => IsNonStaticInnerClass(t as TypeDefinition, resolver)
                ));
 }
 public static bool IsSubclassOf(this TypeDefinition type, string typeName, IMetadataResolver?resolver)
 {
     foreach (var t in type.GetTypeAndBaseTypes(resolver))
     {
         if (t.FullName == typeName)
         {
             return(true);
         }
     }
     return(false);
 }
        public static TypeDefinition?GetBaseType(this TypeDefinition type, IMetadataResolver?resolver)
        {
            var bt = type.BaseType;

            if (bt == null)
            {
                return(null);
            }
            if (resolver != null)
            {
                return(resolver.Resolve(bt));
            }
            return(bt.Resolve());
        }
Пример #10
0
        internal static bool IsNonStaticInnerClass(TypeDefinition?type, IMetadataResolver?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")));
        }
Пример #11
0
        public static string GetPackageName(TypeDefinition type, IMetadataResolver?resolver)
        {
            if (IsPackageNamePreservedForAssembly(type.GetPartialAssemblyName(resolver)))
            {
                return(type.Namespace.ToLowerInvariant());
            }
            switch (PackageNamingPolicy)
            {
            case PackageNamingPolicy.Lowercase:
                return(type.Namespace.ToLowerInvariant());

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

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

            default:
                throw new NotSupportedException($"PackageNamingPolicy.{PackageNamingPolicy} is no longer supported.");
            }
        }
 public static bool ImplementsInterface(this TypeDefinition type, string interfaceName, IMetadataResolver?resolver)
 {
     foreach (var t in type.GetTypeAndBaseTypes(resolver))
     {
         foreach (var i in t.Interfaces)
         {
             if (i.InterfaceType.FullName == interfaceName)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Пример #13
0
 public static string?GetJniTypeName(TypeReference typeRef, IMetadataResolver?resolver)
 {
     return(GetJniTypeName(typeRef, ExportParameterKind.Unspecified, resolver));
 }
Пример #14
0
 internal static string?GetJniTypeName(TypeReference typeRef, ExportParameterKind exportKind, IMetadataResolver?cache)
 {
     return(GetJniTypeName <TypeReference, TypeDefinition> (typeRef, exportKind, t => t.Resolve(), t => {
         TypeReference etype;
         int rank = GetArrayInfo(typeRef, out etype);
         return new KeyValuePair <int, TypeReference> (rank, etype);
     }, t => t.FullName, (t, k) => ToJniName(t, k, cache)));
 }
Пример #15
0
        public static bool AreParametersCompatibleWith(this Collection <ParameterDefinition> a, Collection <ParameterDefinition> b, IMetadataResolver?resolver)
        {
            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, resolver))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #16
0
        public static IEnumerable <MethodDefinition> GetOverriddenMethods(MethodDefinition method, bool inherit, IMetadataResolver?resolver)
        {
            yield return(method);

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

                    method = baseMethod;
                }
            }
        }
Пример #17
0
        public static MethodDefinition GetBaseDefinition(this MethodDefinition method, IMetadataResolver?resolver)
        {
            if (method.IsStatic || method.IsNewSlot || !method.IsVirtual)
            {
                return(method);
            }

            foreach (var baseType in method.DeclaringType.GetBaseTypes(resolver))
            {
                foreach (var m in baseType.Methods)
                {
                    if (!m.IsConstructor &&
                        m.Name == method.Name &&
                        (m.IsVirtual || m.IsAbstract) &&
                        AreParametersCompatibleWith(m.Parameters, method.Parameters, resolver))
                    {
                        return(m);
                    }
                }
            }
            return(method);
        }
Пример #18
0
 public static string ToJniName(TypeDefinition type, IMetadataResolver?resolver)
 {
     return(ToJniName(type, ExportParameterKind.Unspecified, resolver) ??
            "java/lang/Object");
 }
Пример #19
0
 static TypeDefinition Resolve(IMetadataResolver?resolver, TypeReference typeReference) =>
 resolver?.Resolve(typeReference) ??
 typeReference.Resolve() ??
 throw new InvalidOperationException();
        public static IEnumerable <TypeDefinition> GetBaseTypes(this TypeDefinition type, IMetadataResolver?resolver)
        {
            TypeDefinition?t = type;

            while ((t = t.GetBaseType(resolver)) != null)
            {
                yield return(t);
            }
        }
 public static string GetPartialAssemblyQualifiedName(this TypeReference type, IMetadataResolver?resolver)
 {
     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(resolver)));
 }
        public static string GetPartialAssemblyName(this TypeReference type, IMetadataResolver?resolver)
        {
            TypeDefinition?def = (resolver?.Resolve(type)) ?? type.Resolve();

            return((def ?? type).Module.Assembly.Name.Name);
        }
Пример #23
0
        static IEnumerable <MethodDefinition> GetBaseConstructors(TypeDefinition type, IMetadataResolver?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> ());
        }
Пример #24
0
        static bool IsParameterCompatibleWith(TypeSpecification a, TypeSpecification b, IMetadataResolver?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));
        }
Пример #25
0
        static bool IsParameterCompatibleWith(GenericInstanceType a, GenericInstanceType b, IMetadataResolver?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 bool IsAssignableFrom(this TypeReference type, TypeReference c, IMetadataResolver?resolver)
        {
            if (type.FullName == c.FullName)
            {
                return(true);
            }
            var d = (resolver?.Resolve(c)) ?? c.Resolve();

            if (d == null)
            {
                return(false);
            }
            foreach (var t in d.GetTypeAndBaseTypes(resolver))
            {
                if (type.FullName == t.FullName)
                {
                    return(true);
                }
                foreach (var ifaceImpl in t.Interfaces)
                {
                    var i = ifaceImpl.InterfaceType;
                    if (IsAssignableFrom(type, i, resolver))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #27
0
 public static bool IsInstrumentation(TypeDefinition type, IMetadataResolver?resolver)
 {
     return(type.GetBaseTypes(resolver).Any(b => b.FullName == "Android.App.Instrumentation"));
 }