Cover image for AspectJ Cookbook

Book description

When Object Oriented programming (OO) first appeared, it was a revelation. OO gave developers the ability to create software that was more flexible and robust, but as time went on and applications became more sophisticated, too, certain areas of "traditional" OO architectures were found wanting. Aspect-oriented programming (AOP) addresses those issues by extending the OO approach even further. Many developers are interested in AOP--especially in AspectJ, the open source extension of the Java programming language that explicitly supports the AOP approach. Yet, although AspectJ is included with Eclipse, the increasingly popular open source IDE for Java, finding a practical and non-theoretical way to learn this language and other AOP tools and techniques has been a real problem. Until now. The AspectJ Cookbook offers a hands-on solution--in fact, several--with a wide variety of code recipes for solving day-to-day design and coding problems using AOP's unique approach. AOP allows the global properties of a program to determine how it's compiled into an executable program. Before AOP, important program design decisions were difficult to capture in actual code. Instead, the implementation of those design decisions--known as "aspects"--were scattered throughout, resulting in "tangled" code that was hard to develop and maintain. AOP has been compared to the manufacturing of cloth, in which threads are automatically interwoven. Without AOP, programmers must stitch the threads by hand. The AspectJ Cookbook shows readers why, and how, common Java development problems can be solved by using AOP techniques. With our popular problem-solution-discussion format, the book presents real world examples to demonstrate that AOP is more than just a concept; it's a development process that will benefit users in an immediate and visible manner. If you're interested in how AOP is changing the way software is developed, and how you can use AspectJ to make code more modular, easier to develop, maintain, evolve and deploy, this is the book that really delivers.

