//private bool AnySecurityAttributeAdded(IDifferences differences, IReference target, IEnumerable<ISecurityAttribute> attribues1, IEnumerable<ISecurityAttribute> attributes2)
        //{
        //    return AnyAttributeAdded(differences, target, attribues1.SelectMany(a => a.Attributes), attributes2.SelectMany(a => a.Attributes));
        //}

        private void CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes)
        {
            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);

            attributeMapping.AddMapping(0, contractAttributes);
            attributeMapping.AddMapping(1, implAttributes);

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                {
                    ITypeReference type       = group.Representative.Attributes.First().Type;
                    string         attribName = type.FullName();

                    if (FilterAttribute(attribName))
                    {
                        break;
                    }

                    AttributeAdded(target, attribName);

                    differences.AddIncompatibleDifference("CannotAddAttribute",
                                                          "Attribute '{0}' exists on '{1}' in the implementation but not the contract.",
                                                          attribName, target.FullName());

                    break;
                }

                case DifferenceType.Changed:

                    //TODO: Add some more logic to check the two lists of attributes which have the same type.
                    break;

                case DifferenceType.Removed:
                {
                    ITypeReference type       = group.Representative.Attributes.First().Type;
                    string         attribName = type.FullName();

                    if (s_IgnorableAttributes.Contains(attribName))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                                          "Attribute '{0}' exists on '{1}' in the contract but not the implementation.",
                                                          attribName, target.FullName());

                    break;
                }
                }
            }
        }
Пример #2
0
        private DifferenceType DiffConstraints(IDifferences differences, IReference target, IEnumerable <IGenericParameter> implGenericParams, IEnumerable <IGenericParameter> contractGenericParams)
        {
            int beforeCount = differences.Count();

            IGenericParameter[] implParams     = implGenericParams.ToArray();
            IGenericParameter[] contractParams = contractGenericParams.ToArray();

            // We shoudn't hit this because the types/members shouldn't be matched up if they have different generic argument lists
            if (implParams.Length != contractParams.Length)
            {
                return(DifferenceType.Changed);
            }

            for (int i = 0; i < implParams.Length; i++)
            {
                IGenericParameter implParam     = implParams[i];
                IGenericParameter contractParam = contractParams[i];

                if (contractParam.Variance != TypeParameterVariance.NonVariant &&
                    contractParam.Variance != implParam.Variance)
                {
                    differences.AddIncompatibleDifference("CannotChangeVariance",
                                                          "Variance on generic parameter '{0}' for '{1}' is '{2}' in the implementation but '{3}' in the contract.",
                                                          implParam.FullName(), target.FullName(), implParam.Variance, contractParam.Variance);
                }

                string implConstraints     = string.Join(",", GetConstraints(implParam).OrderBy(s => s, StringComparer.OrdinalIgnoreCase));
                string contractConstraints = string.Join(",", GetConstraints(contractParam).OrderBy(s => s, StringComparer.OrdinalIgnoreCase));

                if (!string.Equals(implConstraints, contractConstraints))
                {
                    differences.AddIncompatibleDifference("CannotChangeGenericConstraints",
                                                          "Constraints for generic parameter '{0}' for '{1}' is '{2}' in the implementation but '{3}' in the contract.",
                                                          implParam.FullName(), target.FullName(), implConstraints, contractConstraints);
                }
            }

            if (differences.Count() != beforeCount)
            {
                return(DifferenceType.Changed);
            }

            return(DifferenceType.Unknown);
        }
