Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar
Overview • Motivation for Aspect Oriented Programming • What is Aspect Oriented Programming? • How Does Aspect Oriented Programming Work? • Case Study
Motivation for Aspect Oriented Programming • First Computer Programs • Consisted of cryptic binary strings
Motivation for Aspect Oriented Programming • Next Generation • “Higher-Level” Assembly
Motivation for Aspect Oriented Programming • 1957: Birth of Real “High-Level” Language • IBM’s Fortran • Division created between a computer’s hardware and the coupling that hardware imposed upon the software. • Subprograms, or subroutines • Modular code blocks allowed decomposition into smaller pieces • Increased reusability and maintainability of code
Motivation for Aspect Oriented Programming • Late 1960s: Object Oriented Paradigm • Collection of related data and operations useful to that data • Paved way for Modern C++, Java, C#, etc.
Motivation for Aspect Oriented Programming • Problems with OO • Although specific groups of data and operations are cohesively grouped together, other secondary operations still need to be duplicated across these objects. • High-level, system-wide requirements • Application logging • Security requirements • Other Policy Layer Requirements
Motivation for Aspect Oriented Programming • General Application-wide requirements still need to be supported via these objects but are not explicitly encapsulated in one single object type
Motivation for Aspect Oriented Programming • Assume following scenario • Employee record system • Collection of employee related objects • Each object has cohesive set of operations that perform necessary manipulations on the data contained within that object
Motivation for Aspect Oriented Programming • Employee Record System • Now imagine that the system is extended to support logging each employee change to a logging database.
Motivation for Aspect Oriented Programming • OO Approach • Developers will have to • Dive inside the employee objects • Add additional operations to support logging • Wading through existing code and finding the exact locations to insert logging operations can be a time-consuming, tedious, and error-prone process which can lead to software instability and security flaws. • Writing to a database, or ensuring sufficient privileges are obtained does not coherently fit with what employee objects represent – BAD COHESION (Dr. Rowe = Angry)
What Is Aspect Oriented Programming? • Solution = Aspect Oriented Approach • We want to add new functionality in a single location and simply specify that this new functionality is to be invoked when certain conditions arise. • Added functionality is achieved without wading through existing code and appending duplicate functionality across existing operations.
What Is Aspect Oriented Programming? • Employee Record System • We will be able to create one central collection of operations responsible for logging to a database • Specify that a database write shall be invoked when an employee object is modified.
How Does Aspect Oriented Programming Work? • Cross-Cutting Concern: Some aspects of implementation, such as logging, error handling, standards enforcement, and policy-layer modification are difficult to implement in a modular way. The result is that code is tangled across a system; this leads to quality, productivity, and maintenance issues. These issues are cross-cutting concerns
How Does Aspect Oriented Programming Work? • Advice: This is the additional code that one wants to apply to an existing model to add support for features such as logging, error handling, standards enforcement, and policy-layer modification
How Does Aspect Oriented Programming Work? • Point Cut: This is the term given to the point of execution in the application at which a cross-cutting concern needs to be applied. In our example, a point-cut is reached when a thread of execution within the employee record system enters a method responsible for modifying an employee; a second point-cut is reached when that thread of execution leaves the method responsible for modifying an employee
How Does Aspect Oriented Programming Work? • Aspect: The combination of the point-cut and the advice is termed an aspect. In our example, we add a logging aspect to our employee record system by defining a point-cut and giving the correct advice to execute when that point-cut is reached = +
aspect vs class aspect specific data and operations are “static” aspect is a singleton pointcut declaration advice
How Does Aspect Oriented Programming Work? • Point-cut named “LogEmployeeChange” • Specify specific points where employee object changed • Deals only with objects of type “Employee” (line 13) • Deals specifically with 4 methods contained within the “Employee” class (lines 14-17) • Line 14 explicitly says, “for any ‘public’ method of class ‘Employee’ with any type of return parameter (‘*’), a name of ‘setName’, and a parameter set of anything (‘..’), execute the corresponding Advice • Note: use of wildcards allow the aspect programmer to include a large grouping of functions in a single line.
How Does Aspect Oriented Programming Work? • before Advice • Line 19 says, “Since my associated point-cut has been triggered, execute the code in my block before executing the method which triggered the point-cut.” • The actual instance of the Employee class which will be modified is passed into the before Advice as “Employee e”; the advice can modify Employee e.
How Does Aspect Oriented Programming Work? • After executing before Advice, the method which triggered the point-cut is executed; this method could be “setName”, “setAddress”, etcetera. • Once execution returns from this method, the after Advice is considered.
Case Study: Apache Tomcat • Tomcat: open-source implementation of Java Servlet and JavaServer Pages developed at the Apache Software Foundation. • Object-Oriented
Case Study: Apache Tomcat • Red Shows encapsulated XML Parser in Tomcat
Case Study: Apache Tomcat • Red Shows encapsulated URL Pattern Matching in Tomcat
Case Study: Apache Tomcat • Red Shows Tomcat Logging • A single logging aspect can replace this tangle • Increase Cohesion • Decrease Coupling • Increase Maintainability • No duplicated logging code across multiple classes
Summary • AspectJ and Aspect-Oriented Programming represent a way to cleanly modularize high-level requirements which span across the scope of many different classes and operations. Using AspectJ can greatly alleviate issues related to logging, monitoring, application-spanning requirements, and many other issues that commonly cause headaches for even well-designed Object-Oriented systems