예제 #1
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            if (propertyDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = propertyDeclaration.HasModifier(Modifiers.Static);

            IDictionary <string, List <EntityDeclaration> > dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = propertyDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(propertyDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { propertyDeclaration }));
            }

            var rr = this.Resolver.ResolveNode(propertyDeclaration, null) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(rr))
            {
                var config = rr.Member.IsStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = propertyDeclaration
                });
            }

            if (!this.HasExternal(propertyDeclaration) &&
                !this.HasTemplate(propertyDeclaration.Getter))
            {
                Expression     initializer     = this.GetDefaultFieldInitializer(propertyDeclaration.ReturnType);
                TypeConfigInfo info            = isStatic ? this.CurrentType.StaticConfig : this.CurrentType.InstanceConfig;
                var            autoInitializer = info.AutoPropertyInitializers.FirstOrDefault(f => f.Name == key);

                if (autoInitializer != null)
                {
                    initializer = autoInitializer.Initializer;
                }

                info.Properties.Add(new TypeConfigItem
                {
                    Name                  = key,
                    Entity                = propertyDeclaration,
                    Initializer           = initializer,
                    IsPropertyInitializer = autoInitializer != null
                });
            }
        }
예제 #2
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            if (propertyDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = propertyDeclaration.HasModifier(Modifiers.Static);

            IDictionary <string, List <EntityDeclaration> > dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = propertyDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(propertyDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { propertyDeclaration }));
            }

            if (!propertyDeclaration.Getter.IsNull &&
                !this.HasIgnore(propertyDeclaration) &&
                !this.HasInline(propertyDeclaration.Getter) &&
                propertyDeclaration.Getter.Body.IsNull &&
                !this.HasScript(propertyDeclaration.Getter))
            {
                Expression     initializer = this.GetDefaultFieldInitializer(propertyDeclaration.ReturnType);
                TypeConfigInfo info        = isStatic ? this.CurrentType.StaticConfig : this.CurrentType.InstanceConfig;
                if (!this.AssemblyInfo.AutoPropertyToField)
                {
                    info.Properties.Add(new TypeConfigItem
                    {
                        Name        = key,
                        Entity      = propertyDeclaration,
                        Initializer = initializer
                    });
                }
                else
                {
                    info.Fields.Add(new TypeConfigItem
                    {
                        Name        = key,
                        Entity      = propertyDeclaration,
                        Initializer = initializer
                    });
                }
            }
        }
예제 #3
0
 private OverloadsCollection(IEmitter emitter, PropertyDeclaration propDeclaration, bool isSetter)
 {
     this.Emitter          = emitter;
     this.Name             = propDeclaration.Name;
     this.JsName           = Helpers.GetPropertyRef(propDeclaration, emitter, isSetter, true, true);
     this.Inherit          = !propDeclaration.HasModifier(Modifiers.Static);
     this.Static           = propDeclaration.HasModifier(Modifiers.Static);
     this.CancelChangeCase = !Helpers.IsFieldProperty(propDeclaration, emitter);
     this.IsSetter         = isSetter;
     this.Member           = this.FindMember(propDeclaration);
     this.TypeDefinition   = this.Member.DeclaringTypeDefinition;
     this.Type             = this.Member.DeclaringType;
     this.InitMembers();
     this.Emitter.OverloadsCache[propDeclaration.GetHashCode().ToString()] = this;
 }
예제 #4
0
        internal static IField GetBackingField(BaseRefactoringContext context, PropertyDeclaration propertyDeclaration)
        {
            // automatic properties always need getter & setter
            if (propertyDeclaration == null || propertyDeclaration.Getter.IsNull || propertyDeclaration.Setter.IsNull || propertyDeclaration.Getter.Body.IsNull || propertyDeclaration.Setter.Body.IsNull)
            {
                return(null);
            }
            if (!context.Supports(csharp3) || propertyDeclaration.HasModifier(ICSharpCode.NRefactory.CSharp.Modifiers.Abstract) || ((TypeDeclaration)propertyDeclaration.Parent).ClassType == ClassType.Interface)
            {
                return(null);
            }
            var getterField = ScanGetter(context, propertyDeclaration);

            if (getterField == null)
            {
                return(null);
            }
            var setterField = ScanSetter(context, propertyDeclaration);

            if (setterField == null)
            {
                return(null);
            }
            if (!getterField.Equals(setterField))
            {
                return(null);
            }
            return(getterField);
        }