Пример #3
0
        private DifferenceType DiffConstraints(IDifferences differences, IReference target, IEnumerable<IGenericParameter> implGenericParams, IEnumerable<IGenericParameter> contractGenericParams)
        {
            int beforeCount = differences.Count();
            IGenericParameter[] implParams = implGenericParams.ToArray();
            IGenericParameter[] contractParams = contractGenericParams.ToArray();

            // We shoudn't hit this because the types/members shouldn't be matched up if they have different generic argument lists
            if (implParams.Length != contractParams.Length)
                return DifferenceType.Changed;

            for (int i = 0; i < implParams.Length; i++)
            {
                IGenericParameter implParam = implParams[i];
                IGenericParameter contractParam = contractParams[i];

                if (contractParam.Variance != TypeParameterVariance.NonVariant &&
                    contractParam.Variance != implParam.Variance)
                {
                    differences.AddIncompatibleDifference("CannotChangeVariance",
                        "Variance on generic parameter '{0}' for '{1}' is '{2}' in the implementation but '{3}' in the contract.",
                        implParam.FullName(), target.FullName(), implParam.Variance, contractParam.Variance);
                }

                string implConstraints = string.Join(",", GetConstraints(implParam).OrderBy(s => s));
                string contractConstraints = string.Join(",", GetConstraints(contractParam).OrderBy(s => s));

                if (!string.Equals(implConstraints, contractConstraints))
                {
                    differences.AddIncompatibleDifference("CannotChangeGenericConstraints",
                        "Constraints for generic parameter '{0}' for '{1}' is '{2}' in the implementation but '{3}' in the contract.",
                        implParam.FullName(), target.FullName(), implConstraints, contractConstraints);
                }
            }

            if (differences.Count() != beforeCount)
                return DifferenceType.Changed;

            return DifferenceType.Unknown;
        }
        private bool AnyAttributeAdded(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes)
        {
            bool added = false;

            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);

            attributeMapping.AddMapping(0, implAttributes);
            attributeMapping.AddMapping(1, contractAttributes);

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                    ITypeReference type       = group.Representative.Attributes.First().Type;
                    string         attribName = type.FullName();

                    if (s_IgnorableAttributes.Contains(attribName))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference(this,
                                                          "Attribute '{0}' exists in the contract but not the implementation.",
                                                          attribName, target.FullName());

                    added = true;

                    break;

                case DifferenceType.Changed:

                    //TODO: Add some more logic to check the two lists of attributes which have the same type.
                    break;

                case DifferenceType.Removed:
                    // Removing attributes is OK
                    break;
                }
            }

            return(added);
        }
        private static bool Exclude(IReference reference, HashSet <string> exclusions, string alternateName = null)
        {
            string name     = reference.FullName();
            bool   excluded = exclusions.Contains(name);

            if (!excluded && alternateName != null)
            {
                excluded = exclusions.Contains(alternateName);
            }

            bool exclude = excluded || (reference.Attributes != null &&
                                        reference.Attributes.Any(attribute => attribute.IsEditorBrowseableStateNever()));

            if (exclude && !excluded)
            {
                exclusions.Add(name);
            }

            return(exclude);
        }
        private static bool Exclude(IReference reference, HashSet<string> exclusions, string alternateName = null)
        {
            string name = reference.FullName();
            bool excluded = exclusions.Contains(name);

            if (!excluded && alternateName != null)
            {
                excluded = exclusions.Contains(alternateName);
            }

            bool exclude = excluded || (reference.Attributes != null
                && reference.Attributes.Any(attribute => attribute.IsEditorBrowseableStateNever()));

            if (exclude && !excluded)
            {
                exclusions.Add(name);
            }

            return exclude;
        }
Пример #7
0
        private DifferenceType CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes)
        {
            DifferenceType difference = DifferenceType.Unchanged;
            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);
            AttributeComparer attributeComparer = new AttributeComparer();

            attributeMapping.AddMapping(0, contractAttributes.OrderBy(a => a, attributeComparer));
            attributeMapping.AddMapping(1, implAttributes.OrderBy(a => a, attributeComparer));

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    // Allow for additions
                    differences.Add(new Difference("AddedAttribute",
                                                   $"Attribute '{type.FullName()}' exists on '{target.FullName()}' in the {Implementation} but not the {Contract}."));

                    break;
                }

                case DifferenceType.Changed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    string contractKey       = attributeComparer.GetKey(group[0].Attributes.First());
                    string implementationKey = attributeComparer.GetKey(group[1].Attributes.First());

                    differences.AddIncompatibleDifference("CannotChangeAttribute",
                                                          $"Attribute '{type.FullName()}' on '{target.FullName()}' changed from '{contractKey}' in the {Contract} to '{implementationKey}' in the {Implementation}.");

                    difference = DifferenceType.Changed;
                    break;
                }

                case DifferenceType.Removed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                                          $"Attribute '{type.FullName()}' exists on '{target.FullName()}' in the {Contract} but not the {Implementation}.");


                    // removals of an attribute are considered a "change" of the type
                    difference = DifferenceType.Changed;
                    break;
                }
                }
            }
            return(difference);
        }
