0% found this document useful (0 votes)
116 views20 pages

And How It Is Used by Data Federator Designer Team

AOP is a programming paradigm that increase the modularity of a software system by allowing the separation of crosscutting concerns. AspectJ is a Java framework implementing the AOP programming paradigm Eclipse Public License since 2001 can be used standalone or using AJDT (AspectJ Development Tools)

Uploaded by

bogoromo3628
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views20 pages

And How It Is Used by Data Federator Designer Team

AOP is a programming paradigm that increase the modularity of a software system by allowing the separation of crosscutting concerns. AspectJ is a Java framework implementing the AOP programming paradigm Eclipse Public License since 2001 can be used standalone or using AJDT (AspectJ Development Tools)

Uploaded by

bogoromo3628
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Introduction to AspectJ

(and how it is used by Data Federator Designer Team)

Data Federator Designer Team


19.02.2009
Agenda

 AOP introduction
– AOP-the big picture
– AOP vocabulary
 AspectJ introduction
– AspectJ Pointcut Model
– Dynamic crosscutting
– Static crosscutting
– @AspectJ syntax
 Examples
– Designer validity check framework
– Designer locking mechanism
– Source Definer open/close aspect
– Policy enforcement
– Type-hierarchy modifications

© SAP 2009 / Page 2


AOP-the big picture(1/2)

 What is a concern.
 A specific requirement that a software system must address
in order to satisfy the overall system goals.
 What is a core concern.
 A core concern captures the central functionality of a
module.
 What is a crosscutting concern.
 A crosscutting concern captures requirements that cross
multiple modules.
 Symtoms of crosscutting concerns:
– Code tangling
– Code scattering
 What is AOP.
 programming paradigm that increase the modularity of a
software system by allowing the separation of crosscutting
concerns.

© SAP 2009 / Page 3


AOP-the big picture(2/2)

Implementation of a security concern using Implementation of a security concern using AOP techniques.
conventional techniques.

© SAP 2009 / Page 4


AOP vocabulary (1/2)

 Join Point
 an identifiable point in the execution of the system.
 Pointcut
 A set of join points that satisfies a criteria.
 The pointcat can collect the join points context.
 Implementing a crosscutting concern will require selecting a specific join point.
 Dynamic crosscutting
 Construct to alter the program behavior.
 Contains 2 parts; a pointcut to select join points and an advice to provide behavior
before, after or around the selected join points.
 Static crosscutting
 Construct to alter the static structure of the system.
 Aspect
 Module containing all the crosscutting constructs and so will contain all the crosscutting
logic.
 Weaving
 Introduction of the crosscutting logic/constructs into the target modules.

© SAP 2009 / Page 5


AOP vocabulary (2/2)

A more synthetic view of the AOP vocabulary

© SAP 2009 / Page 6


AspectJ introduction

 Java framework implementing the AOP programming paradigm

 Eclipse Public License since 2001

 Can be used standalone or using AJDT (AspectJ Development Tools)

© SAP 2009 / Page 7


AspectJ Pointcut Model (1/3)

Category Code it represents Example

Method The invocation of the call(public *Account.*(..))


method.
call(public void Account.*() )

call(* *(@RequestParam *))


The method body. execution(<MethodSignature>)

Constructor The invocation of the call(public Account.new())


constructor.
The constructor body. execution(<ConstructorSignature>)

Field access The access to read a field. get(public static !final * Account+.*)

The access to write a field. set(static final *ajia.domain..*.*)

Exception The catch block to handle an within(MyClass) && handler(IOException)


processing exception.

© SAP 2009 / Page 8


AspectJ Pointcut Model (2/3)

Category Code it represents Example

Initialization The loading of a class, including staticinitialization(MyClass)


the initialization of the static
portion.
The initialization of an object initialization(ConstructorSignature)
starting from the return of a
parent class’s constructor until
the end of the first called
constructor.

The initialization of an object preinitialization(ConstructorSignature)


from the first called constructor
to the beginning of its parent
constructor.

Advice The execution of any advice in adviceexecution()


the system.

© SAP 2009 / Page 9


AspectJ Pointcut Model (3/3)

Category Code it represents Example

