public override ProblemCollection Check(TypeNode type) { Type runtimeType = type.GetRuntimeType(); if ( IsTestFixture( runtimeType ) && !runtimeType.IsAbstract && type.IsVisibleOutsideAssembly ) { MemberList constructors = type.GetConstructors(); for ( int i = 0; i < constructors.Length; ++i ) { Member constructor = constructors[i]; // only examine non-static constructors Microsoft.Cci.InstanceInitializer instanceConstructor = constructor as Microsoft.Cci.InstanceInitializer; if ( instanceConstructor == null ) continue; // trigger errors for non-default constructors. if ( ( instanceConstructor.Parameters.Length != 0 ) && ( !instanceConstructor.IsPrivate ) ) { Resolution resolution = GetResolution( runtimeType.Name ); Problem problem = new Problem( resolution ); base.Problems.Add( problem ); } } if ( base.Problems.Count > 0 ) return base.Problems; } return base.Check (type); }
public override ProblemCollection Check(TypeNode type) { if (type == null) return Problems; if (!IsPresenterImplementation(type)) return Problems; var basePresenter = GetBasePresenterTypeNode(type); if (basePresenter == null) throw new InvalidOperationException("Failed to find WebFormsMvp.Presenter`1 even though we found WebFormsMvp.IPresenter."); var presenterBaseType = type; // We have an extra level of base type checking here so that we skip System.Object while (presenterBaseType.BaseType != null && presenterBaseType.BaseType.BaseType != null) { presenterBaseType = presenterBaseType.BaseType; } if (presenterBaseType.Template != basePresenter) return Problems; var viewTypeFromGenericTypeArgument = presenterBaseType.TemplateArguments.Single(); var iViewType = GetIViewTypeNode(type); if (iViewType == null) throw new InvalidOperationException("Failed to find WebFormsMvp.IView even though we found WebFormsMvp.IPresenter."); var badParameters = type .GetConstructors() .Cast<InstanceInitializer>() .SelectMany(c => c.Parameters.Where(p => p.Type.IsAssignableTo(iViewType))) .Where(p => p.Type != viewTypeFromGenericTypeArgument); foreach(var param in badParameters) { Problems.Add(new Problem(GetResolution(new[] { type.Name.Name, viewTypeFromGenericTypeArgument.Name.Name, param.Type.Name.Name }), param) { Certainty = 100, FixCategory = FixCategories.NonBreaking, MessageLevel = MessageLevel.Error }); } return Problems; }
/// <summary> /// See if any of the constructors of a given type (possibly) accepts a delayed parameter. /// If a class is not sealed, it may have a derived class that, for example, has a constructor /// accepting a delayed parameter. /// We conservatively consider this case as "may have" and return true. /// </summary> /// <param name="t"></param> /// <returns></returns> private bool hasConstructorAcceptingDelay(TypeNode t) { MemberList ml = t.GetConstructors(); if (!t.IsSealed) return true; if (ml.Count == 0) return false; foreach (Member m in ml) { if (m is InstanceInitializer) { InstanceInitializer ii = m as InstanceInitializer; ParameterList pl = ii.Parameters; foreach (Parameter p in pl) { if (p is This) continue; // will "this" show up here? if (p.IsUniversallyDelayed) { return true; } } // no need to look at base types /* TypeNode parentType = t.BaseType; while (parentType != null) { if (hasConstructorAcceptingDelay(parentType)) { return true; } parentType = parentType.BaseType; } */ } } return false; }
public virtual bool TypeHasNoVisibleConstructorsOrIsAbstract(TypeNode type, TypeNode referringType){ if (type == null || referringType == null || type.IsAbstract) return true; if (type is Struct || type is EnumNode) return false; TypeNode dummy = referringType; MemberList constructors = type.GetConstructors(); for (int i = 0, n = constructors == null ? 0 : constructors.Count; i < n; i++){ Member constr = constructors[i]; if (constr == null) continue; if (!Checker.NotAccessible(constr, ref dummy, referringType.DeclaringModule, referringType, null)) return false; } return true; }
protected virtual Overloads GetConstructors(int line, int col, TypeNode type){ Node node; Scope scope; int identContext; this.languageService.SearchForNodeAtPosition(line+1, col+1, out node, out scope, out identContext); TypeNode referringType = null; Module referringModule = null; while (scope != null){ TypeScope tScope = scope as TypeScope; if (tScope != null){ referringType = tScope.Type; if (referringType != null){ referringModule = referringType.DeclaringModule; break; } } NamespaceScope nScope = scope as NamespaceScope; if (nScope != null){ referringModule = nScope.AssociatedModule; break; } scope = scope.OuterScope; } bool showPrivate = referringType == type; bool showFamily = referringType != null && referringType.IsAssignableTo(type); bool showInternal = this.MayAccessInternals(referringType, type) || this.MayAccessInternals(referringModule, type); Member selectedMember = this.GetMember(line, col); MemberList members = type == null ? null : type.GetConstructors(); int positionOfSelectedMember = 0; MemberList filteredMembers = new MemberList(); if (type != null && type.IsValueType){ //Add dummy default constructor InstanceInitializer cons = new InstanceInitializer(type, null, null, null); cons.Flags |= MethodFlags.Public; filteredMembers.Add(cons); } for (int i = 0, n = members == null ? 0 : members.Count; i < n; i++){ Method meth = members[i] as Method; if (meth == null) continue; if (meth.IsCompilerControlled) continue; if (meth.IsPrivate && !showPrivate) continue; if ((meth.IsFamily || meth.IsFamilyAndAssembly) && !showFamily) continue; if ((meth.IsAssembly || meth.IsFamilyOrAssembly) && !showInternal) continue; if (meth == selectedMember) positionOfSelectedMember = filteredMembers.Count; filteredMembers.Add(meth); } if (filteredMembers.Count == 0) return null; return new Overloads(filteredMembers, scope, positionOfSelectedMember, this.helper, OverloadKind.Constructors); }
public override ProblemCollection Check(TypeNode type) { if (type == null) { return(Problems); } if (!IsPresenterImplementation(type)) { return(Problems); } var basePresenter = GetBasePresenterTypeNode(type); if (basePresenter == null) { throw new InvalidOperationException("Failed to find WebFormsMvp.Presenter`1 even though we found WebFormsMvp.IPresenter."); } var presenterBaseType = type; // We have an extra level of base type checking here so that we skip System.Object while (presenterBaseType.BaseType != null && presenterBaseType.BaseType.BaseType != null) { presenterBaseType = presenterBaseType.BaseType; } if (presenterBaseType.Template != basePresenter) { return(Problems); } var viewTypeFromGenericTypeArgument = presenterBaseType.TemplateArguments.Single(); var iViewType = GetIViewTypeNode(type); if (iViewType == null) { throw new InvalidOperationException("Failed to find WebFormsMvp.IView even though we found WebFormsMvp.IPresenter."); } var badParameters = type .GetConstructors() .Cast <InstanceInitializer>() .SelectMany(c => c.Parameters.Where(p => p.Type.IsAssignableTo(iViewType))) .Where(p => p.Type != viewTypeFromGenericTypeArgument); foreach (var param in badParameters) { Problems.Add(new Problem(GetResolution(new[] { type.Name.Name, viewTypeFromGenericTypeArgument.Name.Name, param.Type.Name.Name }), param) { Certainty = 100, FixCategory = FixCategories.NonBreaking, MessageLevel = MessageLevel.Error }); } return(Problems); }