예제 #5
0
 public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
 {
     if (!propertyDeclaration.HasModifier(Modifiers.Public) && propertyDeclaration.Name == "Current")
     {
         base.VisitPropertyDeclaration(propertyDeclaration);
     }
 }
예제 #6
0
        protected virtual void EmitPropertyMethod(PropertyDeclaration propertyDeclaration)
        {
            var memberResult = this.Emitter.Resolver.ResolveNode(propertyDeclaration, this.Emitter) as MemberResolveResult;
            var isInterface  = memberResult != null && memberResult.Member.DeclaringType.Kind == TypeKind.Interface;

            if (!isInterface && !propertyDeclaration.HasModifier(Modifiers.Public))
            {
                return;
            }

            if (memberResult != null &&
                propertyDeclaration.Getter.IsNull && propertyDeclaration.Setter.IsNull)
            {
                return;
            }

            if (!propertyDeclaration.Getter.IsNull && this.Emitter.GetInline(propertyDeclaration.Getter) == null)
            {
                XmlToJsDoc.EmitComment(this, this.PropertyDeclaration);

                var ignoreInterface = isInterface &&
                                      memberResult.Member.DeclaringType.TypeParameterCount > 0;
                this.WriteAccessor(propertyDeclaration, memberResult, ignoreInterface);

                if (!ignoreInterface && isInterface)
                {
                    this.WriteAccessor(propertyDeclaration, memberResult, true);
                }
            }
        }
예제 #7
0
            public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
            {
                if (propertyDeclaration.HasModifier(Modifiers.Static) ||
                    propertyDeclaration.HasModifier(Modifiers.Virtual) ||
                    propertyDeclaration.HasModifier(Modifiers.Override) ||
                    propertyDeclaration.HasModifier(Modifiers.New) ||
                    propertyDeclaration.Attributes.Any())
                {
                    return;
                }

                if (IsEmpty(propertyDeclaration.Setter) && IsEmpty(propertyDeclaration.Getter))
                {
                    return;
                }


                var resolved = ctx.Resolve(propertyDeclaration) as MemberResolveResult;

                if (resolved == null || SkipMember(resolved.Member))
                {
                    return;
                }
                var isImplementingInterface = resolved.Member.ImplementedInterfaceMembers.Any();

                if (isImplementingInterface)
                {
                    return;
                }

                if (!propertyDeclaration.Getter.IsNull && StaticVisitor.UsesNotStaticMember(ctx, propertyDeclaration.Getter.Body) ||
                    !propertyDeclaration.Setter.IsNull && StaticVisitor.UsesNotStaticMember(ctx, propertyDeclaration.Setter.Body))
                {
                    return;
                }

                AddIssue(new CodeIssue(propertyDeclaration.NameToken.StartLocation, propertyDeclaration.NameToken.EndLocation,
                                       string.Format(ctx.TranslateString("Property '{0}' can be made static."), propertyDeclaration.Name),
                                       string.Format(ctx.TranslateString("Make '{0}' static"), propertyDeclaration.Name),
                                       script => script.ChangeModifier(propertyDeclaration, propertyDeclaration.Modifiers | Modifiers.Static))
                {
                    IssueMarker = IssueMarker.DottedLine
                });
            }
예제 #8
0
 private OverloadsCollection(IEmitter emitter, PropertyDeclaration propDeclaration, bool isSetter, bool isField)
 {
     this.Emitter = emitter;
     this.IsField = isField;
     this.Name = propDeclaration.Name;
     this.JsName = Helpers.GetPropertyRef(propDeclaration, emitter, isSetter, true, true);
     this.AltJsName = Helpers.GetPropertyRef(propDeclaration, emitter, !isSetter, true, true);
     this.FieldJsName = propDeclaration.Getter != null && propDeclaration.Getter.Body.IsNull ? emitter.GetEntityName(propDeclaration) : null;
     this.Inherit = !propDeclaration.HasModifier(Modifiers.Static);
     this.Static = propDeclaration.HasModifier(Modifiers.Static);
     this.IsSetter = isSetter;
     this.Member = this.FindMember(propDeclaration);
     var p = (IProperty)this.Member;
     this.FieldJsName = this.Emitter.GetEntityName(p);
     this.TypeDefinition = this.Member.DeclaringTypeDefinition;
     this.Type = this.Member.DeclaringType;
     this.InitMembers();
     this.Emitter.Cache.AddNode(propDeclaration, isSetter, this);
 }
