コード例 #1
0
        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);
        }
コード例 #2
0
        public static AccessRights GetCombinedAccessRights(this MemberWithAccess member)
        {
            Contract.Requires(member != null);

            return(AccessVisibilityChecker.CombineTypeAndMemberAccessRights(member.TypeAccessRights,
                                                                            member.MemberAccessRights));
        }
コード例 #3
0
        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));
            }
        }
コード例 #4
0
        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);
            }

        }
コード例 #5
0
        private bool ExceptionIsLessVisible(MemberWithAccess preconditionContainer, IClass exception)
        {
            Contract.Requires(preconditionContainer != null);
            Contract.Requires(exception != null);

            return(!AccessVisibilityChecker.MemberWith(exception.GetAccessRights())
                   .IsAccessibleFrom(preconditionContainer.GetCombinedAccessRights()));
        }
コード例 #6
0
        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;
        }
        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;
        }
コード例 #12
0
        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));
        }
コード例 #13
0
        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));
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
 private AccessVisibilityChecker(MemberWithAccess referencedMember)
 {
     _referencedMember = referencedMember;
 }
コード例 #17
0
 protected abstract IEnumerable <IHighlighting> DoRun(IInvocationExpression invocationExpression,
                                                      ContractRequires contractAssertion,
                                                      MemberWithAccess preconditionContainer);
コード例 #18
0
 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();
 }
コード例 #19
0
        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;
        }
コード例 #20
0
        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;
        }
コード例 #21
0
 public static AccessVisibilityChecker Member(MemberWithAccess member)
 {
     Contract.Requires(member != null);
     Contract.Ensures(Contract.Result <AccessVisibilityChecker>() != null);
     return(new AccessVisibilityChecker(member));
 }