public RuleResult CheckMethod(MethodDefinition method)
            {
                var a = OpCodeEngine.GetBitmask(SimpleMethods.EmptyMethod);
                var b = NamespaceEngine.Exists("TestNamespace");

                return(RuleResult.Success);
            }
Esempio n. 2
0
        public RuleResult CheckAssembly(AssemblyDefinition assembly)
        {
            // check every namespaces inside the assembly using the NamespaceEngine
            foreach (string ns in NamespaceEngine.NamespacesInside(assembly))
            {
                // rule only apply to "internal" namespaces
                if (!ns.EndsWith(".Internal", StringComparison.Ordinal) &&
                    !ns.EndsWith(".Impl", StringComparison.Ordinal))
                {
                    continue;
                }

                foreach (TypeDefinition type in NamespaceEngine.TypesInside(ns))
                {
                    // only report for this assembly
                    if (type.Module.Assembly != assembly)
                    {
                        continue;
                    }

                    // report all types inside that namespace
                    if (type.IsVisible())
                    {
                        Runner.Report(type, Severity.Medium, Confidence.Total);
                    }
                }
            }
            return(Runner.CurrentRuleResult);
        }
 public RuleResult CheckAssembly(AssemblyDefinition assembly)
 {
     foreach (string ns in NamespaceEngine.NamespacesInside(assembly))
     {
         CheckNamespace(ns);
     }
     return(Runner.CurrentRuleResult);
 }
        public RuleResult CheckAssembly(AssemblyDefinition assembly)
        {
            MethodDefinition entry_point = assembly.EntryPoint;

            if (entry_point != null)
            {
                // for EXE assemblies ignore the namespace of the entry point
                ignore.AddIfNew(entry_point.DeclaringType.Namespace);
            }

            // ignore assemblies with a single namespace
            int    count  = 0;
            string single = null;

            foreach (string ns in NamespaceEngine.NamespacesInside(assembly))
            {
                // no type inside the assembly
                if (ns == null)
                {
                    break;
                }

                // only count if there are some visible types inside the namespace
                bool visible = false;
                foreach (TypeDefinition type in NamespaceEngine.TypesInside(ns))
                {
                    if (type.IsVisible())
                    {
                        visible = true;
                        break;
                    }
                }

                if (visible)
                {
                    single = ns;
                    count++;
                }

                // no need to go more than 2
                if (count > 1)
                {
                    break;
                }
            }
            if (count == 1)
            {
                ignore.AddIfNew(single);
            }

            return(RuleResult.Success);
        }
Esempio n. 5
0
        public RuleResult CheckAssembly(AssemblyDefinition assembly)
        {
            // assembly names are very visible, severity == high
            CheckIdentifier(assembly, assembly.Name.Name, Severity.High);

            // check every namespaces inside the assembly using the NamespaceEngine
            // note: we don't reuse CheckIdentifier because we want to avoid
            // creating Namespace instance unless necessary
            foreach (string ns in NamespaceEngine.NamespacesInside(assembly))
            {
                CheckIdentifier(NamespaceDefinition.GetDefinition(ns), ns, Severity.Medium);
            }

            return(Runner.CurrentRuleResult);
        }
