October 2013CSC5021: AspectJ (J P Gibson)1 Aspects in AspectJ Motivation Aspect Oriented Programming: a brief introduction to terminology Installation.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

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.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
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.
Overview of AspectJ Aspect Oriented Software Development Seminar Technion presented by Oren Mishali.
University of British Columbia Software Practices Lab CAS Seminar 06 Fluid AJ - A Simple Fluid AOP Tool Terry Hon Gregor Kiczales.
More on AspectJ. aspect MoveTracking { private static boolean _flag = false; public static boolean testAndClear() { boolean result = _flag; _flag = false;
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Session 15 Modeling Traceability of Concerns in Architectural Views Mark Stobbe October 29,
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Introduction to Aspect- Oriented Programming CS 3360 Gregor Kiczales, et. al. Getting started with AspectJ, CACM, 44(10):59-65, October Fall 2012.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Aspect Oriented Programming Razieh Asadi University of Science & Technology Mazandran Babol Aspect Component Based Software Engineering (ACBSE)
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
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.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Design Rules for Increasing Modularity with CaesarJ Carlos Eduardo Pontual Advisor: Paulo Borba 17/06/2010.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
MCS 270 Spring 2014 Object-Oriented Software Development.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
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)
M1G Introduction to Programming 2 5. Completing the program.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Towards an Aspect-oriented Language Module: Aspects for Petri Nets Tim Molderez, Bart Meyers, Dirk Janssens and Hans Vangheluwe Ansymo Antwerp Systems.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
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.
aspectj tools new and noteworthy Mik Kersten University of British Columbia Adrian Colyer IBM Hursley OOPSLA, October
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
Object-Oriented Design Concepts University of Sunderland.
AspectScope: An Outline Viewer for AspectJ Programs Michihiro Horie, Shigeru Chiba Tokyo Institute of Technology, Japan.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Aspect-Oriented Software Development (AOSD)
Introduction to Exceptions in Java CS201, SW Development Methods.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
Aspect-Oriented Programming
JAsCo an Aspect-Oriented approach tailored for
Aspect Oriented Programming
Plug-In Architecture Pattern
Presentation transcript:

October 2013CSC5021: AspectJ (J P Gibson)1 Aspects in AspectJ Motivation Aspect Oriented Programming: a brief introduction to terminology Installation Experimentation AspectJ – some details AspectJ – things you should know about but we dont have time to cover in detail AspectJ – profiling problem

October 2013CSC5021: AspectJ (J P Gibson)2 Motivation “Ability is what you're capable of doing. Motivation determines what you do. Attitude determines how well you do it.” Lou Holtz

October 2013CSC5021: AspectJ (J P Gibson)3 Motivation Even well designed systems have aspects that cut across a large number of the components For example, consider Apache Tomcat, where certain aspects of functionality are: XML parsing URL pattern matching Logging Session Management Question: which of these are well localised?

October 2013CSC5021: AspectJ (J P Gibson)4 Motivation XML parsing URL pattern matching Logging Session Management: different types Application Session Server Session Standard Session Managers So the code for each session stage should be well localised?

October 2013CSC5021: AspectJ (J P Gibson)5 Motivation Session Expiration: code is distributed through lots of different classes

October 2013CSC5021: AspectJ (J P Gibson)6 Motivation Session Expiration: a better design? Unfortunately, if we do this we will have to compromise by redistributing previously localised code.

October 2013CSC5021: AspectJ (J P Gibson)7 Motivation Separation of Concerns is a time-honored principle of Software design D. Parnas. On the Criteria to Be Used in Decomposing Systems into Modules. Comm. ACM 15, 12 (December 1972), Design principles for decomposition: Information-hiding modules Identify design decisions that are likely to change. Isolate these in separate modules (separation of concerns)

October 2013CSC5021: AspectJ (J P Gibson)8 Motivation Cross-Cutting Concerns In the motivation, XML parsing and URL pattern matching the class hierarchy, Logging and Session Mangement do not. A cross-cutting concern is one that needs to be addressed in more than one of the modules in the hierarchical structure of the software. Cross-cutting concerns are also called aspects. What is an aspect depends on the chosen decomposition!

October 2013CSC5021: AspectJ (J P Gibson)9 Motivation Problems with Cross-Cutting Concerns Cross-cutting concerns pose problems for standard, e.g. OO, programming techniques: hard and error-prone to introduce in an existing system hard to change afterwards hard to understand/explain to newcomers Cross-cutting implementation of cross-cutting concerns does not provide separation of concens.

October 2013CSC5021: AspectJ (J P Gibson)10 Motivation Solutions Possible treatment of cross-cutting concerns: Refactor them away. Change the module hierarchy so that the aspect becomes modular, often through application of adequate design patterns. But: often performance penalties through indirection often leaves some cross-cutting boiler-plate can't hope to capture all aspects

October 2013CSC5021: AspectJ (J P Gibson)11 Aspect Oriented Programming: a brief introduction A programming methodology is called Aspect-Oriented if it provides possibilities to cleanly separate concerns that would otherwise be cross-cutting. There are various Aspect-Oriented methods. They differ in the kinds of aspects they can address and in the ways aspects and their relation to the chosen hierarchical decomposition are expressed.

October 2013CSC5021: AspectJ (J P Gibson)12 Aspect Oriented Programming: a brief introduction Don’t Forget That Good Design Helps Avoid Cross Cutting: so only use Aspects if they are really needed, and not just because the design is bad! Example of Good Design – The Law of Demeter: An object should only call methods on this, instance variables, method arguments. no this.getWife().getMother().getMaidenName() chains. Prevents dependency on too many other classes. The "Law of Demeter" provides a classic solution, see: Lieberherr, Karl. J. and Holland, I. Assuring good style for object-oriented programs IEEE Software, September 1989, pp 38-48

October 2013CSC5021: AspectJ (J P Gibson)13 Aspect Oriented Programming: a brief introduction Join Points Analyse commonly occurring aspects. Cross-cutting implementations can often be formulated in terms like: Before... is called, always check for... If any of... throws an exception,... Everytime... gets changed, notify... Implementations of aspects are attached to certain points in the Implementation, eg: method calls constructor calls field access (read/write) Exceptions These correspond to point in the dynamic execution of the program. Such points are called join points

October 2013CSC5021: AspectJ (J P Gibson)14 Aspect Oriented Programming: a brief introduction Code Example: Figure Editor (from

October 2013CSC5021: AspectJ (J P Gibson)15 Aspect Oriented Programming: a brief introduction Code Example: Figure Editor

October 2013CSC5021: AspectJ (J P Gibson)16 Aspect Oriented Programming: a brief introduction Code Example: Figure Editor – some join points

October 2013CSC5021: AspectJ (J P Gibson)17 Aspect Oriented Programming: a brief introduction A pointcut designates a set of join points for any program execution. At execution, any join point may or may not be selected by a pointcut. Examples: all calls to public methods of the Point class every execution of a constructor with one int argument every write access to a public field Membership of a join point can be determined at runtime

October 2013CSC5021: AspectJ (J P Gibson)18 Aspect Oriented Programming: a brief introduction Advice Advice is code that should be inserted before, after or even instead of existing code at some set of join points. `Mainstream' AOP: Designate sets of join points using some specification language for pointcuts. Declare advice to be executed before/after/instead of the calls/method executions/field accesses etc. selected by the pointcut.

October 2013CSC5021: AspectJ (J P Gibson)19 Aspect Oriented Programming: a brief introduction Example Display updating in the Figure Editor: After every change in a FigureElement's state, update the display.. AOP implementation: pointcut to select every state-changing method in FigureElement classes. `after'-advice which calls display update code.

October 2013CSC5021: AspectJ (J P Gibson)20 Aspect Oriented Programming: a brief introduction Weaving: A program called an aspect weaver is used to weave the advice code into the main program code. Often, join point membership in a pointcut can be decided statically. => no need to insert code at every possible join point. Modern systems: some do weaving and compilation in one step some can do weaving at runtime (e.g. on Java byte-code)

October 2013CSC5021: AspectJ (J P Gibson)21 Aspect Oriented Programming: a brief introduction AspectJ Extensions of the Java language for pointcuts attaching advice static cross-cutting Originally developed at Xerox PARC First versions in Spring of 2000 Hosted by eclipse.org since December 2002

October 2013CSC5021: AspectJ (J P Gibson)22 Installation Download the Eclipse AJDT plugin from

October 2013CSC5021: AspectJ (J P Gibson)23 Installation If AspectJ is already there then you should not need to add it (but may need to update it)

October 2013CSC5021: AspectJ (J P Gibson)24 Installation Check that you have the most recent version of AspectJ (for your Eclipse distribution)

October 2013CSC5021: AspectJ (J P Gibson)25 Installation Check you have all the feature plug-ins that are necessary: You do not need the optional plug-ins (but be aware that the AspectJ plug-in can interact with others and may require installation of these « glue » options)

October 2013CSC5021: AspectJ (J P Gibson)26 Installation Try Out The HelloWorld Example Demo:

October 2013CSC5021: AspectJ (J P Gibson)27 Installation Try Out The HelloWorld Example Demo: package p_helloworld; public class Hello { public static void main(String[] args) { sayHello(); } public static void sayHello() { System.out.print("Hello"); }

October 2013CSC5021: AspectJ (J P Gibson)28 Installation Try Out The HelloWorld Example Demo: package p_helloworld; public aspect World { pointcut greeting() : execution(* Hello.sayHello(..)); before() : greeting() { System.out.println(" World!"); }

October 2013CSC5021: AspectJ (J P Gibson)29 Experimentation Experiment with the Demo: what will/should happen if you add another aspect to the project? package p_helloworld; public aspect MethodCounter { int counter =0; pointcut publicMethodCall() : execution(public * *.*(..)); pointcut afterMain(): execution(public * *.main(..)); after() returning() : publicMethodCall() { counter++; } after() returning() : afterMain() { System.out.println("Method call counter = "+counter); }

October 2013CSC5021: AspectJ (J P Gibson)30 Experimentation Experiment with the Demo: can you write an Aspect that will time the execution of each method call? package p_helloworld; public aspect Profiler{ } This should be easy to complete. Is the on-line documentation good enough?

October 2013CSC5021: AspectJ (J P Gibson)31 Experimentation Experiment with the Demo: can you write an Aspect that will time the execution of each method call? World! Hello void p_helloworld.Hello.sayHello() took nanoseconds Method call counter = 2 void p_helloworld.Hello.main(String[]) took nanoseconds When you run the code with all 3 aspects woven together, you should get output of the following form (more or less):

October 2013CSC5021: AspectJ (J P Gibson)32 AspectJ – some details Reconsider the figure editor

October 2013CSC5021: AspectJ (J P Gibson)33 AspectJ – some details Reconsider the figure editor Primitive pointcuts: call(void Point.setX(int)) each join point that is a call to a method that has the signature void Point.setX(int) Also for interface signatures: call(void FigureElement.moveBy(int,int)) Each call to the moveBy(int,int) method in a class that implements FigureElement

October 2013CSC5021: AspectJ (J P Gibson)34 AspectJ – some details Reconsider the figure editor Pointcuts can be joined using boolean operators &&,||,!. call(void Point.setX(int)) || call(void Point.setY(int)) calls to the setX and setY methods of Point. Join points from different types possible: call(void FigureElement.moveBy(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)) Any call to a state-changing method in the given FigureElement classes

October 2013CSC5021: AspectJ (J P Gibson)35 AspectJ – some details Reconsider the figure editor Named Pointcuts Pointcuts can and should be declared to give them a name: pointcut stateChange() : call(void FigureElement.moveBy(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)); Analogous to method declaration or typedef in C. After declaration, stateChange() can be used wherever a pointcut is expected.

October 2013CSC5021: AspectJ (J P Gibson)36 AspectJ – some details Reconsider the figure editor Wildcards Method signatures can contain wildcards: call(void java.io.PrintStream.println(*)) any PrintStream method named println returning void and taking exactly one argument of any type. call(public * Figure.*(..)) any public method in Figure. call(public * Line.set*(..)) any method in Line with a name starting with set.

October 2013CSC5021: AspectJ (J P Gibson)37 AspectJ – some details Reconsider the figure editor Example The pointcut from before, using wildcards: pointcut stateChange() : call(void FigureElement.moveBy(int,int)) || call(* Point.set*(*)) || call(* Line.set*(*));

October 2013CSC5021: AspectJ (J P Gibson)38 AspectJ – some details Reconsider the figure editor Advice in AspectJ Advice can be attached to join points: before(): stateChange() { System.out.println("about to change state"); } after() returning: stateChange() { System.out.println("just successfully changed state"); }

October 2013CSC5021: AspectJ (J P Gibson)39 AspectJ – some details //After every state changing call, update the display. public aspect DisplayUpdating { pointcut stateChange() : call(void FigureElement.moveBy(int,int)) || call(* Point.set*(*)) || call(* Line.set*(*)); after() returning : stateChange() { Display.update(); } QUESTION: Does this look like a good use of Aspects?

October 2013CSC5021: AspectJ (J P Gibson)40 AspectJ – some details

October 2013CSC5021: AspectJ (J P Gibson)41 AspectJ – some details

October 2013CSC5021: AspectJ (J P Gibson)42 AspectJ – some details

October 2013CSC5021: AspectJ (J P Gibson)43 AspectJ – some details

October 2013CSC5021: AspectJ (J P Gibson)44 AspectJ – some details

October 2013CSC5021: AspectJ (J P Gibson)45 AspectJ – some details

October 2013CSC5021: AspectJ (J P Gibson)46 AspectJ – some details

October 2013CSC5021: AspectJ (J P Gibson)47 AspectJ – some details

October 2013CSC5021: AspectJ (J P Gibson)48 AspectJ – some details Example: Diagonal Moves Define a pointcut for moves with equal dx and dy. pointcut diagHelp(int dx,int dy) : call(void FigureElement.moveBy(int,int)) && args(dx,dy) && if(dx==dy); pointcut diagMove(int dxy) : diagHelp(dxy,int);

October 2013CSC5021: AspectJ (J P Gibson)49 AspectJ – some details About Conditionals AspectJ specification: The boolean expression used can only access static members, variables exposed by the enclosing pointcut or advice, (and thisJoinPoint forms). But still... static methods may be called, which may have side effects! Question: what are the consequences of this?

October 2013CSC5021: AspectJ (J P Gibson)50 AspectJ – some details Before and After `Before' advice: before(formal parameters) : Pointcut {... advice body... } The advice body gets executed every time just before the program flow enters a join point matched by the pointcut. The formal parameters receive values from the pointcut `After' advice: after(formal parameters) returning : Pointcut {...} The advice body gets executed every time just after the program flow exits a join point matched by the pointcutby returning normally. Capture return value: after(...) returning (int ret): Pointcut {...}

October 2013CSC5021: AspectJ (J P Gibson)51 AspectJ – some details What about exceptions? Advice after throwing an exception: after(formal parameters) throwing : Pointcut {...} Capture thrown exception: after(...) throwing (Exception e): Pointcut {...} Match normal and abrupt return: after(...) : Pointcut {...}

October 2013CSC5021: AspectJ (J P Gibson)52 AspectJ – some details Around Advice Run advice instead of original code: Type around(... ) :... {... proceed(... );... } run advice body instead of original call, field access, method body, etc. use proceed to use the original join point, if needed. Hint: this may help you to write the timer Aspect asked for earlier

October 2013CSC5021: AspectJ (J P Gibson)53 AspectJ – some details thisJoinPoint For tracing or logging join points, matched methods might have very different signatures.  difficult to pass all useful information through parameters. Special variable thisJoinPoint available in advice. thisJoinPoint.toString() thisJoinPoint.getTarget() thisJoinPoint.getArgs() thisJoinPoint object expensive to construct. thisJoinPointStaticPart is statically constructed.

October 2013CSC5021: AspectJ (J P Gibson)54 AspectJ – some details Aspects and Classes in AspectJ Aspects are similar to class declarations, but Can also contain pointcut and advice declarations, System and not user controls instantiation, (An aspect gets instantiated the first time, some advice in it needs to be executed.) Aspects can extend only abstract aspects, (They can extend classes and implement interfaces) Classes cannot extend aspects. TO DO: Think about these semantic rules. There are a lot of interesting issues here!

October 2013CSC5021: AspectJ (J P Gibson)55 AspectJ – some details Tracing Aspect (first try) package tracing; public aspect TraceAllCalls { pointcut pointsToTrace() : call(* *.*(..)) ; before() : pointsToTrace() { System.err.println("Enter " + thisJoinPoint); } after() : pointsToTrace() { System.err.println("Exit " + thisJoinPoint); } QUESTION: Where is the problem?

October 2013CSC5021: AspectJ (J P Gibson)56 AspectJ – some details Tracing Aspect (first try) package tracing; public aspect TraceAllCalls { pointcut pointsToTrace() : call(* *.*(..)) && !within(TraceAllCalls); before() : pointsToTrace() { System.err.println("Enter " + thisJoinPoint); } after() : pointsToTrace() { System.err.println("Exit " + thisJoinPoint); } QUESTION: Why do we need the within?

October 2013CSC5021: AspectJ (J P Gibson)57 AspectJ – things you should know about Exceptions in Advice Advice body may throw exceptions: before() : doingIO() { openOutputFile(); } If openOutputFile() throws java.io.IOException: before() throws java.io.IOException : doingIO() { openOutputFile(); } => IOException must be declared/handled at all places where pointcut applies.

October 2013CSC5021: AspectJ (J P Gibson)58 AspectJ – things you should know about Aspects throwing exceptions Sometimes, an aspect can change the behaviour of methods, so that new checked exceptions are thrown: Add synchronization: InterruptedException Execute calls remotely: RemoteException =>Two possibilities: catch and handle exception directly in advice. Might not be appropriate. pass exception out of advice. Needs lots of declarations.

October 2013CSC5021: AspectJ (J P Gibson)59 AspectJ – things you should know about

October 2013CSC5021: AspectJ (J P Gibson)60 AspectJ – things you should know about Aspect Precedence (cont.) Syntax to declare aspect precedence: declare precedence : TypePattern1, TypePattern2,... ; May occur in any aspect. Says that anything matching type pattern 1 has higher precedence than anything matching type pattern 2, etc. aspect CyclicPrecedence { declare precedence : AspectA, AspectB; declare precedence : AspectB, AspectA; } OK iff aspects share no join points.

October 2013CSC5021: AspectJ (J P Gibson)61 AspectJ – things you should know about Aspect Precedence (cont.) If not declared, implicit rule for inheritance: If AspectA extends AspectB, then AspectA has higher priority.  possible to overrule advice from super-aspect. If still not declared, implicit rule for advice within one aspect: If either are after advice, then the one that appears later in the aspect has precedence over the one that appears earlier. Otherwise, then the one that appears earlier in the aspect has precedence over the one that appears later. => first do something in the same order as they appear in the source

October 2013CSC5021: AspectJ (J P Gibson)62 AspectJ – things you should know about Abstract Aspects Reminder: aspects can extend classes extend abstract aspects implement interfaces Abstract aspects may contain abstract methods, like abstract classes abstract pointcut declarations Abstract aspects are the key to writing reusable aspects.

October 2013CSC5021: AspectJ (J P Gibson)63 AspectJ – things you should know about Aspect Instantiation At runtime, aspects have fields like objects. When do they get instantiated? Usually: Instantiate an aspect once per program execution. aspect Id {...} aspect Id issingleton {...} Implemented as singleton => static field in aspect class. NOTE: Things are actually much more complicated when we consider all the different ways in which Java objects (including Aspects) are instantiated

October 2013CSC5021: AspectJ (J P Gibson)64 AspectJ – things you should know about Privileged Aspects Usually, advice code has no access to private members of advised classes. (Note that matching in pointcuts does see private members) But the privileged keyword can help: privileged public aspect A { before(MyFriend f) : this(f) &&... { System.out.println("My best friends secret: " + f._privateField); }

October 2013CSC5021: AspectJ (J P Gibson)65 AspectJ – profiling problem Return to the profiling problem we looked at in previous class TO DO: Implement 3 or more interesting profiling aspects and test on different programs Practical Work For Next Class: Invariant Testing Write an aspect that tests the invariant of a class everytime a method is executed, and writes to an invariant log file whether the test passes or fails.