O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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