advanced aspectj & aspectj 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf ·...
TRANSCRIPT
![Page 2: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/2.jpg)
Outline
Aspect Instantiation (customization)
Aspect Inheritance (reuse)
AspectJ 5 for Java 5
Generics
Annotations
Annotation-based style
2
![Page 3: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/3.jpg)
Aspect instantiation
3
![Page 4: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/4.jpg)
Aspect Instantiation
Similar to classes, aspects can have instance data
Advice is executed in the context of an instance
However, the instantiation is done by AspectJ:no new
public aspect Profiling { int count = 0;
pointcut toProfile(): execution(public * *(..));after(): toProfile() { count++;}
}
4
![Page 5: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/5.jpg)
Instantiation Strategies
AspectJ has predefined instantiation strategies, selected with an aspect modifier
Three main kinds of strategies:
Singleton: one, global aspect instance (default)
Per-object: one aspect instance per object
Per-control-flow: one aspect instance per complete join point
5
![Page 6: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/6.jpg)
Singleton Aspects
Modifier may be omitted
Base program can reach the global instance using:Profiling.aspectOf()
public aspect Profiling issingleton() { int count = 0;
pointcut toProfile(): execution(public * *(..));after(): toProfile() {
count++;}
}
System.out.println(Profiling.aspectOf().count);
6
![Page 7: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/7.jpg)
Per-Object Aspects (1)Modifiers: perthis(Pointcut) or pertarget(Pointcut)
At each join point selected by the Pointcut, an aspect instance is set-up for advice execution
One aspect instance is maintained per executing object (resp. receiving object)
Warning: an advice is only applied at a join point if an aspect instance is set-up for that join point
Advice pcs should be ‘included’ in modifier pc7
![Page 8: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/8.jpg)
Per-Object Aspects (2)
Modifier can use named pointcuts from aspect body
Aspect instances reachable from base code usingProfiling.aspectOf(obj)
public aspect Profiling perthis(toProfile()) { int count = 0;
pointcut toProfile(): execution(public * (Client || Server).*(..));after(): toProfile() {
count++;}
}
System.out.println({Profiling.aspectOf(client1).count,Profiling.aspectOf(server2).count
});
8
![Page 9: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/9.jpg)
Per-Control-Flow Aspects (1)Modifiers: percflow(Pointcut) or percflowbelow(Pointcut)
At (resp. below) each join point selected by the Pointcut, an aspect instance is set-up
The aspect instance is maintained during the entire control-flow of the join point
Again: an advice is only applied at a join point if an aspect instance is set-up for that join point
Advice pcs should lie within cflow of modifier pc9
![Page 10: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/10.jpg)
Per-Control-Flow Aspects (2)
Aspect instances reachable from base code, but only within the relevant control flow, usingProfiling.aspectOf()
public aspect Profiling percflow(major()) { int count = 0;
pointcut toProfile(): execution(public * *(..));after(): toProfile() {
count++;}pointcut major(): execution(void Server.processRequest(..));after(): major() {
System.out.println(count);}
}
10
![Page 11: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/11.jpg)
AspectJ Instantiation: Evaluation
Instantiation modifiers allows essentially the same advice to function very differently (good)
Although explicit instantiation could be more powerful (bad)
Not limited to predefined strategies
Constructor can take configuration arguments
No need for the aspectOf method, which only works when there is some global key
11
![Page 12: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/12.jpg)
Aspect Inheritance and Reuse
12
![Page 13: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/13.jpg)
Pointcut ReuseNamed pointcuts are static members
Can be referenced from other aspects (if access modifiers allow it)
Common idiom: aspect that only defines pointcuts, to share among
public aspect ProgramPoints {public pointcut main(): execution(public * be.ac.vub..*.*(..));public pointcut expensive(): execution(* (be.ac.vub.Calculator || be.ac.vub.Renderer).*(..));
}
public aspect Profiling {after(): ProgramPoints.expensive() { ... }
}
13
![Page 14: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/14.jpg)
Advice Reuse
Reusable advice is defined using an abstract pointcut
The advice is used by providing a definition for this pointcut
public abstract aspect Profiling {int count = 0;abstract pointcut toProfile();after(): toProfile() { count++; }
}
public aspect MyProfiling extends Profiling {pointcut toProfile(): ProgramPoints.expensive();
}
14
![Page 15: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/15.jpg)
abstract RulesCan be declared abstract:
Named pointcuts and methods (no implementation)
Aspects (not instantiable, not applied / woven)
Subaspects can provide implementations for abstract members
When an aspect has abstract members left, it must itself be declared abstract
15
![Page 16: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/16.jpg)
Inheritance RulesAn aspect can implement one or more interfaces, or extend a class or another abstract aspect
Still only argumentless constructor
An aspect cannot extend a concrete aspect, classes cannot extend aspects
Concrete aspect is directly instantiated (woven)
Classes could be explicitly instantiated16
![Page 17: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/17.jpg)
AspectJ 5
17
![Page 18: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/18.jpg)
AspectJ 5 (2005)
AspectJ release with support for the new features of Java 5 (2004)
Generics
Annotations
(Variable arguments)
(Autoboxing)
18
![Page 19: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/19.jpg)
Aside: Generics
19
![Page 20: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/20.jpg)
Subtype Polymorphism
output(o) { write(o.toString());}output(book);output(person);
“output” accepts different types
These types share a contract
20
![Page 21: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/21.jpg)
Parametric Polymorphism
choose(a,b) { if(cond.holds()) return a; else return b;}choose(book1,book2).read();choose(pers1,pers2).marry();
“choose” accepts parameters of any type
Types of arguments and return value are related
21
![Page 22: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/22.jpg)
interface Stringable { String toString();}void output(Stringable o) {...}
class Book implements Stringable{...}output(book);
Typing Subtype PM
Classes inherit from a common supertype
Represents the contract
22
![Page 23: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/23.jpg)
Object choose(Object a,Object b){...}((Book)choose(book1,book2)).read();
Typing Parametric PMGeneral typing does not express type relations
Requires casts
Allows violations by callee and caller
Object choose(Object o1,Object o2) {return anything;}((Book)choose(pers,book)).read();
23
![Page 24: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/24.jpg)
Idea: Parametric Typing
Type variable can be declared to capture the type relations
Substitution of a type value
No casts required
<T> T choose(T a, T b){...}
<Book>choose(book1,book2).read();
24
![Page 25: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/25.jpg)
Java 5 Generics
Type variables at the level of classes, bound per instantiation
Generic containers: killer app for parametric polymorphism
Type variables at the level of methods, bound per invocation
(Mechanisms to interact with subtype polymorphism)
Type variables and arguments are erased after compilation
interface List<E> {E get(int index);void add(int index, E element);
}
void process(List<String> l) {l.get(0).startsWith(“foo”); //OKl.add(1, new Object()) //Err
}
25
![Page 26: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/26.jpg)
Generics in AspectJ 5 (1)
Goal 1: Coping with Generics in the base language
Type patterns can match generic types and their instantiations
(Quite complex because of erasuresee AspectJ 5 developer notebook for details)
Inter-type declarations can introduce new generic members
26
![Page 27: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/27.jpg)
Generics in AspectJ 5 (2)
Goal 2: Generic aspects
Aspects declare type variables
When used as type annotations, very similar to generic classes
abstract aspect GenericTracer<T> {abstract pointcut trace(T t);abstract void report(String d, T t);before(T t): trace(t) { report(“Entering”,t); }after (T t): trace(t) { report(“Leaving”,t); }
}
aspect A extends GenericTracer<Server> {pointcut trace(Server s):
execution(public Server.*(..)) && this(s);void report(String d, Server s) {
System.out.println(d + “at” + s.getName());}
}
27
![Page 28: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/28.jpg)
Generics in AspectJ 5 (3)Type vars can also be used in the type patterns of pointcuts and ITDs
New kind of aspect reuse, based on types
Reuse of aspect behavior by simply providing type(s)
interface Observer { void notify(); }abstract aspect ObserverProtocol<Subject> {
Collection<Observer> Subject.observers;void Subject.notifyObservers() {
for(Observer o: observers)o.notify(); }
after(Subject s): this(s) && execution(void Subject.set*(..)) { s.notifyObservers(); }
}aspect MyObserverInstance extends ObserverProtocol<Server> { } // No body
28
![Page 29: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/29.jpg)
abstract / Inheritance Rule
Caveat: type variables can only be declared for abstract aspects
Reason: implicit instantiation
Type variable of generic class is normally provided at instantiation
29
![Page 30: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/30.jpg)
Annotations
30
![Page 31: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/31.jpg)
Annotations
Metadata (=structured extra information)
Added by the programmer to source tree nodes (classes, methods, fields,...)
Predefined annotations understood by javace.g. @Override
Other annotations can be inspected through reflection
31
![Page 32: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/32.jpg)
Annotations
Good pointcut (observer protocol):set(* Point.*)
Fragile pointcut (caching):call(String X.a())||call(Object Y.b*(..))||call(* C.*(*)) ..
Solution: annotations@Cachable
32
![Page 33: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/33.jpg)
Caching
Annotations to tag methods that could be cached@Cachable(timeToLive=500)
pointcut enableCaching(Cachable c) : call(* *(..)) && @annotation(c) && if(c.timeToLive()>treshold);
33
![Page 34: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/34.jpg)
Annotations& Pointcuts
Pointcuts can reason on the presence of annotationsExamples:
within(@Secure *)
call(@Cachable *.*(..))
handler(!@Catastrophic *)
34
![Page 35: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/35.jpg)
Annotations& Pointcuts
Allows new style of aspect application based on semantic tagsSolves possibly fragile pointcuts
e.g. execution(* a()) || execution (* b()) .....
becomes call(@Cachable *.*(..))
Base code is no longer obliviousBUT base code is no longer oblivious!
35
![Page 36: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/36.jpg)
Library Aspects
AspectJ libraries with reusable aspects.Weg Isberg’s library of 30+ aspectsInvariant Example: idempotent methods
Methods that should only run once
Use annotations to tag themAnnotation defines semantic of method, not aspect specific
Developer takes into account this semantics
36
![Page 37: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/37.jpg)
Idempotent Methodpublic abstract aspect IdempotentMethod pertarget(idempotentMethod()){! protected pointcut idempotentMethod();!! declare error : idempotentMethod() && execution(!void *(..)) :! ! "Idempotent methods must return void";! declare error : idempotentMethod() && execution(* *(*, ..)) :! ! "Idempotent methods must not take an argument";!! boolean hit;! void around() : idempotentMethod() {! ! if (!hit) {! ! ! proceed();! ! ! hit = true;! ! }! }}static aspect IdempotentMethodTest extends IdempotentMethod {!
protected pointcut idempotentMethod() : execution(@Idempotent * *(..));}//Example methodpublic class MyBeanFactory {
@Idempotent public void init() {....}...
}
37
![Page 38: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/38.jpg)
Const
public aspect Const {!! declare error : withincode(@ReadOnly * *(..)) && set(* *) ! ! : "no set in read-only method";!! declare error : withincode(@ReadOnly * *(..)) && call(!@ReadOnly * *(..)) ! ! : "no calls to non-read-only methods inside read-only method";!! declare error : set(@ReadOnly * *) && !withincode(new(..)) ! ! : "no writes to read-only fields outside of constructors";! declare error : within(@ReadOnly *) && call(!@ReadOnly * *(..)) ! ! : "no calls to non-read-only methods inside read-only classes";! declare error : within(@ReadOnly *) && set(* *) ! ! : "no sets inside read-only classes";! // this is duplicative - omit or reinforce the point?! declare error : within(@ReadOnly *) && execution(!@ReadOnly * *(..)) ! ! : "all methods in read-only classes should be read-only";}
Introduce Const-like feature in Java using aspects:
38
![Page 39: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/39.jpg)
Other invariants
Re-throw VM errorNaming conventions (e.g. fields start with “f”)No calls from production into test packagesForces getter-setter methodsNo System.out or System.errNo null parametersEnforce singleton pattern
39
![Page 40: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/40.jpg)
Annotation-based development style
40
![Page 41: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/41.jpg)
Annotation-based development style
AspectJ aspects in pure JavaUses annotations to specify non-Java constructsWeaving can happen at load-time or at linking timeAdvantages:
A java compiler suffices
Integrates better with existing toolchain/IDEThere is no official new language
41
![Page 42: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/42.jpg)
Example
The following AspectJ pointcut:pointcut anyCall() : call(* *.*(..));
Translates to:@Pointcut("call(* *.*(..))") void anyCall() {}
42
![Page 43: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/43.jpg)
Pointcuts//Pointcut arguments:@Pointcut("call(* *.*(int)) && args(i) && target(callee)")void someCall(int i, Foo callee) {}
//If pointcut:@Pointcut("call(* *.*(int)) && args(i) && if()")public static boolean someCallWithIfTest(int i) { return i > 0;}
//Abstract pointcut@Pointcut("")protected abstract void toOverrideInSubclass();
43
![Page 44: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/44.jpg)
Advice//Simple Advice@Before("execution(* *.*(..))")void anyExec() {
logger.info(“Something happened”);}
//Advice with arguments@After("execution(* *.*(..)) && target(myTarget)")public void anyExec(Object myTarget) {
logger.info(“Something happend on “+ myTarget);}
//Advice with joinpoint reflection@After("execution(* *.*(..)) && target(myTarget)")public void anyExec(JoinPoint thisJoinPoint, Object myTarget) {
logger.info(thisJoinPoint.getSignature()+“ happened on “+ myTarget);
}
44
![Page 45: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/45.jpg)
Advice (2)//Around Advice@Around(“execution(@Idempotent void *.*(..))”)public Object skipMethod(ProceedingJoinPoint thisJoinPoint) {
Object result = null;if(!hasAlreadyExecuted) { hasAlreadyExecuted=true; result = thisJoinPoint.proceed();}return result;
}
45
![Page 46: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/46.jpg)
Advice (3)
//Pointcut-Advice Binding@Pointcut("call(* *.*(..)) && @annotation(info) && if()”)protected static boolean cachableMethods(Cachable info) {
return info.timeToLive()>treshold;}
@Around(“cachableMethods(info)”)public Object cache(ProceedingJoinPoint thisJoinPoint, Cachable info) {
Object result = getFromCache(thisJoinPoint);if(result==null) { result= thisJoinPoint.proceed(); storeInCache(thisJoinPoint,result,info.timeToLive());}return result;
}
46
![Page 47: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/47.jpg)
Declare Statements//Declare Warning/Error@DeclareWarning("call(* javax.sql..*(..)) && !within(org.xyz.daos..*)")static final String aMessage = "Only DAOs should be calling JDBC.";
@DeclareError("execution(* IFoo+.*(..)) && !within(org.foo..*)")static final String badIFooImplementors = "Only foo types can implement IFoo"
//Declare Precedence@DeclarePrecedence("Security*, org.xyz.TransactionSupport, org.xyz.Persistence")public class SystemArchitecture {}
47
![Page 48: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/48.jpg)
Inter-type declarations
//Declare Marker Interface@DeclareParents("org.xyz.domain..*")Serializable implementedInterface;
//Declare Parentspublic interface IDGen{ public long getId(); }public class IDGenImpl implements IDGen { .... }
@DeclareParents(value="xy..*",defaultImpl=org.IDGenImpl.class)private IDGen implementedInterface;
@Before("execution(* *.*(..)) && this(m)")void feelingMoody(IdGen m) { System.out.println("this is my id ”+m.getId());}
Only supports injecting interfaces (aka mixins)
48
![Page 49: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/49.jpg)
Aspects@Aspectpublic class MyLoggingAspect { .... }
//Controlling Instantiation@Aspect("perthis(execution(* abc..*(..)))")public class MyLoggingAspect {}
49
![Page 50: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/50.jpg)
Limitations
Types have to be fully referenced in pointcutsNOT: @Pointcut("call(* List.*(..))")
BUT: @Pointcut("call(* java.util.List.*(..))")
Limited intertype declarationsNo privileged aspects
50
![Page 51: Advanced AspectJ & AspectJ 5ssel.vub.ac.be/ssel/_media/teaching/courses/aosd/aspectj5.pdf · AspectJ 5 for Java 5 Generics Annotations Annotation-based style 2. Aspect instantiation](https://reader035.vdocument.in/reader035/viewer/2022062917/5ece4fd5265e064ba52745e9/html5/thumbnails/51.jpg)
OverviewElement Becomes with Annotation
Aspect Class @Aspect
Named pointcut Method @Pointcut
Advice Method @Before, @After, @Around,...
Inter-type parent declaration Field @DeclareParents
Error/warning declaration Static final String field @DeclareWarning,
@DeclareError51