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

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.Text;
using System.Xml;
using System.Reflection;

namespace Arebis.CodeAnalysis.Static.Processors.Rules
{
	[CodeModelMatchingRule(
        "modifierrule", 
        "Matches methods by their modifiers or by their types modifiers.",
        "target=Target of the modifier.|modifiers=Comma-separated list of modifiers.")]
	public class ModifierMatchingRule : BaseMatchingRule
	{
		private RuleTarget target;
		private MethodAttributes methodAttributes;
		private TypeAttributes typeAttributes;
        private bool reverse;

		public override void Initialize(XmlNode ruleInstance)
		{
			this.target = (RuleTarget)Enum.Parse(typeof(RuleTarget), ruleInstance.Attributes["target"].Value, true);
			switch(this.target)
			{
				case RuleTarget.Method:
					InitializeMethod(ruleInstance);
					break;
				case RuleTarget.Type:
					InitializeType(ruleInstance);
					break;
				default:
					throw new InvalidOperationException(String.Format("Invalid target '{0}' for {1}.", this.target, this.GetType().Name));
			}

            if (ruleInstance.Attributes["reverse"] != null)
                this.reverse = Convert.ToBoolean(ruleInstance.Attributes["reverse"].Value);
        }

		private void InitializeMethod(XmlNode ruleInstance)
		{
			string modifierstring = ruleInstance.Attributes["modifiers"].Value;
			foreach (string s in modifierstring.Split(','))
				this.methodAttributes =
					this.methodAttributes
					| (MethodAttributes)Enum.Parse(typeof(MethodAttributes), s.Trim(), true);
		}

		private void InitializeType(XmlNode ruleInstance)
		{
			string modifierstring = ruleInstance.Attributes["modifiers"].Value;
			foreach (string s in modifierstring.Split(','))
				this.typeAttributes = 
					this.typeAttributes 
					| (TypeAttributes)Enum.Parse(typeof(TypeAttributes), s.Trim(), true);
		}

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

		private bool MatchesOnMethod(ModelMethod method)
		{
			return this.reverse ^ ((method.MethodBase.Attributes & this.methodAttributes) == this.methodAttributes);
		}

		private bool MatchesOnType(Type type)
		{
            return this.reverse ^ ((type.Attributes & this.typeAttributes) == this.typeAttributes);
		}
	}
}

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.

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