Beispiel #1
0
        private static EmitBlock GetEmitBlock(bool configEnabled, TypeAccessibility?typeAccessibility, IEnumerable <TypeDescriptor> types)
        {
            var bridgeTypes = Substitute.For <BridgeTypes>();

            foreach (var type in types)
            {
                TypeSystem.AddBridgeType(bridgeTypes, type);
            }

            var emitter = EmitterHelper.GetEmitter(
                bridgeTypes: bridgeTypes,
                assemblyInfo: new AssemblyInfo()
            {
                Reflection = new ReflectionConfig()
                {
                    Enabled           = configEnabled,
                    TypeAccessibility = typeAccessibility
                }
            }
                );

            var block = new EmitBlock(emitter);

            return(block);
        }
Beispiel #2
0
        public IType[] GetReflectableTypes()
        {
            var config         = this.Emitter.AssemblyInfo.Reflection;
            var configInternal = ((AssemblyInfo)this.Emitter.AssemblyInfo).ReflectionInternal;
            //bool? enable = config.Disabled.HasValue ? !config.Disabled : (configInternal.Disabled.HasValue ? !configInternal.Disabled : true);
            bool?enable = null;

            if (config.Disabled.HasValue && !config.Disabled.Value)
            {
                enable = true;
            }
            else if (configInternal.Disabled.HasValue)
            {
                enable = !configInternal.Disabled.Value;
            }
            else if (!config.Disabled.HasValue)
            {
                enable = true;
            }

            TypeAccessibility?typeAccessibility = config.TypeAccessibility.HasValue ? config.TypeAccessibility : (configInternal.TypeAccessibility.HasValue ? configInternal.TypeAccessibility : null);
            string            filter            = !string.IsNullOrEmpty(config.Filter) ? config.Filter : (!string.IsNullOrEmpty(configInternal.Filter) ? configInternal.Filter : null);

            var hasSettings = !string.IsNullOrEmpty(config.Filter) ||
                              config.MemberAccessibility != null ||
                              config.TypeAccessibility.HasValue ||
                              !string.IsNullOrEmpty(configInternal.Filter) ||
                              configInternal.MemberAccessibility != null ||
                              configInternal.TypeAccessibility.HasValue;

            if (enable.HasValue && !enable.Value)
            {
                return(new IType[0]);
            }

            if (enable.HasValue && enable.Value && !hasSettings)
            {
                this.Emitter.IsAnonymousReflectable = true;
            }

            if (typeAccessibility.HasValue)
            {
                this.Emitter.IsAnonymousReflectable = typeAccessibility.Value.HasFlag(TypeAccessibility.Anonymous);
            }

            List <IType> reflectTypes    = new List <IType>();
            var          thisAssemblyDef = this.Emitter.Translator.AssemblyDefinition;

            foreach (var bridgeType in this.Emitter.BridgeTypes)
            {
                var result  = false;
                var type    = bridgeType.Value.Type;
                var typeDef = type.GetDefinition();
                //var thisAssembly = bridgeType.Value.TypeInfo != null;
                var thisAssembly = bridgeType.Value.TypeDefinition?.Module.Assembly.Equals(thisAssemblyDef) ?? false;
                var external     = typeDef != null && this.Emitter.Validator.IsExternalType(typeDef);

                if (enable.HasValue && enable.Value && !hasSettings && thisAssembly)
                {
                    result = true;
                }

                if (typeDef != null)
                {
                    var skip = this.SkipFromReflection(typeDef, bridgeType.Value);

                    if (skip)
                    {
                        continue;
                    }

                    var attr = typeDef.Attributes.FirstOrDefault(a => a.AttributeType.FullName == "Bridge.ReflectableAttribute");

                    if (attr == null)
                    {
                        attr = Helpers.GetInheritedAttribute(typeDef, "Bridge.ReflectableAttribute");

                        if (attr != null)
                        {
                            if (attr.NamedArguments.Count > 0 && attr.NamedArguments.Any(arg => arg.Key.Name == "Inherits"))
                            {
                                var inherits = attr.NamedArguments.First(arg => arg.Key.Name == "Inherits");

                                if (!(bool)inherits.Value.ConstantValue)
                                {
                                    attr = null;
                                }
                            }
                            else
                            {
                                attr = null;
                            }
                        }
                    }

                    if (attr != null)
                    {
                        if (attr.PositionalArguments.Count == 0)
                        {
                            if (thisAssembly)
                            {
                                reflectTypes.Add(type);
                                continue;
                            }
                        }
                        else
                        {
                            var value = attr.PositionalArguments.First().ConstantValue;

                            if ((!(value is bool) || (bool)value) && thisAssembly)
                            {
                                reflectTypes.Add(type);
                            }

                            continue;
                        }
                    }

                    if (external && attr == null)
                    {
                        if (!string.IsNullOrWhiteSpace(filter) && EmitBlock.MatchFilter(type, filter, thisAssembly, result))
                        {
                            reflectTypes.Add(type);
                        }

                        continue;
                    }
                }

                if (typeAccessibility.HasValue && thisAssembly)
                {
                    result = false;

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.All))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.Anonymous) && type.Kind == TypeKind.Anonymous)
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.NonAnonymous) && type.Kind != TypeKind.Anonymous)
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.NonPrivate) && (typeDef == null || !typeDef.IsPrivate))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.Public) && (typeDef == null || typeDef.IsPublic || typeDef.IsInternal))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.None))
                    {
                        continue;
                    }
                }

                if (!string.IsNullOrEmpty(filter))
                {
                    result = EmitBlock.MatchFilter(type, filter, thisAssembly, result);

                    if (!result)
                    {
                        continue;
                    }
                }

                if (result)
                {
                    reflectTypes.Add(type);
                }
            }

            return(reflectTypes.ToArray());
        }
Beispiel #3
0
        public IType[] GetReflectableTypes()
        {
            var config         = this.Emitter.AssemblyInfo.Reflection;
            var configInternal = ((AssemblyInfo)this.Emitter.AssemblyInfo).ReflectionInternal;
            //bool? enable = config.Disabled.HasValue ? !config.Disabled : (configInternal.Disabled.HasValue ? !configInternal.Disabled : true);
            bool?enable = null;

            if (config.Disabled.HasValue && !config.Disabled.Value)
            {
                enable = true;
            }
            else if (configInternal.Disabled.HasValue)
            {
                enable = !configInternal.Disabled.Value;
            }
            else if (!config.Disabled.HasValue)
            {
                enable = true;
            }

            TypeAccessibility?typeAccessibility = config.TypeAccessibility.HasValue ? config.TypeAccessibility : (configInternal.TypeAccessibility.HasValue ? configInternal.TypeAccessibility : null);
            string            filter            = !string.IsNullOrEmpty(config.Filter) ? config.Filter : (!string.IsNullOrEmpty(configInternal.Filter) ? configInternal.Filter : null);

            var hasSettings = !string.IsNullOrEmpty(config.Filter) ||
                              config.MemberAccessibility != null ||
                              config.TypeAccessibility.HasValue ||
                              !string.IsNullOrEmpty(configInternal.Filter) ||
                              configInternal.MemberAccessibility != null ||
                              configInternal.TypeAccessibility.HasValue;

            if (enable.HasValue && !enable.Value)
            {
                return(new IType[0]);
            }

            if (enable.HasValue && enable.Value && !hasSettings)
            {
                this.Emitter.IsAnonymousReflectable = true;
            }

            if (typeAccessibility.HasValue)
            {
                this.Emitter.IsAnonymousReflectable = typeAccessibility.Value.HasFlag(TypeAccessibility.Anonymous);
            }

            List <IType> reflectTypes    = new List <IType>();
            var          thisAssemblyDef = this.Emitter.Translator.AssemblyDefinition;

            foreach (var bridgeType in this.Emitter.BridgeTypes)
            {
                var result  = false;
                var type    = bridgeType.Value.Type;
                var typeDef = type.GetDefinition();
                //var thisAssembly = bridgeType.Value.TypeInfo != null;
                var thisAssembly = bridgeType.Value.TypeDefinition?.Module.Assembly.Equals(thisAssemblyDef) ?? false;
                if (enable.HasValue && enable.Value && !hasSettings && thisAssembly)
                {
                    result = true;
                }

                if (typeDef != null)
                {
                    var skip = this.SkipFromReflection(typeDef, bridgeType.Value);

                    if (skip)
                    {
                        continue;
                    }

                    var attr = typeDef.Attributes.FirstOrDefault(a => a.AttributeType.FullName == "Bridge.ReflectableAttribute");

                    if (attr == null)
                    {
                        attr = Helpers.GetInheritedAttribute(typeDef, "Bridge.ReflectableAttribute");

                        if (attr != null)
                        {
                            if (attr.NamedArguments.Count > 0 && attr.NamedArguments.Any(arg => arg.Key.Name == "Inherits"))
                            {
                                var inherits = attr.NamedArguments.First(arg => arg.Key.Name == "Inherits");

                                if (!(bool)inherits.Value.ConstantValue)
                                {
                                    attr = null;
                                }
                            }
                            else
                            {
                                attr = null;
                            }
                        }
                    }

                    if (attr != null)
                    {
                        if (attr.PositionalArguments.Count == 0)
                        {
                            if (thisAssembly)
                            {
                                reflectTypes.Add(type);
                                continue;
                            }
                        }
                        else
                        {
                            var value = attr.PositionalArguments.First().ConstantValue;

                            if ((!(value is bool) || (bool)value) && thisAssembly)
                            {
                                reflectTypes.Add(type);
                            }

                            continue;
                        }
                    }

                    if (this.Emitter.Validator.IsExternalType(typeDef) && attr == null)
                    {
                        continue;
                    }
                }

                if (typeAccessibility.HasValue && thisAssembly)
                {
                    result = false;

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.All))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.Anonymous) && type.Kind == TypeKind.Anonymous)
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.NonAnonymous) && type.Kind != TypeKind.Anonymous)
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.NonPrivate) && (typeDef == null || !typeDef.IsPrivate))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.Public) && (typeDef == null || typeDef.IsPublic || typeDef.IsInternal))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.None))
                    {
                        continue;
                    }
                }

                if (!string.IsNullOrEmpty(filter))
                {
                    var fullName = type.FullName;
                    var parts    = filter.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (var part in parts)
                    {
                        string pattern;
                        bool   exclude = part.StartsWith("!");

                        if (part == "this")
                        {
                            result = !exclude && thisAssembly;
                        }
                        else
                        {
                            if (part.StartsWith("regex:"))
                            {
                                pattern = part.Substring(6);
                            }
                            else
                            {
                                pattern = "^" + Regex.Escape(part).Replace("\\*", ".*").Replace("\\?", ".") + "$";
                            }

                            if (Regex.IsMatch(fullName, pattern))
                            {
                                result = !exclude;
                            }
                        }
                    }

                    if (!result)
                    {
                        continue;
                    }
                }

                if (result)
                {
                    reflectTypes.Add(type);
                }
            }

            return(reflectTypes.ToArray());
        }
Beispiel #4
0
 private static EmitBlock GetEmitBlock(bool configEnabled, TypeAccessibility?typeAccessibility, bool inThisAssembly, string typeName, string attribute = null, IEnumerable <object> positionalArguments = null)
 {
     return(GetEmitBlock(configEnabled, typeAccessibility, new[] { new TypeDescriptor(typeName, inThisAssembly, attribute, positionalArguments) }));
 }
Beispiel #5
0
            private void TestByOneReflectableAttribute(string typeName, bool inThisAssembly, bool hasArgument, object argument = null, TypeAccessibility?typeAccessibility = null)
            {
                var attribute = hasArgument ? "Bridge.ReflectableAttribute" : null;

                var block            = GetEmitBlock(true, typeAccessibility, inThisAssembly, typeName, attribute, argument != null ? new object[] { argument } : null);
                var reflectableTypes = block.GetReflectableTypes();

                Assert.NotNull(reflectableTypes, typeName + " NotNull");

                var expectResult = inThisAssembly;

                if (expectResult)
                {
                    Assert.AreEqual(1, reflectableTypes.Length, typeName + " Count");
                    Assert.AreEqual(typeName, reflectableTypes[0].FullName, typeName + " FullName");
                }
                else
                {
                    Assert.AreEqual(0, reflectableTypes.Length, typeName + " Count");
                }
            }