Table of Contents

  1. AspectJ Cookbook
  2. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  3. A Note Regarding Supplemental Files
  4. Preface
    1. Audience
    2. About This Book
    3. Assumptions This Book Makes
    4. Conventions Used in This Book
    5. Using the Code Examples
    6. We’d Like to Hear from You
    7. Safari Enabled
    8. Acknowledgments
  5. 1. Aspect Orientation Overview
    1. A Brief History of Aspect Orientation
    2. AspectJ
    3. A Definition of Aspect Orientation
      1. Cross-Cutting Concerns
      2. Aspects
      3. Advice
      4. Join Points
      5. Pointcuts
      6. Putting It All Together
    4. Where to Go for More Information
  6. 2. Getting Started with AspectJ
    1. Introduction
    2. 2.1. Installing AspectJ
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 2.2. Developing a Simple Aspect
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 2.3. Compiling an Aspect and Multiple Java Files
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 2.4. Weaving Aspects into Jars
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 2.5. Weaving Aspects at Load Time
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 2.6. Generating Javadoc Documentation
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 2.7. Compiling an AspectJ Project Using Eclipse
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 2.8. Selecting the Aspects That Are Woven in a Build Within Eclipse
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 2.9. Building an AspectJ Project Using Ant
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  7. 3. Deploying AspectJ Applications
    1. Introduction
    2. 3.1. Deploying a Command-Line AspectJ Application
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 3.2. Deploying an AspectJ Application as a Fully Contained Executable JAR File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 3.3. Deploying a Java Servlet That Uses AspectJ
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 3.4. Deploying a JSP That Uses AspectJ
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 3.5. Deploying an Axis Web Service That Uses AspectJ
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  8. 4. Capturing Joing Points on Methods
    1. Introduction
    2. 4.1. Capturing a Method Call
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 4.2. Capturing the Parameter Values Passed on a Method Call
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 4.3. Capturing the Target of a Method Call
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 4.4. Capturing a Method When It Is Executing
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 4.5. Capturing the Value of the this Reference When a Method Is Executing
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  9. 5. Capturing Join Points on Exception Handling
    1. Introduction
    2. 5.1. Capturing When an Exception Is Caught
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 5.2. Capturing the Thrown Exception
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 5.3. Capturing the Object Handling the Exception
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  10. 6. Capturing Join Points on Advice
    1. Introduction
    2. 6.1. Capturing When Advice Is Executing
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 6.2. Excluding Join Points That Are a Result of Advice Execution
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 6.3. Exposing the Original Join Point When Advice Is Being Advised
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  11. 7. Capturing Join Points on Class Object Construction
    1. Introduction
    2. 7.1. Capturing a Call to a Constructor
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 7.2. Capturing a Constructor When It Is Executing
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 7.3. Capturing When an Object Is Initialized
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 7.4. Capturing When an Object Is About to Be Initialized
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 7.5. Capturing When a Class Is Initialized
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  12. 8. Capturing Join Points on Attributes
    1. Introduction
    2. 8.1. Capturing When an Object’s Attribute Is Accessed
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 8.2. Capturing the Value of the Field Being Accessed
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 8.3. Capturing When an Object’s Field Is Modified
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 8.4. Capturing the Value of a Field When It Is Modified
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  13. 9. Capturing Join Points Within Programmatic Scope
    1. Introduction
    2. 9.1. Capturing All Join Points Within a Particular Class
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 9.2. Capturing All Join Points Within a Particular Package
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 9.3. Capturing All Join Points Within a Particular Method
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  14. 10. Capturing Join Points Based on Control Flow
    1. Introduction
    2. 10.1. Capturing All Join Points Within a Program’s Control Flow Initiated by an Initial Join Point
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 10.2. Capturing All Join Points Within a Program’s Control Flow, Excluding the Initial Join Point
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  15. 11. Capturing Join Points Based on Object Type
    1. Introduction
    2. 11.1. Capturing When the this Reference Is a Specific Type
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 11.2. Capturing When a Join Point’s Target Object Is a Specific Type
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 11.3. Capturing When the Arguments to a Join Point Are a Certain Number, Type, and Ordering
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  16. 12. Capturing Join Points Based on a Boolean or Combined Expression
    1. Introduction
    2. 12.1. Capturing When a Runtime Condition Evaluates to True on a Join Point
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 12.2. Combining Pointcuts Using a Logical AND (&&)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 12.3. Combining Pointcuts Using a Logical OR (||)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 12.4. Capturing All Join Points NOT Specified by a Pointcut Declaration
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 12.5. Declaring Anonymous Pointcuts
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 12.6. Reusing Pointcuts
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  17. 13. Defining Advice
    1. Introduction
    2. 13.1. Accessing Class Members
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 13.2. Accessing the Join Point Context
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 13.3. Executing Advice Before a Join Point
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 13.4. Executing Advice Around a Join Point
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 13.5. Executing Advice Unconditionally After a Join Point
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 13.6. Executing Advice Only After a Normal Return from a Join Point
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 13.7. Executing Advice Only After an Exception Has Been Raised in a Join Point
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 13.8. Controlling Advice Precedence
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 13.9. Advising Aspects
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  18. 14. Defining Aspect Instantiation
    1. Introduction
    2. 14.1. Defining Singleton Aspects
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 14.2. Defining an Aspect per Instance
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 14.3. Defining an Aspect per Control Flow
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  19. 15. Defining Aspect Relationships
    1. Introduction
    2. 15.1. Inheriting Pointcut Definitions
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 15.2. Implementing Abstract Pointcuts
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 15.3. Inheriting Classes into Aspects
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 15.4. Declaring Aspects Inside Classes
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  20. 16. Enhancing Classes and the Compiler
    1. Introduction
    2. 16.1. Extending an Existing Class
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 16.2. Declaring Inheritance Between Classes
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 16.3. Implementing Interfaces Using Aspects
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 16.4. Declaring a Default Interface Implementation
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 16.5. Softening Exceptions
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 16.6. Extending Compilation
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  21. 17. Implementing Creational Object-Oriented Design Patterns
    1. Introduction
    2. 17.1. Implementing the Singleton Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 17.2. Implementing the Prototype Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 17.3. Implementing the Abstract Factory Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 17.4. Implementing the Factory Method Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 17.5. Implementing the Builder Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  22. 18. Implementing Structural Object-Oriented Design Patterns
    1. Introduction
    2. 18.1. Implementing the Composite Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 18.2. Implementing the Flyweight Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 18.3. Implementing the Adapter Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 18.4. Implementing the Bridge Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 18.5. Implementing the Decorator Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 18.6. Implementing the Proxy Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  23. 19. Implementing Behavioral Object-Oriented Design Patterns
    1. Introduction
    2. 19.1. Implementing the Observer Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 19.2. Implementing the Command Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 19.3. Implementing the Iterator Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 19.4. Implementing the Mediator Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 19.5. Implementing the Chain of Responsibility Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. 19.6. Implementing the Memento Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. 19.7. Implementing the Strategy Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. 19.8. Implementing the Visitor Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. 19.9. Implementing the Template Method Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. 19.10. Implementing the State Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. 19.11. Implementing the Interpreter Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  24. 20. Applying Class and Component Scale Aspects
    1. Introduction
    2. 20.1. Validating Parameters Passed to a Method
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 20.2. Overriding the Class Instantiated on a Call to a Constructor
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 20.3. Adding Persistence to a Class
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 20.4. Applying Mock Components to Support Unit Testing
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  25. 21. Applying Application Scale Aspects
    1. Introduction
    2. 21.1. Applying Aspect-Oriented Tracing
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 21.2. Applying Aspect-Oriented Logging
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 21.3. Applying Lazy Loading
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 21.4. Managing Application Properties
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  26. 22. Applying Enterprise Scale Aspects
    1. Introduction
    2. 22.1. Applying Development Guidelines and Rules
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 22.2. Applying Transactions
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 22.3. Applying Resource Pooling
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 22.4. Remoting a Class Transparently Using RMI
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. 22.5. Applying a Security Policy
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  27. 23. Applying Aspect-Oriented Design Patterns
    1. Introduction
    2. 23.1. Applying the Cuckoo’s Egg Design Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. 23.2. Applying the Director Design Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. 23.3. Applying the Border Control Design Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. 23.4. Applying the Policy Design Pattern
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  28. A. The AspectJ Runtime API
    1. org.aspectj.lang
      1. JoinPoint and JoinPoint.StaticPart
        1. The JoinPoint interface
        2. The JoinPoint.StaticPart interface
    2. Signature
      1. The Signature Interface
    3. org.aspectj.lang.reflect
      1. The Specialized Subinterfaces of Signature
        1. The AdviceSignature interface
        2. The CatchClauseSignature interface
        3. The CodeSignature interface
        4. The ConstructorSignature interface
        5. The FieldSignature interface
        6. The InitializerSignature interface
        7. The MemberSignature interface
        8. The MethodSignature interface
      2. SourceLocation
    4. The SoftException Class
    5. The NoAspectBoundException Class
  29. Index
  30. Colophon
  31. SPECIAL OFFER: Upgrade this ebook with O’Reilly