Пример #8
0
        private bool CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes, IReference member = null)
        {
            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);
            AttributeComparer attributeComparer = new AttributeComparer();

            attributeMapping.AddMapping(0, contractAttributes.OrderBy(a => a, attributeComparer));
            attributeMapping.AddMapping(1, implAttributes.OrderBy(a => a, attributeComparer));

            string errString = $"'{target.FullName()}'";

            if (target is IParameterDefinition || target is IGenericParameter)
            {
                errString  = target is IGenericParameter ? "generic param" : "parameter";
                errString += $" '{target.FullName()}' on member '{member?.FullName()}'";
            }

            bool changed = false;

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    // Allow for additions
                    differences.Add(new Difference("AddedAttribute",
                                                   $"Attribute '{type.FullName()}' exists on {errString} in the {Implementation} but not the {Contract}."));

                    changed = true;
                    break;
                }

                case DifferenceType.Changed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    string contractKey       = attributeComparer.GetKey(group[0].Attributes.First());
                    string implementationKey = attributeComparer.GetKey(group[1].Attributes.First());

                    differences.AddIncompatibleDifference("CannotChangeAttribute",
                                                          $"Attribute '{type.FullName()}' on {errString} changed from '{contractKey}' in the {Contract} to '{implementationKey}' in the {Implementation}.");

                    changed = true;
                    break;
                }

                case DifferenceType.Removed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                                          $"Attribute '{type.FullName()}' exists on {errString} in the {Contract} but not the {Implementation}.");


                    // removals of an attribute are considered a "change" of the type
                    changed = true;
                    break;
                }
                }
            }
            return(changed);
        }
Пример #9
0
        //private bool AnySecurityAttributeAdded(IDifferences differences, IReference target, IEnumerable<ISecurityAttribute> attribues1, IEnumerable<ISecurityAttribute> attributes2)
        //{
        //    return AnyAttributeAdded(differences, target, attribues1.SelectMany(a => a.Attributes), attributes2.SelectMany(a => a.Attributes));
        //}

        private void CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable<ICustomAttribute> implAttributes, IEnumerable<ICustomAttribute> contractAttributes)
        {
            AttributesMapping<IEnumerable<ICustomAttribute>> attributeMapping = new AttributesMapping<IEnumerable<ICustomAttribute>>(_settings);
            attributeMapping.AddMapping(0, contractAttributes);
            attributeMapping.AddMapping(1, implAttributes);

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                    case DifferenceType.Added:
                        {
                            ITypeReference type = group.Representative.Attributes.First().Type;
                            string attribName = type.FullName();

                            if (FilterAttribute(attribName))
                                break;

                            AttributeAdded(target, attribName);

                            differences.AddIncompatibleDifference("CannotAddAttribute",
                                "Attribute '{0}' exists on '{1}' in the implementation but not the contract.",
                                attribName, target.FullName());

                            break;
                        }
                    case DifferenceType.Changed:

                        //TODO: Add some more logic to check the two lists of attributes which have the same type.
                        break;

                    case DifferenceType.Removed:
                        {
                            ITypeReference type = group.Representative.Attributes.First().Type;
                            string attribName = type.FullName();

                            if (s_IgnorableAttributes.Contains(attribName))
                                break;

                            differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                "Attribute '{0}' exists on '{1}' in the contract but not the implementation.",
                                attribName, target.FullName());

                            break;
                        }
                }
            }
        }
Пример #10
0
        private bool AnyAttributeAdded(IDifferences differences, IReference target, IEnumerable<ICustomAttribute> implAttributes, IEnumerable<ICustomAttribute> contractAttributes)
        {
            bool added = false;

            AttributesMapping<IEnumerable<ICustomAttribute>> attributeMapping = new AttributesMapping<IEnumerable<ICustomAttribute>>(_settings);
            attributeMapping.AddMapping(0, implAttributes);
            attributeMapping.AddMapping(1, contractAttributes);

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                    case DifferenceType.Added:
                        ITypeReference type = group.Representative.Attributes.First().Type;
                        string attribName = type.FullName();

                        if (s_IgnorableAttributes.Contains(attribName))
                            break;

                        differences.AddIncompatibleDifference(this,
                            "Attribute '{0}' exists in the contract but not the implementation.",
                            attribName, target.FullName());

                        added = true;

                        break;
                    case DifferenceType.Changed:

                        //TODO: Add some more logic to check the two lists of attributes which have the same type.
                        break;

                    case DifferenceType.Removed:
                        // Removing attributes is OK
                        break;
                }
            }

            return added;
        }