AspectJ - Java based AOP System. AspectJ - basic features Compatible extension to Java: –Upward Compatibility - all legal Java programs are legal AspectJ.

Slides:



Advertisements
Similar presentations
AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems.
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.
Written by: Dr. JJ Shepherd
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
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.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
University of British Columbia Software Practices Lab CAS Seminar 06 Fluid AJ - A Simple Fluid AOP Tool Terry Hon Gregor Kiczales.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
More on AspectJ. aspect MoveTracking { private static boolean _flag = false; public static boolean testAndClear() { boolean result = _flag; _flag = false;
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Io package as Java’s basic I/O system continue’d.
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.
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 Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Internet Software Development More stuff on Threads Paul Krause.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
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.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
AspectJ TM programming techniques and use in Eclipse AOSD Seminar presented by Hagai Cibulski.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
A Distributed Aspect-Oriented System for J2EE Applications Muga Nishizawa and Shigeru Chiba (Tokyo Institute of Technology, Japan) Background - As benefits.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
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.
Virtual Support for Dynamic Join Points C. Bockisch, M. Haupt, M. Mezini, K. Ostermann Presented by Itai Sharon
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Session 7 Methods Strings Constructors this Inheritance.
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.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
Introduction to Object-Oriented Programming Lesson 2.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
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.
Comparison of Different AOP Approaches Presented by: Xiaojing Wang.
Classes, Interfaces and Packages
Introduction to Aspect- Oriented Programming CS 3360 Gregor Kiczales, et. al. Getting started with AspectJ, CACM, 44(10):59-65, October Spring 2012.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington 1.
A first language for Aspect-Oriented Programming
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Threads in Java Two ways to start a thread
Multi Threading.
Java Programming Language
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
Chapter 3: Using Methods, Classes, and Objects
Creating and Modifying Text part 2
Aspect-Oriented Programming
Aspect-Oriented Design and Patterns
CSCD 330 Network Programming
Presentation transcript:

AspectJ - Java based AOP System

AspectJ - basic features Compatible extension to Java: –Upward Compatibility - all legal Java programs are legal AspectJ programs –Platform Compatibility - all legal AspectJ programs run on all standard Java VM –Tool Compatibility - existing tools can be extended to support AspectJ in a natural way: IDEs, documentation and design tools –Programmer Compatibility - Programming with AspectJ must feel like a natural extension to programming with Java

AspectJ - basic features (contd.) AspectJ program structure: –Classes - like in Java –Aspects - for concerns that crosscut class structure AspectJ crosscutting implementation –Dynamic - define additional implementation to run at certain point of program execution –Static - define new operation on existing types (introduction)

AspectJ - language overview Basic concepts: –Joinpoint - certain points in a program’s execution (for example - a call to a class’ method –Pointcut - Program constructs to designate joinpoints and to collect specific context at these points –Advice - Code that runs upon meeting certain conditions –Aspect - class like crosscutting unit, where advice and pointcuts form weaving rules

Joinpoints certain points in a program’s execution –have context associated with them (for example a method call joinpoint can have the target object as a part of the context –in AspectJ, available joinpoints are limited to the following: Method call and execution Constructor call and execution Read/Write access to field Exception handler execution Object and class Initialization execution

Pointcut designators Program constructs to designate Joinpoints set of Joinpoints and (optionally) some values from their execution context AspectJ provides primitive pointcut designators which can be used to compose named/anonymous user defined pointcuts

Pointcut designators (contd.) primitive pointcut designators: –calls(signature) / receptions(signature) / executions(signature) - matches call/reception/execution join points at which the method/constructor called matches signature; method signature: ResultTypeName RecvrTypeName.meth_id (params) constructor signature: NewObjectTypeName.new (params) –gets(signature) / gets(signature) [val] / sets(signature) / sets(signature) [oldVal] / sets(signature) [oldVal] [newVal] - matches get / set join points where the field accessed matches signature; field signature: FieldTypeName ObjectTypeName.field_id

Pointcut designators (contd.) primitive pointcut designators (contd): –handles(ThrowableType) - matches exception handler execution join point of the specified type –instanceof(CurrentlyExecutingObjectType) - “this” is of specified type –within(ClassName) / withincode(signature) - the executing code is contained within ClassName / within the specified method –cflow(pointcut_designator) - for example - all joint points in control flow of call to the method

Pointcut designators (contd.) primitive pointcut designators (contd): context collecting pointcuts: –this(object) - all the join points where “this” is the object specified –target(object) - the method is called on specified object –args(arg1,..) - the join points where the arguments are as specified –named / unnamed pointcuts (like named / unnamed classes) –use ||, &&, !,.., *, + to compose pointcuts

Advices Associate code with a specific pointcut Advice types available: before, after (after returning / after throwing), around the context is passed to advice by the pointcut example: pointcut move (Line l): receptions (void l.MoveXY (int, int)); after (Line l): move (l) {/*print msg*/}

Advices (contd.) Order of advice execution: –any around advise (most specific first); the invocation of proceed () invokes next most specific around advise –any before advice, most specific first –the computation itself –all after advice, less specific first –return value from step (3) is returned to the innermost call to proceed () and that piece of around advice continues running –when the innermost around advice finishes, the surrounding around advice continues running –once the outermost piece of around advice returns, control continues back from the join point

Advices (contd.) Aspect precedence (for two pieces of advice a1,a2 defined in aspects A1,A2 respectively) –if A1 = A2, the advice that appears first in the aspect’s declaration body is more specific –A1 extends A2 --> a1 is more specific –declaration of A1 includes a dominates modifier that mentions A2 --> a1 is more specific than a2

Aspects Modular units of crosscutting implementation declaration - similar to class declaration include pointcut declarations, advice declarations and declarations permitted in class declaration aspect inheritance, abstract aspects are supported

Example1 - thread pooling Thread pooling - more effective and resource saving approach to thread managenment AOP implementation in a modular fashion - source code need not be modified original code - simple multithreaded Server

// UppercaseServer.java import java.io.*; import java.net.*; public class UppercaseServer { public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java UppercaseServer "); System.exit(1); } int portNum = Integer.parseInt(args[0]); ServerSocket serverSocket = new ServerSocket(portNum); while(true) { Socket requestSocket = serverSocket.accept(); Thread serverThread = new Thread(new UppercaseWorker(requestSocket)); serverThread.start(); } } } The server source code

class UppercaseWorker implements Runnable { private Socket _requestSocket; public UppercaseWorker(Socket requestSocket) throws IOException { System.out.println("Creating new worker"); _requestSocket = requestSocket; } public void run() { BufferedReader requestReader = null; Writer responseWriter = null; try { requestReader = new BufferedReader(new InputStreamReader(_requestSocket.getInputStream())); responseWriter = new OutputStreamWriter(_requestSocket.getOutputStream());

while(true) { String requestString = requestReader.readLine(); if (requestString == null) {break;} System.out.println("Got request: " + requestString); responseWriter.write(requestString.toUpperCase() + "\n"); responseWriter.flush(); } } catch(IOException ex) {} finally { try { if (responseWriter != null) {responseWriter.close();} if (requestReader != null) {requestReader.close();} _requestSocket.close(); } catch (IOException ex2) {} } System.out.println("Ending the session"); }

// ThreadPool.java - simple class that acts like stack for available threads import java.util.*; public class ThreadPool { List _waitingThread = new Vector(); public void put(DelegatingThread thread) { System.out.println("Putting back: " + thread); _waitingThread.add(thread); } public DelegatingThread get() { if (_waitingThread.size() != 0) { DelegatingThread availableThread =(DelegatingThread)_waitingThread.remove(0); System.out.println("Providing for work: " + availableThread); return availableThread; } return null; } static class DelegatingThread extends Thread { private Runnable _delegatee; public void setDelegatee(Runnable delegatee) { _delegatee = delegatee; } public void run() {_delegatee.run();} }}

Adding thread pooling // ThreadPooling.java public aspect ThreadPooling { ThreadPool pool = new ThreadPool(); //===================================================================== // Thread creation //===================================================================== pointcut threadCreation(Runnable runnable) : call(Thread.new(Runnable)) && args(runnable); Thread around(Runnable runnable) : threadCreation(runnable) { ThreadPool.DelegatingThread availableThread = pool.get(); If (availableThread == null) { availableThread = new ThreadPool.DelegatingThread(); } availableThread.setDelegatee(runnable); return availableThread; }

Adding thread pooling (contd.) //==================================================================== = // Session //==================================================================== = pointcut session(ThreadPool.DelegatingThread thread) : execution(void ThreadPool.DelegatingThread.run()) && this(thread); void around(ThreadPool.DelegatingThread thread) : session(thread) { while(true) { proceed(thread); pool.put(thread); synchronized(thread) { try { thread.wait(); } catch(InterruptedException ex) {} } } }

Adding thread pooling (contd.) //=========================================================== // Thread start //=========================================================== pointcut threadStart(ThreadPool.DelegatingThread thread) : call(void Thread.start()) && target(thread); void around(Thread thread) : threadStart(thread) { if (thread.isAlive()) { // wake it up synchronized(thread) { thread.notifyAll(); } } else { proceed(thread); } } }

The implementation in details threadCreation() captures the creating a new thread object taking a Runnable object as the argument. Advise the threadCreation() pointcut to first check the thread pool for available threads. If no thread is available, create a new one. Set the delegatee to the Runnable object passed in and return that object. No proceed() so the actual operation is not executed session() captures the run() method's execution of any ThreadPool.DelegatingThread objects.

The implementation in details (contd.) By putting session() inside a while(true) loop, you advise session() to never finish the servicing. That ensures a thread, once created, never dies. Once a request is processed, you put the thread back into thread pool and put the thread into waiting state. threadStart() captures a call to the Thread.start() method. It uses isAlive() to check if the thread previously started (thread obtained from a pool and now in a waiting state) Wake up the thread by notifying it. If the thread had not started yet, proceed with starting the thread.

Example 2 - Enforcement modularization Implement policy enforcement to ensure no duplicate listener are added to the models, and listeners do not loiter around when the view they represent become usable. Policy enforcement implementation in more useful and easy way - no documentation,code reviews and so on.

Problems with listeners let you add a listener object more than once, which leads to duplicate work if an event- notification method carries an expensive operation. Easy to forget to remove listeners before destroying a view - cause the listener to consuming memory.

aspect structural view need to implement 2 concerns: –uniqueness concern –no loitering-views concern

Base aspect: EventListenerManagement This aspect contains an addListenerCall() pointcut that captures calls to methods adding a listener. // EventListenerManagement.java import java.util.*; public abstract aspect EventListenerManagement { pointcut addListenerCall(Object model, EventListener listener) : call(void *.add*Listener(EventListener+)) && target(model) && args(listener) && modelAndListenerTypeMatch(); abstract pointcut modelAndListenerTypeMatch(); }

Implement the uniqueness concern checks whether that listener was previously added. If that listener is already present, the operation does not proceed; otherwise, it adds the listener advises the addListenerCall() pointcut to check for the listener's uniqueness by looking in a list obtained by invoking getCurrentListeners(). It proceeds with adding the listener only if the list doesn't include a listener:

Implement the uniqueness concern - contd. // EventListenerUniqueness.java import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public abstract aspect EventListenerUniqueness extends EventListenerManagement { void around(Object model, EventListener listener) : addListenerCall(model, listener) {

Implement the uniqueness concern - contd. EventListener[] listeners = getCurrentListeners(model); if (!Utils.isInArray(listeners, listener)) { System.out.println("Accepting " + listener); proceed(model, listener); } else { System.out.println("Already listening " + listener); } } public abstract EventListener[] getCurrentListeners(Object model); }

Implement the uniqueness concern - contd. the concrete aspect TableModelListenerUniqueness extends EventListenerUniqueness to apply the aspect to TableModel and related classes. It provides an implementation for the modelAndListenerTypeMatch() pointcut to restrict the model type to AbstractTableModel and the listener type to TableModelListener.

Implement the uniqueness concern - contd. // TableModelListenerUniqueness.java import java.util.EventListener; import javax.swing.event.TableModelListener; import javax.swing.table.*; aspect TableModelListenerUniqueness extends EventListenerUniqueness { pointcut modelAndListenerTypeMatch() : target(AbstractTableModel) && args(TableModelListener); public EventListener[] getCurrentListeners(Object model) { return ((AbstractTableModel)model).getListeners(TableModelListener.class); } }

Implement a no loitering-views concern ensuring that no view loiters after its destruction. It advises addListenerCall() to proceed with the listener obtained by calling the getWeakListener() method.

Implement a no loitering-views concern - contd. // EventListenerWeakening.java import java.lang.ref.*; import java.util.*; import javax.swing.event.*; public abstract aspect EventListenerWeakening extends EventListenerManagement dominates EventListenerUniqueness { void around(Object model, EventListener listener) : addListenerCall(model, listener) { proceed(model, getWeakListener(listener)); } public abstract EventListener getWeakListener(EventListener listener); }

Implement a no loitering-views concern - contd. The TableModelListenerWeakening aspect handles table-related listeners. It uses a specialized WeakEventListener that implements TableModelListener by delegating to the referent object.

Implement a no loitering-views concern - contd. // TableModelListenerWeakening.java import java.util.*; import javax.swing.event.*; import javax.swing.table.*; public aspect TableModelListenerWeakening extends EventListenerWeakening { pointcut modelAndListenerTypeMatch() : target(AbstractTableModel) && args(TableModelListener); public EventListener getWeakListener(EventListener listener) { System.out.println("Weakening " + listener); return new WeakTableModelListener((TableModelListener)listener); } }

Implement a no loitering-views concern - contd. public class WeakTableModelListener extends WeakEventListener implements TableModelListener { public WeakTableModelListener(TableModelListener delegatee) { super(delegatee); } public void tableChanged(TableModelEvent e) { TableModelListener listener = (TableModelListener)getDelegatee(); listener.tableChanged(e); }

Implement a no loitering-views concern - contd. static aspect TableRemoveGarbageCollectedListeners extends WeakEventListener.RemoveGarbageCollectedListeners { pointcut lexicalScopeMatch() : within(WeakTableModelListener); public void removeListener(EventObject event, EventListener listener) { ((TableModel)event.getSource()).removeTableModelListener((TableModelListener)listener); } } }

Example 3: Characteristic- based implementation modularization Operations with the same characteristics should typically implement common behaviors. for example you may need to authenticate access to all security-critical data.

Characteristic-based implementation modularization declare the aspect adding characteristic- based crosscutting behavior as an abstract aspect. declare an abstract pointcut for methods with characteristics under consideration. write an advice performing the required implementation.

SlowMethodAspect's implementation abstract slowMethods() pointcut and advises it to first put a wait cursor, proceed with the original operation, and finally restore the original cursor.

SlowMethodAspect's implementation - contd. // SlowMethodAspect.java import java.util.*; import java.awt.*; import java.awt.event.*; public abstract aspect SlowMethodAspect { abstract pointcut slowMethods(Component uiComp); void around(Component uiComp) : slowMethods(uiComp) { Cursor originalCursor = uiComp.getCursor(); Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); uiComp.setCursor(waitCursor);

SlowMethodAspect's implementation - contd. try { proceed(uiComp); } finally { uiComp.setCursor(originalCursor); } } }

SlowMethodAspect's implementation - contd. Two test components, GUIComp1 and GUIComp2, nest a concrete implementation of the aspect. public static aspect SlowMethodsParticipant extends SlowMethodAspect { pointcut slowMethods(Component uiComp) : execution(void GUIComp1.performOperation1()) && this(uiComp); }

SlowMethodAspect's implementation - contd. public static aspect SlowMethodsParticipant extends SlowMethodAspect { pointcut slowMethods(Component uiComp) : (execution(void GUIComp2.performOperation1()) || execution(void GUIComp2.performOperation2())) && this(uiComp); }