SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
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 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
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
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
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
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;);
    }
}
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
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
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
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
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
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
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
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 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
        }
Example
    Fields1
n

    Servlet Based
n
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
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
Example
    SerialNumber
n
Aspect Extension
    Aspects can extend classes
n

    Aspects can implement interfaces
n

    Aspects can extend abstract aspects
n

        The sub aspect inherits pointcuts
    n
Example
    Abstract tracing Aspect
n
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
Privileged Aspects
    Aspects normally obey Java access
n

    control rules
    Aspects that can break encapsulation
n
        privileged aspect Id {
    n

        }
Example
    UpdateAspect
n
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
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
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 invasive/non-invasive
n

    instrumentation

    Flexibility of pointcut designators
n
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
To Learn More
    www.aspectj.org
n

    www.aosd.net
n

    CACM 10/2001
n

    These slides at:
n

        www.sauria.com
    n

Weitere ähnliche Inhalte

Was ist angesagt?

storage class
storage classstorage class
storage class
student
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
Jake Bond
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
kapil078
 
김재석, C++ 프로그래머를 위한 C#, NDC2011
김재석, C++ 프로그래머를 위한 C#, NDC2011김재석, C++ 프로그래머를 위한 C#, NDC2011
김재석, C++ 프로그래머를 위한 C#, NDC2011
devCAT Studio, NEXON
 

Was ist angesagt? (19)

Template classes and ROS messages
Template classes and ROS messagesTemplate classes and ROS messages
Template classes and ROS messages
 
2.dynamic
2.dynamic2.dynamic
2.dynamic
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
 
C chap22
C chap22C chap22
C chap22
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Core Java
Core JavaCore Java
Core Java
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
College1
College1College1
College1
 
storage class
storage classstorage class
storage class
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
175035 cse lab-05
175035 cse lab-05 175035 cse lab-05
175035 cse lab-05
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
 
From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
 
JavaScript: The Language
JavaScript: The LanguageJavaScript: The Language
JavaScript: The Language
 
김재석, C++ 프로그래머를 위한 C#, NDC2011
김재석, C++ 프로그래머를 위한 C#, NDC2011김재석, C++ 프로그래머를 위한 C#, NDC2011
김재석, C++ 프로그래머를 위한 C#, NDC2011
 

Ähnlich wie SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

Aspect oriented programming_with_spring
Aspect oriented programming_with_springAspect oriented programming_with_spring
Aspect oriented programming_with_spring
Guo Albert
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programming
daotuan85
 
Pointcuts and Analysis
Pointcuts and AnalysisPointcuts and Analysis
Pointcuts and Analysis
Wiwat Ruengmee
 
10 modeling and_notations
10 modeling and_notations10 modeling and_notations
10 modeling and_notations
Majong DevJfu
 

Ähnlich wie SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ (20)

Aspect oriented programming_with_spring
Aspect oriented programming_with_springAspect oriented programming_with_spring
Aspect oriented programming_with_spring
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programming
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
Reflection in Go
Reflection in GoReflection in Go
Reflection in Go
 
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the UglyKotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
 
Dica ii chapter slides
Dica ii chapter slidesDica ii chapter slides
Dica ii chapter slides
 
Aspect-Oriented Technologies
Aspect-Oriented TechnologiesAspect-Oriented Technologies
Aspect-Oriented Technologies
 
Cleaner Code: How Clean Code is Functional Code
Cleaner Code: How Clean Code is Functional CodeCleaner Code: How Clean Code is Functional Code
Cleaner Code: How Clean Code is Functional Code
 
Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scala
 
Shiksharth com java_topics
Shiksharth com java_topicsShiksharth com java_topics
Shiksharth com java_topics
 
Cleaner Code - CodeStock 2019 Edition
Cleaner Code - CodeStock 2019 EditionCleaner Code - CodeStock 2019 Edition
Cleaner Code - CodeStock 2019 Edition
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
 
Pointcuts and Analysis
Pointcuts and AnalysisPointcuts and Analysis
Pointcuts and Analysis
 
10 modeling and_notations
10 modeling and_notations10 modeling and_notations
10 modeling and_notations
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Android classes-in-pune-syllabus
Android classes-in-pune-syllabusAndroid classes-in-pune-syllabus
Android classes-in-pune-syllabus
 