Beispiel #6
0
        public IType[] GetReflectableTypes()
        {
            var config         = this.Emitter.AssemblyInfo.Reflection;
            var configInternal = ((AssemblyInfo)this.Emitter.AssemblyInfo).ReflectionInternal;

            bool?enable = config.Enabled.HasValue ? config.Enabled : (configInternal.Enabled.HasValue ? configInternal.Enabled : null);
            TypeAccessibility?typeAccessibility = config.TypeAccessibility.HasValue ? config.TypeAccessibility : (configInternal.TypeAccessibility.HasValue ? configInternal.TypeAccessibility : null);
            string            filter            = !string.IsNullOrEmpty(config.Filter) ? config.Filter : (!string.IsNullOrEmpty(configInternal.Filter) ? configInternal.Filter : null);

            var hasSettings = !string.IsNullOrEmpty(config.Filter) ||
                              config.MemberAccessibility != null ||
                              config.TypeAccessibility.HasValue ||
                              !string.IsNullOrEmpty(configInternal.Filter) ||
                              configInternal.MemberAccessibility != null ||
                              configInternal.TypeAccessibility.HasValue;

            if (enable.HasValue && !enable.Value)
            {
                return(new IType[0]);
            }

            if (enable.HasValue && enable.Value && !hasSettings)
            {
                this.Emitter.IsAnonymousReflectable = true;
            }

            if (typeAccessibility.HasValue)
            {
                this.Emitter.IsAnonymousReflectable = typeAccessibility.Value.HasFlag(TypeAccessibility.Anonymous);
            }

            List <IType> reflectTypes = new List <IType>();

            foreach (var bridgeType in this.Emitter.BridgeTypes)
            {
                var result       = false;
                var type         = bridgeType.Value.Type;
                var thisAssembly = bridgeType.Value.TypeInfo != null;

                if (enable.HasValue && enable.Value && !hasSettings && thisAssembly)
                {
                    result = true;
                }

                var typeDef = type.GetDefinition();

                if (typeDef != null)
                {
                    var skip = typeDef.Attributes.Any(a =>
                                                      a.AttributeType.FullName == "Bridge.GlobalMethodsAttribute" ||
                                                      a.AttributeType.FullName == "Bridge.NonScriptableAttribute" ||
                                                      a.AttributeType.FullName == "Bridge.MixinAttribute");

                    if (!skip && typeDef.FullName != "System.Object")
                    {
                        var name = BridgeTypes.ToJsName(typeDef, this.Emitter);

                        if (name == "Object")
                        {
                            skip = true;
                        }
                    }

                    if (skip)
                    {
                        continue;
                    }

                    var attr = typeDef.Attributes.FirstOrDefault(a => a.AttributeType.FullName == "Bridge.ReflectableAttribute");

                    if (attr != null)
                    {
                        if (attr.PositionalArguments.Count == 0)
                        {
                            if (thisAssembly)
                            {
                                reflectTypes.Add(type);
                            }

                            continue;
                        }

                        var value = attr.PositionalArguments.First().ConstantValue;

                        if ((!(value is bool) || (bool)value) && thisAssembly)
                        {
                            reflectTypes.Add(type);
                        }

                        continue;
                    }
                }

                if (typeAccessibility.HasValue && thisAssembly)
                {
                    result = false;

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.All))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.Anonymous) && type.Kind == TypeKind.Anonymous)
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.NonAnonymous) && type.Kind != TypeKind.Anonymous)
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.NonPrivate) && (typeDef == null || !typeDef.IsPrivate))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.Public) && (typeDef == null || typeDef.IsPublic || typeDef.IsInternal))
                    {
                        result = true;
                    }

                    if (typeAccessibility.Value.HasFlag(TypeAccessibility.None))
                    {
                        continue;
                    }
                }

                if (!string.IsNullOrEmpty(filter))
                {
                    var fullName = type.FullName;
                    var parts    = filter.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (var part in parts)
                    {
                        string pattern;
                        bool   exclude = part.StartsWith("!");

                        if (part == "this")
                        {
                            result = !exclude && thisAssembly;
                        }
                        else
                        {
                            if (part.StartsWith("regex:"))
                            {
                                pattern = part.Substring(6);
                            }
                            else
                            {
                                pattern = "^" + Regex.Escape(part).Replace("\\*", ".*").Replace("\\?", ".") + "$";
                            }

                            if (Regex.IsMatch(fullName, pattern))
                            {
                                result = !exclude;
                            }
                        }
                    }

                    if (!result)
                    {
                        continue;
                    }
                }

                if (result)
                {
                    reflectTypes.Add(type);
                }
            }

            return(reflectTypes.ToArray());
        }