2I1AC3 : Génie logiciel et Patrons de conception Chapter 5 Aspect Oriented Programming J'ai toujours rêvé d'un ordinateur qui soit aussi facile à utiliser qu'un téléphone. Mon rêve s'est réalisé. Je ne sais plus comment utiliser mon téléphone Bjarne Stroustrup Régis Clouard regis.clouard @ ensicaen.fr ENSICAEN 14050 Caen
Introduction 2 OOP limitations Code redundancy Code tangling mix-up of multiple concerns at one place Code scattering a concern is scattered over many places Cross-cutting a concern than spans multiple units of OO modularity cannot be implemented without code-tangling and codescattering
Cross-cutting 3 Class A Class B Class C Action1 Action1 Action 2 Action 1 Action2 Action1 Example : Functional Integrity Constraint
Code-scattering 4 Class A Class B Class C call method 1 definition method 1 call method 2 call mthod 2 definition method 2 call method 1 call method 2 Example: Data Persistence
Solutions 5 Respect design principles of Chapter 1! Aspect oriented Programming Gregor Kiczales en 1996 à Xerox PARC Inspiration: Artificial Intelligence Object language Meta-programming Reflection New programming paradigm Software programming history Procedural paradigm OOP paradigm OAP paradigm Separates business concerns from crosscutting concerns
Aspect Oriented Programming 6 OOP without aspect OOP with aspect Class A Class B Class C Class A Class B Class C Aspect
Definitions 7 Aspect Unit of modularity. Can contain fields and methods like a regular Java class. Joint Point Places where crosscutting concerns can be woven in. Ex. method call, field access, object creation. Pointcut Declaration that selects join points and collects contexts at that point (regular expressions) Code Advice Code to be executed at a join point.
Illustration 8 Object program Aspect program Class A Class B Class C Joint Point A1 Joint Point B1 Joint Point C1 Aspect Pointcut1(A1,A2,B1,C2) Pointcut2(A3,B2,C1,C2) Joint Point A2 Joint Point A3 Joint Point B2 Joint Point C2 Pointcut1 Code advice 1 Pointcut1 & Pointcut2 Code advice 2
Joint point models 9 public class HelloWorld { Method call private int max=10; public static void main(string args[]) { new HelloWorld().affichage(); public void affichage() { for (int i=0;i<max;i++) { try { System.out.println("HelloWorld!"); catch (Exception e) { max--; System.err.println("Problème d'affichage");
Joint point models 10 public class HelloWorld { Exception private int max=10; public static void main(string args[]) { new HelloWorld().affichage(); public void affichage() { for (int i=0;i<max;i++) { try { System.out.println("HelloWorld!"); catch (Exception e) { max--; System.err.println("Problème d'affichage");
Joint point models 11 public class HelloWorld { Constructor call private int max=10; public static void main(string args[]) { new HelloWorld().affichage(); public void affichage() { for (int i=0;i<max;i++) { try { System.out.println("HelloWorld!"); catch (Exception e) { max--; System.err.println("Problème d'affichage");
Joint point models 12 public class HelloWorld { Field access private int max=10; public static void main(string args[]) { new HelloWorld().affichage(); public void affichage() { for (int i=0;i<max;i++) { try { System.out.println("HelloWorld!"); catch (Exception e) { max--; System.err.println("Problème d'affichage");
Code Advice 13 Code advice is a method like construct that expresses the action to be taken at the join points that are captured by a pointcut. Before advice executes prior to the join point After advice executes following the join point Around advice In such code advice, the keyword 'proceed' is used to specify where to insert the join point s execution can continue original execution, bypass execution or cause execution with an altered context can cause execution of the join point multiple times
Weaving 14 An OAP application Set of classes Set of aspects Weaving Weaving rules specify how to integrate the final system
Types of weaver 15 1/ Static weaver Source to source translation. Aspect code Class code Weaver Intermediate source code Compiler bytecode
Types of weaver 16 2/ Dynamic weaver Bytecode enhancement, first compile source with original compiler, then weave aspects into class files. Weaver Determine if a code advice must be executed Class code Pre-Weaver modifies the class code Modifier class code Virtual Machine Bytecode
AOP paradigm 17 Separation functional / non functional parts Functional part: business concerns eg, Add or delete an employee Non functional part: crosscutting concerns eg, Security and access control
Examples 18 AspectJ (Open Source) Static weaver URL AspectJ http://www.eclipse.org/aspectj Plug-in Eclipse http://eclipse.org/ajdt
Example 1: HelloWorld (before 19 weaving) Hello.java World.aj public class Hello { public static void main(string a[]){ new Hello().sayHello(); public aspect World { pointcut salutation() : execution (*Hello.sayHello(..)); public void sayhello() { System.out.println("Hello!"); after() :salutation() { System.out.println("Salut à toi aussi");
Example 1: HelloWorld (after 20 weaving) Hello.java World.aj public class Hello { public aspect World { public static void main(string a[]) { new Hello().sayHello(); System.out.println("Salut à toi aussi"); public void sayhello() { System.out.println("Hello!"); pointcut salutation() :execution (*Hello.sayHello(..)); after() :salutation() { System.out.println("Salut à toi aussi");
Example 2: Debug (before 21 weaving) Example2.java Debug.aj public class Exemple2 { private int _x=0; public static void main(string args){ public aspect Debug { pointcut methodex2() : execution(*exemple2.*(..)); for (int i=0;i<10;i++) { Exemple2.increment(); pointcut CallIncr() : execution(*increment(..)); pointcut Ensemble() : public void increment() { methodex2() & CallIncr(); _x++; around() :Ensemble() { System.out.println(x); proceed System.out.println(x);
Example 2: Debug (after 22 weaving) Example2.java After weaving public class Exemple2 { private int _x=0; public static void main(string a[]){ Debug.aj Before weaving public aspect Debug { pointcut methodex2() : execution(*exemple2.*(..)); for (int i=0;i<10;i++){ System.out.println(_x); Exemple2.increment(); System.out.println(_x); pointcut CallIncr() : execution(*increment(..)); pointcut Ensemble() : methodex2() & CallIncr(); public void increment() { _x++; around() :Ensemble() { System.out.println(_x); proceed System.out.println(_x);
Example 3: Design Pattern 23 revisited Singleton OOP : Drawbacks Use of non classical constructor Restrict inheritance Singleton AOP Add a joint point cut on the constructor An attribute stores the instance reference The code advice returns a new instance (1 st call) or the attribute reference
Conclusion 24 OOP + AOP = OOP AOP is available as extensions of many programming languages Java C++ Php C# D Smalltalk The future of programming?