Aspect-Oriented Software Development (AOSD) Tutorial #3 AspectJ - continued.

Slides:



Advertisements
Similar presentations
AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems.
Advertisements

Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
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 An Aspect-Aware Outline Viewer Michihiro Horie and Shigeru Chiba Tokyo Institute of Technology, Japan.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
Overview of AspectJ Aspect Oriented Software Development Seminar Technion presented by Oren Mishali.
Aspect-Oriented Software Development (AOSD) Tutorial #2 AspectJ Basics.
Aspect-Oriented Software Development (AOSD) Tutorial #5 Categories of Aspects – contd.; LTL properties formalization; Assume – guarantee specifications.
Aspect-Oriented Software Development (AOSD) Tutorial #4 Categories of Aspects.
Aspect-Oriented Software Development (AOSD) Tutorial #7 Assume – guarantee specifications; EAOP.
Aspect-Oriented Software Development (AOSD) Tutorial #4 Categories of Aspects.
Aspect-Oriented Software Development (AOSD) Tutorial #8 Composition Filters.
Aspect-Oriented Software Development (AOSD) Tutorial #2 AspectJ Basics.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Aspect-Oriented Software Development (AOSD) Tutorial #5 Categories of Aspects – contd.; LTL properties formalization.
Aspect-Oriented Programming with AspectJ™ AspectJ.org Xerox PARC Erik Hilsdale Gregor Kiczales with Bill Griswold, Jim Hugunin, Wes Isberg, Mik Kersten.
Aspect-Oriented Software Development (AOSD) Tutorial #7 Assume – guarantee specifications; EAOP.
Aspect-Oriented Software Development (AOSD) Tutorial #3 AspectJ - continued.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
Session 2: AspectJ Mark Stobbe September 13,
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Kiczales and Mezini - FOAL AOP and Modular Reasoning [ICSE05] Start with first-principles definition of modularity and modular reasoning –localization,
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
AOSD1 Aspect-Oriented Software Design Karl Lieberherr Theo Skotiniotis.
Working with arrays (we will use an array of double as example)
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Aspect-Oriented Programming and Modular Reasoning G. KiczalesM. Mezini Presented by Alex Berendeyev.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
IDENTIFYING SEMANTIC DIFFERENCES IN ASPECTJ PROGRAMS Martin Görg and Jianjun Zhao Computer Science Department, Shanghai Jiao Tong University.
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.
1 Contract-based Verification for Aspect-oriented Refactoring Naoyasu Ubayashi(Kyushu Institute of Technology) Jinji Piao(Kyushu Institute of Technology)
Alloy-based Lightweight Verification for Aspect-oriented Architecture Naoyasu Ubayashi(Kyushu Institute of Technology) Yuki Sato(Kyushu Institute of Technology)
AOP and observer pattern. Design pattern General reusable solution to a commonly occurring problem in software design Not a finished design that can be.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
AOSD'04, Lancaster, UK 1 Remote Pointcut - A Language Construct for Distributed AOP Muga Nishizawa (Tokyo Tech) Shigeru Chiba (Tokyo Tech) Michiaki Tatsubori.
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington 1.
A first language for Aspect-Oriented Programming
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Classes and Objects Introduced
An Interface Mechanism for Encapsulating Weaving in Class-based AOP
Aspect-Oriented Programming
CSC 113 Tutorial QUIZ I.
CMSC 202 Interfaces.
Object initialization: constructors
Building Java Programs
CMSC 202 Interfaces.
Building Java Programs
Generics, Lambdas, Reflections
An Extensible Contract Verifier for AspectJ
Building Java Programs
Aspect Oriented Programming
CMSC 202 Interfaces.
Topic 29 classes and objects, part 3
Presentation transcript:

Aspect-Oriented Software Development (AOSD) Tutorial #3 AspectJ - continued

Aspect-Oriented Software Development (236608) 2 Today: AspectJ - Continued Additional pointcut types –cflow –cflowbelow Intertype declarations Compilation errors declarations Examples English – AspectJ phrase book (selected entries)