Android training in Nagpur
Android training in Nagpur Android training in Nagpur
Android training in Nagpur
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
Scalable machine learning
Scalable machine learningScalable machine learning
Scalable machine learning
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 

Mehr von Ted Leung

MySQL User Conference 2009: Python and MySQL
MySQL User Conference 2009: Python and MySQLMySQL User Conference 2009: Python and MySQL
MySQL User Conference 2009: Python and MySQL
Ted Leung
 
Northwest Python Day 2009
Northwest Python Day 2009Northwest Python Day 2009
Northwest Python Day 2009
Ted Leung
 
PyCon UK 2008: Challenges for Dynamic Languages
PyCon UK 2008: Challenges for Dynamic LanguagesPyCon UK 2008: Challenges for Dynamic Languages
PyCon UK 2008: Challenges for Dynamic Languages
Ted Leung
 
OSCON 2008: Open Source Community Antipatterns
OSCON 2008: Open Source Community AntipatternsOSCON 2008: Open Source Community Antipatterns
OSCON 2008: Open Source Community Antipatterns
Ted Leung
 
OSCON 2007: Open Design, Not By Committee
OSCON 2007: Open Design, Not By CommitteeOSCON 2007: Open Design, Not By Committee
OSCON 2007: Open Design, Not By Committee
Ted Leung
 
Ignite The Web 2007
Ignite The Web 2007Ignite The Web 2007
Ignite The Web 2007
Ted Leung
 
ApacheCon US 2007: Open Source Community Antipatterns
ApacheCon US 2007:  Open Source Community AntipatternsApacheCon US 2007:  Open Source Community Antipatterns
ApacheCon US 2007: Open Source Community Antipatterns
Ted Leung
 
OSCON 2005: Build Your Own Chandler Parcel
OSCON 2005: Build Your Own Chandler ParcelOSCON 2005: Build Your Own Chandler Parcel
OSCON 2005: Build Your Own Chandler Parcel
Ted Leung
 
PyCon 2005 PyBlosxom
PyCon 2005 PyBlosxomPyCon 2005 PyBlosxom
PyCon 2005 PyBlosxom
Ted Leung
 
SeaJUG March 2004 - Groovy
SeaJUG March 2004 - GroovySeaJUG March 2004 - Groovy
SeaJUG March 2004 - Groovy
Ted Leung
 
OSCON 2004: XML and Apache
OSCON 2004: XML and ApacheOSCON 2004: XML and Apache
OSCON 2004: XML and Apache
Ted Leung
 
OSCON 2004: A Developer's Tour of Chandler
OSCON 2004: A Developer's Tour of ChandlerOSCON 2004: A Developer's Tour of Chandler
OSCON 2004: A Developer's Tour of Chandler
Ted Leung
 
IQPC Canada XML 2001: How to develop Syntax and XML Schema
IQPC Canada XML 2001: How to develop Syntax and XML SchemaIQPC Canada XML 2001: How to develop Syntax and XML Schema
IQPC Canada XML 2001: How to develop Syntax and XML Schema
Ted Leung
 
IQPC Canada XML 2001: How to Use XML Parsing to Enhance Electronic Communication
IQPC Canada XML 2001: How to Use XML Parsing to Enhance Electronic CommunicationIQPC Canada XML 2001: How to Use XML Parsing to Enhance Electronic Communication
IQPC Canada XML 2001: How to Use XML Parsing to Enhance Electronic Communication
Ted Leung
 
ApacheCon 2000 Everything you ever wanted to know about XML Parsing
ApacheCon 2000 Everything you ever wanted to know about XML ParsingApacheCon 2000 Everything you ever wanted to know about XML Parsing
ApacheCon 2000 Everything you ever wanted to know about XML Parsing
Ted Leung
 

Mehr von Ted Leung (20)

DjangoCon 2009 Keynote
DjangoCon 2009 KeynoteDjangoCon 2009 Keynote
DjangoCon 2009 Keynote
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
 
Seeding The Cloud
Seeding The CloudSeeding The Cloud
Seeding The Cloud
 
Programming Languages For The Cloud
Programming Languages For The CloudProgramming Languages For The Cloud
Programming Languages For The Cloud
 
