SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
-
Upload
ted-leung -
Category
Technology
-
view
905 -
download
0
description
Transcript of SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
![Page 2: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/2.jpg)
Overviewn Why do we need AOP?n What is AOPn AspectJ
![Page 3: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/15.jpg)
Example Object Model
![Page 16: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/16.jpg)
Examplesn Fieldsn Wildcardsn ExceptionFlow
![Page 17: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/18.jpg)
Examplen Fields1n Servlet Based
![Page 19: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/21.jpg)
Examplen SerialNumber
![Page 22: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/23.jpg)
Examplen Abstract tracing Aspect
![Page 24: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/26.jpg)
Examplen UpdateAspect
![Page 27: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/29.jpg)
Musingsn AspectJSPn Eclipsen No need for .lst’s
![Page 30: SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051610/54944688b47959744d8b4ab3/html5/thumbnails/32.jpg)
To Learn Moren www.aspectj.orgn www.aosd.netn CACM 10/2001n These slides at:
n www.sauria.com