AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
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.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
University of British Columbia Software Practices Lab Fluid AOP Join Point Models Terry Hon Gregor Kiczales.
University of British Columbia Software Practices Lab 2005 CASCON A Fluid AOP Editor Terry Hon Gregor Kiczales.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
University of British Columbia Software Practices Lab CAS Seminar 06 Fluid AJ - A Simple Fluid AOP Tool Terry Hon Gregor Kiczales.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Introduction to Aspect- Oriented Programming CS 3360 Gregor Kiczales, et. al. Getting started with AspectJ, CACM, 44(10):59-65, October Fall 2012.
Modern Concurrency Abstractions for C# by Nick Benton, Luca Cardelli & C´EDRIC FOURNET Microsoft Research.
Programming Languages and Paradigms Object-Oriented Programming.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 1 Debugging Support.
Writing Classes (Chapter 4)
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
Introduction to Object-Oriented Programming
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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
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.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
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
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
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.
Overview of C++ Polymorphism
AspectScope: An Outline Viewer for AspectJ Programs Michihiro Horie, Shigeru Chiba Tokyo Institute of Technology, Japan.
Methods.
1 Aspectual Caml an Aspect-Oriented Functional Language Hideaki Tatsuzawa Hidehiko Masuhara Akinori Yonezawa University of Tokyo.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Conclusão Parte 1 – Qualidade de Software por Construção Alessandro Garcia © Alessandro Garcia, 2016 Departamento de Informática.
Java Primer 1: Types, Classes and Operators
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
Implementing Classes Yonglei Tao.
Aspect-Oriented Programming
Interface.
Building Java Programs
Interfaces.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Overview of C++ Polymorphism
AN INTRODUCTION TO OBJECTS AND CLASSES
Introduction to Object-Oriented Programming
Building Java Programs
Aspect Oriented Programming
Introduction to Methods and Interfaces
Presentation transcript:

AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009

Compatibility with Java AspectJ provides additional keywords and constructs to write aspects –Aspects operate with reference to features in standard Java code An aspect can have ordinary Java code –Member variables and methods –Can implement interfaces –There is also aspect inheritance which we will discuss later The AspectJ compiler produces standard Java byte code –Can be executed on any Java virtual machine

AspectJ and Eclipse AJDT: AspectJ Development Tools –An Eclipse plug-in –Facilitates writing AspectJ program while exploiting the powerful features of the Eclipse environment –Already installed on the lab machines

AspectJ Join Point Model We looked at one type of join point –A Method Call Types of join points –Call to a method or constructor –Execution of a method or constructor –Getting or setting a class field –Initialisation of a class or object –Execution of an exception handler –Others which we will not discuss

Understanding the Join Point Model (1) class Account { private int balance; public Account(int startingBalance) { this.balance = startingBalance; } public void setBalance(int newBalance) { this.balance = newBalance; } public int getBalance( ) { return this.balance; } class Test { public static void main(String args[ ]) { Account a = new Account(200); int x = a.getBalance( ); a.setBalance(x+100); }

Understanding the Join Point Model (2) Let us look at the following join points in our code –Calls to methods and constructors –Execution of methods and constructors –Getting value of a field –Setting value of a field We will not consider the other types of join points at this stage

The Call Graph in Our Example Account a = new Account(200); Constructor call Account constructor execution balance value set Test.main( ) Methods in Account Fields in Account start of end of Constructor execution start of end of Set Field start of end of int x = a.getBalance( ) Method call getBalance( ) execution get balance value start of end of Method execution start of end of Get Field start of end of a.setBalance(x+100) Method call setBalance( ) execution balance value set start of end of Method execution start of end of Set Field start of end of

Pointcut AspectJ construct –Specified using the pointcut keyword Used to specify which join points are of interest –pointcut constructorCall( ): call Account.new(int) ; –pointcut getBalanceMethodCall( ): call int getBalance( ) ; –pointcut setBalanceFieldValue( ): set int balance ; –pointcut setBalanceMethodExecution( ): execution void setBalance(int) ; ( ( ( ( ) ) ) )

Advice Method like construct Used to specify behaviour we want to execute once a join point is matched by a specified pointcut Three types of advice –Before –After –Around

Before Advice Used to execute code before the code at the matched join point executes before( ): constructorCall( ) { System.out.println(“Constructor call about to start”); }

After Advice Used to execute code after the code at the matched join point has executed after( ): constructorCall( ) { System.out.println(“Constructor call has now finished”); }

Around Advice (1) Can be used in two ways –Wrap code around the execution of the code for the matched join point Requires a call to special proceed method in the advice body Can be thought of as a combined before and after advice –Circumvent the code that would otherwise have executed at the matched join point Requires that there is no call to proceed in the advice body

Around Advice: Wrapping Around a Join Point void around( ): setBalanceMethodExecution( ) { System.out.println(“Who gives you extra?”); proceed( ); System.out.println(“Howard does”); } Note the return value. Why is this needed for around advice?

Around Advice: Circumventing a Join Point void around( ): setBalanceMethodExecution( ) { System.out.println(“I won’t let you change your balance); }

Why Distinguish Call and Execution Join Points Allow one to specify two different temporal points during program execution –When the call happens –When the method actually executes AspectJ weaver requires access to your Java code –If you are using third party libraries, you do not have access to the code –call pointcut designator is useful to trap calls to methods in third party library classes –execution pointcut is useful to apply aspect behaviour with reference to methods in one’s own classes

Outline of an AspectJ Aspect aspect MyAspect { // pointcut definitions // advices // any Java members // inter-type declarations // to be discussed later }

Using AJDT Similar to using Eclipse to write Java code

AspectJ HelloWorld Open the HelloWorld Java project in Eclipse Convert it to an AspectJ project Write an aspect that does the following: –Captures the call to printMessage method and prints a message before and after to confirm it has captured the call. –Note: You may want to use the call pointcut designator and before, after advice

Tracing Exercise Open the TracingExample application in Eclipse Convert it to an AspectJ project Write a simple tracing aspect –Print trace messages before and after: The call to constructor of Account The setting of the balance field in Account The call to getBalance() method in Account The execution of the setBalance() method in Account