예제 #9
0
 public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
 {
     if (propertyDeclaration.HasModifier(Modifiers.Static))
     {
         CheckDependency(propertyDeclaration.ReturnType);
         if (!propertyDeclaration.Getter.IsNull)
         {
             propertyDeclaration.Getter.AcceptVisitor(this);
         }
     }
 }
예제 #10
0
        private OverloadsCollection(IEmitter emitter, PropertyDeclaration propDeclaration, bool isSetter)
        {
            this.Emitter          = emitter;
            this.Name             = propDeclaration.Name;
            this.JsName           = Helpers.GetPropertyRef(propDeclaration, emitter, isSetter, true, true);
            this.AltJsName        = Helpers.GetPropertyRef(propDeclaration, emitter, !isSetter, true, true);
            this.FieldJsName      = propDeclaration.Getter != null && propDeclaration.Getter.Body.IsNull ? emitter.GetEntityName(propDeclaration) : null;
            this.Inherit          = !propDeclaration.HasModifier(Modifiers.Static);
            this.Static           = propDeclaration.HasModifier(Modifiers.Static);
            this.CancelChangeCase = !Helpers.IsFieldProperty(propDeclaration, emitter);
            this.IsSetter         = isSetter;
            this.Member           = this.FindMember(propDeclaration);
            var p = (IProperty)this.Member;

            this.FieldJsName    = Helpers.IsAutoProperty(p) ? (Helpers.IsFieldProperty(p, this.Emitter) ? this.Emitter.GetEntityName(p) : Helpers.GetPropertyRef(p, this.Emitter, true, true, true, false, true)) : null;
            this.TypeDefinition = this.Member.DeclaringTypeDefinition;
            this.Type           = this.Member.DeclaringType;
            this.InitMembers();
            this.Emitter.OverloadsCacheNodes[new Tuple <AstNode, bool>(propDeclaration, isSetter)] = this;
        }
예제 #11
0
 public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
 {
     if (propertyDeclaration.HasModifier(Modifiers.Static))
     {
         this.CheckDependency(propertyDeclaration.ReturnType);
         if (!propertyDeclaration.Getter.IsNull)
         {
             propertyDeclaration.Getter.AcceptVisitor(this);
         }
     }
 }
예제 #12
0
		EventDeclaration CreateChangedEventDeclaration (RefactoringContext context, PropertyDeclaration propertyDeclaration)
		{
			return new EventDeclaration {
				Modifiers = propertyDeclaration.HasModifier (Modifiers.Static) ? Modifiers.Public | Modifiers.Static : Modifiers.Public,
				ReturnType = context.CreateShortType("System", "EventHandler"),
				Variables = {
					new VariableInitializer {
						Name = propertyDeclaration.Name + "Changed"
					}
				}
			};
		}
예제 #13
0
 EventDeclaration CreateChangedEventDeclaration(RefactoringContext context, PropertyDeclaration propertyDeclaration)
 {
     return(new EventDeclaration {
         Modifiers = propertyDeclaration.HasModifier(Modifiers.Static) ? Modifiers.Public | Modifiers.Static : Modifiers.Public,
         ReturnType = context.CreateShortType("System", "EventHandler"),
         Variables =
         {
             new VariableInitializer {
                 Name = propertyDeclaration.Name + "Changed"
             }
         }
     });
 }