MySQL User Conference 2009: Python and MySQL
MySQL User Conference 2009: Python and MySQLMySQL User Conference 2009: Python and MySQL
MySQL User Conference 2009: Python and MySQL
 
PyCon US 2009: Challenges and Opportunities for Python
PyCon US 2009: Challenges and Opportunities for PythonPyCon US 2009: Challenges and Opportunities for Python
PyCon US 2009: Challenges and Opportunities for Python
 
Northwest Python Day 2009
Northwest Python Day 2009Northwest Python Day 2009
Northwest Python Day 2009
 
PyCon UK 2008: Challenges for Dynamic Languages
PyCon UK 2008: Challenges for Dynamic LanguagesPyCon UK 2008: Challenges for Dynamic Languages
PyCon UK 2008: Challenges for Dynamic Languages
 
OSCON 2008: Open Source Community Antipatterns
OSCON 2008: Open Source Community AntipatternsOSCON 2008: Open Source Community Antipatterns
OSCON 2008: Open Source Community Antipatterns
 
OSCON 2007: Open Design, Not By Committee
OSCON 2007: Open Design, Not By CommitteeOSCON 2007: Open Design, Not By Committee
OSCON 2007: Open Design, Not By Committee
 
Ignite The Web 2007
Ignite The Web 2007Ignite The Web 2007
Ignite The Web 2007
 
ApacheCon US 2007: Open Source Community Antipatterns
ApacheCon US 2007:  Open Source Community AntipatternsApacheCon US 2007:  Open Source Community Antipatterns
ApacheCon US 2007: Open Source Community Antipatterns
 
OSCON 2005: Build Your Own Chandler Parcel
OSCON 2005: Build Your Own Chandler ParcelOSCON 2005: Build Your Own Chandler Parcel
OSCON 2005: Build Your Own Chandler Parcel
 
PyCon 2005 PyBlosxom
PyCon 2005 PyBlosxomPyCon 2005 PyBlosxom
PyCon 2005 PyBlosxom
 
SeaJUG March 2004 - Groovy
SeaJUG March 2004 - GroovySeaJUG March 2004 - Groovy
SeaJUG March 2004 - Groovy
 
OSCON 2004: XML and Apache
OSCON 2004: XML and ApacheOSCON 2004: XML and Apache
OSCON 2004: XML and Apache
 
OSCON 2004: A Developer's Tour of Chandler
OSCON 2004: A Developer's Tour of ChandlerOSCON 2004: A Developer's Tour of Chandler
OSCON 2004: A Developer's Tour of Chandler
 
IQPC Canada XML 2001: How to develop Syntax and XML Schema
IQPC Canada XML 2001: How to develop Syntax and XML SchemaIQPC Canada XML 2001: How to develop Syntax and XML Schema
IQPC Canada XML 2001: How to develop Syntax and XML Schema
 
IQPC Canada XML 2001: How to Use XML Parsing to Enhance Electronic Communication
IQPC Canada XML 2001: How to Use XML Parsing to Enhance Electronic CommunicationIQPC Canada XML 2001: How to Use XML Parsing to Enhance Electronic Communication
IQPC Canada XML 2001: How to Use XML Parsing to Enhance Electronic Communication
 
ApacheCon 2000 Everything you ever wanted to know about XML Parsing
ApacheCon 2000 Everything you ever wanted to know about XML ParsingApacheCon 2000 Everything you ever wanted to know about XML Parsing
ApacheCon 2000 Everything you ever wanted to know about XML Parsing
 

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ

  • 1. Aspect Oriented Programming with AspectJ Ted Leung Sauria Associates, LLC twl@sauria.com
  • 2. Overview Why do we need AOP? n What is AOP n AspectJ n
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  • 16. Examples Fields n Wildcards n ExceptionFlow n
  • 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. Example Fields1 n Servlet Based n
  • 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. 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. Example SerialNumber n
  • 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. Example Abstract tracing Aspect n
  • 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. Privileged Aspects Aspects normally obey Java access n control rules Aspects that can break encapsulation n privileged aspect Id { n }
  • 26. Example UpdateAspect n
  • 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. 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. Musings AspectJSP n Eclipse n No need for .lst’s n
  • 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. 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. To Learn More www.aspectj.org n www.aosd.net n CACM 10/2001 n These slides at: n www.sauria.com n