Aspect-Oriented Software Development (236608) 3 Task 1: Greeting before printing Public class Test { public static void main(String[] args) { f(); } static void f() { g(); } static void g() { System.out.println( “ message from g ” ); } Task: Before each printing operation, print a greeting message

Aspect-Oriented Software Development (236608) 4 English – AspectJ Phrase Book (3) pointcut exceptF(): withincode(void Test.f()); after() throwing : exceptF() { … } “All the exceptions thrown by f()” “All the exceptions thrown while f() is executed:” pointcut exceptF2(): cflow(call(void Test.f())); after() throwing : exceptF2() { … }

Aspect-Oriented Software Development (236608) 5 English – AspectJ Phrase Book (4) pointcut exceptTest(): within (Test); after() throwing : exceptTest() { … } “All the exceptions thrown by functions of class Test” What is the relationship between: (1) pointcut except1(): cflow(call(void Test.g())); (2) pointcut except2(): cflowbelow(call(void Test.f())); Equality!

Aspect-Oriented Software Development (236608) 6 Task 1: Proposed solution-1 public aspect Printing { pointcut fPC(): execution(void Test.f()); pointcut gPC(): execution(void Test.g()); pointcut printPC(): call(void java.io.PrintStream.println(String)); before(): cflow(fPC()) && cflow(gPC()) && printPC() { System.out.println("hello"); } before(): cflow(fPC() && gPC()) && printPC() { System.out.println("shalom"); } matched by printPC pointcut!

Aspect-Oriented Software Development (236608) 7 Task 1: Proposed solution-2 public aspect Printing { pointcut fPC(): execution(void Test.f()); pointcut gPC(): execution(void Test.g()); pointcut printPC(): call(void java.io.PrintStream.println(String)); before(): cflow(fPC()) && cflow(gPC()) && printPC() && !within(Printing) { System.out.println("hello"); } before(): cflow(fPC() && gPC()) && printPC() && !within(Printing) { System.out.println("shalom"); } can not be matched! (f() and g() have no common join-points) Output: hello message from g

Aspect-Oriented Software Development (236608) 8 Cflow Pointcuts Combination P Q cflow(P) cflow(Q)cflow(P) && cflow(Q) P && Q cflow(P && Q)

Aspect-Oriented Software Development (236608) 9 Example Class: Point - reminder class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public void MoveTo(Point p) {setX(p.x); setY(p.y); } public int getX() { return x; } public int getY() { return y; } }

Aspect-Oriented Software Development (236608) 10 Task 2: Named Points Task2: Every point should have a name, and each time the point is moved, the user should get a message with the name of the point and its new coordinates. Pontcut = ? pointcut moved(Point pt): target(pt) && (call(void setX(int)) || call(void setY(int)));

Aspect-Oriented Software Development (236608) 11 Task 2 – contd. Advice = ? Add the “name” field to Point // Inside the aspect body! private String Point.name = ""; public String Point.getName() {return name;} public void Point.setName(String newName) {name = newName;} Who can access the “name” field = ? Only the aspect! (private field of the aspect) Who can access getName(), setName()? Everybody! (public access) inter-type declarations

Aspect-Oriented Software Development (236608) 12 Task 2 – version2. Another way to add the “name” field to Point Assume there is a class NamedObject in a base system: public class NamedObject { private String name; … public NamedObject() {this.name = "";} … public void setName(String newName) {this.name = newName;} public String getName() {return this.name;} } declare parents: Point extends NamedObject; Add to the aspect:

Aspect-Oriented Software Development (236608) 13 Task 2 – contd. Advice = ? – contd. Advice type = ? after returning The advice: after(Point pt) returning: moved (pt) { System.out.println("Point "+pt.getName()+" moved to ("+pt.getX()+","+pt.getY()+")"); }

Aspect-Oriented Software Development (236608) 14 Task 2 – contd. Is that enough to add the name? Theoretically, yes. But we’d better have some initialization For example: When point number “i” is created, give it the name “Pointi” How? – Task 2-A –“around” constructor calls –generate the needed name –set the generated value to the name field

Aspect-Oriented Software Development (236608) 15 Task 2-A pointcut createPoint(int x, int y): args(x,y) && call(Point.new(..)); Pointcut = ? (reminder) Advice = ? Point around(int x, int y): createPoint(x,y){ Point newPoint = proceed(x,y); … //generate name newPoint.setName( … ); //update the “ name ” field return newPoint; }

Aspect-Oriented Software Development (236608) 16 Task 2-A – contd. private int pointsCounter = 0; Name generation: - add points counter to the aspect! Advice as a whole: Point around(int x, int y): createPoint(x,y){ Point newPoint = proceed(x,y); return newPoint; } pointsCounter ++; newPoint.setName("Point"+pointsCounter);

Aspect-Oriented Software Development (236608) 17 Task 3: Contract Enforcement Example aspect RegistrationProtection { pointcut register(): call(void Registry.register(FigureElement)); pointcut canRegister(): withincode(static *FigureElement.make*(..)); before(): register() && !canRegister() { throw new IllegalAccessException("Illegal call " + thisJoinPoint); } } Constraint : only the factory methods can add an element to the registry of figure elements. Meaning: ensures that no figure element is added to the registry more than once. Implementation: Not an elegant solution! Can be solved at compilation time, and not at runtime

Aspect-Oriented Software Development (236608) 18 More to Inter-type Declarations declare error: Pointcut: String; declare warning: Pointcut: String; New powerful types of compilation warnings and errors! Syntax: For the contract enforcement example: For example, to identify method calls that should not exist in a correct program aspect RegistrationProtection { pointcut register(): call(void Registry.register(FigureElement)); pointcut canRegister(): withincode(static *FigureElement.make*(..)); } declare error: register() && !canRegister(): "Illegal call" Attention! Only static information will be used at the join-points!

Aspect-Oriented Software Development (236608) 19 More to AspectJ: Subtypes at pointcuts Example base classes: public class NamedObject { …} public class NamedFigure extends NamedObject { …} Example aspect: public aspect NamedObjectsObserver { …}

Aspect-Oriented Software Development (236608) 20 Subtypes at pointcuts – contd. class public aspect NamedObjectsObserver { //matches calls to functions of NamedObject only pointcut publicCalled1(): call(public * NamedObject.*(..)); //matches calls to functions of NamedObject and of NamedFigure pointcut publicCalled2(): call(public * NamedObject+.*(..)); //matches calls to functions of NamedFigure only pointcut publicCalled3(): call(public * (NamedObject+ && !NamedObject).*(..)); after() : publicCalled1() { System.out.println("Public (1)"+thisJoinPoint); } after() : publicCalled2() { System.out.println("Public (2)"+thisJoinPoint); } after() : publicCalled3() { System.out.println("Public (3)"+thisJoinPoint); } }