Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE, professor Mikhail Gratchev, PhD student Alexander Maslennikov, PhD student Saint-Petersburg State University
Aspect.NET, Pilsen, May Introduction to AOP (part 1) Aspect-oriented programming (AOP) is an attempt to solve the problems of development complex software. Each of software modules (classes, methods, procedures, etc.) solves some definite logically independent task (event logging, authentification, security, assertions,..). To use new modules in our product, we need to inject their calls into the sources.
Aspect.NET, Pilsen, May Introduction to AOP (part 2) As a result we have a lot of tangled code in our sources. What shall we do when we decide to remove or modify such a cross-cutting concern or add another one?
Aspect.NET, Pilsen, May Introduction to AOP (part 3) AOP suggests: “Let’s remove them from sources and weave directly to binaries instead, according to our rules described in special language” Approaches: Extend an existing language (AspectJ, Aspect#) Need special compiler for every language version. Dynamic execution (hooks at definite points of execution) (Loom.NET, RAIL) Don’t know what kind of code is currently executing. Low performance -Static weaving into assemblies (Aspect.NET) High performance Resulting code could be examined explicitly (Reflector) A lot of existing.NET tools can work with resulting assemblies
Aspect.NET, Pilsen, May Aspect.NET goals Aspect.NET is an aspect-oriented tool that allows you to define some functionality by specific aspect units and weave them into your assemblies. So, the cross-cutting concerns appear to be structured into aspects which makes the code clearer. Aspect.NET is implemented as Visual Studio.NET 2005 (Whidbey) add-in, to use AOP technology in a comfortable manner, alongside with using ubiquitous VS.NET software development features.
Aspect.NET, Pilsen, May Aspect.NET Overview Let’s turn cross-cutting concerns into separate standalone units (classes), referred to as aspects. Aspect contain: Data (fields) Modules (aspect’s methods ) Actions (public methods to be called at specially defined join points of target code) Weaving rules (Determine the set of join points) Generally, aspect is a class whose methods are annotated by specific AOP weaving attributes.
Aspect.NET, Pilsen, May Aspect.NET ML Aspect definition is specified in a simple meta- language – Aspect.NET.ML. Meta-language allows you to describe a set of desired join-points and reuse such declarations for other aspects (ruleset). Then special converter turns it into.NET class source code, with meta-language annotations represented as custom attributes. This aspect class is compiled into an assembly by a common use.NET Framework compiler.
Aspect.NET, Pilsen, May Aspect Library (DLL) %aspect Test//ML language public class Test { %modules private static void TestRun() { WriteLine(”test”); } %rules Aspect.ML Converter C# Compiler public class Test: Aspect//Attribute annotation { [AspectAction(“%before %call Write*")] public static void TestRunAction() { Test.TestRun(); }
Aspect.NET, Pilsen, May Aspect.NET ML Example %aspect Politeness public class Politeness { %modules private static void SayScanningHello() { Console.WriteLine("Welcome to Aspect.NET scanning system!"); } %rules %before %call *SomeMethod %action public static void SayScanningHelloAction() { Politeness.SayScanningHello(); } }// Politeness
Aspect.NET, Pilsen, May Custom attributes (example) public class Politeness: Aspect { private static void SayScanningHello() { Console.WriteLine("Welcome to Aspect.NET scanning system!"); } [AspectAction(“%before %call *SomeMethod”)] public static void SayScanningHelloAction() { Politeness.SayScanningHello(); } }// Politeness
Aspect.NET, Pilsen, May Current Aspect.NET ML expressive power (1/3) Can inject actions before, after or instead call instructions Actions have full access to the woven context through the properties of base Aspect class: Object This;\\this keyword Object TargetObject;\\p.TargetMethod(..); MemberInfo TargetMemberInfo; \\TargetMethod as MethodInfo Type WithinType;\\this.GetType(); MethodBase WithinMethod;\\this.CurrentMethod(); string SourceFilePath;\\*.cs filepath string SourceFileLine.\\source line number And more…
Aspect.NET, Pilsen, May Current Aspect.NET ML expressive power (2/3) Weaving rules are specified by a mask and a regular expression. %before %call Namespace.Class.MethodName or %before %call *MethodName Signature filtering. %instead %call static public void *Method(float, string,..)
Aspect.NET, Pilsen, May Current Aspect.NET ML expressive power (3/3) Arguments capturing AspectAction(“%after %call *Method(int) && args(arg[1])”) static public void MethodAction(int i) { Console.WriteLine({0}, i); } Additional restrictions on join points placement %within(*SomeType) %withincode(*SomeMethod) %instead %call *Method && %within(*MyType) && %!withincode(*.ctor)
Aspect.NET, Pilsen, May Introducing Aspect.NET Framework Design Compiler Application Source Code Aspect Library Aspect Source Code Aspect.NET.ML Converter Application Weaver User Target Application Aspect.NET Framework
Aspect.NET, Pilsen, May Examples of Weaving Target application – BankManagement system static void Main(string[] args) { BankAccount acc1 = new BankAccount(); acc1.deposit(20);//apply aspects here acc1.withdraw(20);//apply aspects here } BankAccountContractAspect – Design by Contract: Invariant, Pre & Post checks of BankManagement.Deposit() and BankManagement.withdraw(); UsageLicensingAspect – checks permissions of current machine to perform these operations.
Aspect.NET, Pilsen, May
Aspect.NET, Pilsen, May Class Diagram
Aspect.NET, Pilsen, May Filtering discovered joinpoints
Aspect.NET, Pilsen, May Decompiled Results BankAccount account1 = new BankAccount(); Aspect.InternalSetArgsTypes("float"); Aspect.InternalSetMemberName("BankManagement.BankAccount.deposit"); Aspect.InternalSetTargetObject(account1); UsageLicensingAspect.DepositWrapper(20f);\\instead of \\account1.deposit(20) Aspect.InternalSetArgsTypes("float"); Aspect.InternalSetMemberName("BankManagement.BankAccount.withdraw“) Aspect.InternalSetTargetObject(account1); UsageLicensingAspect.WithdrawWrapper(20f); \\instead of \\account1. withdraw(20) Console.WriteLine("Final balance is {0}", account1.Balance);
Aspect.NET, Pilsen, May Microsoft Phoenix Reflection is very poor for instrumenting MSIL assemblies (even in.NET 2.0). Microsoft Phoenix is a framework for building compilers and a wide range of tools for program analysis, optimization, and testing. Phoenix provides high level instructions for MSIL code. Phoenix tools can be implemented as compiler phases. Phoenix lets work with debug information instead of addressing unmanaged COM interfaces DIA. So we use it. We are collaborating with Phoenix developers at Microsoft Research (the project supported by MSR).
Aspect.NET, Pilsen, May AOP problems and our approach Comprehension Must be able to predict behavior (Aspect.NET: View join points in source) Must be easy to understand (Aspect.NET: Easy meta- language) Integration into existing tools and software processes (Aspect.NET: Nice add-in to Visual Studio) Debuggability Source-level debugging is critical (Aspect.NET: we are planning this feature) Testing AOP introduces new fault models (Aspect.NET: Unit testing within VS) Evolution Performance (Aspect.NET: Static weaving gives minimal overhead)
Aspect.NET, Pilsen, May Q&A Where to find?