SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
-
Upload
ted-leung -
Category
Technology
-
view
908 -
download
0
description
Transcript of SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
Overviewn Why do we need AOP?n What is AOPn 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
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
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
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
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[]) {}
}
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
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
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)
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)
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
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.*
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
Example Object Model
Examplesn Fieldsn Wildcardsn ExceptionFlow
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}
Examplen Fields1n Servlet Based
Advice precedencen What happens when lots of advice
matches?n Dominates keyword
n aspect A dominates TypePattern {}
n Subaspect advice takes precedencen Otherwise undetermined
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
Examplen SerialNumber
Aspect Extensionn Aspects can extend classesn Aspects can implement interfacesn Aspects can extend abstract aspects
n The sub aspect inherits pointcuts
Examplen Abstract tracing Aspect
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
Privileged Aspectsn Aspects normally obey Java access
control rulesn Aspects that can break encapsulation
n privileged aspect Id {}
Examplen UpdateAspect
Tool Supportn Antn IDE Support
n Emacsn Forten JBuilder
n AJDocn AJBrowsern Debugger
n Uses .lst files to do aspect weaving
AspectJ Statusn 1.0
n Released 11/30/2001
n 1.1n Faster increment compilation
n 2.0n Dynamic crosscutsn Work on bytecode files
Musingsn AspectJSPn Eclipsen No need for .lst’s
Development usesn Loggingn Tracingn Timingn Exception handling / loggingn Various kinds of invasive/non-invasive
instrumentation
n Flexibility of pointcut designators
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
To Learn Moren www.aspectj.orgn www.aosd.netn CACM 10/2001n These slides at:
n www.sauria.com