예제 #14
0
        private OverloadsCollection(IEmitter emitter, PropertyDeclaration propDeclaration, bool isSetter, bool isField)
        {
            this.Emitter     = emitter;
            this.IsField     = isField;
            this.Name        = propDeclaration.Name;
            this.JsName      = Helpers.GetPropertyRef(propDeclaration, emitter, isSetter, true, true);
            this.AltJsName   = Helpers.GetPropertyRef(propDeclaration, emitter, !isSetter, true, true);
            this.FieldJsName = propDeclaration.Getter != null && propDeclaration.Getter.Body.IsNull ? emitter.GetEntityName(propDeclaration) : null;
            this.Inherit     = !propDeclaration.HasModifier(Modifiers.Static);
            this.Static      = propDeclaration.HasModifier(Modifiers.Static);
            this.IsSetter    = isSetter;
            this.Member      = this.FindMember(propDeclaration);
            var p = (IProperty)this.Member;

            this.CancelChangeCase = this.Member.DeclaringTypeDefinition == null || !this.Emitter.Validator.IsObjectLiteral(this.Member.DeclaringTypeDefinition);
            this.FieldJsName      = this.Emitter.GetEntityName(p, true);
            this.TypeDefinition   = this.Member.DeclaringTypeDefinition;
            this.Type             = this.Member.DeclaringType;
            this.InitMembers();
            this.Emitter.OverloadsCacheNodes[new Tuple <AstNode, bool>(propDeclaration, isSetter)] = this;

            this.SetCaseFromNameAttr();
        }
		internal static IField GetBackingField (BaseRefactoringContext context, PropertyDeclaration propertyDeclaration)
		{
			// automatic properties always need getter & setter
			if (propertyDeclaration == null || propertyDeclaration.Getter.IsNull || propertyDeclaration.Setter.IsNull || propertyDeclaration.Getter.Body.IsNull || propertyDeclaration.Setter.Body.IsNull)
				return null;
			if (!context.Supports(csharp3) || propertyDeclaration.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Abstract) || ((TypeDeclaration)propertyDeclaration.Parent).ClassType == ClassType.Interface)
				return null;
			var getterField = ScanGetter (context, propertyDeclaration);
			if (getterField == null)
				return null;
			var setterField = ScanSetter (context, propertyDeclaration);
			if (setterField == null)
				return null;
			if (!getterField.Equals(setterField))
				return null;
			return getterField;
		}
예제 #16
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            if (propertyDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = propertyDeclaration.HasModifier(Modifiers.Static);

            IDictionary <string, List <EntityDeclaration> > dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = propertyDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(propertyDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { propertyDeclaration }));
            }

            var rr = this.Resolver.ResolveNode(propertyDeclaration, null) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(rr))
            {
                var config = rr.Member.IsStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = propertyDeclaration
                });
            }

            var isResolvedProperty = false;
            MemberResolveResult resolvedProperty = null;

            CheckFieldProperty(propertyDeclaration, ref isResolvedProperty, ref resolvedProperty);

            if (!propertyDeclaration.Getter.IsNull &&
                !this.HasIgnore(propertyDeclaration) &&
                !this.HasInline(propertyDeclaration.Getter) &&
                propertyDeclaration.Getter.Body.IsNull &&
                !this.HasScript(propertyDeclaration.Getter))
            {
                Expression     initializer = this.GetDefaultFieldInitializer(propertyDeclaration.ReturnType);
                TypeConfigInfo info        = isStatic ? this.CurrentType.StaticConfig : this.CurrentType.InstanceConfig;

                if (!isResolvedProperty)
                {
                    resolvedProperty   = Resolver.ResolveNode(propertyDeclaration, null) as MemberResolveResult;
                    isResolvedProperty = true;
                }

                bool autoPropertyToField = false;
                if (resolvedProperty != null && resolvedProperty.Member != null)
                {
                    autoPropertyToField = Helpers.IsFieldProperty(resolvedProperty.Member, AssemblyInfo);
                }
                else
                {
                    autoPropertyToField = AssemblyInfo.AutoPropertyToField;
                }

                var autoInitializer = info.AutoPropertyInitializers.FirstOrDefault(f => f.Name == key);

                if (autoInitializer != null)
                {
                    initializer = autoInitializer.Initializer;
                }

                if (!autoPropertyToField)
                {
                    if (resolvedProperty != null && resolvedProperty.Member.ImplementedInterfaceMembers.Count > 0 && resolvedProperty.Member.ImplementedInterfaceMembers.Any(m => Helpers.IsFieldProperty(m, this.AssemblyInfo)))
                    {
                        throw new EmitterException(propertyDeclaration, string.Format("The property {0} is not marked as FieldProperty but implemented interface member has such attribute", resolvedProperty.Member.ToString()));
                    }

                    info.Properties.Add(new TypeConfigItem
                    {
                        Name        = key,
                        Entity      = propertyDeclaration,
                        Initializer = initializer
                    });
                }
                else
                {
                    if (resolvedProperty != null && resolvedProperty.Member.ImplementedInterfaceMembers.Count > 0 && !resolvedProperty.Member.ImplementedInterfaceMembers.All(m => Helpers.IsFieldProperty(m, this.AssemblyInfo)))
                    {
                        throw new EmitterException(propertyDeclaration, string.Format("The property {0} is marked as FieldProperty but implemented interface member has no such attribute", resolvedProperty.Member.ToString()));
                    }

                    info.Fields.Add(new TypeConfigItem
                    {
                        Name        = key,
                        Entity      = propertyDeclaration,
                        Initializer = initializer
                    });
                }
            }
        }
