26-Jun-15 Aspect-Oriented Programming. 2 Programming paradigms Procedural programming Executing a set of commands in a given sequence Fortran, C, Cobol,

Slides:



Advertisements
Similar presentations
A Brief Introduction to Aspect-Oriented Programming Zhenxiao Yang.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
1 JAC : Aspect Oriented Programming in Java An article review by Yuval Nir and Limor Lahiani.
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 32 Slide 1 Aspect-oriented Software Development.
1 An Aspect-Aware Outline Viewer Michihiro Horie and Shigeru Chiba Tokyo Institute of Technology, Japan.
An Overview of AspectJ Sabine Hauert. Today's Deserts  Whipped introduction served with its three berries syrup  Chocolate history soufflé  Design.
Secure Systems Research Group - FAU Aspect Oriented Programming Carlos Oviedo Secure Systems Research Group.
20-May-15 Aspect-Oriented Programming Dave Matuszek University of Pennsylvania.
Overview of AspectJ Aspect Oriented Software Development Seminar Technion presented by Oren Mishali.
Aspect-Oriented Programming In Eclipse ® Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January.
University of British Columbia Software Practices Lab Fluid AOP Join Point Models Terry Hon Gregor Kiczales.
Aspect-Oriented Software Development (AOSD) Tutorial #2 AspectJ Basics.
University of British Columbia Software Practices Lab CAS Seminar 06 Fluid AJ - A Simple Fluid AOP Tool Terry Hon Gregor Kiczales.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Aspect-Oriented Software Development (AOSD) Tutorial #2 AspectJ Basics.
More on AspectJ. aspect MoveTracking { private static boolean _flag = false; public static boolean testAndClear() { boolean result = _flag; _flag = false;
Aspect-Oriented Programming with AspectJ™ AspectJ.org Xerox PARC Erik Hilsdale Gregor Kiczales with Bill Griswold, Jim Hugunin, Wes Isberg, Mik Kersten.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Introduction to Aspect- Oriented Programming CS 3360 Gregor Kiczales, et. al. Getting started with AspectJ, CACM, 44(10):59-65, October Fall 2012.
Introduction to Object-oriented programming and software development Lecture 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
Aspect Oriented Programming Razieh Asadi University of Science & Technology Mazandran Babol Aspect Component Based Software Engineering (ACBSE)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 32 Slide 1 Aspect-oriented Software Development 1.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
Aspect Oriented Programming Gülşah KARADUMAN.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
AOSD1 Aspect-Oriented Software Design Karl Lieberherr Theo Skotiniotis.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Aspect-Oriented Programming and Modular Reasoning G. KiczalesM. Mezini Presented by Alex Berendeyev.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Inter-Type Declarations in AspectJ Awais Rashid Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
AspectJ – AOP for Java Tom Janofsky. Instructor at Penn State Abington Consultant with Chariot Solutions JUG Member.
Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
Chapter 8: Aspect Oriented Programming Omar Meqdadi SE 3860 Lecture 8 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Aspect Oriented Development Alex Beatty.  Purpose  Cross-cutting Concerns  Join Points, Pointcuts, and Advices  Weaving  Invasive vs. Non-Invasive.
Alloy-based Lightweight Verification for Aspect-oriented Architecture Naoyasu Ubayashi(Kyushu Institute of Technology) Yuki Sato(Kyushu Institute of Technology)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Introduction to Object-Oriented Programming Lesson 2.
Introduction to OOP CPS235: Introduction.
Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd
问题 Code scattering Blocks of duplicated code Blocks of complementary code, and different modules implementing complementary parts of the concern Code.
Comparison of Different AOP Approaches Presented by: Xiaojing Wang.
aspectj tools new and noteworthy Mik Kersten University of British Columbia Adrian Colyer IBM Hursley OOPSLA, October
Classes, Interfaces and Packages
R R R A Brief Introduction to Aspect-Oriented Programming.
Introduction to Aspect- Oriented Programming CS 3360 Gregor Kiczales, et. al. Getting started with AspectJ, CACM, 44(10):59-65, October Spring 2012.
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington 1.
AspectScope: An Outline Viewer for AspectJ Programs Michihiro Horie, Shigeru Chiba Tokyo Institute of Technology, Japan.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Why Should You Care About Aspect-Oriented Programming? Karl Lieberherr CCIS.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
An Interface Mechanism for Encapsulating Weaving in Class-based AOP
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
Java Programming Language
A Brief Introduction to Aspect-Oriented Programming
AspectAda Aspect-Oriented Programming for Ada95
Presentation transcript:

26-Jun-15 Aspect-Oriented Programming

2 Programming paradigms Procedural programming Executing a set of commands in a given sequence Fortran, C, Cobol, Pascal Functional programming Evaluating a function defined in terms of other functions Lisp, ML, OCaml Logic programming Proving a theorem by finding values for the free variables Prolog Object-oriented programming (OOP) Organizing a set of objects, each with its own set of responsibilities Smalltalk, Java, C#, C++ (to some extent), Python, Ruby Aspect-oriented programming (AOP) Executing code whenever a program shows certain behaviors AspectJ (a Java extension) Does not replace O-O programming, but rather complements it

3 The problem Some programming tasks cannot be neatly encapsulated in objects, but must be scattered throughout the code Examples: Logging (tracking program behavior to a file) Profiling (determining where a program spends its time) Tracing (determining what methods are called when) Session tracking, session expiration Special security management The result is crosscutting code--the necessary code “cuts across” many different classes and methods

4 Example class Fraction { int numerator; int denominator;... public Fraction multiply(Fraction that) { traceEnter("multiply", new Object[] {that}); Fraction result = new Fraction( this.numerator * that.numerator, this.denominator * that.denominator); result = result.reduceToLowestTerms(); traceExit("multiply", result); return result; }... } Now imagine similar code in every method you might want to trace

5 Consequences of crosscutting code Redundant code Same fragment of code in many places (violates DRY) Difficult to reason about Non-explicit structure The big picture of the tangling isn’t clear Difficult to change Have to find all the code involved......and be sure to change it consistently...and be sure not to break it by accident Inefficient when crosscutting code is not needed Adapted from: aspectj/downloads/PARC-Workshop-2002.ppt

6 AspectJ TM AspectJ is a small, well-integrated extension to Java Based on the 1997 PhD thesis by Christina Lopes, A Language Framework for Distributed Programming AspectJ modularizes crosscutting concerns That is, code for one aspect of the program (such as tracing) is collected together in one place The AspectJ compiler is free and open source AspectJ works with several IDEs, including Eclipse Best online writeup: aspectj.html Parts of this lecture were taken from the above paper

7 Terminology A join point is a well-defined point in the program flow A pointcut is a group of join points Advice is code that is executed at a pointcut Introduction modifies the members of a class and the relationships between classes An aspect is a module for handling crosscutting concerns Aspects are defined in terms of pointcuts, advice, and introduction Aspects are reusable and inheritable Each of these terms will be discussed in greater detail

8 The Figure Element example

9 Example I A pointcut named move that chooses various method calls: pointcut move(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)); Advice (code) that runs before the move pointcut: before(): move() { System.out.println("About to move"); } Advice that runs after the move pointcut: after(): move() { System.out.println("Just successfully moved"); }

10 Join points A join point is a well-defined point in the program flow We want to execute some code (“advice”) each time a join point is reached We do not want to clutter up the code with explicit indicators saying “This is a join point” AspectJ provides a syntax for indicating these join points “from outside” the actual code A join point is a point in the program flow “where something happens” Examples: When a method is called When an exception is thrown When a variable is accessed

11 Pointcuts Pointcut definitions consist of a left-hand side and a right-hand side, separated by a colon The left-hand side consists of the pointcut name and the pointcut parameters (i.e. the data available when the events happen) The right-hand side consists of the pointcut itself Example pointcut: pointcut setter(): call(void setX(int)); The name of this pointcut is setter The pointcut has no parameters The pointcut itself is call(void setX(int)) The pointcut refers to any time the void setX(int) method is called

12 Example pointcut designators I When a particular method body executes: execution(void Point.setX(int)) When a method is called: call(void Point.setX(int)) When an exception handler executes: handler(ArrayOutOfBoundsException) When the object currently executing (i.e. this ) is of type SomeType: this(SomeType)

13 Example pointcut designators II When the target object is of type SomeType target(SomeType) When the executing code belongs to class MyClass within(MyClass) When the join point is in the control flow of a call to a Test's no-argument main method cflow(call(void Test.main()))

14 Pointcut designator wildcards It is possible to use wildcards to declare pointcuts: execution(* *(..)) Chooses the execution of any method regardless of return or parameter types call(* set(..)) Chooses the call to any method named set regardless of return or parameter type In case of overloading there may be more than one such set method; this pointcut picks out calls to all of them

15 Pointcut designators based on types You can select elements based on types. For example, execution(int *()) Chooses the execution of any method with no parameters that returns an int call(* setY(long)) Chooses the call to any setY method that takes a long as an argument, regardless of return type or declaring type call(* Point.setY(int)) Chooses the call to any of Point ’s setY methods that take an int as an argument, regardless of return type call(*.new(int, int)) Chooses the call to any classes’ constructor, so long as it takes exactly two int s as arguments

16 Pointcut designator composition Pointcuts compose through the operations or (“ || ”), and (“ && ”) and not (“ ! ”) Examples: target(Point) && call(int *()) Chooses any call to an int method with no arguments on an instance of Point, regardless of its name call(* *(..)) && (within(Line) || within(Point)) Chooses any call to any method where the call is made from the code in Point ’s or Line ’s type declaration within(*) && execution(*.new(int)) Chooses the execution of any constructor taking exactly one int argument, regardless of where the call is made from !this(Point) && call(int *(..)) Chooses any method call to an int method when the executing object is any type except Point

17 Pointcut designators based on modifiers call(public * *(..)) Chooses any call to a public method execution(!static * *(..)) Chooses any execution of a non-static method execution(public !static * *(..)) Chooses any execution of a public, non-static method Pointcut designators can be based on interfaces as well as on classes

18 Example I, repeated A pointcut named move that chooses various method calls: pointcut move(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)); Advice (code) that runs before the move pointcut: before(): move() { System.out.println("About to move"); } Advice that runs after the move pointcut: after(): move() { System.out.println("Just successfully moved"); }