Esempio n. 6
0
        public RuleResult CheckAssembly(AssemblyDefinition assembly)
        {
            if (!CheckName(assembly.Name.Name, false))
            {
                Runner.Report(assembly, Severity.Medium, Confidence.High);
            }

            // check every namespaces inside the assembly using the NamespaceEngine
            foreach (string ns in NamespaceEngine.NamespacesInside(assembly))
            {
                if (!CheckName(ns, false))
                {
                    Runner.Report(NamespaceDefinition.GetDefinition(ns), Severity.Medium, Confidence.High);
                }
            }
            return(Runner.CurrentRuleResult);
        }
        public override void TearDown()
        {
            // check every namespaces inside the assembly set being analyzed
            foreach (string ns in NamespaceEngine.AllNamespaces())
            {
                // skip specialized namespaces
                if (NamespaceDefinition.IsSpecialized(ns))
                {
                    continue;
                }

                if (ignore.Contains(ns))
                {
                    continue;
                }

                int count = 0;
                foreach (TypeDefinition type in NamespaceEngine.TypesInside(ns))
                {
                    if (type.IsVisible())
                    {
                        count++;
                    }
                    // no early termination here so we can report the correct count
                }

                // don't report namespaces that are not visible outside the assembly
                // e.g. VS.NET adds a .Properties namespace to SWF apps
                if ((count > 0) && (count < Minimum))
                {
                    NamespaceDefinition n = NamespaceDefinition.GetDefinition(ns);
                    string msg            = String.Format(CultureInfo.CurrentCulture,
                                                          "Only {0} visible types are defined inside this namespace.", count);
                    // overloads of Report cannot be used here since the 'target' has been lost in the runner
                    Runner.Report(new Defect(this, n, n, Severity.Low, Confidence.Total, msg));
                }
            }
            ignore.Clear();
            base.TearDown();
        }
        public RuleResult CheckAssembly(AssemblyDefinition assembly)
        {
            // check every namespaces inside the assembly using the NamespaceEngine
            foreach (string ns in NamespaceEngine.NamespacesInside(assembly))
            {
                // shortest invalid namespace would be "a.b.c.d.e"
                // so we can skip anything less than 2 * MaxDepth
                // note: overflow does not matter
                if (ns.Length < unchecked (MaxDepth + MaxDepth))
                {
                    continue;
                }

                // count the levels (i.e. number of dots + 1)
                int levels = CountLevels(ns);
                if (levels <= MaxDepth)
                {
                    continue;
                }

                // we have some exceptions for namespace specialization
                // i.e. stuff we often prefer to have in a sub-namespace
                // and for internal (non-visible) namespaces
                if (levels == MaxDepth + 1)
                {
                    if (NamespaceDefinition.IsSpecialized(ns))
                    {
                        continue;
                    }
                    else if (ns.EndsWith(".Internal", StringComparison.Ordinal) || ns.EndsWith(".Impl", StringComparison.Ordinal))
                    {
                        continue;
                    }
                }

                Severity severity = (levels < MaxDepth * 2) ? Severity.Medium : Severity.High;
                Runner.Report(NamespaceDefinition.GetDefinition(ns), severity, Confidence.High);
            }
            return(Runner.CurrentRuleResult);
        }
 public void DoSomethingNew()
 {
     var a = OpCodeEngine.GetBitmask(SimpleMethods.EmptyMethod);
     var b = NamespaceEngine.Exists("TestNamespace");
 }
        public RuleResult CheckType(TypeDefinition type)
        {
            string ns = type.Namespace;

            if (type.IsGeneratedCode() || string.IsNullOrEmpty(ns))
            {
                return(RuleResult.DoesNotApply);
            }

            int    ifaceOffset = type.IsInterface ? 1 : 0;
            int    lastDot     = ns.LastIndexOf('.');
            string name        = type.Name;

            //type name is smaller or equal to namespace it cannot be a defect
            if (name.Length - ifaceOffset <= (ns.Length - lastDot))
            {
                return(RuleResult.Success);
            }

            string lastComponent = ns.Substring(lastDot + 1);

            if (type.IsInterface)
            {
                name = name.Substring(1);
            }
            if (!name.StartsWith(lastComponent, StringComparison.Ordinal))
            {
                return(RuleResult.Success);
            }

            //if first char of suggestion does not start with a uppercase, can ignore it
            //ie. Foo.Bar.Barometer
            if (!char.IsUpper(name [lastComponent.Length]))
            {
                return(RuleResult.Success);
            }

            string suggestion = name.Substring(lastComponent.Length);

            //if base type name is or ends with suggestion, likely not clearer if we rename it.
            //would bring ambiguity or make suggestion looks like base of basetype
            if (null != type.BaseType)
            {
                string base_name = type.BaseType.Name;
                if (base_name.EndsWith(suggestion, StringComparison.Ordinal))
                {
                    return(RuleResult.Success);
                }

                //equally, if base type starts with the prefix, it is likely a wanted pattern
                if (DoesNameStartWithPascalWord(base_name, lastComponent))
                {
                    return(RuleResult.Success);
                }
            }

            if (type.IsInterface)
            {
                suggestion = string.Concat("I", suggestion);
            }

            //we have a really interesting candidate now, let's check that removing prefix
            //would not cause undesirable ambiguity with a type from a parent namespace
            while (0 != ns.Length)
            {
                foreach (TypeDefinition typ in NamespaceEngine.TypesInside(ns))
                {
                    if (null == typ)
                    {
                        break;
                    }
                    if (suggestion == typ.Name)                     //ambiguity
                    {
                        return(RuleResult.Success);
                    }
                }
                ns = ns.Substring(0, Math.Max(0, ns.LastIndexOf('.')));
            }

            //main goal is to keep the API as simple as possible so this is more severe for visible types
            Severity severity = type.IsVisible() ? Severity.Medium : Severity.Low;

            string msg = String.Format(CultureInfo.InvariantCulture, "Consider renaming type to '{0}'.", suggestion);

            Runner.Report(type, severity, Confidence.Normal, msg);
            return(RuleResult.Failure);
        }
Esempio n. 11
0
 static private bool IsTypeFromAlienNamespace(TypeReference type)
 {
     return(!NamespaceEngine.Exists(type.Namespace));
 }