Aspect-Oriented Programming

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Overview of AspectJ Aspect Oriented Software Development Seminar Technion presented by Oren Mishali.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 1 Debugging Support.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Writing Classes (Chapter 4)
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.
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.
Introduction to Object-Oriented Programming
Aspect Oriented Programming Razieh Asadi University of Science & Technology Mazandran Babol Aspect Component Based Software Engineering (ACBSE)
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd
Comparison of Different AOP Approaches Presented by: Xiaojing Wang.
AOSD'04, Lancaster, UK 1 Remote Pointcut - A Language Construct for Distributed AOP Muga Nishizawa (Tokyo Tech) Shigeru Chiba (Tokyo Tech) Michiaki Tatsubori.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington 1.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
User-Written Functions
Chapter 7 User-Defined Methods.
Java Primer 1: Types, Classes and Operators
Java Programming Language
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
Chapter 3: Using Methods, Classes, and Objects
CS102 – Exceptions David Davenport Latest: May 2015
Programming Language Concepts (CIS 635)
Error Handling Summary of the next few pages: Error Handling Cursors.
ATS Application Programming: Java Programming
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Exception Handling Chapter 9.
Java Programming Language
Group Status Project Status.
Object Oriented Programming
Exception Handling Chapter 9 Edited by JJ.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Lecture 11 Objectives Learn what an exception is.
CMSC 202 Exceptions 2nd Lecture.
Introduction to Object-Oriented Programming
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Classes, Objects and Methods
CSC 143 Java Errors and Exceptions.
CMSC 202 Exceptions.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Aspect-Oriented Programming The most recent programming paradigm, developed around 2001 In a way, its orthogonal to OOP The idea: you have some implementation that must stretch across units (classes, procedures) you implement it as an aspect the compiler then adds to the implementation of each unit your aspect Example: we have an OO-based windowing system we want to add a security component that ensures that resizing can only be done under certain circumstances rather than redefining all of our classes, we define the aspect and let the compiler insert the code into each appropriate class for us!

Cross-Cutting Aspects We refer to aspects as cross-cutting because they cut across classes Consider the hierarchy to the right we want to add (or alter aspect1) rather than implement (or update) aspect1 in all classes, the compiler will do this for us with little effort on our part

How AOP Works Aspects are stand-alone modules containing three types of components inter-type declarations – variables that are added to the objects of the system advice – code that is added to the objects of the system along with where it should be added (at the start of a method, after, in an exception handler, etc) pointcut – the location(s) within the class that the advice can be applied pointcuts are defined as one or more join points that describe such things as types of methods, type of parameters passed to the methods, etc a pointcut might have multiple join points we can use regular expressions to be more generic about our join points (e.g., using * for any type of parameter) While we can have multiple aspects to add to an OOP system, multiple aspects (pieces of advice) are hard to automatically implement into a given class that is, having multiple aspects is not necessarily scalable

Pointcuts Pointcuts map advice to a location within the OO code there might be a one-to-one mapping of advice to a given object there might be a one-to-many mapping of advice to a group of objects Join points can include invoked by constructor when object is constructed, calls to super start of method, end of method start of loop, exit of loop when a particular member is initialized, accessed, modified upon exception thrown or caught There are several types of pointcuts, here are three Kinded – match a particular kind of join point Dynamic – check runtime types and bind variables Scope – limit the scope by matching on some variable-based condition

Advice Can be specified to run at different times before the join point code executes after the join point code executes in place of the join point code (known as “around”) only if the join point code throws an exception (known as “after throwing”) after the finally clause of a join point executes (known as after finally) note that since a finally clause always executes no matter if an exception is thrown or not, this type of advice runs automatically different combinations of the above can be applied such as around and after throwing

Weaving The goal of AOP is to simplify the programmer’s task so that an aspect can be added to an OOP system without having to re-implement multiple classes and yet the aspect must be added to a number of class definitions How? By weaving the aspect into the existing classes who does the weaving? the aspect weaver the aspect weaver takes OO code + aspects and combines them Weaving can produce either a new source program in the original language interpreted code to the interpreted environment where the OO code was already loaded into the environment and now the weaver adds the aspects without weaving, AOP is really just more OOP, it is the weaving aspect that makes AOP different

Example: Adding Logging We have a banking system with classes handling withdraw, overdraw, deposit, balance, summary We add an aspect called logging to automatically log all actions For join points, we define them as around for withdraw/deposit after for the rest Advice differs because the specific text being logged will differ, but they will all be similar in that they will all open a text file output a message to the textfile messages will be similar but specialized and include the time/date, client’s name and success/failure of operation close the textfile