19 Kinds of advice AspectJ has several kinds of advice; here are some of them: Before advice runs as a join point is reached, before the program proceeds with the join point After advice on a particular join point runs after the program proceeds with that join point after returning advice is executed after a method returns normally after throwing advice is executed after a method returns by throwing an exception after advice is executed after a method returns, regardless of whether it returns normally or by throwing an exception Around advice on a join point runs as the join point is reached, and has explicit control over whether the program proceeds with the join point

20 Example II, with parameters You can access the context of the join point: pointcut setXY(FigureElement fe, int x, int y): call(void FigureElement.setXY(int, int)) && target(fe) && args(x, y); after(FigureElement fe, int x, int y) returning: setXY(fe, x, y) { System.out.println(fe + " moved to (" + x + ", " + y + ")."); }

21 Introduction An introduction is a member of an aspect, but it defines or modifies a member of another type (class). With introduction we can add methods to an existing class add fields to an existing class extend an existing class with another implement an interface in an existing class convert checked exceptions into unchecked exceptions

22 Example introduction aspect CloneablePoint { declare parents: Point implements Cloneable; declare soft: CloneNotSupportedException: execution(Object clone()); Object Point.clone() { return super.clone(); } }

23 Approximate syntax An aspect is: aspect nameOfAspect { body } An aspect contains introductions, pointcuts, and advice A pointcut designator is: when ( signature ) The signature includes the return type The “ when ” is call, handler, execution, etc. A named pointcut designator is: name ( parameters ): pointcutDesignator Advice is: adviceType ( parameters ): pointcutDesignator { body } Introductions are basically like normal Java code

