advanced aspectj ja aspectj 5 - ut · advanced aspectj reflective api aspektide eelnevus (aspect...

59
Advanced AspectJ ja AspectJ 5.0 Aleksei Bogdanov [email protected] 05. aprill 2006

Upload: others

Post on 25-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Advanced AspectJ ja AspectJ 5.0

Aleksei [email protected]

05. aprill 2006

Page 2: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Kava● AspectJ in Action, peatükk 4

● The AspectJ 5 Development Kit Developer's Notebook

● AOP@Work: AOP and metadata: A perfect match

Page 3: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Advanced AspectJ

Page 4: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Advanced AspectJ

● Reflective API● Aspektide eelnevus (Aspect precedence)● Aspektide seostumine (Aspect association)● Erindite pehmendamine (Exception softening)● Privilegeeritud aspektid (Priveleged aspects)

Page 5: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Reflective API● Liidesed, mis tagavad juurdepääsu ühendpunktiga

seotud dünaamilisele ja staatilisele infole

● Dünaamiline info on kättesaadav ka lõikepunktide this(), target() ja args() kaudu

● Reflective API keerulisem kuid pakub rohkem võimalusi

Page 6: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Ühendpunktiga seotud objektid●thisJoinPointühendpunkti dünaamiline info (käsitletav objekt, sihtobjekt, meetodi argumendid)●thisJoinPointStaticPartühendpunkti staatiline info (signatuur, tüüp, asukoht koodis)●thisEnclosingJoinPointStaticPartümbritseva ühendpunkti staatiline info

Page 7: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception
Page 8: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

org.aspectj.lang.JoinPoint liidese meetodid

● getThis()

● getTarget()

● getArgs()

● getStaticPart()

Page 9: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

org.aspectj.lang.JoinPoint.StaticPart liidese meetodid

● getKind()

● getSignature()

● getSourceLocation()

Page 10: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Reflective API: näide

public class Test { public static void main(String[] args) {

SavingsAccount account = new SavingsAccount(12456);

account.credit(100); }}

Page 11: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

import org.aspectj.lang.*;import org.aspectj.lang.reflect.*;

public aspect JoinPointTraceAspect { private int _indent = -1;

pointcut tracePoints() :!within(JoinPointTraceAspect) && !call(*.new(..)) && !execution(*.new(..)) && !initialization(*.new(..)) && !staticinitialization(*);

before() : tracePoints() {_indent++;println("========= " + thisJoinPoint + " ===========");println("Dynamic join point information:");printDynamicJoinPointInfo(thisJoinPoint);println("Static join point information:");printStaticJoinPointInfo(thisJoinPointStaticPart);println("Enclosing join point information:");printStaticJoinPointInfo(thisEnclosingJoinPointStaticPart);

}

Page 12: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

private void printDynamicJoinPointInfo(JoinPoint joinPoint) {println("This: " + joinPoint.getThis() +

" Target: " + joinPoint.getTarget());StringBuffer argStr = new StringBuffer("Args: ");Object[] args = joinPoint.getArgs();for (int length = args.length, i = 0; i < length; ++i) { argStr.append(" [" + i + "] = " + args[i]);}println(argStr);

}

private void printStaticJoinPointInfo( JoinPoint.StaticPart joinPointStaticPart) {

println("Signature: " + joinPointStaticPart.getSignature()+ " Kind: " + joinPointStaticPart.getKind());

SourceLocation sl = joinPointStaticPart.getSourceLocation();println("Source location: " +

sl.getFileName() + ":" + sl.getLine()); }

Page 13: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

========= call(void Account.credit(float)) ===========Dynamic join point information:This: null Target: SavingsAccount@1ad086aArgs: [0] = 100.0Static join point information:Signature: void Account.credit(float) Kind: method-callSource location: Test.java:4Enclosing join point information:Signature: void Test.main(String[]) Kind: method-executionSource location: Test.java:3

========= execution(void Account.credit(float)) ===========Dynamic join point information:This: SavingsAccount@1ad086a Target: SavingsAccount@1ad086aArgs: [0] = 100.0Static join point information:Signature: void Account.credit(float) Kind: method-executionSource location: Account.java:12Enclosing join point information:Signature: void Account.credit(float) Kind: method-executionSource location: Account.java:12

Page 14: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Aspektide eelnevus

● Võib olla vajalik juhul kui ühele ühendpunktile rakendub rohkem kui üks aspekt

● Eelnevuse spetsifitseerimata on aspektide rakendamise järjekord juhuslik

● AspectJ lubab aspektide eelnevust määrata

Page 15: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Eelnevusreeglid

Page 16: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Aspektide eelnevus: süntaksdeclare precedence : TypePattern1, TypePattern2, ..;

Näited:● declare precedence : AuthenticationAspect,

AuthorizationAspect;● declare precedence : AuthenticationAspect, *;● declare precedence : *, CachingAspect;

Page 17: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Juhiste eelnevus

● Kui ühele ühendpunktile rakendub mitu sama aspekti juhist, siis esimesena rakendub see juhis mis paikneb aspektis eespool

Page 18: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Aspektide seostumine● Üks isend aspektist virtuaalmasina kohta (per

virtual machine) (vaikimisi)● Üks isend aspektist iga objekti kohta (per object)● Üks isend aspektist töövoo kohta (per control-

flow)

● Üks isend aspektist tüübimalli kohta (per type pattern) (AspectJ 5.0)

Page 19: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Aspekti seostumine: süntaksaspect <AspectName> [<association-specifier>(<Pointcut>)] {

... aspect body}

Näide:

public abstract aspect CacheManagementAspect perthis(access()) {

... }

Page 20: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

public aspect AssociationDemoAspect { public AssociationDemoAspect() {

System.out.println("Creating aspect instance"); }

pointcut accountOperationExecution(Account account): (execution(* Account.credit(..)) || execution(* Account.debit(..)))

&& this(account);

before(Account account): accountOperationExecution(account) {System.out.println("JoinPoint: " + thisJoinPointStaticPart

+ "\n\taspect: " + this + "\n\tobject: " + account); }}

Vaikimisi seostumine: näide

Page 21: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

public class TestAssociation { public static void main(String[] args) throws Exception {

SavingsAccount account1 = new SavingsAccount(12245);SavingsAccount account2 = new SavingsAccount(67890);account1.credit(100);account1.debit(100);account2.credit(100);account2.debit(100);

}}

Page 22: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

> ajc *.java> java TestAssociationCreating aspect instanceJoinPoint: execution(void Account.credit(float))

aspect: AssociationDemoAspect@187aecaobject: SavingsAccount@e48e1b

JoinPoint: execution(void Account.debit(float))aspect: AssociationDemoAspect@187aecaobject: SavingsAccount@e48e1b

JoinPoint: execution(void Account.credit(float))aspect: AssociationDemoAspect@187aecaobject: SavingsAccount@12dacd1

JoinPoint: execution(void Account.debit(float))aspect: AssociationDemoAspect@187aecaobject: SavingsAccount@12dacd1

Page 23: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

per-object seostumineKahte tüüpi:● perthis()aspekti isend seostub ühendpunkti objektiga this

argumendiga määratud lõikepunktis● pertarget()aspekti isend seostub ühendpunkti objektiga target

argumendiga määratud lõikepunktisSeostumine toimub hetkel, kui aspekt rakendub

antud objektile esimest korda

Page 24: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

per-object seostumine: näidepublic aspect AssociationDemoAspect perthis(accountOperationExecution(Account)) {

public AssociationDemoAspect() {System.out.println("Creating aspect instance");

}

pointcut accountOperationExecution(Account account): (execution(* Account.credit(..)) || execution(* Account.debit(..)))

&& this(account);

before(Account account): accountOperationExecution(account) {System.out.println("JoinPoint: " + thisJoinPointStaticPart

+ "\n\taspect: " + this + "\n\tobject: " + account); }}

Page 25: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

> ajc *.java> java TestAssociationCreating aspect instanceJoinPoint: execution(void Account.credit(float))

aspect: AssociationDemoAspect@e48e1bobject: SavingsAccount@12dacd1

JoinPoint: execution(void Account.debit(float))aspect: AssociationDemoAspect@e48e1bobject: SavingsAccount@12dacd1

Creating aspect instanceJoinPoint: execution(void Account.credit(float))

aspect: AssociationDemoAspect@1ad086aobject: SavingsAccount@10385c1

JoinPoint: execution(void Account.debit(float))aspect: AssociationDemoAspect@1ad086aobject: SavingsAccount@10385c1

Page 26: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

per-control-flow seostumineKahte tüüpi seostumine:● percflow()

aspekti isend seostub töövooga (control flow) lõikepunkti rahuldavas ühendpunktis

● percflowbelow()aspekti isend seostub ühendpunkti järgneva töövooga lõikepunkti rahuldavas ühendpunktis

Page 27: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

per-control-flow seostumine: näidepublic aspect AssociationDemoAspect percflow(accountOperationExecution(Account)) {

public AssociationDemoAspect() {System.out.println("Creating aspect instance");

}

pointcut accountOperationExecution(Account account): (execution(* Account.credit(..)) || execution(* Account.debit(..)))&& this(account);

before(Account account): accountOperationExecution(account)|| (execution(* Account.setBalance(..)) && this(account)) {System.out.println("JoinPoint: " + thisJoinPointStaticPart

+ "\n\taspect: " + this + "\n\tobject: " + account); }}

Page 28: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

> ajc *.java> java TestAssociationCreating aspect instanceJoinPoint: execution(void Account.credit(float))

aspect: AssociationDemoAspect@10385c1object: SavingsAccount@42719c

JoinPoint: execution(void Account.setBalance(float))aspect: AssociationDemoAspect@10385c1object: SavingsAccount@42719c

Creating aspect instanceJoinPoint: execution(void Account.debit(float))

aspect: AssociationDemoAspect@30c221object: SavingsAccount@42719c

JoinPoint: execution(void Account.setBalance(float))aspect: AssociationDemoAspect@30c221object: SavingsAccount@42719c

Page 29: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Creating aspect instanceJoinPoint: execution(void Account.credit(float))

aspect: AssociationDemoAspect@119298dobject: SavingsAccount@f72617

JoinPoint: execution(void Account.setBalance(float))aspect: AssociationDemoAspect@119298dobject: SavingsAccount@f72617

Creating aspect instanceJoinPoint: execution(void Account.debit(float))

aspect: AssociationDemoAspect@1e5e2c3object: SavingsAccount@f72617

JoinPoint: execution(void Account.setBalance(float))aspect: AssociationDemoAspect@1e5e2c3object: SavingsAccount@f72617

Page 30: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

per-type-pattern seostumine● pertypewithin()

aspekti isend seostub tüübiga, mis rahuldab etteantud tüübimalli

● Kasulik juhul, kui on vaja säilitada olekut iga tüübi jaoks mingist tüüpide hulgast

Näide:

public aspect InstanceTracking pertypewithin(org.xyz..*) {

... }

Page 31: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Erindite pehmendamine● Annab võimaluse käsitleda ühendpunkti poolt

tekitatud kontrollitav erindit (checked exception) kontrollimata erindina (unchecked exception)

● Kaob vajadus erindit töödelda või panna see meetodi spetsifikatsiooni

● Erindite töötlemise ühte kohta koondamine

Page 32: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Erindi pehmendamine: süntaksdeclare soft : <ExceptionTypePattern> :

<pointcut>;

Näide:

declare soft: RemoteException : call(* Foo.*(..));

Page 33: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Erindi pehmendamine: näideimport java.rmi.RemoteException;

public class TestSoftening { public static void main(String[] args) {

TestSoftening test = new TestSoftening();test.perform();

}

public void perform() throws RemoteException {throw new RemoteException();

}}

public aspect SofteningTestAspect { declare soft : RemoteException : call(void TestSoftening.perform());}

Page 34: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

import java.rmi.RemoteException;

public class TestSoftening {public static void main(String[] args) {

TestSoftening test = new TestSoftening();try {

test.perform();} catch (RemoteException ex) {

throw new SoftException(ex);}

}

public void perform() throws RemoteException {throw new RemoteException();

}}

Page 35: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Privilegeeritud aspektid● Annavad võimaluse Java juurdepääsu reeglitest

üle saada● Juurdepääs klassi private väljadele

Süntaks:

privileged public aspect PrivilegeTestAspect {

...

}

Page 36: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

AspectJ 5

Page 37: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Mis on AspectJ 5?

● Olulised muudatused keeles ja vahendites

● Java 5 toetamine

● AspectJ võimaluste laiendamine

Page 38: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

AspectJ 5

● Annotatsioonid (Annotations)

● Klassimallid (Generics)

● per-type-pattern seostumine

● Laadimisaegne põimimine (load-time weaving)

● Muud võimalused

Page 39: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Annotatsioonid● Programmi liikmete metainfo väljendamiseks● Saab rakendada pakettide ja tüüpide

deklaratsioonidele, konstruktoritele, meetoditele, väljadele, parameetritele ja muutujatele

Näide:@Authenticated(role="supervisor")

public void someMethod() {...}

Page 40: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Annotatsioonid ja AspectJ

● AspectJ lubab lisada annotatsioone aspektidele, meetoditele, klassi- ja isendiväljadele, konstruktoritele, parameetritele ja juhistele

● Võimalus kasutada annotatsioone ühendpunkti sobitamisel

Page 41: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Elementide mallid● Nime järgi: @<qualified-name>

@Immutable

@Foo @Goo

● Malli järgi: @(<type-pattern>)

@(Foo || Goo)

@(org.xyz..*)

Page 42: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Tüüpide mallid

● (@Immutable *)

● ((@Immutable Foo+) || Goo)

● (@Immutable @NonPersistent org.xyz..*)

Page 43: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Signatuuride mallid

● @SensitiveData * *

● @SensitiveData List org.xyz..*.*

● @Oneway * *(..)

● * *.*(@Immutable *,..)

Page 44: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Dünaamiline kontekst● @this● @target● @args● @within● @withincode● @annotation

call(* *(..)) && @target(Classified)

Page 45: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Näited● within(@Secure *)● call(@Oneway * *(..))● set(@Cachable * *)● pointcut insideCriticalMethod(Critical c) :

@withincode(c);● declare parents : (@Secured *) implements

SecuredObject;● declare precedence : (@Security *),*;

Page 46: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Annotatsioonide kasutamine● Metaandmete esitamine

@Transactional(kind=Required)public void credit(float amount) {...}

● Lihtsamini hallatavad lõikepunktidpublic pointcut transactedOps()

: execution(@Transactional * *.*(..));

Page 47: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Annotatsioonide kasutamine● Annotatsiooniga varustamine (supplying annotation)

declare annotation : * Account.*(..) : @Authenticated(permission="banking");

● Annotatsioonide tarbimine (consuming annotation)

pointcut transactedOps() : execution(@Transactional * *.*(..));

Page 48: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Mitmedimensionaalne signatuur

Page 49: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

AspectJ annotatsioonid● @Aspect

public class Foo {}● @Aspect("perthis(execution(* abc..*(..)))")

public class Foo {}● @Pointcut("call(* *.*(..))")

void anyCall() {}● @Before("call(* org.aspectprogrammer..*(..)) &&

this(Foo)") public void callFromFo{ System.out.println("Call from Foo"); }

Page 50: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Klassimallid (generics)

public interface List<E> { Iterator<E> iterator(); void add(E anItem); E remove(E anItem); }

Page 51: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Klassimallide parameetrid

● class Foo<T> {...}

● class Foo<T,S> {...}

● class Foo<T extends Number> {...}

● class Foo<T extends Number & Comparable> {...}

Page 52: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Klassimallide väärtustamine

● List<String>

● List<?>

● List<? extends Number>

● List<? super Double>

Page 53: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Klassimallid ja AspectJ● Klassimallide tugi lõikepunktide ja aspektide

defineerimisel

● Võimalus luua abstraktsete aspektide klassimalle

● Sobitamine lõikepunkti avaldises toimub tüübi kustutamise (erasure) põhimõttel.

Page 54: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Näideclass C {

public void foo(List<? extends Number> listOfSomeNumberType) {}

public void bar(List<?> listOfSomeType) {}

public void goo(List<Double> listOfDoubles) {}

}

Page 55: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

● execution(* C.*(List))

● execution(* C.*(List<?>))

● execution(* C.*(List<? extends Object+>))

● args(List<Double>)

Page 56: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Aspektide klassimallid (generic aspects)

● Saab defineerida ainult abstraktsete aspektide jaoks

Näide: public abstract aspect

ParentChildRelationship<P,C> { ... }

Page 57: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

● Lisaks AspectJ tugi muudele uuendustele:

– Autoboxing ja unboxing

– Varargs

– Covariance

– Loendid (enumerations)

Page 58: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Laadimisaegne põimimine (load-time weaving)

● Põimimine lükatakse edasi kuni klassi laadimiseni virtuaalmasinasse

● "weaving class loader"

Page 59: Advanced AspectJ ja AspectJ 5 - ut · Advanced AspectJ Reflective API Aspektide eelnevus (Aspect precedence) Aspektide seostumine (Aspect association) Erindite pehmendamine (Exception

Küsimused