예제 #17
0
        protected virtual void EmitPropertyMethod(PropertyDeclaration propertyDeclaration, Accessor accessor, bool setter)
        {
            var memberResult = this.Emitter.Resolver.ResolveNode(propertyDeclaration, this.Emitter) as MemberResolveResult;

            if (memberResult != null &&
                (memberResult.Member.Attributes.Any(a => a.AttributeType.FullName == "Bridge.FieldPropertyAttribute" ||
                    a.AttributeType.FullName == "Bridge.IgnoreAttribute" ||
                    a.AttributeType.FullName == "Bridge.ExternalAttribute") ||
                (propertyDeclaration.Getter.IsNull && propertyDeclaration.Setter.IsNull)))
            {
                return;
            }

            if (!accessor.IsNull && this.Emitter.GetInline(accessor) == null)
            {
                //this.EnsureComma();
                this.EnsureNewLine();
                this.ResetLocals();

                var prevMap = this.BuildLocalsMap();
                var prevNamesMap = this.BuildLocalsNamesMap();

                if (setter)
                {
                    this.AddLocals(new ParameterDeclaration[] { new ParameterDeclaration { Name = "value" } }, accessor.Body);
                }

                XmlToJsDoc.EmitComment(this, this.PropertyDeclaration);
                var overloads = OverloadsCollection.Create(this.Emitter, propertyDeclaration, setter);
                string name = overloads.GetOverloadName();
                name = setter ? "set" + name : "get" + name;
                TransformCtx.CurClassMethodNames.Add(new TransformCtx.MethodInfo() {
                    Name = name,
                    IsPrivate = propertyDeclaration.HasModifier(Modifiers.Private),
                });

                this.Write(name);
                this.WriteEqualsSign();
                this.WriteFunction();
                this.WriteOpenParentheses();
                if(propertyDeclaration.HasModifier(Modifiers.Static)) {
                    this.Write(setter ? "value" : "");
                }
                else {
                    this.WriteThis();
                    this.Write(setter ? ", value" : "");
                }
                this.WriteCloseParentheses();
                this.WriteSpace();
                this.BeginFunctionBlock();

                var script = this.Emitter.GetScript(accessor);

                if (script == null)
                {
                    accessor.Body.AcceptVisitor(this.Emitter);
                }
                else
                {
                    foreach (var line in script)
                    {
                        this.Write(line);
                        this.WriteNewLine();
                    }
                }

                this.EndFunctionBlock();
                this.ClearLocalsMap(prevMap);
                this.ClearLocalsNamesMap(prevNamesMap);
                this.Emitter.Comma = true;
            }
        }
예제 #18
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            if (propertyDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = propertyDeclaration.HasModifier(Modifiers.Static);

            IDictionary<string, List<EntityDeclaration>> dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = propertyDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(propertyDeclaration);
            }
            else
            {
                dict.Add(key, new List<EntityDeclaration>(new[] { propertyDeclaration }));
            }

            if (!propertyDeclaration.Getter.IsNull
                && !this.HasInline(propertyDeclaration.Getter)
                && propertyDeclaration.Getter.Body.IsNull
                && !this.HasScript(propertyDeclaration.Getter))
            {
                Expression initializer = this.GetDefaultFieldInitializer(propertyDeclaration.ReturnType);
                TypeConfigInfo info = isStatic ? this.CurrentType.StaticConfig : this.CurrentType.InstanceConfig;
                if (!this.AssemblyInfo.AutoPropertyToField)
                {
                    info.Properties.Add(new TypeConfigItem
                    {
                        Name = key,
                        Entity = propertyDeclaration,
                        Initializer = initializer
                    });
                }
                else
                {

                    info.Fields.Add(new TypeConfigItem
                    {
                        Name = key,
                        Entity = propertyDeclaration,
                        Initializer = initializer
                    });
                }
            }
        }
