260 likes | 480 Views
Università di Pisa. Aspect Oriented Programming in .NET with CodeBricks. Antonio Cisternino Academic Days Milan, 2004. Supported by Microsoft Research grant. Introduction. Aspect Oriented Programming is a way of modularize crosscutting concerns in computer programs
E N D
Università di Pisa Aspect Oriented Programming in .NET with CodeBricks Antonio Cisternino Academic Days Milan, 2004 Supported by Microsoft Research grant
Introduction • Aspect Oriented Programming is a way of modularize crosscutting concerns in computer programs • AOP 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?
Agenda • AOP and Code Generation • CodeBricks: quick intro • [a]C#: annotated join points • ARE: a generic program rewriting system • Conclusions
Aspect Oriented Programming • 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. • 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 program • AOP is a form of generative programming: programs are synthesized from aspects and code
AspectJ: a shallow approach to AOP • AspectJ 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 them • Pointcuts are defined by means of pattern matching • It 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)
AspectJ: a shallow approach to AOP public aspect AutoLog{ pointcut publicMethods() : execution(public * org.apache.cactus..*(..)); pointcut logObjectCalls() : execution(* Logger.*(..)); pointcut loggableCalls() : publicMethods() && ! logObjectCalls(); before() : loggableCalls(){ Logger.entry(thisJoinPoint.getSignature().toString()); } after() : loggableCalls(){ Logger.exit(thisJoinPoint.getSignature().toString()); } }
Aspectwerkz: runtime bytecode generation • AspectWerkz 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 classes • Aspects and join points are connected through XML files • The library supports matching on code attributes, the new feature of Java 1.5 (custom attributes)
Agenda • AOP and Code Generation • CodeBricks: quick intro • [a]C#: annotated join points • ARE: a generic program rewriting system • Conclusions
Code Manipulation based on methods Perceived transformation Programming language Type T Real transformation Intermediate (or machine) language Type T Source program Target program
Partial application + inlining • CodeBricks 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 strategy • Code bricks are built around methods and represent high order values
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);
High order splice delegate void Cmd(); void Log(string info, Cmd c) { Console.WriteLine(“Enter: {0}”, info); c(); 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));
AOP with CodeBricks • Previous 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 method • The compiler can generate patterns based on CodeBricks for aspect waving (this allows for dynamic weaving of aspects)
Agenda • AOP and Code Generation • CodeBricks: quick intro • [a]C#: annotated join points • ARE: a generic program rewriting system • Conclusions
Annotations for better join points? • AspectWerkz shows that code with annotations may help to define better join points • The annotation model of CLR and JVM is limited to methods • Join points are inside methods • We have extended C# to allow annotations inside method bodies
[a]C# public void m() { Console.WriteLine("Parallelizable code sample"); [Parallel("Begin of a parallelizable block")] { Console.WriteLine("Code exec by the main thread"); [Process("First process")] { // Computation here } [Process] { // Computation here } } // Join of processes here Console.WriteLine("Here is sequential"); }
[a]C# runtime operations • Operations available at runtime are: • Annotation tree inspection • Extrusion • Injection • Replacement
[a]C# and AOP • Annotations may help to define pointcuts • 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?
Agenda • AOP and Code Generation • CodeBricks: quick intro • [a]C#: annotated join points • ARE: a generic program rewriting system • Conclusions
Assembly Rewriting Engine • It 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 STEEs • Because 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 code
Schema AR AR AR Stage name Stage name Stage name
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 do • 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 code foreach (Method m in Assembly) if (annotated(m, stagename)) replaceCalls(m, a); else if (annotatedbody(m, stagename)) replace(m,a);
The logger example revised class LogAspect : Transformation { [MethodCall(SomeWork)] void Log(Context c) { Console.WriteLine(“Entering SomeWork”); c.MethodCall(); Console.WriteLine(“Exiting SomeWork”); } } //... void SomeWork() { ... } //... SomeWork();
And pattern matching? • Transformations are expressed on the methods using custom attributes • The transformation should be type-safe • 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.
Agenda • AOP and Code Generation • CodeBricks: quick intro • [a]C#: annotated join points • ARE: a generic program rewriting system • Conclusions
Conclusions • CodeBricks 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 CodeBricks • Annotations can help AOP systems to define better join points • A multi-stage code transformation system is required to have “before runtime stages”