24 Example aspect I aspect PointWatching { private Vector Point.watchers = new Vector(); public static void addWatcher(Point p, Screen s) { p.Watchers.add(s); } public static void removeWatcher(Point p, Screen s) { p.Watchers.remove(s); } static void updateWatcher(Point p, Screen s) { s.display(p); } // continued on next slide

25 Example aspect II // continued from previous slide pointcut changes(Point p): target(p) && call(void Point.set*(int)); after(Point p): changes(p) { Iterator iter = p.Watchers.iterator(); while ( iter.hasNext() ) { updateWatcher(p, (Screen)iter.next()); } } }

26 Simple tracing aspect SimpleTracing { pointcut tracedCall(): call(void FigureElement.draw(GraphicsContext)); before(): tracedCall() { System.out.println("Entering: " + thisJoinPoint); } }

27 Checking pre- and post-conditions aspect PointBoundsChecking { pointcut setX(int x): (call(void FigureElement.setXY(int, int)) && args(x, *)) || (call(void Point.setX(int)) && args(x)); pointcut setY(int y):| (call(void FigureElement.setXY(int, int)) && args(*, y)) || (call(void Point.setY(int)) && args(y)); before(int x): setX(x) { if ( x MAX_X ) throw new IllegalArgumentException("x is out of bounds."); } before(int y): setY(y) { if ( y MAX_Y ) throw new IllegalArgumentException("y is out of bounds."); } }

28 Concluding remarks Aspect-oriented programming (AOP) is a new paradigm--a new way to think about programming AOP is somewhat similar to event handling, where the “events” are defined outside the code itself AspectJ is not itself a complete programming language, but an adjunct to Java AspectJ does not add new capabilities to what Java can do, but adds new ways of modularizing the code AspectJ is free, open source software Like all new technologies, AOP may--or may not--catch on in a big way

29 Updates The preceding slides, while accurate enough, do not reflect the most recent changes in AspectJ Good reference: The AspectJ TM 5 Development Kit Developer's Notebook

30 The End