Click here to Skip to main content
12,348,536 members (68,419 online)
Click here to Skip to main content

Stats

46.6K views
842 downloads
60 bookmarked
Posted

Static Code Analysis

, 15 Mar 2010 CPOL
A static code analyzer building method call networks + sample applications.
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.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Reflection;

namespace Arebis.CodeAnalysis.Static.Processors.Rules
{
	[CodeModelMatchingRule(
        "namerule", 
        "Matches methods by their name, their types name or their assemblies name.",
        "target=Target of the name.|like=Wildcard expression the name should match.|match=Regular expression the name should match.")]
	public class NameMatchingRule : BaseMatchingRule
	{
		private RuleTarget target;
		private Regex match;

		public override void Initialize(XmlNode ruleInstance)
		{
			this.target = (RuleTarget)Enum.Parse(typeof(RuleTarget), ruleInstance.Attributes["target"].Value, true);
            if (ruleInstance.Attributes["match"] != null)
                this.match = new Regex(ruleInstance.Attributes["match"].Value, RegexOptions.Compiled);
            else if (ruleInstance.Attributes["like"] != null)
                this.match = GetLikeRegex(ruleInstance.Attributes["like"].Value, RegexOptions.Compiled);
            else
                throw new InvalidOperationException(String.Format("Name matching rule must have either match or like attribute in \"{0}\".", ruleInstance.OuterXml));
		}

        /// <summary>
        /// Returns a RegEx to match the given pattern with wildcards * and ?.
        /// </summary>
        public static Regex GetLikeRegex(string pattern, RegexOptions options)
        {
            pattern = Regex.Escape(pattern);
            pattern = "^" + pattern.Replace("\\*", ".*").Replace("\\?", ".") + "$";
            return new Regex(pattern, options);
        }

		public override bool Matches(ModelMethod method)
		{
			switch (this.target)
			{
				case RuleTarget.Method:
					return this.MatchesOnMethod(method);
				case RuleTarget.Type:
					return this.MatchesOnType(method.DeclaringType);
				case RuleTarget.Assembly:
					return this.MatchesOnAssembly(method.DeclaringType.Assembly);
				default:
					throw new InvalidOperationException(String.Format("Invalid target '{0}' for {1}.", this.target, this.GetType().Name));
			}
		}

		public bool MatchesOnMethod(ModelMethod method)
		{
			return (this.match.IsMatch(method.Name));
		}

		public bool MatchesOnType(Type type)
		{
			return ((type.FullName != null) && (this.match.IsMatch(type.FullName)));
		}

		public bool MatchesOnAssembly(Assembly assembly)
		{
			return (this.match.IsMatch(assembly.GetName().Name));
		}
	}
}

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)

Share

About the Author

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

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 15 Mar 2010
Article Copyright 2010 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid