Aspect Oriented Programming
        with AspectJ

           Ted Leung
     Sauria Associates, LLC
        twl@sauria.com
Overview
    Why do we need AOP?
n

    What is AOP
n

    AspectJ
n
Why do we need AOP?
    Modular designs are not cut and dried
n

    Responsibilities can be assigned to one or
n
    more...
What is AOP?
    Introduces the notion of crosscutting
n

    concerns
        Concerns that you want to modularize
    n
...
Why bother with AOP?
    Capture the crosscutting concern
n

    explicitly
        Both the behavior of the concern
    n...
AspectJ and AOP
    AspectJ is an aspect-oriented extension to
n
    Java
    One Concept : Join Points
n

    Four constr...
Hello World AOP Style
public class HelloWorld {
  public static void main(String args[]) {
  }
}




public aspect HelloAs...
Join Points
    A well defined point in the program flow
n

    Created each time:
n

        A method is called
    n

  ...
Pointcuts : dynamic crosscuts
    A declarative specification of a set of
n

    join points
    Easy to change versus cop...
Pointcut designators
    field get and set
n
        set(int MyClass.field)
    n


    Method call
n
        call(int add...
Composing pointcuts
    Pointcut designators can be composed
n

    &&
n
         target(MyClass) && call(void draw())
   ...
Signatures
    Basic signature:
n
        float compute(float, float)
    n


    On a specific class
n
        float Acco...
Type Patterns
    A type name
n
        vector
    n


    Wildcards
n
        java.util.*List
    n

        org.apache.x...
Advice
    Code that runs at each join point
n

    selected by a pointcut
    Kinds of advice
n

        before
    n

  ...
Example Object Model
Examples
    Fields
n

    Wildcards
n

    ExceptionFlow
n
Accessing pointcut context
    We want to be able to access program
n

    values at a join point
    Pointcuts can take p...
Example
    Fields1
n

    Servlet Based
n
Advice precedence
    What happens when lots of advice
n

    matches?
    Dominates keyword
n
        aspect A dominates ...
Introduction: static crosscuts
    Aspect can introduce:
n
        introduce fields
    n

             Modifiers Type Typ...
Example
    SerialNumber
n
Aspect Extension
    Aspects can extend classes
n

    Aspects can implement interfaces
n

    Aspects can extend abstract...
Example
    Abstract tracing Aspect
n
Associated aspects
    How many aspects are instantiated?
n

    singleton
n
        By default
    n

    aspect Id perth...
Privileged Aspects
    Aspects normally obey Java access
n

    control rules
    Aspects that can break encapsulation
n
 ...
Example
    UpdateAspect
n
Tool Support
    Ant
n

    IDE Support
n
        Emacs
    n

        Forte
    n

        JBuilder
    n

    AJDoc
n

 ...
AspectJ Status
    1.0
n

        Released 11/30/2001
    n


    1.1
n

        Faster increment compilation
    n


    ...
Musings
    AspectJSP
n

    Eclipse
n

    No need for .lst’s
n
Development uses
    Logging
n

    Tracing
n

    Timing
n

    Exception handling / logging
n

    Various kinds of inva...
Application uses
    Consistency maintenance / checking
n
        Keeping both sides of a bi-directional relationship
    ...
To Learn More
    www.aspectj.org
n

    www.aosd.net
n

    CACM 10/2001
n

    These slides at:
n

        www.sauria.co...
Upcoming SlideShare
Loading in …5
×

SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

1,221 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,221
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
55
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

  1. 1. Aspect Oriented Programming with AspectJ Ted Leung Sauria Associates, LLC twl@sauria.com
  2. 2. Overview Why do we need AOP? n What is AOP n AspectJ n
  3. 3. Why do we need AOP? Modular designs are not cut and dried n Responsibilities can be assigned to one or n more classes Examples: n Every servlet for the administrative part of the site n must check for a logged in administrator user Site navigation : changing country in the UI (via n multiple means) must update a bunch of data structures Maintaining both ends of a two way relationship n Logging and tracing n
  4. 4. What is AOP? Introduces the notion of crosscutting n concerns Concerns that you want to modularize n Concerns whose implementation is all over n Introduces language mechanisms for n identifying and capturing crosscutting concerns
  5. 5. Why bother with AOP? Capture the crosscutting concern n explicitly Both the behavior of the concern n The specification of its applicability n Change is easier n Change the aspect – no grepping n Aspects can be plugged in or out n
  6. 6. AspectJ and AOP AspectJ is an aspect-oriented extension to n Java One Concept : Join Points n Four constructs n Pointcut Designators (Pointcuts) n Advice n Introduction n Aspects n Aspects are composed of advice and n introductions, and attached to pointcuts
  7. 7. Hello World AOP Style public class HelloWorld { public static void main(String args[]) { } } public aspect HelloAspect { pointcut entry() : execution(public static void main(String[])); after() : entry() { System.out.println(quot;Hello Worldquot;); } }
  8. 8. Join Points A well defined point in the program flow n Created each time: n A method is called n A method executes n A field is get/set n An Exception handler executes n A dynamic initializer (constructor) executes n A static initializer executes n
  9. 9. Pointcuts : dynamic crosscuts A declarative specification of a set of n join points Easy to change versus copying calls or n code to where they belong Examples: n Calls to method X from within the control n flow of method Y Calls to public methods n
  10. 10. Pointcut designators field get and set n set(int MyClass.field) n Method call n call(int add(int, int)) n Method execution n execution(int add(int,int) n Exception Handling n handler(IOException) n Constructor Execution n initialization(class) n Lexical control flow n within(class), withincode(method) n Dynamic control flow n cflow(pointcut), cflowbelow(pointcut) n
  11. 11. Composing pointcuts Pointcut designators can be composed n && n target(MyClass) && call(void draw()) n || n call(void draw) && n target(MyClass1) || target(package.*)) ! n call(void draw()) && !target(MyClass) n
  12. 12. Signatures Basic signature: n float compute(float, float) n On a specific class n float Account.compute(float, float) n Any 0-ary method on a specific class n Account.*() n Account.*(int) n Any public method returning an int n public int *.*(..) n Any method throwing IOException n public *.*(..) throws IOException n
  13. 13. Type Patterns A type name n vector n Wildcards n java.util.*List n org.apache.xerces.xni.* n org.w3c..* n Subtypes n java.util.AbstractList+ n Arrays n java.util.String[] n Composition n java.io.* || java.nio.* n
  14. 14. Advice Code that runs at each join point n selected by a pointcut Kinds of advice n before n after n after returning n after exception n around n
  15. 15. Example Object Model
  16. 16. Examples Fields n Wildcards n ExceptionFlow n
  17. 17. Accessing pointcut context We want to be able to access program n values at a join point Pointcuts can take parameters n pointcut name(Type1 arg1, Type2 arg2) : n args(arg1, arg2) && pointcut Advice can use those parameters n Around(Type1 arg1, Type2 arg2) : name(arg1, n arg2) { … use arg1 & arg2 in advice code }
  18. 18. Example Fields1 n Servlet Based n
  19. 19. Advice precedence What happens when lots of advice n matches? Dominates keyword n aspect A dominates TypePattern { n } Subaspect advice takes precedence n Otherwise undetermined n
  20. 20. Introduction: static crosscuts Aspect can introduce: n introduce fields n Modifiers Type TypePattern.Id { = Expr }; n introduce methods n Modifiers TypePattern.new(Formals){ Body } n Modifiers TypePattern.Id(Formals) { Body } n Implement an interface n declare parents : TypePattern implements n TypeList; Extend a class n declare parents : TypePattern extends n TypeList; Can introduce on many classes at once n
  21. 21. Example SerialNumber n
  22. 22. Aspect Extension Aspects can extend classes n Aspects can implement interfaces n Aspects can extend abstract aspects n The sub aspect inherits pointcuts n
  23. 23. Example Abstract tracing Aspect n
  24. 24. Associated aspects How many aspects are instantiated? n singleton n By default n aspect Id perthis(Pointcut) n 1 per currently executing object n aspect Id pertarget(Pointcut) n 1 per target object n aspect Id percflow(Pointcut) n 1 per control flow entrance n aspect Id percflowbelow(Pointcut) n 1 per cflowbelow entrance n
  25. 25. Privileged Aspects Aspects normally obey Java access n control rules Aspects that can break encapsulation n privileged aspect Id { n }
  26. 26. Example UpdateAspect n
  27. 27. Tool Support Ant n IDE Support n Emacs n Forte n JBuilder n AJDoc n AJBrowser n Debugger n Uses .lst files to do aspect weaving n
  28. 28. AspectJ Status 1.0 n Released 11/30/2001 n 1.1 n Faster increment compilation n 2.0 n Dynamic crosscuts n Work on bytecode files n
  29. 29. Musings AspectJSP n Eclipse n No need for .lst’s n
  30. 30. Development uses Logging n Tracing n Timing n Exception handling / logging n Various kinds of invasive/non-invasive n instrumentation Flexibility of pointcut designators n
  31. 31. Application uses Consistency maintenance / checking n Keeping both sides of a bi-directional relationship n up to date Policies n Security n Session Handling n Failure / Retry n Synchronization n Context Passing n Avoids huge argument lists or carrier objects n Multiple Views n
  32. 32. To Learn More www.aspectj.org n www.aosd.net n CACM 10/2001 n These slides at: n www.sauria.com n

×