brief introduction into aspect oriented programming (aop) · limited modularity in object-oriented...

28
Motivation AOP AspectJ Summary Brief Introduction into Aspect Oriented Programming (AOP) Anette Lang 1 Florian Kronenberg 2 1 University of Bonn 2 RWTH Aachen 30-06-07 / XP Know-How Meeting Anette Lang, Florian Kronenberg AOP

Upload: others

Post on 23-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Brief Introduction into Aspect OrientedProgramming (AOP)

Anette Lang1 Florian Kronenberg2

1University of Bonn

2RWTH Aachen

30-06-07 / XP Know-How Meeting

Anette Lang, Florian Kronenberg AOP

Page 2: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Outline

1 MotivationLimited Modularity in Object-Oriented ProgrammingLogging as a typical example for Scattering and Tangling

2 Aspect-Oriented ProgrammingModularisation of Cross-cutting ConcernsAOP Building Blocks

3 AspectJOverviewExample: Have Fun Logging with AspectJAdvice Pointcut Types

Anette Lang, Florian Kronenberg AOP

Page 3: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Our Goals.

efficient software development requires modularity, alsocalled separation of concernshistory of encapsulating logically related source code intofunctional modules:binary code⇒ assembler language (instructions)⇒ Procedural Programming (procedures)⇒ Object Oriented Programming (classes)modules provide a certain functionality throughwell-defined interfaces offering the following advantages:

changes can be done locallysystem structure is understandablemaintainance is easymodules are reusable

Anette Lang, Florian Kronenberg AOP

Page 4: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Limitations of object oriented programming (OOP)

natural decomposition of core concerns (components)into modules [e. g. use cases, classes]some concerns do not fit into this scheme: they are stillpart of many different modules

Anette Lang, Florian Kronenberg AOP

Page 5: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Limitations of object oriented programming (OOP)(contd.)

using OOP:Scattering: code implementing one crosscutting concern(aspect) is spread throughout the whole source code [e. g.logging]Tangling: code implementing crosscutting concerns isinterwoven with code implementing the local module⇒ code implementing crosscutting concerns cannot beeasily encapsulated by using means of OOP [tyranny of thedominant decomposition]⇒ the larger the project and the many crosscuttingconcerns occur, the more redundant, less readable, lessmaintainable and less reusable the code implementingcrosscutting concernsinteresting for XP: everyone is allowed to change every partof a project’s source code – everyone should be able tounderstand itAnette Lang, Florian Kronenberg AOP

Page 6: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Logging in Tomcat

Figure: Logging in Tomcat

Bad modularization of concern logging.Code referring to logging is scattered across mostpackages.

Anette Lang, Florian Kronenberg AOP

Page 7: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Example for Scattering and TanglingLogging

Have a look at some methods in ProjectDoALot. . .

