aspect.net — aspect-oriented toolkit for microsoft.net based on phoenix and whidbey dmitry...

22
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

Upload: hope-johnston

Post on 06-Jan-2018

228 views

Category:

Documents


0 download

DESCRIPTION

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?

TRANSCRIPT

Page 1: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey

Dmitry Grigoriev, PhD studentVladimir O. Safonov, Senior Member, IEEE, professorMikhail Gratchev, PhD studentAlexander Maslennikov, PhD student

Saint-Petersburg State University

Page 2: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 2

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.

Page 3: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 3

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?

Page 4: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 4

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

Page 5: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 5

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.

Page 6: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 6

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.

Page 7: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 7

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.

Page 8: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 8

Aspect Library(DLL)

%aspect Test //ML languagepublic class Test {%modules private static void TestRun() { WriteLine(”test”); }%rules

Aspect.MLConverter

C# Compiler

public class Test: Aspect//Attribute annotation { [AspectAction(“%before %call Write*")] public static void TestRunAction() { Test.TestRun(); } }}

Page 9: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 9

Aspect.NET ML Example%aspect Politenesspublic 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

Page 10: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 10

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

Page 11: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 11

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…

Page 12: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 12

Current Aspect.NET ML expressive power (2/3)

• Weaving rules are specified by a mask and a regular expression.%before %call Namespace.Class.MethodNameor%before %call *MethodName

• Signature filtering.%instead %call static public void *Method(float, string, ..)

Page 13: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 13

Current Aspect.NET ML expressive power (3/3)

• Arguments capturingAspectAction(“%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)

Page 14: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 14

Introducing Aspect.NET Framework Design

CompilerApplicationSource Code

AspectLibrary

AspectSource Code

Aspect.NET.ML

Converter

Application

Weaver

User

TargetApplicatio

n

Aspect.NET Framework

Page 15: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 15

Examples of Weaving

• Target application – BankManagement systemstatic 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.

Page 16: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 16

Page 17: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 17

Class Diagram

Page 18: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 18

Filtering discovered joinpoints

Page 19: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 19

Decompiled ResultsBankAccount 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);

Page 20: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 20

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).

Page 21: Aspect.NET — aspect-oriented toolkit for Microsoft.NET based on Phoenix and Whidbey Dmitry Grigoriev, PhD student Vladimir O. Safonov, Senior Member, IEEE,

Aspect.NET, Pilsen, May 2006 21

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)