seajug dec 2001: aspect-oriented programming with aspectj

32
Aspect Oriented Programming with AspectJ Ted Leung Sauria Associates, LLC [email protected]

Upload: ted-leung

Post on 18-Dec-2014

907 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Aspect Oriented Programmingwith AspectJ

Ted LeungSauria Associates, LLC

[email protected]

Page 2: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Overviewn Why do we need AOP?n What is AOPn AspectJ

Page 3: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Why do we need AOP?n Modular designs are not cut and driedn Responsibilities can be assigned to one or

more classesn Examples:

n Every servlet for the administrative part of the site must check for a logged in administrator user

n Site navigation : changing country in the UI (via multiple means) must update a bunch of data structures

n Maintaining both ends of a two way relationshipn Logging and tracing

Page 4: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

What is AOP?n Introduces the notion of crosscutting

concernsn Concerns that you want to modularizen Concerns whose implementation is all over

n Introduces language mechanisms for identifying and capturing crosscutting concerns

Page 5: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Why bother with AOP?n Capture the crosscutting concern

explicitlyn Both the behavior of the concernn The specification of its applicability

n Change is easiern Change the aspect – no grepping

n Aspects can be plugged in or out

Page 6: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

AspectJ and AOPn AspectJ is an aspect-oriented extension to

Javan One Concept : Join Pointsn Four constructs

n Pointcut Designators (Pointcuts)n Advicen Introductionn Aspects

n Aspects are composed of advice and introductions, and attached to pointcuts

Page 7: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Hello World AOP Style

public aspect HelloAspect {pointcut entry() : execution(public static void main(String[]));

after() : entry() {System.out.println("Hello World");

}}

public class HelloWorld {public static void main(String args[]) {}

}

Page 8: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Join Pointsn A well defined point in the program flown Created each time:

n A method is calledn A method executesn A field is get/setn An Exception handler executesn A dynamic initializer (constructor) executesn A static initializer executes

Page 9: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Pointcuts : dynamic crosscutsn A declarative specification of a set of

join pointsn Easy to change versus copying calls or

code to where they belongn Examples:

n Calls to method X from within the control flow of method Y

n Calls to public methods

Page 10: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Pointcut designatorsn field get and set

n set(int MyClass.field)

n Method calln call(int add(int, int))

n Method executionn execution(int add(int,int)

n Exception Handlingn handler(IOException)

n Constructor Executionn initialization(class)

n Lexical control flown within(class), withincode(method)

n Dynamic control flown cflow(pointcut), cflowbelow(pointcut)

Page 11: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Composing pointcutsn Pointcut designators can be composedn &&

n target(MyClass) && call(void draw())

n ||n call(void draw) && target(MyClass1) || target(package.*))

n !n call(void draw()) && !target(MyClass)

Page 12: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Signatures

n Basic signature:n float compute(float, float)

n On a specific classn float Account.compute(float, float)

n Any 0-ary method on a specific classn Account.*()n Account.*(int)

n Any public method returning an intn public int *.*(..)

n Any method throwing IOExceptionn public *.*(..) throws IOException

Page 13: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Type Patternsn A type name

n vector

n Wildcardsn java.util.*Listn org.apache.xerces.xni.*n org.w3c..*

n Subtypesn java.util.AbstractList+

n Arraysn java.util.String[]

n Compositionn java.io.* || java.nio.*

Page 14: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Advicen Code that runs at each join point

selected by a pointcutn Kinds of advice

n beforen after

n after returningn after exception

n around

Page 15: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Example Object Model

Page 16: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Examplesn Fieldsn Wildcardsn ExceptionFlow

Page 17: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Accessing pointcut contextn We want to be able to access program

values at a join pointn Pointcuts can take parameters

n pointcut name(Type1 arg1, Type2 arg2) :args(arg1, arg2) && pointcut

n Advice can use those parametersn Around(Type1 arg1, Type2 arg2) : name(arg1, arg2) {

… use arg1 & arg2 in advice code}

Page 18: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Examplen Fields1n Servlet Based

Page 19: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Advice precedencen What happens when lots of advice

matches?n Dominates keyword

n aspect A dominates TypePattern {}

n Subaspect advice takes precedencen Otherwise undetermined

Page 20: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Introduction: static crosscutsn Aspect can introduce:

n introduce fieldsn Modifiers Type TypePattern.Id { = Expr };

n introduce methodsn Modifiers TypePattern.new(Formals){ Body }n Modifiers TypePattern.Id(Formals) { Body }

n Implement an interfacen declare parents : TypePattern implementsTypeList;

n Extend a classn declare parents : TypePattern extendsTypeList;

n Can introduce on many classes at once

Page 21: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Examplen SerialNumber

Page 22: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Aspect Extensionn Aspects can extend classesn Aspects can implement interfacesn Aspects can extend abstract aspects

n The sub aspect inherits pointcuts

Page 23: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Examplen Abstract tracing Aspect

Page 24: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Associated aspectsn How many aspects are instantiated?n singleton

n By defaultn aspect Id perthis(Pointcut)

n 1 per currently executing objectn aspect Id pertarget(Pointcut)

n 1 per target objectn aspect Id percflow(Pointcut)

n 1 per control flow entrancen aspect Id percflowbelow(Pointcut)

n 1 per cflowbelow entrance

Page 25: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Privileged Aspectsn Aspects normally obey Java access

control rulesn Aspects that can break encapsulation

n privileged aspect Id {}

Page 26: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Examplen UpdateAspect

Page 27: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Tool Supportn Antn IDE Support

n Emacsn Forten JBuilder

n AJDocn AJBrowsern Debugger

n Uses .lst files to do aspect weaving

Page 28: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

AspectJ Statusn 1.0

n Released 11/30/2001

n 1.1n Faster increment compilation

n 2.0n Dynamic crosscutsn Work on bytecode files

Page 29: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Musingsn AspectJSPn Eclipsen No need for .lst’s

Page 30: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Development usesn Loggingn Tracingn Timingn Exception handling / loggingn Various kinds of invasive/non-invasive

instrumentation

n Flexibility of pointcut designators

Page 31: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Application usesn Consistency maintenance / checking

n Keeping both sides of a bi-directional relationship up to date

n Policiesn Securityn Session Handlingn Failure / Retryn Synchronization

n Context Passingn Avoids huge argument lists or carrier objects

n Multiple Views

Page 32: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

To Learn Moren www.aspectj.orgn www.aosd.netn CACM 10/2001n These slides at:

n www.sauria.com