예제 #19
0
        public override void VisitPropertyDeclaration(PropertyDeclaration e)
        {
            var pd = e.Annotation<PropertyDefinition>();
            if (pd != null)
            {
                if (!e.PrivateImplementationType.IsNull && pd.DeclaringType.IsCompilerGenerated())
                {
                    // 将接口实现转换为显式实现
                    var convert = true;
                    if (pd.DeclaringType.Interfaces.Count > 1)
                    {
                        //TagSnapshotRange<T> IEnumerator<TagSnapshotRange<T>>.Current { get { …… } }
                        //object IEnumerator.Current { get { …… } }
                        var trInterface = e.PrivateImplementationType.Annotation<TypeReference>();
                        var hasSameNameInterfaces = pd.DeclaringType.Interfaces.Select(x => x.Resolve()).Where(x => x == null || x.WellFullName != trInterface.WellFullName && x.Properties.Any(y => y.WellName == pd.WellName));
                        if (hasSameNameInterfaces.Any())
                        {
                            if (hasSameNameInterfaces.Any(x => x == null) ||
                                !trInterface.HasGenericParameters ||
                                hasSameNameInterfaces.Any(x => x.HasGenericParameters))
                            {
                                convert = false;
                            }
                        }
                    }
                    if (convert)
                    {
                        e.PrivateImplementationType = null;
                        e.Modifiers |= Modifiers.Public;
                    }
                }

                var accessor = pd.GetMethod ?? pd.SetMethod;
                if (accessor != null && accessor.HasOverrides)
                {
                    // 属性重载或接口属性的显式实现
                    // bool ICollection.xxx
                    //====>
                    // bool ICollection.IsSynchronized
                    var access = accessor.Overrides.First();
                    if (access.Name.StartsWith("get_") || access.Name.StartsWith("set_"))
                    {
                        var opn = access.Name.Substring(4);
                        var opd = access.DeclaringType.Resolve().Properties.FirstOrDefault(x => x.Name == opn);
                        if (opd != null) e.Name = opd.WellName;
                    }
                }
                else if (e.HasModifier(Modifiers.Override))
                {

                }
            }
            base.VisitPropertyDeclaration(e);
        }
