private bool IsAvailable(IInvocationExpression expression, out MemberWithAccess preconditionContainer, out MemberWithAccess lessVisibleMember) { preconditionContainer = null; lessVisibleMember = null; var contractAssertion = CodeContractAssertion.FromInvocationExpression(expression); if (contractAssertion == null || contractAssertion.AssertionType != ContractAssertionType.Requires) { return(false); } var preconditionHolder = expression.GetContainingStatement() .With(x => x.GetContainingTypeMemberDeclaration()) .With(x => x.DeclaredElement) .With(MemberWithAccess.FromDeclaredElement); preconditionContainer = preconditionHolder; if (preconditionContainer == null) { return(false); } // Looking for a "enclosing" members that are less visible then a contract holder. // The only exception is a field with ContractPublicPropertyName attribute. lessVisibleMember = ProcessReferenceExpressions(contractAssertion.OriginalPredicateExpression) .FirstOrDefault(member => !FieldFromPreconditionMarkedWithContractPublicPropertyName(member) && !AccessVisibilityChecker.Member(member).IsAccessibleFrom(preconditionHolder)); return(lessVisibleMember != null); }
public static AccessRights GetCombinedAccessRights(this MemberWithAccess member) { Contract.Requires(member != null); return(AccessVisibilityChecker.CombineTypeAndMemberAccessRights(member.TypeAccessRights, member.MemberAccessRights)); }
protected override IEnumerable <IHighlighting> DoRun(IInvocationExpression invocationExpression, ContractRequires contractAssertion, MemberWithAccess preconditionContainer) { var preconditionException = contractAssertion.GenericArgumentDeclaredType .With(x => x.Resolve()) .With(x => x.DeclaredElement as IClass); if (preconditionException != null) { if (ExceptionIsLessVisible(preconditionContainer, preconditionException)) { yield return (new RequiresExceptionInconsistentVisibiityHighlighting(invocationExpression, preconditionException, preconditionContainer)); } if (DoesntHaveAppropriateConstructor(preconditionException)) { yield return(new RequiresExceptionValidityHighlighting(invocationExpression, preconditionException, preconditionContainer)); } } if (!MessageIsAppropriateForContractRequires(contractAssertion.Message)) { yield return(new InvalidRequiresMessageHighlighting(invocationExpression, contractAssertion.Message)); } }
protected override IEnumerable<IHighlighting> DoRun(IInvocationExpression invocationExpression, ContractRequires contractAssertion, MemberWithAccess preconditionContainer) { var preconditionException = contractAssertion.GenericArgumentDeclaredType .With(x => x.Resolve()) .With(x => x.DeclaredElement as IClass); if (preconditionException != null) { if (ExceptionIsLessVisible(preconditionContainer, preconditionException)) { yield return new RequiresExceptionInconsistentVisibiityHighlighting(preconditionException, preconditionContainer); } if (DoesntHaveAppropriateConstructor(preconditionException)) { yield return new RequiresExceptionValidityHighlighting(preconditionException, preconditionContainer); } } if (!MessageIsAppropriateForContractRequires(contractAssertion.Message)) { yield return new InvalidRequiresMessageHighlighting(contractAssertion.Message); } }
private bool ExceptionIsLessVisible(MemberWithAccess preconditionContainer, IClass exception) { Contract.Requires(preconditionContainer != null); Contract.Requires(exception != null); return(!AccessVisibilityChecker.MemberWith(exception.GetAccessRights()) .IsAccessibleFrom(preconditionContainer.GetCombinedAccessRights())); }
private bool ExceptionIsLessVisible(MemberWithAccess preconditionContainer, IClass exception) { Contract.Requires(preconditionContainer != null); Contract.Requires(exception != null); return !AccessVisibilityChecker.MemberWith(exception.GetAccessRights()) .IsAccessibleFrom(preconditionContainer.GetCombinedAccessRights()); }
internal RequiresExceptionInconsistentVisibiityHighlighting( IClass exceptionDeclaration, MemberWithAccess preconditionContainer) { Contract.Requires(exceptionDeclaration != null); Contract.Requires(preconditionContainer != null); _exceptionDeclaration = exceptionDeclaration; _preconditionContainer = preconditionContainer; }
internal RequiresExceptionValidityHighlighting(IInvocationExpression invocationExpression, IClass exceptionDeclaration, MemberWithAccess preconditionContainer) { Contract.Requires(exceptionDeclaration != null); Contract.Requires(preconditionContainer != null); Contract.Requires(invocationExpression != null); _range = invocationExpression.GetHighlightingRange(); _exceptionDeclaration = exceptionDeclaration; _preconditionContainer = preconditionContainer; }
internal RequiresExceptionInconsistentVisibiityHighlighting(IInvocationExpression invocationExpression, IClass exceptionDeclaration, MemberWithAccess preconditionContainer) { Contract.Requires(exceptionDeclaration != null); Contract.Requires(preconditionContainer != null); Contract.Requires(invocationExpression != null); _range = invocationExpression.GetHighlightingRange(); _exceptionDeclaration = exceptionDeclaration; _preconditionContainer = preconditionContainer; }
internal RequiresInconsistentVisibiityHighlighting(ICSharpStatement preconditionStatement, MemberWithAccess preconditionContainer, MemberWithAccess lessVisibleReferencedMember) { Contract.Requires(preconditionStatement != null); Contract.Requires(preconditionContainer != null); Contract.Requires(lessVisibleReferencedMember != null); _range = preconditionStatement.GetHighlightingRange(); _preconditionContainer = preconditionContainer; _lessVisibleReferencedMember = lessVisibleReferencedMember; }
private bool FieldFromPreconditionMarkedWithContractPublicPropertyName(MemberWithAccess member) { if (member.MemberType != MemberType.Field) { return(false); } var field = (IField)member.DeclaredElement; return(field.HasAttributeInstance( new ClrTypeName(typeof(ContractPublicPropertyNameAttribute).FullName), false)); }
public bool IsAccessibleFrom(MemberWithAccess contractHolder) { Contract.Requires(contractHolder != null); Contract.Assert(_referencedMember != null); // If type visibilities are the same, lets ignore them! if (_referencedMember.TypeAccessRights == contractHolder.TypeAccessRights) { return(MemberWith(_referencedMember.MemberAccessRights) .IsAccessibleFrom(contractHolder.MemberAccessRights)); } var referencedMemberCombinedVisibility = _referencedMember.GetCombinedAccessRights(); var contractHolderCombinedVisibility = contractHolder.GetCombinedAccessRights(); return(MemberWith(referencedMemberCombinedVisibility).IsAccessibleFrom(contractHolderCombinedVisibility)); }
public static bool BelongToTheSameType(this MemberWithAccess preconditionContainer, MemberWithAccess referencedMember) { Contract.Requires(preconditionContainer != null); Contract.Requires(referencedMember != null); var preconditionContainingType = preconditionContainer.DeclaredElement.GetContainingType(); var enclosingMemberContiningType = referencedMember.DeclaredElement.GetContainingType(); if (preconditionContainingType == null || enclosingMemberContiningType == null) { return(false); } return(preconditionContainingType.GetClrName().FullName == enclosingMemberContiningType.GetClrName().FullName); }
public static bool BelongToTheSameProject(this MemberWithAccess preconditionContainer, MemberWithAccess referencedMember) { Contract.Requires(preconditionContainer != null); Contract.Requires(referencedMember != null); var containerProject = preconditionContainer.DeclaredElement.GetContainingType() .With(x => x.GetSingleOrDefaultSourceFile()) .Return(x => x.GetProject()); var referencedMemberProject = referencedMember.DeclaredElement.GetContainingType() .With(x => x.GetSingleOrDefaultSourceFile()) .Return(x => x.GetProject()); if (containerProject == null || referencedMemberProject == null) { return(false); } return(containerProject.Guid == referencedMemberProject.Guid); }
private AccessVisibilityChecker(MemberWithAccess referencedMember) { _referencedMember = referencedMember; }
protected abstract IEnumerable <IHighlighting> DoRun(IInvocationExpression invocationExpression, ContractRequires contractAssertion, MemberWithAccess preconditionContainer);
protected override IEnumerable <IHighlighting> DoRun(IInvocationExpression invocationExpression, ContractRequires contractAssertion, MemberWithAccess preconditionContainer) { Contract.Requires(invocationExpression != null); Contract.Requires(contractAssertion != null); Contract.Requires(preconditionContainer != null); Contract.Ensures(Contract.Result <IEnumerable <IHighlighting> >() != null); throw new System.NotImplementedException(); }
public static bool BelongToTheSameType(this MemberWithAccess preconditionContainer, MemberWithAccess referencedMember) { Contract.Requires(preconditionContainer != null); Contract.Requires(referencedMember != null); var preconditionContainingType = preconditionContainer.DeclaredElement.GetContainingType(); var enclosingMemberContiningType = referencedMember.DeclaredElement.GetContainingType(); if (preconditionContainingType == null || enclosingMemberContiningType == null) return false; return preconditionContainingType.GetClrName().FullName == enclosingMemberContiningType.GetClrName().FullName; }
public static bool BelongToTheSameProject(this MemberWithAccess preconditionContainer, MemberWithAccess referencedMember) { Contract.Requires(preconditionContainer != null); Contract.Requires(referencedMember != null); var containerProject = preconditionContainer.DeclaredElement.GetContainingType() .With(x => x.GetSingleOrDefaultSourceFile()) .Return(x => x.GetProject()); var referencedMemberProject = referencedMember.DeclaredElement.GetContainingType() .With(x => x.GetSingleOrDefaultSourceFile()) .Return(x => x.GetProject()); if (containerProject == null || referencedMemberProject == null) return false; return containerProject.Guid == referencedMemberProject.Guid; }
public static AccessVisibilityChecker Member(MemberWithAccess member) { Contract.Requires(member != null); Contract.Ensures(Contract.Result <AccessVisibilityChecker>() != null); return(new AccessVisibilityChecker(member)); }