Example in AspectJ aspect Logger { pointcut method() : withdraw(* *(..)); before() : method() { logfile.println("User " + u + " is attempting a withdrawal of " + amount + " from bank account " + account); } after() : method() { if(...) logfile.println("Success"); else logfile.println("Failure"); The … in the if statement tests whatever condition is needed to determine success or failure

AspectJ An extension to Java that adds aspects, pointcuts, and advice aspect FaultHandler { private boolean Server.disabled = false; private void reportFault() { System.out.println("Failure! Please fix it."); } public static void fixServer(Server s) { s.disabled = false; pointcut services(Server s): target(s) && call(public * *(..)); before(Server s): services(s) { if (s.disabled) throw new DisabledException(); after(Server s) throwing (FaultException e): services(s) { s.disabled = true; reportFault(); An extension to Java that adds aspects, pointcuts, and advice seems to be the most popular AOP language comes with an aspect weaver as part of the compiler/JVM Here is an example of an aspect implemented in AspectJ

Explanation The aspect starts with the word aspect and its name This is followed by any inter-type declarations (Server.disabled here) Methods are then defined which will be invoked by the advice notice that one method is static in that it will be shared among all objects Next, we have a pointcut to define when the advice will be executed here, there is a single pointcut tying this aspect to the one class Server the pointcut has two pieces of advice, one to run before and one to run only after throwing an exception

Continued Let’s take a closer look at the code to understand it pointcut services(Server s): target(s) && call(public * *(..)); The pointcut is named services the pointcut will occur when the object being interacted with is a Server and the Server is having any public method invoked the call(public * *(..)) means invoking a public method named * with any return type and any parameters (by type and number) the weaver will then add the advice to all public methods of the Server class The advice are two sets of code the if statement (for before) is added before any method call that is, if a method is to call a public method and disabled is true, then throw the exception the second advice is added to the exception handler of such methods where it resets disabled to true and calls reportFault()

More on AspectJ If you look back at the example, you will see that the inter-type declaration, methods and advice are all Java It is the pointcuts and the specification of join points that are new Pointcuts consist of call – when a method is called execution – when a method’s body is executed handler – when an exception handler executes this(type) – when the currently executing object is of the given type target(type) – when the referenced object is of the given type within – when the executing code belongs to a given class cflow – when the executing code is in the control flow of a specified method pointcuts can be combined using &&, ||, !

Advice The code in the advice starts with the join point (at what point in the given code the advice should execute) the advice is passed a parameter and if it is an after join point, it can specify a return type after(Object o) returning(int x): somepointcut(o) { … } note that x does not have to be part of the advice but instead it can be part of the method which the advice will be embedded into for after throwing, you can specify an exception type so that the advice only executes if the given type of exception is thrown

Example Pointcuts pointcut cflow(call(public * main(…)))&&(this(class2)||this(class3)) this pointcut identifies any point in the code where the executing object is of type class 2 or class 3 and it was reached from this class’ main pointcut example(Object1 o) : target(o) && call(* foobar(..)); this pointcut identifies for an object of type Object1, the method named foobar was called Let’s assume to the second pointcut we have the following advice after(Object1 o) throwing(Object1Exception e): example(o) {…} this advice only executes if the specific Object1Exception was thrown, any other form of exception thrown by o does not cause this advice to be invoked

Complete Example Original Java code public class Demo { static Demo d; public static void main(String[] args){ new Demo().go(); } void go(){ d = new Demo(); d.foo(1,d); System.out.println(d.bar(new Integer(3))); void foo(int i, Object o){ System.out.println("Demo.foo(" + i + ", " + o + ")\n"); String bar (Integer j){ System.out.println("Demo.bar(" + j + ")\n"); return "Demo.bar(" + j + ")"; Original Java code

aspect GetInfo { static final void println(String s){ System.out.println(s); } pointcut goCut(): cflow(this(Demo) && execution(void go())); pointcut demoExecs(): within(Demo) && execution(* *(..)); Object around(): demoExecs() && !execution(* go()) && goCut() { println("Intercepted message: " + thisJoinPointStaticPart.getSignature().getName()); println("in: " + thisJoinPointStaticPart.getSignature().getDeclaringType().getName()); printParameters(thisJoinPoint); println("Running original method: \n" ); Object result = proceed(); println(" result: " + result ); return result; } static private void printParameters(JoinPoint jp) { println("Arguments: " ); Object[] args = jp.getArgs(); String[] names = ((CodeSignature)jp.getSignature()).getParameterNames(); Class[] types = ((CodeSignature)jp.getSignature()).getParameterTypes(); for (int i = 0; i < args.length; i++) { println(" " + i + ". " + names[i] + " : " + types[i].getName() + " = " + args[i]);