예제 #20
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            if (propertyDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = propertyDeclaration.HasModifier(Modifiers.Static);

            IDictionary <string, List <EntityDeclaration> > dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = propertyDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(propertyDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { propertyDeclaration }));
            }

            var rr = this.Resolver.ResolveNode(propertyDeclaration, null) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(rr))
            {
                var config = rr.Member.IsStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = propertyDeclaration
                });
            }

            if (!this.HasExternal(propertyDeclaration) &&
                !this.HasTemplate(propertyDeclaration.Getter))
            {
                Expression     initializer = this.GetDefaultFieldInitializer(propertyDeclaration.ReturnType);
                TypeConfigInfo info        = isStatic ? this.CurrentType.StaticConfig : this.CurrentType.InstanceConfig;

                bool autoPropertyToField = false;
                if (rr != null && rr.Member != null && Helpers.IsAutoProperty((IProperty)rr.Member))
                {
                    var rules = Rules.Get(this.Emitter, rr.Member);

                    if (rules.AutoProperty.HasValue)
                    {
                        autoPropertyToField = rules.AutoProperty.Value == AutoPropertyRule.Plain;
                    }
                    else
                    {
                        autoPropertyToField = this.HasFieldAttribute(rr.Member);

                        if (!autoPropertyToField && rr.Member.ImplementedInterfaceMembers.Count > 0)
                        {
                            foreach (var interfaceMember in rr.Member.ImplementedInterfaceMembers)
                            {
                                autoPropertyToField = this.HasFieldAttribute(interfaceMember, false);

                                if (autoPropertyToField)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                var autoInitializer = info.AutoPropertyInitializers.FirstOrDefault(f => f.Name == key);

                if (autoInitializer != null)
                {
                    initializer = autoInitializer.Initializer;
                }

                if (!autoPropertyToField)
                {
                    info.Properties.Add(new TypeConfigItem
                    {
                        Name                  = key,
                        Entity                = propertyDeclaration,
                        Initializer           = initializer,
                        IsPropertyInitializer = autoInitializer != null
                    });
                }
                else
                {
                    info.Fields.Add(new TypeConfigItem
                    {
                        Name        = key,
                        Entity      = propertyDeclaration,
                        Initializer = initializer
                    });
                }
            }
        }
        protected virtual void EmitPropertyMethod(PropertyDeclaration propertyDeclaration, Accessor accessor, bool setter)
        {
            var memberResult = this.Emitter.Resolver.ResolveNode(propertyDeclaration, this.Emitter) as MemberResolveResult;

            if (memberResult != null &&
                (memberResult.Member.Attributes.Any(a => a.AttributeType.FullName == "Bridge.FieldPropertyAttribute" ||
                                                    a.AttributeType.FullName == "Bridge.IgnoreAttribute" ||
                                                    a.AttributeType.FullName == "Bridge.ExternalAttribute") ||
                 (propertyDeclaration.Getter.IsNull && propertyDeclaration.Setter.IsNull)))
            {
                return;
            }

            if (!accessor.IsNull)
            {
                //this.EnsureComma();
                this.EnsureNewLine();
                this.ResetLocals();

                var prevMap      = this.BuildLocalsMap();
                var prevNamesMap = this.BuildLocalsNamesMap();

                if (setter)
                {
                    this.AddLocals(new ParameterDeclaration[] { new ParameterDeclaration {
                                                                    Name = "value"
                                                                } }, accessor.Body);
                }

                XmlToJsDoc.EmitComment(this, this.PropertyDeclaration);
                var    overloads = OverloadsCollection.Create(this.Emitter, propertyDeclaration, setter);
                string name      = overloads.GetOverloadName();
                name = setter ? "set" + name : "get" + name;
                TransformCtx.CurClassMethodNames.Add(new TransformCtx.MethodInfo()
                {
                    Name      = name,
                    IsPrivate = propertyDeclaration.HasModifier(Modifiers.Private),
                });

                this.Write(name);
                this.WriteEqualsSign();
                this.WriteFunction();
                this.WriteOpenParentheses();
                if (propertyDeclaration.HasModifier(Modifiers.Static))
                {
                    this.Write(setter ? "value" : "");
                }
                else
                {
                    this.WriteThis();
                    this.Write(setter ? ", value" : "");
                }
                this.WriteCloseParentheses();
                this.WriteSpace();
                this.BeginFunctionBlock();

                MarkTempVars();
                accessor.Body.AcceptVisitor(this.Emitter);
                EmitTempVars();

                this.EndFunctionBlock();
                this.ClearLocalsMap(prevMap);
                this.ClearLocalsNamesMap(prevNamesMap);
                this.Emitter.Comma = true;
            }
        }
예제 #22
0
 private OverloadsCollection(IEmitter emitter, PropertyDeclaration propDeclaration, bool isSetter)
 {
     this.Emitter = emitter;
     this.Name = propDeclaration.Name;
     this.JsName = Helpers.GetPropertyRef(propDeclaration, emitter, isSetter, true, true);
     this.AltJsName = Helpers.GetPropertyRef(propDeclaration, emitter, !isSetter, true, true);
     this.Inherit = !propDeclaration.HasModifier(Modifiers.Static);
     this.Static = propDeclaration.HasModifier(Modifiers.Static);
     this.CancelChangeCase = !Helpers.IsFieldProperty(propDeclaration, emitter);
     this.IsSetter = isSetter;
     this.Member = this.FindMember(propDeclaration);
     this.TypeDefinition = this.Member.DeclaringTypeDefinition;
     this.Type = this.Member.DeclaringType;
     this.InitMembers();
     this.Emitter.OverloadsCache[propDeclaration.GetHashCode().ToString() + isSetter.GetHashCode().ToString()] = this;
 }
예제 #23
0
            public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
            {
                base.VisitPropertyDeclaration(propertyDeclaration);

                if (!propertyDeclaration.HasModifier(Modifiers.Override))
                {
                    return;
                }

                bool hasGetter = !propertyDeclaration.Getter.IsNull;
                bool hasSetter = !propertyDeclaration.Setter.IsNull;

                if (!hasGetter && !hasSetter)
                {
                    return;
                }

                if (hasGetter && propertyDeclaration.Getter.Body.Statements.Count != 1)
                {
                    return;
                }

                if (hasSetter && propertyDeclaration.Setter.Body.Statements.Count != 1)
                {
                    return;
                }

                var resultProperty = ctx.Resolve(propertyDeclaration) as MemberResolveResult;

                if (resultProperty == null)
                {
                    return;
                }
                var baseProperty = InheritanceHelper.GetBaseMember(resultProperty.Member) as IProperty;

                if (baseProperty == null)
                {
                    return;
                }

                bool hasBaseGetter = baseProperty.Getter != null;
                bool hasBaseSetter = baseProperty.Setter != null;

                if (hasBaseGetter)
                {
                    if (hasGetter)
                    {
                        var expr = propertyDeclaration.Getter.Body.Statements.FirstOrNullObject();

                        if (expr == null || !(expr is ReturnStatement))
                        {
                            return;
                        }

                        var memberReferenceExpression = (expr as ReturnStatement).Expression as MemberReferenceExpression;

                        if (memberReferenceExpression == null ||
                            memberReferenceExpression.MemberName != propertyDeclaration.Name ||
                            !(memberReferenceExpression.FirstChild is BaseReferenceExpression))
                        {
                            return;
                        }
                    }
                }

                if (hasBaseSetter)
                {
                    if (hasSetter)
                    {
                        var match = setterPattern.Match(propertyDeclaration.Setter.Body.Statements.FirstOrNullObject());
                        if (!match.Success)
                        {
                            return;
                        }
                        var memberReferenceExpression = match.Get("left").Single() as MemberReferenceExpression;
                        if (memberReferenceExpression == null ||
                            memberReferenceExpression.MemberName != propertyDeclaration.Name ||
                            !(memberReferenceExpression.FirstChild is BaseReferenceExpression))
                        {
                            return;
                        }
                    }
                }

                var title = ctx.TranslateString("Redundant property override");

                AddIssue(new CodeIssue(propertyDeclaration, title, ctx.TranslateString("Remove redundant property override"), script => script.Remove(propertyDeclaration))
                {
                    IssueMarker = IssueMarker.GrayOut
                });
            }
예제 #24
0
            public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
            {
                base.VisitPropertyDeclaration(propertyDeclaration);

                if (!propertyDeclaration.HasModifier(Modifiers.Override))
                {
                    return;
                }

                bool hasGetter = !propertyDeclaration.Getter.IsNull;
                bool hasSetter = !propertyDeclaration.Setter.IsNull;

                if (!hasGetter && !hasSetter)
                {
                    return;
                }

                if (hasGetter && propertyDeclaration.Getter.Body.Statements.Count != 1)
                {
                    return;
                }

                if (hasSetter && propertyDeclaration.Setter.Body.Statements.Count != 1)
                {
                    return;
                }

                var resultProperty = ctx.Resolve(propertyDeclaration) as MemberResolveResult;
                var basetype       = resultProperty.Member.DeclaringTypeDefinition.DirectBaseTypes.FirstOrDefault();

                if (basetype == null)
                {
                    return;
                }
                var baseProperty = basetype.GetMembers(f => f.Name.Equals(propertyDeclaration.Name)).FirstOrDefault();

                if (baseProperty == null)
                {
                    return;
                }

                bool hasBaseGetter = ((baseProperty as IProperty).Getter != null);
                bool hasBaseSetter = ((baseProperty as IProperty).Setter != null);

                if (hasBaseGetter)
                {
                    if (hasGetter)
                    {
                        var expr = propertyDeclaration.Getter.Body.Statements.FirstOrNullObject();

                        if (expr == null || !(expr is ReturnStatement))
                        {
                            return;
                        }

                        var memberReferenceExpression = (expr as ReturnStatement).Expression as MemberReferenceExpression;

                        if (memberReferenceExpression == null ||
                            memberReferenceExpression.MemberName != propertyDeclaration.Name ||
                            !(memberReferenceExpression.FirstChild is BaseReferenceExpression))
                        {
                            return;
                        }
                    }
                }

                if (hasBaseSetter)
                {
                    if (hasSetter)
                    {
                        var expr = propertyDeclaration.Setter.Body.Statements.FirstOrNullObject();

                        if (expr == null || !(expr.FirstChild is AssignmentExpression))
                        {
                            return;
                        }

                        var memberReferenceExpression = (expr.FirstChild as AssignmentExpression).Left as MemberReferenceExpression;

                        if (memberReferenceExpression == null ||
                            memberReferenceExpression.MemberName != propertyDeclaration.Name ||
                            !(memberReferenceExpression.FirstChild is BaseReferenceExpression))
                        {
                            return;
                        }
                    }
                }

                var title = ctx.TranslateString("Redundant property override");

                AddIssue(propertyDeclaration, title, ctx.TranslateString("Remove redundant property override"), script => {
                    script.Remove(propertyDeclaration);
                });
            }