public void doSomething(){logger.trace(enter doSomething());//execute doSomething()int i=1;logger.trace(”leave doSomething()”);}public void doSomethingElse(){logger.trace(enter doSomethingElse());//execute doSomethingElse()int i=2;logger.trace(”leave doSomethingElse()”);}

Anette Lang, Florian Kronenberg AOP

Page 8: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Example for Scattering and TanglingLogging

Have a look at some methods in ProjectDoALot. . .

public void doSomething(){logger.trace(enter doSomething());//execute doSomething()int i=1;logger.trace(”leave doSomething()”);}public void doSomethingElse(){logger.trace(enter doSomethingElse());//execute doSomethingElse()int i=2;logger.trace(”leave doSomethingElse()”);}

Anette Lang, Florian Kronenberg AOP

Page 9: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Example for Scattering and TanglingLogging

Have a look at some methods in ProjectDoALot. . .

public void doSomething(){logger.trace(enter doSomething());//execute doSomething()int i=1;logger.trace(”leave doSomething()”);}public void doSomethingElse(){logger.trace(enter doSomethingElse());//execute doSomethingElse()int i=2;logger.trace(”leave doSomethingElse()”);}

Anette Lang, Florian Kronenberg AOP

Page 10: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Example for Scattering and TanglingLogging

Have a look at some methods in ProjectDoALot. . .

public void doSomething(){logger.trace(enter doSomething());//execute doSomething()int i=1;logger.trace(”leave doSomething()”);}public void doSomethingElse(){logger.trace(enter doSomethingElse());//execute doSomethingElse()int i=2;logger.trace(”leave doSomethingElse()”);}

Anette Lang, Florian Kronenberg AOP

Page 11: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Example for Scattering and TanglingLogging

public void doSomethingCompletelyElse(){logger.trace(enter doSomethingElse());//execute doSomethingCompletelyElse()int i=2;logger.trace(”leave doSomethingCompletelyElse()”);

}

logging instructions are scattered through the methodsin each method logging instructions and functional codeare tangled

⇒ AOP might help. . .

Anette Lang, Florian Kronenberg AOP

Page 12: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Example for Scattering and TanglingLogging

public void doSomethingCompletelyElse(){logger.trace(enter doSomethingElse());//execute doSomethingCompletelyElse()int i=2;logger.trace(”leave doSomethingCompletelyElse()”);

}

logging instructions are scattered through the methodsin each method logging instructions and functional codeare tangled

⇒ AOP might help. . .

Anette Lang, Florian Kronenberg AOP

Page 13: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Limited Modularity in OOPLogging with Scattering and Tangling

Example for Scattering and TanglingLogging

public void doSomethingCompletelyElse(){logger.trace(enter doSomethingElse());//execute doSomethingCompletelyElse()int i=2;logger.trace(”leave doSomethingCompletelyElse()”);

}

logging instructions are scattered through the methodsin each method logging instructions and functional codeare tangled

⇒ AOP might help. . .Anette Lang, Florian Kronenberg AOP

Page 14: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

ModularisationBuilding Blocks

Modularisation of crosscutting concerns

regard the first decomposition as a 2-dimensional sceningx-axis: program code parts (e. g. methods)y-axis: modules

enlarge the model by using a third dimensionz-axis: aspects(x,y): possible Join Pointsf(x,y,z): additional or different code implementing aspect z,that is executed at Join Point (x,y) (advice){(x,y)|f(x,y,z) is defined}: Pointcut

concerns

Aspekt

Figure: Aspects as a 3rd dimension of decomposition .Anette Lang, Florian Kronenberg AOP

Page 15: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

ModularisationBuilding Blocks

Definitions

Joinpointmodel of where to apply cross-cutting concerns to classeslocation in the source code, where aspect implementingcode can be executedlogging-example: method call

Pointcutmeans to identify joinpointsset of Joinpoints, where an advice implementing an aspectis executedlogging-example: set of all method calls in the project:ProjectDoALot.*(..)

Advicemeans to influence structure and behavior at joinpointsimplementation of behavior to be executed upon incurring ajoinpointlogging-example: System.out.println(. . . )

Anette Lang, Florian Kronenberg AOP

Page 16: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

ModularisationBuilding Blocks

AspectModularization of a Cross-cutting concerncontains pointcuts and advices

Weavermerges core concerns and aspects into a functionalsystem.

Anette Lang, Florian Kronenberg AOP

Page 17: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Overview of AspectJ

Aspect-Oriented Programming Languages?But: We are familiar with OO-languages such as JavaWe use it for the implementation of core concerns.We want to specify aspects without learning a whole newprogramming language.

AspectJAspect-oriented extension to JavaJava-like syntax for aspect definitionsAspectJ-compiler (ajc) compiles AspectJ aspects andoptionally weaves them with Java classesWeaving is supported at compile-time (by ajc) or atload-time (by a Java agent)Powerful language for the definition of pointcutsTool-Support: tight integration with eclipse.

Anette Lang, Florian Kronenberg AOP

Page 18: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Overview of AspectJ

Aspect-Oriented Programming Languages?But: We are familiar with OO-languages such as JavaWe use it for the implementation of core concerns.We want to specify aspects without learning a whole newprogramming language.

AspectJAspect-oriented extension to JavaJava-like syntax for aspect definitionsAspectJ-compiler (ajc) compiles AspectJ aspects andoptionally weaves them with Java classesWeaving is supported at compile-time (by ajc) or atload-time (by a Java agent)Powerful language for the definition of pointcutsTool-Support: tight integration with eclipse.

Anette Lang, Florian Kronenberg AOP

Page 19: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Example in AspectJLogging

public aspect Tracing{pointcut traceCall(): call(* ProjectDoALot.*(..));

before(): traceCall() {System.out.println(” enter ” + thisJoinPoint + ”\”);

}

after(): traceCall()}System.out.println(” leave ” + thisJoinPoint + ”\”);

}

public void doSomething(){//execute doSomething()int i=1;

}

Anette Lang, Florian Kronenberg AOP

Page 20: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Example in AspectJLogging

public aspect Tracing{pointcut traceCall(): call(* ProjectDoALot.*(..));

before(): traceCall() {System.out.println(” enter ” + thisJoinPoint + ”\”);

}

after(): traceCall()}System.out.println(” leave ” + thisJoinPoint + ”\”);

}

public void doSomething(){//execute doSomething()int i=1;

}

Anette Lang, Florian Kronenberg AOP

Page 21: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Example in AspectJLogging

public aspect Tracing{pointcut traceCall(): call(* ProjectDoALot.*(..));

before(): traceCall() {System.out.println(” enter ” + thisJoinPoint + ”\”);

}

after(): traceCall()}System.out.println(” leave ” + thisJoinPoint + ”\”);

}

public void doSomething(){//execute doSomething()int i=1;

}

Anette Lang, Florian Kronenberg AOP

Page 22: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Basic Advice Types

before: Execute advice behavior before the joinpointafter: Execute advice behavior after the joinpointaround: Control proceeding of the joinpoint. Behavior canbe added before and after joinpoint. Access to the context(e.g. parameters, return values).

Method Call

Execution Time

before after

around

Anette Lang, Florian Kronenberg AOP

Page 23: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Pointcuts

defined by pointcut expressions in the AspectJ pointcutlanguagecomposed from

primitive pointcutsparameters (e.g. types, wildcards)boolean operators (for combination)

Guess what happens with these examples?:

target(Point) && call(int *())

call(* *(..)) && (within(Line)|| within(Point))

within(*) && execution(*.new(int))

!this(Point) && call(int *(..))

Anette Lang, Florian Kronenberg AOP

Page 24: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Pointcuts

defined by pointcut expressions in the AspectJ pointcutlanguagecomposed from

primitive pointcutsparameters (e.g. types, wildcards)boolean operators (for combination)

Guess what happens with these examples?:

target(Point) && call(int *())

call(* *(..)) && (within(Line)|| within(Point))

within(*) && execution(*.new(int))

!this(Point) && call(int *(..))

Anette Lang, Florian Kronenberg AOP

Page 25: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Pointcuts

defined by pointcut expressions in the AspectJ pointcutlanguagecomposed from

primitive pointcutsparameters (e.g. types, wildcards)boolean operators (for combination)

Guess what happens with these examples?:

target(Point) && call(int *())

call(* *(..)) && (within(Line)|| within(Point))

within(*) && execution(*.new(int))

!this(Point) && call(int *(..))

Anette Lang, Florian Kronenberg AOP

Page 26: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

Pointcuts

defined by pointcut expressions in the AspectJ pointcutlanguagecomposed from

primitive pointcutsparameters (e.g. types, wildcards)boolean operators (for combination)

Guess what happens with these examples?:

target(Point) && call(int *())

call(* *(..)) && (within(Line)|| within(Point))

within(*) && execution(*.new(int))

!this(Point) && call(int *(..))

Anette Lang, Florian Kronenberg AOP

Page 27: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

OverviewExample: Logging with AspectJAdvice Pointcut Types

A Selection of Primitive Pointcuts

call(Signature): Aufruf einer Methodeexecution(Signature): AusfÃ1

4hrung einer Methodewithin(Type): Innerhalb einer Klasse/Typget(Signature): Lesender Zugriff auf ein Feldset(Signature): Schreibender Zugriff auf ein Feld

Anette Lang, Florian Kronenberg AOP

Page 28: Brief Introduction into Aspect Oriented Programming (AOP) · Limited Modularity in Object-Oriented Programming Logging as a typical example for Scattering and Tangling 2 Aspect-Oriented

MotivationAOP

AspectJSummary

Summary

Aspect-Oriented Programming (AOP) is more powerfulthan OOP in the Separation of Concerns.AOP helps modularizing Cross-Cutting Concerns.AspectJ brings AOP to the Java World

Anette Lang, Florian Kronenberg AOP