Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Static Code Analysis

, 15 Mar 2010
A static code analyzer building method call networks + sample applications.
CodeAnalyzerAndToDoSample.zip
Generator
bin
.noclean
Arebis.CodeAnalysis.Static.dll
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
Arebis.Common.dll
CGen.exe
Content.cst
Content.cst.gen
COperation.cst
COperation.cst.gen
files
GeneratorMain.cst
GeneratorMain.cst.gen
Index.cst
Index.cst.gen
IndexTop.cst
IndexTop.cst.gen
SOperation.cst
SOperation.cst.gen
UIOperation.cst
UIOperation.cst.gen
Output
SampleApp
LocalTestRun.testrunconfig
ToDoApplication
Infrastructure
Properties
Service References
ToDoContract
Properties
ToDoSample.vsmdi
ToDoService
Entities.edmx
Infrastructure
Properties
ToDoSample.Service.csproj.user
ToDoSampleDb.mdf
ToDoSampleDb_log.ldf
ToDoUnitTests
Properties
Testing
ToDoSample.UnitTests.csproj.user
UnitTests
StaticCodeAnalyzer
Arebis.CodeAnalysis.Static
Arebis.CodeAnalysis.Static.csproj.user
CodeModel.cd
Processors
Rules
Properties
Arebis.Common
Arebis
Collections
Generic
Reflection
Properties
System
Collections
Generic
Net2
Threading
Net2
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Xml;
using Arebis.CodeAnalysis.Static.Processors.Rules;

namespace Arebis.CodeAnalysis.Static.Processors
{
    public class RulesProcessor : IProcessor
    {
        private Dictionary<string, Type> matchingRuleTypes = new Dictionary<string, Type>();
        private XmlNode processorInstance;

        public void Initialize(XmlNode processorInstance)
        {
            this.processorInstance = processorInstance;

            // Load ruledefinition types:
            foreach (XmlNode path in processorInstance.SelectNodes(@"definitions/assembly"))
                this.RegisterMatchingRuleTypes(Assembly.LoadFrom(path.Attributes["path"].Value));
        }

        public void Process(CodeModel codeModel)
        {
            // Build RuleRunContext:
            // (The RuleRunContext allows several rule instances of the same type
            // to share a cache on a per session base.)
            RuleRunContext context = new RuleRunContext();

            // Build RuleSets:
            List<RuleSet> ruleSets = new List<RuleSet>();
            foreach (XmlNode rulesetNode in this.processorInstance.SelectNodes(@"rulesets/ruleset"))
            {
                RuleSet ruleSet = new RuleSet(rulesetNode.Attributes["name"].Value);
                ruleSets.Add(ruleSet);
                foreach (XmlNode ruleNode in rulesetNode.SelectNodes("*"))
                {
                    Type ruleType;
                    if (this.matchingRuleTypes.TryGetValue(ruleNode.Name, out ruleType))
                    {
                        BaseMatchingRule rule = (BaseMatchingRule)Activator.CreateInstance(ruleType);
                        rule.Context = context;
                        rule.Initialize(ruleNode);
                        ruleSet.Rules.Add(rule);
                    }
                }
            }

            // Apply rulesets:
            // (The next line can safely be replaced by a regular foreach as this:)
            //foreach (ModelMethod method in codeModel.Methods) {
            Parallel.ForEach(codeModel.Methods, delegate(ModelMethod method)
            {
                foreach (RuleSet ruleSet in ruleSets)
                {
                    // If tag value already set, no need to evaluate this ruleset anymore...
                    if (method.Tags.Contains(ruleSet.Name))
                        continue;

                    // Test & apply ruleset:
                    if (ruleSet.Matches(method))
                    {
                        // If rule name is "_skip", move to next method:
                        if (ruleSet.Name == "_skip")
                            break;

                        // Apply ruleset:
                        method.Tags.Add(ruleSet.Name);
                    }
                }
            }); // If using regular foreach, remove ");" here
        }

        public void RegisterMatchingRuleTypes(Assembly assembly)
        {
            foreach (Type type in assembly.GetExportedTypes())
                if (typeof(BaseMatchingRule).IsAssignableFrom(type))
                    this.RegisterMatchingRuleType(type);
        }

        public void RegisterMatchingRuleType(Type type)
        {
            if (!typeof(BaseMatchingRule).IsAssignableFrom(type))
                throw new ArgumentException("Cannot register a maching rule type: invalid type.", "type");
            foreach (CodeModelMatchingRuleAttribute attr in type.GetCustomAttributes(typeof(CodeModelMatchingRuleAttribute), false))
                this.matchingRuleTypes[attr.RuleElementName] = type;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 15 Mar 2010
Article Copyright 2010 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid