Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aspect Oriented Programming in.NET with CodeBricks Antonio Cisternino Academic Days Milan, 2004 Università di Pisa Supported by Microsoft Research grant.

Similar presentations


Presentation on theme: "Aspect Oriented Programming in.NET with CodeBricks Antonio Cisternino Academic Days Milan, 2004 Università di Pisa Supported by Microsoft Research grant."— Presentation transcript:

1 Aspect Oriented Programming in.NET with CodeBricks Antonio Cisternino Academic Days Milan, 2004 Università di Pisa Supported by Microsoft Research grant

2 Introduction Aspect Oriented Programming is a way of modularize crosscutting concerns in computer programsAspect Oriented Programming is a way of modularize crosscutting concerns in computer programs AOP tools are responsible for synthesizing the program by combining different aspectsAOP tools are responsible for synthesizing the program by combining different aspects CodeBricks provides support for meta-programming within CLR: how can we use the code generation facilities to support AOP?CodeBricks provides support for meta-programming within CLR: how can we use the code generation facilities to support AOP?

3 Agenda AOP and Code Generation AOP and Code Generation CodeBricks: quick introCodeBricks: quick intro [a]C#: annotated join points[a]C#: annotated join points ARE: a generic program rewriting systemARE: a generic program rewriting system ConclusionsConclusions

4 Aspect Oriented Programming Traditional code organization privileges functional aspect of code: other aspects, like logging, security, concurrency, tend to crosscut the program.Traditional code organization privileges functional aspect of code: other aspects, like logging, security, concurrency, tend to crosscut the program. Aspect Oriented Programming aims to develop tools modularizing crosscutting concerns.Aspect Oriented Programming aims to develop tools modularizing crosscutting concerns. AOP languages (like AspectJ) are based on the single notion of join points: positions in the source code where the code of a crosscutting aspect should join the programAOP languages (like AspectJ) are based on the single notion of join points: positions in the source code where the code of a crosscutting aspect should join the program AOP is a form of generative programming: programs are synthesized from aspects and codeAOP is a form of generative programming: programs are synthesized from aspects and code

5 AspectJ: a shallow approach to AOP AspectJ is one of the most popular systems for AOPAspectJ is one of the most popular systems for AOP It extends the Java language with constructs to specify join points and how aspects are weaved into themIt extends the Java language with constructs to specify join points and how aspects are weaved into them Pointcuts are defined by means of pattern matchingPointcuts are defined by means of pattern matching It is possible to insert code before and after a given pointcutIt is possible to insert code before and after a given pointcut Inter-type declarations allow to interact with the static structure of a program (i.e. types)Inter-type declarations allow to interact with the static structure of a program (i.e. types)

6 AspectJ: a shallow approach to AOP public aspect AutoLog{ pointcut publicMethods() : pointcut publicMethods() : execution(public * org.apache.cactus..*(..)); execution(public * org.apache.cactus..*(..)); pointcut logObjectCalls() : pointcut logObjectCalls() : execution(* Logger.*(..)); execution(* Logger.*(..)); pointcut loggableCalls() : pointcut loggableCalls() : publicMethods() && ! logObjectCalls(); publicMethods() && ! logObjectCalls(); before() : loggableCalls(){ before() : loggableCalls(){ Logger.entry(thisJoinPoint.getSignature().toString()); Logger.entry(thisJoinPoint.getSignature().toString()); } after() : loggableCalls(){ after() : loggableCalls(){ Logger.exit(thisJoinPoint.getSignature().toString()); Logger.exit(thisJoinPoint.getSignature().toString()); }}

7 Aspectwerkz: runtime bytecode generation AspectWerkz provides AOP facilities in the form of Java library rather than languageAspectWerkz provides AOP facilities in the form of Java library rather than language The library is capable of manipulating bytecode inside the class loader for injecting aspect code into classesThe library is capable of manipulating bytecode inside the class loader for injecting aspect code into classes Aspects and join points are connected through XML filesAspects and join points are connected through XML files The library supports matching on code attributes, the new feature of Java 1.5 (custom attributes)The library supports matching on code attributes, the new feature of Java 1.5 (custom attributes)

8 Agenda AOP and Code GenerationAOP and Code Generation CodeBricks: quick intro CodeBricks: quick intro [a]C#: annotated join points[a]C#: annotated join points ARE: a generic program rewriting systemARE: a generic program rewriting system ConclusionsConclusions

9 Code Manipulation based on methods Programming language Intermediate (or machine) language Real transformation Perceived transformation Type T Source programTarget program

10 Partial application + inlining CodeBricks library is built around the notion of partial applicationCodeBricks library is built around the notion of partial application Code generation is expressed as partial application assuming that code is generated relying on an inlining strategyCode generation is expressed as partial application assuming that code is generated relying on an inlining strategy Code bricks are built around methods and represent high order valuesCode bricks are built around methods and represent high order values

11 Examples delegate int F(int v); delegate int G(int i, int j, int k); public int add(int x, int y) { return x+y; } //… Code c = new Code(typeof(Add).GetMethod(“add”)); Code inc = c.Bind(1, new Free()); // add(1, _) Free x = new Free(); Code twice = c.Bind(x, x); // add(_0, _0) Code threeAdd = c.Bind(c.Bind(new Free(), new Free()), new Free()); // add(add(_, _), _) F f = (F)inc.MakeDelegate(typeof(F)); f(2); G g = (G)threeAdd.MakeDelegate(typeof(G)); g(1, 2, 3);

12 High order splice delegate void Cmd(); void Log(string info, Cmd c) { Console.WriteLine(“Enter: {0}”, info); Console.WriteLine(“Enter: {0}”, info); c(); c(); Console.WriteLine(“Exit: {0}”, info); Console.WriteLine(“Exit: {0}”, info);} void SomeWork() {... } //... Code log = new Code(typeof(Exmp).GetMethod(“Log”)); Code tgt = new Code(typeof(Exmp).GetMethod(“SomeWork”)); Cmd norm = new Cmd(SomeWork); // Log(“info”, SomeWork) Cmd wlog = (Cmd)log.Bind(“SomeWork”, tgt).MakeDelegate(typeof(Cmd));

13 AOP with CodeBricks Previous example shows how CodeBricks can provide before and after code injectionsPrevious example shows how CodeBricks can provide before and after code injections But you always need to abstract the code you want to surround into a methodBut you always need to abstract the code you want to surround into a method The compiler can generate patterns based on CodeBricks for aspect waving (this allows for dynamic weaving of aspects)The compiler can generate patterns based on CodeBricks for aspect waving (this allows for dynamic weaving of aspects)

14 Agenda AOP and Code GenerationAOP and Code Generation CodeBricks: quick introCodeBricks: quick intro [a]C#: annotated join points [a]C#: annotated join points ARE: a generic program rewriting systemARE: a generic program rewriting system ConclusionsConclusions

15 Annotations for better join points? AspectWerkz shows that code with annotations may help to define better join pointsAspectWerkz shows that code with annotations may help to define better join points The annotation model of CLR and JVM is limited to methodsThe annotation model of CLR and JVM is limited to methods Join points are inside methodsJoin points are inside methods We have extended C# to allow annotations inside method bodiesWe have extended C# to allow annotations inside method bodies

16 [a]C# public void m() { Console.WriteLine("Parallelizable code sample"); Console.WriteLine("Parallelizable code sample"); [Parallel("Begin of a parallelizable block")] { [Parallel("Begin of a parallelizable block")] { Console.WriteLine("Code exec by the main thread"); Console.WriteLine("Code exec by the main thread"); [Process("First process")] { [Process("First process")] { // Computation here // Computation here } [Process] { [Process] { // Computation here // Computation here } } // Join of processes here } // Join of processes here Console.WriteLine("Here is sequential"); Console.WriteLine("Here is sequential");}

17 [a]C# runtime operations Operations available at runtime are:Operations available at runtime are: –Annotation tree inspection –Extrusion –Injection –Replacement

18 [a]C# and AOP Annotations may help to define pointcutsAnnotations may help to define pointcuts With code-level annotations programmers may declare join points:With code-level annotations programmers may declare join points: –AOP systems may get benefit from annotations –The main source code becomes a little aware of the subsequent code transformation [a]C# transformations are performed at runtime: but what does it means exactly runtime?[a]C# transformations are performed at runtime: but what does it means exactly runtime?

19 Agenda AOP and Code GenerationAOP and Code Generation CodeBricks: quick introCodeBricks: quick intro [a]C#: annotated join points[a]C#: annotated join points ARE: a generic program rewriting system ARE: a generic program rewriting system ConclusionsConclusions

20 Assembly Rewriting Engine It is possible to encode information into binaries so that the programmer perceives that the information is provided into its own languageIt is possible to encode information into binaries so that the programmer perceives that the information is provided into its own language A general rewriting system would support several applications in a standard way complementing the reflection support already available in STEEsA general rewriting system would support several applications in a standard way complementing the reflection support already available in STEEs Because the target program and the transformation system are expressed in the same language the program can be specialized by executing its own codeBecause the target program and the transformation system are expressed in the same language the program can be specialized by executing its own code AOP systems can rely on this general infrastructure for generating their own codeAOP systems can rely on this general infrastructure for generating their own code

21 Schema AR Stage name AR Stage name

22 The Algorithm We assume that a program that wants to perform a computation at stage n provide some metadata to indicate where and what to doWe assume that a program that wants to perform a computation at stage n provide some metadata to indicate where and what to do Methods handled by the rewriting engine should have a known signature: Code m(Context c);Methods handled by the rewriting engine should have a known signature: Code m(Context c); The basic operation is type safe code replacement of explicitly (annotations) or implicitly (method calls) annotated codeThe basic operation is type safe code replacement of explicitly (annotations) or implicitly (method calls) annotated code foreach (Method m in Assembly) if (annotated(m, stagename)) replaceCalls(m, a); if (annotated(m, stagename)) replaceCalls(m, a); else if (annotatedbody(m, stagename)) replace(m,a); else if (annotatedbody(m, stagename)) replace(m,a);

23 The logger example revised class LogAspect : Transformation { [MethodCall(SomeWork)] [MethodCall(SomeWork)] void Log(Context c) { void Log(Context c) { Console.WriteLine(“Entering SomeWork”); Console.WriteLine(“Entering SomeWork”); c.MethodCall(); c.MethodCall(); Console.WriteLine(“Exiting SomeWork”); Console.WriteLine(“Exiting SomeWork”); }}//... void SomeWork() {... } //...SomeWork();

24 And pattern matching? Transformations are expressed on the methods using custom attributesTransformations are expressed on the methods using custom attributes The transformation should be type-safeThe transformation should be type-safe ARE will not provide general support for pattern matching (maybe some restricted form)ARE will not provide general support for pattern matching (maybe some restricted form) An AOP compiler can anticipate pattern matching and output an appropriate transformation class.An AOP compiler can anticipate pattern matching and output an appropriate transformation class.

25 Agenda AOP and Code GenerationAOP and Code Generation CodeBricks: quick introCodeBricks: quick intro [a]C#: annotated join points[a]C#: annotated join points ARE: a generic program rewriting systemARE: a generic program rewriting system Conclusions Conclusions

26 Conclusions CodeBricks aims to become a general CLR support for meta-programming at runtimeCodeBricks aims to become a general CLR support for meta-programming at runtime We have discussed how the typical transformations of AOP could be mapped on CodeBricksWe have discussed how the typical transformations of AOP could be mapped on CodeBricks Annotations can help AOP systems to define better join pointsAnnotations can help AOP systems to define better join points A multi-stage code transformation system is required to have “before runtime stages”A multi-stage code transformation system is required to have “before runtime stages”


Download ppt "Aspect Oriented Programming in.NET with CodeBricks Antonio Cisternino Academic Days Milan, 2004 Università di Pisa Supported by Microsoft Research grant."

Similar presentations


Ads by Google