AspectJ – AOP for Java Tom Janofsky. Instructor at Penn State Abington Consultant with Chariot Solutions JUG Member.

Slides:



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

1..28 comparison of the leading aop tools report on February developerWorks articles Mik Kersten University of British Columbia.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
WEAVING CODE EXTENSIONS INTO JAVASCRIPT Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington, Microsoft Research.
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.
Aspect Oriented Programming - AspectJ Radhika Rajput.
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.
Aspect-Oriented Programming In Eclipse ® Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January.
University of British Columbia Software Practices Lab Fluid AOP Join Point Models Terry Hon Gregor Kiczales.
Advice Weaving in AspectJ Alex Gontmakher. Outline Possible implementation approaches Quick JVM primer AJC implementation Performance Evaluation.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Review Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Review What have we done during the course? Where to learn more? What is for the.
University of British Columbia Software Practices Lab CAS Seminar 06 Fluid AJ - A Simple Fluid AOP Tool Terry Hon Gregor Kiczales.
Aspect Oriented Programming Written by Michael Beder.
More on AspectJ. aspect MoveTracking { private static boolean _flag = false; public static boolean testAndClear() { boolean result = _flag; _flag = false;
Ranga Rodrigo. Class is central to object oriented programming.
Introduction to AOP.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
Aspect Oriented Programming (AOP) in.NET Brent Krueger 12/20/13.
Aspect Oriented Programming Razieh Asadi University of Science & Technology Mazandran Babol Aspect Component Based Software Engineering (ACBSE)
Session 2: AspectJ Mark Stobbe September 13,
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 32 Slide 1 Aspect-oriented Software Development 1.
POS 406 Java Technology And Beginning Java Code
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
Aspect Oriented Programming Gülşah KARADUMAN.
A Meta-Level Specification and Profile for AspectJ in UML Joerg Evermann School of Information Management Victoria University of Wellington.
Introduction to Exception Handling and Defensive Programming.
Methodology: The AOP Refactoring Process Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton Advisor:
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.
Object Oriented Software Development
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.
Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Aspect Oriented Development Alex Beatty.  Purpose  Cross-cutting Concerns  Join Points, Pointcuts, and Advices  Weaving  Invasive vs. Non-Invasive.
Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
问题 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.
aspectj tools new and noteworthy Mik Kersten University of British Columbia Adrian Colyer IBM Hursley OOPSLA, October
AOSD'04, Lancaster, UK 1 Remote Pointcut - A Language Construct for Distributed AOP Muga Nishizawa (Tokyo Tech) Shigeru Chiba (Tokyo Tech) Michiaki Tatsubori.
Aspect Security - RaviShekhar Gopalan - Prof. Lieberherr Software Security (CSG379)
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.
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Aspect-Oriented Software Development (AOSD)
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
Leveraging ColdSpring to build a robust Flex applications Chris Scott, Cynergy Systems.
Software Engineering Lecture 7
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
Aspect-Oriented Programming
Aspect-Oriented Design and Patterns
Java Programming Language
Packages and Interfaces
JAsCo an Aspect-Oriented approach tailored for
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Aspect Oriented Programming
Plug-In Architecture Pattern
Presentation transcript:

AspectJ – AOP for Java Tom Janofsky

Instructor at Penn State Abington Consultant with Chariot Solutions JUG Member

Agenda What is AOP? AspectJ Join points Pointcuts Advice Introductions Practical Uses Conclusions

What is AOP Aspect Oriented Programming Not an OO replacement Technique for handling ‘Crosscutting concerns’ Tries to eliminate code-scattering and tangling Examples: log all exceptions, capture all SQL statements, authenticate users before access

Cross Cutting BankAcctProduct getBalance() getOwner() setOwner() setBalance() toXML() store() getSKU() getQty() setSKU() setQty() toXML() store() Serializable to XML Persistable

Why AOP? Some things cannot be modeled well in object hierarchies Similarities in XDoclet,Dynamic Proxies, CLR (& JSR 201) meta data, EJB/JSP Containers Wants to – Separate concerns – Provide language for designating crosscuts

How can it be used? Still unclear Development – Check contracts, Logging – Ensure good coding practices – Tracing, Testing Optional runtime components – Debugging, profiling Implement core system features – Caching, Security

How do you do AOP? Write your components Write your aspects Weave – Compile (AspectJ 1.0) – Link (AspectJ 1.1) – Load (Classloader) – Run (VM)

What is AspectJ? An open source language 100% Java compatible An AOP implementation Extension to Java, new syntax Started at Xerox, now Eclipse project

Definitions AOP Aspect AspectJ Join point Pointcut Advice Introduction (Inter-type declaration)

Getting and installing Download from eclipse.org/aspectj Run executable JAR Use aspectjrt.jar on classpath Includes structure browser, debugger

Writing an aspect Write the class Write the aspect (.java or.aj) Weave with ‘ajc’ compiler Must have aspectjrt.jar for compile and runtime

Join points Points in a programs execution – Method call call( public void setOwner(String) ) – Constructor call initialization (BankAccount.new() ) – Method call execution – Constructor call execution – Field get – Field set

Join points (cont.) Exception handler execution Class initialization Object initialization No finer join points in AspectJ (loops, if checks)

Join point patterns Names can be matched with * call ( * * BankAccount.*(*)) – Matches all calls on BankAccount, regardless of visibility or return type, with one argument call ( * *.(*)) – Matches all method calls with 1 parameter call ( * *.(..)) – Matches all method calls

Join Point Patterns Cont Subtypes can be matched with a + – call (public void BankAccount+(..)) Can also match on throws patterns – call (public void BankAccount+(..) throws Exception+) Watch out for infinite recursion! – Aspects match aspects too – Use ! within()

Pointcuts Structure for selecting join points in a program and collecting context (args, target, source) Declaring a named pointcut – pointcut changeBalance() : call (public void BankAccount.setBalance(java.math.BigDecim al)); Can be combined with logical (set) operators, &&, ||, and !

Pointcuts cont. Valid on interfaces and classes Syntax – pointcut name ([parameters]) : designator (ajoinpoint); – Name will be used to handle actions – ajoinpoint is a signature match – Designator decides when this join point will match

Set Operators public aspect BankAspectOr { pointcut change() : call (public void setBalance(java.math.BigDecimal)) || call (public void setOwner(String)); before() : change() { System.out.println(thisJoinPoint.getSignature()) ; }

Available pointcuts call execution initialization handler get set this

Available pointcuts cont. args target cflow cflowbelow staticinitialization withincode within if adviceexecution preinitialization

Call pointcut Use when you are interested in the invocation of a method Control is still in calling object, use execution() for control in called object Format: – call (public void BankAccount.setOwner(String));

Handler pointcut Captures the execution of an exception handler anywhere in the primary application Format: – handler (ClassCastException) Remember + patterns apply here as well

State based designators Can be used to expose object to advice, or narrow pointcut selection – this,target,args Format: pointcut setBalance(BankAccount b) : call(public void setBalance(*)) && target (b); before (BankAccount b) : setBalance(b) { //b is accessible here }

Other designators cflow,cflowbelow – Allow us to match join points within a certain program flow staticinitialization – Match class initialization within, withincode – Match class, method Dynamic – If, adviceexecution Pointcut Id (Can combine pointcuts using names and boolean operators)

Advice The second half of AOP Advice is what gets executed when a join point is matched Advice is always relative to a joinpoint Format – type ([parameters]) : join point id (param list) { … }

Advice Type before – excellent for preconditions – argument checking, setup code, lazy init after – can be qualified with: after returning, or after throwing – Cleanup of resources – checking/manipulating the return value around – the most powerful advice – can replace invocation, or just surround – use proceed() to call method

thisJoinPoint info about the join point that was just matched – the source location of the current join point – the kind of join point that was matched – various string representations of the join point – the argument(s) to the method selected by the join point – the signature of the method selected by the join point – the target object – the executing object – thisJoinPointStaticPart exposes args, target, and this if designated (no reflection required)

Accessing Objects Use target, args, and this similarly Can be done declaratively – Add a parameter to the pointcut declaration – Add && args(s) to the designator – Add parameter to advice designator – Add variable name to advice body Also all available reflectively

Exceptions and precedence Aspects can’t throw exceptions that the pointcuts they are advising don’t throw – Wrap in runtime Precedence – use the precedence keyword in an aspect: declare precedence : A, B; – Sub aspects execute before parents. – Otherwise undefined. Multiple advice in an aspect: – natural order (before, after) – order of declaration

Inter-type Declarations AspectJ can be used to change the structure of existing code – add members (id fields, dirty flag) – add methods (toXML, storeToJDBC) – add types that extend existing types or implement interfaces – declare custom compilation errors or warnings – convert checked exceptions to unchecked Can use from aspects, or regular code Write normal variable and methods in your aspect, but prefix them with your class name

Inter-type declarations cont. Very powerful Can do wacky things – Add concrete fields & methods to interfaces (no constructors) – Modify aspects – Make an existing class dynamically implement an interface – Make an existing class extend another

Problems Difficult to know is code is advised – Helped by tool support? Crossing component boundaries Patent nonsense? How will we model? When usages are appropriate? Not a JSR, integration questions

Conclusions Powerful, but is it a good idea? Tool support is in: – Eclipse, JBuilder, Forte, IDEA 4.0? Other implementations – AspectWerkz (XML) – Nanning (Java) – HyperJ (IBM)

More Info at Slides and sample code at