AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC •...

42
AspectJ John Sung

Transcript of AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC •...

Page 1: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

AspectJ

John Sung

Page 2: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Presentation Assumptions

• Already know the concepts of AOP

• Know the problem AOP is attempting to solve.

• Have some knowledge of Java

• Familiar with OOD

Page 3: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Outline

• Introduction to AspectJ

• Structure Shyness with Context Passing

• Implementing Branches with AspectJ

• Conceptual Analysis of DJ, AspectJ, Fred

• Master’s Thesis

Page 4: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Introduction to AspectJ

• Developed at Xerox PARC

• Launched in August 1998

• Provide AOP tool to programmers

• Build and support AspectJ community

• Improve AspectJ and AOP

Page 5: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Concepts of AspectJ

• Join Points

• Pointcuts

• Advice

• Aspect

Page 6: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Highlevel View of AspectJ

Java Program

AspectJ

Advice

pointcut advice body

join point

Page 7: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Join Points

• Transition points or boundaries• Method Call

• Class Boundary

• Method Execution

• Access or Modification of member variable

• Exception Handlers

• Static and Dynamic Initialization

Page 8: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Graphical View of Join Points

foo(){x = 0;if (x < 0)

throw KException;}

barint x;

bar1.foo();

Page 9: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Specifying Join Points with Designators

• call(method signature)• handler(exception name)• cflow(join point designator)• this(type name)• target(type name)• within(class name)• execution(method signature)• get(signature), set(signature)• initialization(signature), staticinitialization(type name)

Page 10: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Designators with Wildcards

• call(* foo())

• call(public bar.*(..))

• call(void foo(..))

• call(* *(..))

• call(*.new(int, int))

• handler(File*Exception)

Page 11: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Pointcuts

• Set of join points

• Treats pointcut designators as predicates

• Can use &&, || , and ! predicate modifiers

• Allows parameter passing from pointcut to the advice

Page 12: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Generic Pointcuts

pointcut name(args) : pointcut_designators;

Page 13: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Pointcut Examples

• pointcut MemberRead() : call(* *get*(..)) || call( * *Get*(..));

• pointcut MemberReadOrWrite(): MemberRead() || call(* *set*(..)) || call(* *Set*(..));

• pointcut A1(int t2, String m1): !target(t2) && (call(* c*.foo*(..)) || * get(m1));

Page 14: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Advice

• Action to take when pointcut holds• Advice Forms

• before(param) : pointcut(param) {body}• after(param) : pointcut(param) {body}• after(param) returning []: pointcut(param) {body}• after(param) throwing []: pointcut(param) {body}• type around(param) [throws typelist] :

pointcut(param) {body}

Page 15: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Advice Examples

• before() : MemberRead() {...}

• after() : MemberReadOrWrite() {..}

• before(): MemberRead() && within(Lock) {..}

Page 16: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Parameter Information Flow

Join PointDesignator

Pointcut

Advice AdviceBody

Page 17: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Parameter Example

pointcut MemberRead(int index) :

args(index) &&

(call(*get*(..)) || call(*Get*(..)));

before(int index) : MemberRead(index) {

System.out.println(index);

}

Page 18: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Introduction

• Additions to a class• Data members• Methods• Inheritance and Interface Specification

• Easy to group different concerns within a program into aspects• Structure• Collaborations

Page 19: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Introduction Examples

• public int foo.bar(int x);

• private int foo.counter;

• declare parents: mammal extends animal;

• declare parents: MyThread implements MyThreadInterface;

Page 20: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Aspect

• Encapsulates an aspect

• Looks like a class definition in java

• aspect aspect_name { ... }

• Abstract aspects

Page 21: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Reflection in AspectJ

• thisJoinPoint

• Similar to “this” in Java

• Able to find various information about the join point that triggered the advice.

Page 22: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

thisJoinPoint Example

aspect TraceNonStaticMethods {

before(Point p): target(p) && call(* *(..)) { System.out.println("Entering " + thisJoinPoint + " in " + p);

} }

• thisJoinPoint.toString()

• thisJoinPoint.getArgs()

Page 23: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Structure Shyness with Context Passing

• Need to add functionality to a method

• Need to add context to do it

• Traditional method• change signature of method or overload

• change method body or add method body

Page 24: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

example – context passing

workers need to know the caller:• capabilities• charge backs• to customize result

caller1

caller2

Service

worker 1 worker 3worker 2

Page 25: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

context-passing aspects

