20
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007

Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007

Embed Size (px)

Citation preview

Aspect Oriented Programming

Sumathie SundaresanCS590 :: Summer 2007

June 30, 2007

Background

Procedural Languages

like – Fortran, Pascal, C

Object Oriented Programming

like – Java, C++

Aspect Oriented Programming

Frameworks like – AspectJ etc.

Advantages of OOP

Modularity Reduce large problems to smaller

Reusability Inheritance Polymorphism

Reliability Robustness Maintainability Extendibility

Problems with OOP

Not much strong reusability of code Every time inherit one base class Or Use delegation

A functional unit can not be isolated, even they crosscut programs at many places Logging, security checks, transaction

management

Crosscutting concerns increases the cost of developing and maintenance cost.

Introduction

Aspect Oriented Programming Was discovered in Palo Alto Research Center

during 1980s and 1990s Is to Object Oriented Programming (like C++ to C) Does not replace Object Oriented Programming

technique, it complements it Is a technique

Allows programmers to modularize the crosscutting concerns

Separate crosscutting concerns from an application

Terminology

Aspect Is a unit of modularity, encapsulation and

abstraction Is like a class in object oriented technology Can be used to implement crosscutting concerns

in a modular fashion Summarize behaviors that affect multiple classes

into reusable modules

Cross-cutting concerns Find out certain behavior to occur at one or more

points in a program

Cont'd..... Join Points

Identify the points in the execution of components where aspect should be applied

For example: execution of method & constructor

referred from www.cs.umd.edu

Cont'd..... Pointcuts

Used to select join points Advice

Specifies what to do at joint points For example

Before(...) : PointcutExpr(...); After(...) : PointcutExpr(...); Around(...) : PointcutExpr(...);

Weaving Task which is done by compiler in linking classes

and aspect together Is no longer needed if there is an aspect oriented

compiler available.

Examplepackage hello;public class HelloWorld { public void sayHello() { System.out.println("Hello"); } public static void main(String[] args) { new HelloWorld().sayHello(); }}

public aspect World { pointcut greeting(): execution(* HelloWorld.sayHello(..));

after() returning: greeting() { System.out.println("World!"); }}

Advice

Join Point

AOP tools in Java AspectJ(we are using this)

JBOSS AOP

Spring AOP

JAC

Installing AspectJ Command line

Download AspectJ from aspectj.org Start the installation by running java-jar

java –jar aspectj-1.5.3.jar Setup path and classpath Compile AspectJ programs by

ajc YourJavaClass.java YourAspect.aj>ajc Hello.java World.aj

Eclipse http://www.eclipse.org/ajdt/downloads/ Get the ajdt_1.4.1_for_eclipse_3.2 zip file Extract the zip file in to the eclipse folder http://www.eclipse.org/aspectj/doc/next/runtime-api/in

dex.html

What is AJDT?

“AspectJ Development Tools”

Comprehensive tool support for using AspectJ in Eclipse Integrating the AspectJ compiler New editors/views/wizards for working with AspectJ

artefacts

Tool support is critical for AOP For day to day development of AO programs For learning AO concepts and understanding the

power of AO

The outline view

• Shows aspect structure post compilation:– Advice– Inter-type declarations– Declared warnings/errors– Defined Pointcuts

• Useful feedback loop– Did my pointcut match anything?

• Fully navigable– Jump to advised locations

Source Annotations

• Gutter annotations in editor view– Highlight lines affected by advice– Hover help shows advice in affect– Context menu for annotations allows navigation to

advice

• Important visual cue for developers– Not all users have the outline view active

Aspect Visualizer Shows scattering,

tangling, crosscutting

SeeSoft style

Optionally: Look at affect of

particular aspects View at the package or

class level Navigate from affected

lines back to source Even zoom in !

Good Modularity: URL Pattern Matching

• URL pattern matching in org.apache.tomcat– red shows relevant lines of code– nicely fits in two boxes (using inheritance)

Problems: Logging is not modularized*

instances of logging in org.apache.tomcat• red represents lines of code that handle logging • occurences are not in just one place, and • in a significant number of places

Summary: Purpose of AOP

AOP is an extension of OOP AOP does not replace OOP AOP has functions, classes and aspects Make design and code more modular Isolate the concerns for separate

development Separate expression of behavioral concerns

from structural ones Poor composition can lead to interference

The Future of AOP Language Design

Standardization across frameworks Tool Development

IDE support, UML support, aspect discovery, re-factoring, re-cutting, source level debuggers

Dynamic Source Weaving (Wicca - debugs dynamically composed aspect-enabled programs)

Software Engineering Finding aspects that help define modularity in aspects Concern Manipulation Environment (CME)

Open source Eclipse project targeting AO technologies Theory

Consistency in varying compiler interpretation Fast compilation Advanced crosscut constructs

Thank you!

Questions?