Aspect NET aspectoriented toolkit for Microsoft NET based
Aspect. NET — aspect-oriented toolkit for Microsoft. NET based on Phoenix and Whidbey Dmitry Grigoriev, Ph. D student Vladimir O. Safonov, Senior Member, IEEE, professor Mikhail Gratchev, Ph. D student Alexander Maslennikov, Ph. D student Saint-Petersburg State University Aspect. NET, Pilsen, May 2006
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 2006 2
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 2006 3
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 (Aspect. J, 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 2006 4
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 2006 5
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 2006 6
Aspect. NET ML • Aspect definition is specified in a simple metalanguage – 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 2006 7
%aspect Test //ML language public class Test { %modules Aspect. ML private static void Test. Run() { Converter Write. Line(”test”); } %rules public class Test: Aspect//Attribute annotation { [Aspect. Action(“%before %call Write*")] public static void Test. Run. Action() { Test. Run(); } } } Aspect Library (DLL) C# Compiler Aspect. NET, Pilsen, May 2006 8
Aspect. NET ML Example %aspect Politeness public class Politeness { %modules private static void Say. Scanning. Hello() { Console. Write. Line("Welcome to Aspect. NET scanning system!"); } %rules %before %call *Some. Method %action public static void Say. Scanning. Hello. Action() { Politeness. Say. Scanning. Hello(); } }// Politeness Aspect. NET, Pilsen, May 2006 9
Custom attributes (example) public class Politeness: Aspect { private static void Say. Scanning. Hello() { Console. Write. Line("Welcome to Aspect. NET scanning system!"); } [Aspect. Action(“%before %call *Some. Method”)] public static void Say. Scanning. Hello. Action() { Politeness. Say. Scanning. Hello(); } }// Politeness Aspect. NET, Pilsen, May 2006 10
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; Object Target. Object; Member. Info Target. Member. Info; Type Within. Type; Method. Base Within. Method; string Source. File. Path; string Source. File. Line. \this keyword \p. Target. Method(. . ); \Target. Method as Method. Info \this. Get. Type(); \this. Current. Method(); \*. cs filepath \source line number • And more… Aspect. NET, Pilsen, May 2006 11
Current Aspect. NET ML expressive power (2/3) • Weaving rules are specified by a mask and a regular expression. %before %call Namespace. Class. Method. Name or %before %call *Method. Name • Signature filtering. %instead %call static public void *Method(float, string, . . ) Aspect. NET, Pilsen, May 2006 12
Current Aspect. NET ML expressive power (3/3) • Arguments capturing Aspect. Action(“%after %call *Method(int) && args(arg[1])”) static public void Method. Action(int i) { Console. Write. Line({0}, i); } Additional restrictions on join points placement %within(*Some. Type) %withincode(*Some. Method) %instead %call *Method && %within(*My. Type) && %!withincode(*. ctor) Aspect. NET, Pilsen, May 2006 13
Introducing Aspect. NET Framework Design User Aspect. NET Framework Aspect Source Code Aspect. NET. ML Converter Aspect Library Weaver Application Source Code Compiler Target Application Aspect. NET, Pilsen, May 2006 14
Examples of Weaving • Target application – Bank. Management system static void Main(string[] args) { Bank. Account acc 1 = new Bank. Account(); acc 1. deposit(20); //apply aspects here acc 1. withdraw(20); //apply aspects here } • Bank. Account. Contract. Aspect – Design by Contract: Invariant, Pre & Post checks of Bank. Management. Deposit() and Bank. Management. withdraw(); • Usage. Licensing. Aspect – checks permissions of current machine to perform these operations. Aspect. NET, Pilsen, May 2006 15
Aspect. NET, Pilsen, May 2006 16
Class Diagram Aspect. NET, Pilsen, May 2006 17
Filtering discovered joinpoints Aspect. NET, Pilsen, May 2006 18
Decompiled Results Bank. Account account 1 = new Bank. Account(); Aspect. Internal. Set. Args. Types("float"); Aspect. Internal. Set. Member. Name("Bank. Management. Bank. Account. deposit"); Aspect. Internal. Set. Target. Object(account 1); Usage. Licensing. Aspect. Deposit. Wrapper(20 f); \instead of \account 1. deposit(20) Aspect. Internal. Set. Args. Types("float"); Aspect. Internal. Set. Member. Name("Bank. Management. Bank. Account. withdraw“) Aspect. Internal. Set. Target. Object(account 1); Usage. Licensing. Aspect. Withdraw. Wrapper(20 f); \instead of \account 1. withdraw(20) Console. Write. Line("Final balance is {0}", account 1. Balance); Aspect. NET, Pilsen, May 2006 19
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 2006 20
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 metalanguage) • 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 2006 21
Q&A • Where to find? http: //www. msdnaa. net/curriculum/? id=6334 Aspect. NET, Pilsen, May 2006 22
- Slides: 22