workers need to know the caller:• capabilities• charge backs• to customize result

caller1

caller2

Service

worker 1 worker 3worker 2

Page 26: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

context-passing aspects

pointcut invocations(Caller c):this(c) && call(void Service.doService(String));

Page 27: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

context-passing aspects

pointcut invocations(Caller c):this(c) && call(void Service.doService(String));

pointcut workPoints(Worker w):target(w) && call(void Worker.doTask(Task));

Page 28: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

pointcut invocations(Caller c):this(c) && call(void Service.doService(String));

pointcut workPoints(Worker w):target(w) && call(void Worker.doTask(Task));

pointcut perCallerWork(Caller c, Worker w):cflow(invocations(c)) && workPoints(w);

context-passing aspects

Page 29: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

abstract aspect CapabilityChecking {

pointcut invocations(Caller c):this(c) && call(void Service.doService(String));

pointcut workPoints(Worker w):target(w) && call(void Worker.doTask(Task));

pointcut perCallerWork(Caller c, Worker w):cflow(invocations(c)) && workPoints(w);

before (Caller c, Worker w): perCallerWork(c, w) { w.checkCapabilities(c);

}}

context-passing aspects

Page 30: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Branches using AspectJ

• AspectJ have similar concepts as Fred• Predicates on method calls and arguments

• Bodies to be executed

Page 31: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Factorial in Fred

(define-msg fact)

(define-branch (and (eq? (dp-msg dp) fact)(= (car (dp-args dp)) 1))

1)

(define-branch (eq? (dp-msg dp) fact)(let ((x (car (dp-args dp))))

(* x (fact (- x 1)))))

fact

return 1

x

x==1

x-1

*

x $

Page 32: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Branches in AspectJ

AspectJ

FactClass.fact(int x)

BaseAdvice

RecursionAdvice

InitialCall

Java Program

if (x==1) if (x>1) x-1

Page 33: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Branches in AspectJ

class FactClass {}aspect FactExample {

static int factReturn;static int FactClass.fact(int x) {

return factReturn;}pointcut factRecursion(int x):

args(x) && call(static int FactClass.fact(int))&& if (x > 1);

pointcut factBase(int x):args(x) && call(static int FactClass.fact(int))&& if(x==1);

before(int x) : factRecursion(x) {System.out.print("recurse x = ");System.out.println(x);factReturn = x * FactClass.fact(x-1);

}

before(int x) : factBase(x) {System.out.print("base x = ");System.out.println(x);factReturn = 1;

}}

Page 34: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Conceptual Analysis

• Why can I implement Branches in AspectJ?

• Taking a look at DJ, AspectJ and Branches• Describe different concepts used

• Relationships between concepts

• Possible future directions

Page 35: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Conceptual Breakdown

• DJ• Traversals, Advice, Strategy Graph, Class

Graph, Visitor, Incremental Behavior

• AspectJ• Introduction, Predicated Execution, Advice,

Incremental Behavior

• Branches• Incremental Behavior , Predicated Execution

Page 36: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Logical View of Concepts

Predicated Execution

Incremental Behavior

Advice

Traversal Strategy Graph

Class GraphVisitor

Branches

AspectJ

DJ

Introduction

Page 37: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

DJ Conceptual Application

• Concepts applied to data structure

• Concepts have bigger scope• Things are applied to bigger groups

• Incrementally add visitors

Page 38: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

AspectJ Conceptual Application

• Introduction of almost anything within a java class• Allows very flexible grouping of program

• Predicated execution on control flow

• Incrementally add aspects

Page 39: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Branches Conceptual Application

• Each node is a function call

• Each exit from a node is a branch

• Predicated execution based on arguments on a branch

• Introduction of Branches and nodes

• Incrementally add functions and branches

Page 40: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Conceptual Application

Predicated Execution

Introduction

Advice

Traversal

Strategy Graph

Class Graph

Visitor

Branches

AspectJ

DJData Structure

Algorithms

Control Flow

Java Elements

Function Call

Incremental Behavior

Page 41: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

Master’s Thesis

• Attempt to implement AOP concepts using different AOP tools

• Mix different AOP tools together

• Generalized view of AOP tools and concepts

Page 42: AspectJ - Northeastern University · Introduction to AspectJ • Developed at Xerox PARC • Launched in August 1998 • Provide AOP tool to programmers • Build and support AspectJ

AspectJ Resources

• www.AspectJ.org• downloads, documentation, articles, examples

• aosd.net• collection of AO software development