public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            var type     = (NamedTypeBase)elementToAnalyze;
            var siblings = GetSiblings(type, context.Types);

            return(type.Methods.Count(m => m.IsCtor && m.Parameters.Any(p => p.Type != null && siblings.Any(s => s == (NamedTypeBase)p.Type))) > 0);
        }
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            var type = (NamedTypeBase)elementToAnalyze;

            return
                (type.Methods.Where(m => m.IsCtor).All(m => (VisibilityModifierBase)m.Visibility == VisibilityModifierBase.Private)
                 &&
                 type.Methods.Any(m => m.IsCtor));
        }
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            MethodBase m = elementToAnalyze as MethodBase;

            if (m == null)
            {
                throw new ArgumentException();
            }
            return(m.IsOverride);
        }
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            MethodBase m = elementToAnalyze as MethodBase;

            if (m == null)
            {
                throw new ArgumentException();
            }
            if (m.ActualReturnTypes == null || m.DeclaredReturnType == null || !m.ActualReturnTypes.Any())
            {
                return(false);
            }
            return(m.ActualReturnTypes.All(at =>
                                           at.Bases.Any(atb => (NamedTypeBase)atb == (NamedTypeBase)m.DeclaredReturnType)));
        }
示例#5
0
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            var candidates = new List <NamedTypeBase>();
            var type       = (NamedTypeBase)elementToAnalyze;

            {
                if (context.Types.Count(t => t.Bases != null && t.Bases.Any(b => (NamedTypeBase)b == type) && t.Methods != null && t.Methods.Count > 0) > 1)
                {
                    if ((type.Fields != null && type.Fields.Any(f => (NamedTypeBase)f.Type == type)))
                    {
                        candidates.Add(type);
                    }
                }
            }

            foreach (var candidate in candidates)
            {
                var list = context.Types.Where(t => t.Bases != null && t.Bases.Any(b => (NamedTypeBase)b == candidate) && t.Methods != null && t.Methods.Count > 0).ToList();
                list.Add(candidate);
                var allmethods = list.SelectMany(l => l.Methods);
                if (allmethods.Count() < 1)
                {
                    return(false);
                }
                var candidatemethods = allmethods.Where(m => !m.IsStatic && (VisibilityModifierBase)m.Visibility == VisibilityModifierBase.Public && list.All(l => l.Methods.Any(m2 => m2.Name == m.Name && m2.IsGeneric == m.IsGeneric))).Where(m => (NamedTypeBase)m.Parent == candidate).ToList();
                if (candidatemethods.Count() < 1)
                {
                    return(false);
                }

                foreach (var candidateMethod in candidatemethods.Cast <MethodBase>())
                {
                    var candidateNodes  = context.CallGraph.Nodes.Where(n => n.Caller != null && n.Callee != null && n.Callee.Name == candidateMethod.Name && n.Caller.Name == candidateMethod.Name && n.Caller.Parent != n.Callee.Parent && (NamedTypeBase)n.Callee.Parent == candidate);
                    var sameSourceCount = candidateNodes.GroupBy(n => n.Caller.Parent.Name).Count();

                    if (sameSourceCount != candidateNodes.Count())
                    {
                        continue;
                    }
                    if (candidateNodes.Count() == list.Count - 2)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            var type     = (NamedTypeBase)elementToAnalyze;
            var siblings = GetSiblings(type, context.Types);

            if (siblings.Count < 1)
            {
                return(false);
            }


            var candidates = siblings.Where(s => type.Fields.Any(f => (NamedTypeBase)f.Type == s)).ToList();

            if (context.CallGraph.Nodes.Any(n => n.Callee != null && n.Caller != null && n.Callee.IsOverride && n.Caller.IsOverride && BaseExtensions.CheckForOverrideCompatiblity(n.Callee, n.Caller)))
            {
                return(true);
            }
            return(false);
        }
示例#7
0
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            var type            = (NamedTypeBase)elementToAnalyze;
            var candidateFields = type.Fields.Where(f => f.IsStatic && (NamedTypeBase)f.Type == type && (f.IsInitializedOnce || f.IsInitializedOnCreation)).ToList();

            if (candidateFields.Count != 1)
            {
                return(false);
            }
            var candidateField = candidateFields.Single();

            if ((VisibilityModifierBase)candidateField.Visibility == VisibilityModifierBase.Public)
            {
                return(true);
            }
            if (candidateField.DirectlyReturningMethods.Count == 1 && (VisibilityModifierBase)candidateField.DirectlyReturningMethods[0].Visibility == VisibilityModifierBase.Public)
            {
                return(true);
            }
            return(false);
        }
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            var type        = (NamedTypeBase)elementToAnalyze;
            var descendants = context.Types.Where(t => t.Bases.Any(b => (NamedTypeBase)b == type)).ToList();

            foreach (var d in descendants)
            {
                var fieldTypes = d.Fields.Select(f => f.Type).Cast <NamedTypeBase>().Distinct(new TypeComparer()).ToList();
                for (int i = 0; i < fieldTypes.Count; i++)
                {
                    if (fieldTypes[i] == null)
                    {
                        continue;
                    }
                    for (int j = i + 1; j < fieldTypes.Count; j++)
                    {
                        if (fieldTypes[j] == null)
                        {
                            continue;
                        }
                        if (fieldTypes[i].IsArray || fieldTypes[j].IsArray)
                        {
                            continue;
                        }
                        var ancestor = AreSiblings(fieldTypes[i], fieldTypes[j]);
                        if (ancestor != null)
                        {
                            if (ancestor.Fields.Any(f => (NamedTypeBase)f.Type == type))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            var type        = (NamedTypeBase)elementToAnalyze;
            var descendants = context.Types.Where(t => t.Bases.Any(b => (NamedTypeBase)b == type)).ToList();

            if (descendants.Count < 2)
            {
                return(false);
            }

            var compositedescendants1 = descendants.Where(
                //Bármely field típusa a nem generikus IEnumerable vagy a generikus IEnumerable és a típusparaméter az ős
                //megengedjük-e a nem generikus IEnumerable-t is? (arraylist)
                t => t.Fields.Any(f => (f.Type.Name.Contains("IEnumarable") && (!f.Type.IsGeneric || (f.Type.IsGeneric && f.Type.GenericParameters.Any(gp => gp.IsSubstituted && (NamedTypeBase)gp.Type == type)))
                                        ||
                                        //vagy bármely ősre igaz ugyanez
                                        f.Type.Bases.Any(fb => fb.Name.Contains("IEnumerable") && (!fb.IsGeneric || (fb.IsGeneric && fb.GenericParameters.Any(gp => gp.IsSubstituted && (NamedTypeBase)gp.Type == type)))
                                                         )))).ToList();
            var compositedescendants2 = descendants.Where(t => t.Fields.Any(f => f.Type.IsArray && BaseExtensions.CompareForArrayTypeCompatibility((NamedTypeBase)f.Type, type))).ToList();
            var compositedescendants  = compositedescendants1.Union(compositedescendants2);
            var leafdescendants       = descendants.Except(compositedescendants);

            if (compositedescendants.Count() < 1 || leafdescendants.Count() < 1)
            {
                return(false);
            }



            if (!context.CallGraph.Nodes.Any(n => n.Callee != null && n.Caller != null && (NamedTypeBase)n.Callee.Parent == type && compositedescendants.Any(t => t == (NamedTypeBase)n.Caller.Parent) && n.Caller.IsOverride && BaseExtensions.CheckForOverrideCompatiblity(n.Caller, n.Callee)))
            {
                return(false);
            }

            return(true);
        }
        public bool Check(ICodeElement elementToAnalyze, RecognitionContextBase context)
        {
            var type = (NamedTypeBase)elementToAnalyze;

            return(!DerivesFrom(type, "Exception"));
        }