Control-flow Join point in control flow or below cflow(call(* Account.debit(..))


of those selected by the specified
pointcut cflow(execution(Account.new(..))

cflowbelow(<Pointcut>)
Lexical Select join points occurring inside within(Account)
Structure the lexical scope of specified
based classes, aspects, and methods. traced() && !within(TraceAspect)

withincode(*Account.debit(..))

Execution Pointcuts select join points that this(Account)


object match either the type of this, which
is the current execution object, or target(Account)
the target object, which is the
object on which the method is
being called.

© SAP 2009 / Page 10


Dynamic crosscutting(1/2)

AspectJ supports dynamic crosscutting through advice that defines crosscutting


action at the join points selected by a pointcut.

Advices types:
 Before advice
 Executed before the execution of the captured join point
 After advice
 Executed after the execution of the join point
 After (finally) – regardless of the outcome
– after() : call(* Account.*(..)) {......}
 After returning normally
– after() returning : call(* Account.*(..)) {...}
– after( ) returning(Object value) : callPointCut( ) { /*use the value*/}
 After throwing an exception
– after() throwing : execution(* Controller+.*(..)) {...}
– after() throwing (Throwable ex) : execution(* * (..)){...}

© SAP 2009 / Page 11


Dynamic crosscutting(2/2)

 Around advice
 Surrounds the advised join points
 Can execute the join point with the same or different context any number (including 0
times)
 Can bypass the advised join point completely or execute the join poit with a different
context
 Example
public aspect AroundAdviceRecipe {
pointcut callBazPointCut( ) : call(int MyClass.baz( ));

int around( ) : callBazPointCut( ) && !within(AroundAdviceRecipe +){


…….
return proceed( );
}
}

© SAP 2009 / Page 12


Static crosscutting

 Inter-type declarations
 Member introduction
– Declare the new methods and attributes to be added to the existing class within the
aspect.
 Type-hierarchy modifications
– Introduce a new inheritance relationship between classes.
– Add an interface to a class.
 Annotation supplementation
– Add annotations to the advised join points.
 Weave-time error and warning declarations
 extend the capabilities of the compiler to enforce application specific rules (see the
examples).
 Exception softening
 Specify a set of exceptions that should be converted to uncaught exceptions when raised
on the join points selected by a specific pointcut.

© SAP 2009 / Page 13


@AspectJ syntax

 Supported since AspectJ5 version


 Need Java5 or later

© SAP 2009 / Page 14


Examples – Designer validity check framework

 The goal
 Verify the validity of the name attribute on the domain objects.
 How to use it
@ValidityCheck
public void setName(String name) {…}
or
@ValidityCheck(
checkerClass = TableNameChecker.class,
messageKey = ValidityCheckMessages
.VALIDITY_EXCEPTION_NOT_VALID_TABLE)
public void setName(String name) {...}
 How it’s working
@Aspect
public class ValidityCheckerAspect {

@Pointcut("execution(@ValidityCheck * *(..))")
void validityCheckPointcutOnMethod() {}

@Before("validityCheckPointcutOnMethod()")
public void verifyValidationCheckOnMethod(JoinPoint thisJoinPoint) {…}

© SAP 2009 / Page 15


Examples – Designer locking mechanism

 The goal
 Lock the projects opened/created by a user.
 Remove the lock when the project is closed/deleted.
 Verify if a user can open/save/delete/import a project.
 How it’s working
@Aspect
public class LockingServiceAspect {
@Pointcut("execution(public void com.bobj.datafederator.designer.domain."
+ "ProjectDaoService.save(..)) && args(p)")
void projectSavePointcut(Project p) {}

@Around("projectSavePointcut(project)")
public Object projectSaveJoinAdvice(Project project, ProceedingJoinPoint jp)throws Throwable
{…}

@Pointcut("execution(public void com.bobj.datafederator.designer.domain.aa."


+ "DFUser.closeProject(..)) && args(projectId)")
void dfUserCloseProjectPointcut(String projectId) {}

@AfterReturning("dfUserCloseProjectPointcut(projectId)")
public void dfUserCloseProjectAdvice(String projectId) {…}

© SAP 2009 / Page 16


Examples – Source Definer open/close aspect

 The goal
 Any operation should be forbidden on a closed DataFederatorSourceDefiner
 How it‘s working
public aspect SourceDefinerStateAspect {
pointcut isClosed(DataFederatorSourceDefiner b) :
execution (public !static * DataFederatorSourceDefiner+.* (..))
&& target(b) && !execution(public void open(..)) && !execution(public void close(..))
&& !execution(public boolean isClosed());

before(DataFederatorSourceDefiner b) throws DefinerStateException : isClosed(b) {



}
}

© SAP 2009 / Page 17


Example – Policy enforcement (static
crosscutting example)

 The goal
 extend the capabilities of the compiler to enforce application specific rules
– No System.out.* in the code
– No non final public fields
 How it’s working

@Aspect

public class PolicyEnforcement {

@DeclareWarning("get(public static final java.io.PrintStream java.lang.System.out)")

public static final String NO_SYS_OUT = "No use of System.out allowed";

@DeclareWarning( "get(public !final * *) || set(public * *)")

public static final String NO_PUBLIC_FIELDS = "Don't use public fields";

© SAP 2009 / Page 18


Example – Type-hierarchy modifications
(static crosscutting example)

 The goal
 Add a new interface to a class and inject the interface implementation also.
 How it’s working
@Aspect
final class ObserverAspect {   
/**
     * the classes annotated with the
     * {@link com.google.code.aoplib4j.aspectj.gof.observer.Subject} annotation
     * will automatically
     * implement the {@link GofSubject} interface and the implementation
     * will be {@link GofSubjectImpl}.
     */
    @DeclareParents(
            value = "@com.google.code.aoplib4j.aspectj.gof.observer.Subject *",
            defaultImpl = GofSubjectImpl.class)
    private GofSubject subject = null;

© SAP 2009 / Page 19


Thank you!

© SAP 2009 / Page 20

You might also like