Aspect-Oriented Programming - Advance Java Web Technology - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Advance Java Web Technology which includes Sockets, Protocols, File Transfer, Mail Transfer Protocol, Internet Protocol, Transmission Control Protocol, Hostnames, Dns Servers, Host Configuration Protocol etc. Key important points are: Aspect-Oriented Programming, Programming Paradigms, INT Denominator, Public Fraction Multiply, Consequences of Crosscutting Code, Join Point, Figure Element Example, Pointcuts, Exception Handler

Typology: Slides

2012/2013

Uploaded on 03/19/2013

dharamnishth
dharamnishth 🇮🇳

2.5

(2)

50 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Aspect-Oriented Programming
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Aspect-Oriented Programming - Advance Java Web Technology - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Aspect-Oriented Programming

Docsity.com

Programming paradigms

  • Procedural programming
    • Executing a set of commands in a given sequence
    • Fortran, C, Cobol
  • Functional programming
    • Evaluating a function defined in terms of other functions
    • Lisp, ML, OCaml
  • Logic programming
    • Proving a theorem by finding values for the free variables
    • Prolog
  • Object-oriented programming (OOP)
    • Organizing a set of objects, each with its own set of responsibilities
    • Smalltalk, Java, C++ (to some extent)
  • Aspect-oriented programming (AOP)
    • Executing code whenever a program shows certain behaviors
    • AspectJ (a Java extension)
    • Does not replace O-O programming, but rather complements it

Docsity.com

Example

  • class Fraction { int numerator; int denominator; ... public Fraction multiply(Fraction that) { traceEnter("multiply", new Object[] {that}); Fraction result = new Fraction( this.numerator * that.numerator, this.denominator * that.denominator); result = result.reduceToLowestTerms(); traceExit("multiply", result); return result; } ... }
  • Now imagine similar code in every method you might want to trace Docsity.com

Consequences of crosscutting code

• Redundant code

  • Same fragment of code in many places

• Difficult to reason about

  • Non-explicit structure
  • The big picture of the tangling isn’t clear

• Difficult to change

  • Have to find all the code involved...
  • ...and be sure to change it consistently
  • ...and be sure not to break it by accident

• Inefficient when crosscuting code is not needed

Adapted from: www.parc.xerox.com/research/csl/projects/ aspectj/downloads/PARC-Workshop-2002.pptDocsity.com

Terminology

• A join point is a well-defined point in the

program flow

• A pointcut is a group of join points

• Advice is code that is executed at a pointcut

• Introduction modifies the members of a class

and the relationships between classes

• An aspect is a module for handling

crosscutting concerns

  • Aspects are defined in terms of pointcuts, advice, and introduction Docsity.com

The Figure Element example

Docsity.com

Join points

• A join point is a well-defined point in the

program flow

  • We want to execute some code (“advice”) each time a join point is reached
  • We do not want to clutter up the code with explicit indicators saying “This is a join point”
  • AspectJ provides a syntax for indicating these join points “from outside” the actual code

• A join point is a point in the program flow

“where something happens”

  • Examples:
    • When a method is called Docsity.com

Pointcuts

• Pointcut definitions consist of a left-hand side

and a right-hand side, separated by a colon

  • The left-hand side consists of the pointcut name and the pointcut parameters (i.e. the data available when the events happen)
  • The right-hand side consists of the pointcut itself

• Example pointcut:

pointcut setter(): call(void setX(int));

  • The name of this pointcut is setter
  • The pointcut has no parameters
  • The pointcut itself is call(void setX(int)) Docsity.com

Example pointcut designators II

• When the target object is of type SomeType

  • target(SomeType)

• When the executing code belongs to class

MyClass

  • within(MyClass)

• When the join point is in the control flow of a

call to a Test's no-argument main method

  • cflow(call(void Test.main()))

Docsity.com

Pointcut designator wildcards

• It is possible to use wildcards to declare

pointcuts:

  • execution(* *(..))
    • Chooses the execution of any method regardless of return or parameter types
  • call(* set(..))
    • Chooses the call to any method named set regardless of return or parameter type
    • In case of overloading there may be more than one such set method; this pointcut picks out calls to all of them Docsity.com

Pointcut designator composition

  • Pointcuts compose through the operations or (“||”), and

(“&&”) and not (“!”)

  • Examples:
    • target(Point) && call(int *())
      • Chooses any call to an int method with no arguments on an instance of Point, regardless of its name
    • call(* *(..)) && (within(Line) || within(Point))
      • Chooses any call to any method where the call is made from the code in Point’s or Line’s type declaration
    • within() && execution(.new(int))
      • Chooses the execution of any constructor taking exactly one int argument, regardless of where the call is made from
    • !this(Point) && call(int *(..))
      • Chooses any method call to an int method when the executing object is any type except Point Docsity.com

Pointcut designators based on modifiers

  • call(public * *(..))
    • Chooses any call to a public method
  • execution(!static * *(..))
    • Chooses any execution of a non-static method
  • execution(public !static * *(..))
    • Chooses any execution of a public, non-static method
  • Pointcut designators can be based on Docsity.com

Kinds of advice

• AspectJ has several kinds of advice; here are

some of them:

  • Before advice runs as a join point is reached, before the program proceeds with the join point
  • After advice on a particular join point runs after the program proceeds with that join point - after returning advice is executed after a method returns normally - after throwing advice is executed after a method returns by throwing an exception - after advice is executed after a method returns, regardless of whether it returns normally or by throwing an exception Docsity.com

Example II, with parameters

• You can access the context of the join point:

  • pointcut setXY(FigureElement fe, int x, int y): call(void FigureElement.setXY(int, int)) && target(fe) && args(x, y);
  • after(FigureElement fe, int x, int y) returning: setXY(fe, x, y) { System.out.println(fe + " moved to (" + x + ", " + y + ")."); }

Docsity.com