34
AspectWerkz 2 - and the road to AspectJ™ 5 Jonas Bonér Senior Software Engineer BEA Systems

AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Embed Size (px)

Citation preview

Page 1: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

AspectWerkz 2 -and the road to AspectJ™ 5

Jonas BonérSenior Software Engineer

BEA Systems

Page 2: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

• Overview• Plain Java™ AOP• Integration • Dynamicity• Extensible Aspect Container• AW Bench

Outline

Page 3: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Overview

• Plain Java™ AOP (syntax is JLS compatible)• Annotation-defined aspects• Integrates well in J2EE environments• Load time, runtime and static weaving• Runtime deployment and undeployment of aspects• Open Source, founded Q4 2002• Performance is comparable to ~ AspectJ™ -XnoInline• Extensible Aspect Container

Page 4: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Overview

• Pointcuts call(..), execution(..), set(..), get(..), cflow(..),

cflowbelow(..), handler(..), staticinitialization(..), within(..), withincode(..), hasfield(..), hasmethod(..), args(..), this(..), target(..)

• Life-cycle singleton, perthis(..), pertarget(..), perclass(..),

percflow(..)

• Intertype declarations (ITD) through Mixins (interface + methods implementing this interface)

• Pointcut pattern language very similar to AspectJ™: <annotation>* <modifier>* <type> <type>.<name>(<type>*)

Page 5: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Plain Java AOP

• JLS compatible• Annotation-defined aspects

– Java 5 @Annotation(…)– Java 1.3/1.4 /** @Annotation(…) */

• Benefits– Does not break IDEs, tools etc.– Gives the impression of flattening out the learning curve

(psychological effect)

• Drawbacks:– JLS compatibility: can not break type-checking, f.e. ITDs– Less elegant and concise syntax

Page 6: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Code-style defined aspect -AspectJ 5

import foo.bar.Baz;

aspect AsynchAspect { private ExecutorService m_threadPool = ...

void around(): execution(void Baz.*(..)) { m_threadPool.execute(new Runnable() { public void run() { try { // proceed the execution in a new thread proceed(); } catch (Throwable e) { // handle exception } } ); } }

Plain Java AOP

Page 7: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

@Aspect class AsynchAspect { private ExecutorService m_threadPool = ...

@Around(“execution(void foo.bar.Baz.*(..))”) void execute(JoinPoint jp) throws Throwable { m_threadPool.execute(new Runnable() { public void run() { try { // proceed the execution in a new thread jp.proceed(); } catch (Throwable e) { // handle exception } } ); } }

Annotation-defined aspect - AspectWerkz Plain Java AOP

Page 8: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

@Aspect class AsynchAspect { private ExecutorService m_threadPool = ...

@Around(“execution(void foo.bar.Baz.*(..))”) void execute(ProceedingJoinPoint jp) throws Throwable { m_threadPool.execute(new Runnable() { public void run() { try { // proceed the execution in a new thread jp.proceed(); } catch (Throwable e) { // handle exception } } ); } }

Annotation-defined aspect - AspectJ 5 Plain Java AOP

Page 9: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

pointcut transactedOps() : execution(public void Account.credit(..)) || execution(public void Account.debit(..)) || execution(public void Customer.setAddress(..)) || execution(public void Customer.addAccount(..)) || execution(public void Customer.removeAccount(..));

Annotation-driven AOPPlain Java AOP

• Annnotion-based pointcuts can – Raise the abstraction level – Be more robust and predictable– Rely on standard annotations

• EJB 3, JSR-250, JSR-181

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

Page 10: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

• Challenges1. Allow the user to have strong typed direct access

to parameter values 2. Should be able to pass them on in the

proceed(..) without wrapping them3. Performance comparable with AspectJ™ 1.2

ChallengesPlain Java AOP

Page 11: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

• Solutions1. Use args(..) for parameter access2. Custom proceed: user defines a custom JoinPoint interface

and defines his own proceed(..)3. Implementation of the interface is generated on the fly

public interface MyJP extends JoinPoint { Object proceed(long[] l, String s);}

@Around(..)Object advice(MyJP jp, long[] l, String s) { ... // modify the values return jp.proceed(l, s);}

ChallengesPlain Java AOP

Page 12: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Integration

• Load time weaving• Deployment modules (aop.xml)• Proxy based weaving• XML-based pointcut definitions

Page 13: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Load-time weaving

• Classes are weaved when loaded• Pre Java 5

– JRockit™ pre-processor– Class loader patching (credit to JMangler)

• Standardized in Java 5– JVMTI (agents)

• Implemented in AspectJ™ 5– Java 5 using JVMTI agent– Java 1.3/1.4 using JRockit™ pre-processor

Integration

Page 14: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Deployment modules

• Need to control which aspects are used in which module (class loader)– Include or exclude aspects higher up in class

loader hierarchy (or in my own module)

=> need for an AOP ‘deployment descriptor’

• Enabled through the aop.xml file• Is put in the META-INF directory in a JAR,

WAR or EAR file• Implemented in AspectJ™ 5

Integration

Page 15: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Proxy-based weaving

• Create a proxy

Target target = (Target)Proxy.newInstance( Target.class

);

• Proxy for target class is created (on the fly) and weaved before returned

• The target instance will now be advised by all the matching aspects (that are found in the class hierarchy)

• Implemented for AspectJ™ 5

Integration

Page 16: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Proxy-based weaving

• Benefits– Very transparent and lightweight– Same performance as static weaving (much

better than all other proxy implementations)– Same aspects and pointcuts can be used – no need for using Type+ etc.– Good enough in many situations in J2EE

environments• Drawbacks

– Only ‘method execution’ pointcuts– Can not advise:

•final methods•private methods

Integration

Page 17: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

XML-based pointcut definitions

• AspectWerkz allows defining the whole aspect in XML (aop.xml)– Is very widely used by users– Had many benefits (f.e. no extra compilation step)– Powerful

• Drawbacks– Sometimes too powerful– Error prone– Hard to keep consistent semantics

• Solved in a better way in AspectJ™ 5– More restricted but guarantees consistent semantics– Still does what most users want; provide definition for

abstract pointcuts (e.g. “late binding”)

Integration

Page 18: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Sample: XML-based pointcut definitions

• Allows extending an abstract aspect• Needs to provide definition for all abstract

pointcuts

<aspectj> <aspects> <concrete-aspect name=“myaspects.DeploymentTimeAspect”

extends=“lib.AbstractLibraryAspect”> <pointcut name=“abstractPointcut” expression=“within(com.biz..*)”/> </concrete-aspect> </aspect></aspectj>

Integration

Page 19: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Dynamicity

• Runtime deployment of aspects (runtime weaving)

• Programmable per-instance deployment of aspects

Page 20: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Runtime weaving

• ”Hot” deployment and undeployment of aspects

• Based on HotSwap API• Atomic ’change sets’• Support for rollback• Resulting code is staticallly woven

Dynamicity

Page 21: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Sample: Runtime weaving

public void enableMonitoring() { Deployer.deploy(ResponseTimeAspect.class);

}

public void disableMonitoring() { Deployer.undeploy(ResponseTimeAspect.class);

}

public void enableMonitoring(String pointcut) { String xmlDef = "<aspect>" + "<pointcut name=‘toMonitor' expression='" + pointcut + "'/></aspect>"; Deployer.deploy(ResponseTimeAspect.class, xmlDef); }

Dynamicity

Page 22: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Challenges

• Same performance for runtime deployed aspects as for regularly woven aspects

• Zero overhead for target code after an aspect has been undeployed

• Ensure consistent and atomic aspect deployments– At specific join point – “all or nothing”

Dynamicity

Page 23: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Architecture

1. JoinPoint abstraction – Generated at runtime– Contains all data and logic for this join point– Has a public method:

public static final invoke(…){…}

– Inlined by VM– Also functions as the “closure” for around advice (can hold state

etc.)

2. Weave in a call to this static method in the target class (replaces the join point “code block”)

3. Can swap the logic in the JoinPoint without affecting the target class

Dynamicity

Page 24: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Limitations

• HotSwap limitations: – New methods can not be added at runtime

(supported in spec. but currently not in any impl.)

– Means that execution pointcuts for around advice needs to be prepared (we add empty method)

• Need wrapper methods to access private methods and fields in target class

Dynamicity

Page 25: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Programmable per-instance deployment

• Advice can be added to specific instance– Runtime deployment – Is pure per-instance (always ”this” instance)

• Target classes must be ‘prepared’ – set to implement the Advisable interface

• Credit to JBoss AOP

• Drawbacks– Introduces a new API

Dynamicity

Page 26: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Sample: Programmable per-instance deployment

• Prepare the classes you might want to advise later

<advisable pointcut-type="call|execution|set|get" expression="within(com.biz..*)"/> • Add advice to a specific instance (only) at runtime

POJO pojo = new POJO();

((Advisable) pojo).aw_addAdvice( "execution(@javax.ejb.TransactionAttribute)", new BeforeAdvice() { public void invoke(JoinPoint jp) { // start a new transaction } } );

Dynamicity

Aspects.aspectOf(TxAspect.class).beginTx(jp);

Page 27: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Extensible aspect container

• Pluggable weaver• Support for different ’aspect models’• Reference implementation supports

– AspectWerkz– Spring AOP– AOP Alliance– AspectJ™ (not complete)

• Dynamicity etc. to all aspect models• Weaver implementations

– ASM– Javassist– JRockit™ VM weaving (prototype)

Overview

Page 28: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Architecture overviewExtensible aspect container

Page 29: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Discussion

• Why did we implement it?1. Proof of concept for the merge with

AspectJ™

2. To ensure correct semantics when different frameworks co-exist (aspect precedence etc.)

3. To give (almost) equally good performance for all the ‘aspect models’

Extensible aspect container

Page 30: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Performance

• Spring aspects are executed – up to 13 times faster when running in the aspect container– Similar for all proxy based implementations

• AspectJ™ aspects are executed– from equally fast (before/after/non-inlined around)– to 6 times slower (inlined around)

• For details see the AW Bench microbenchmark suite

Extensible aspect container

Page 31: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Benefits for AspectJ users

• Production stable load-time weaving– Implemented in AspectJ™ 5

• Runtime deployment of aspects• Can reuse old AspectJ™ pointcut libraries

Extensible aspect container

Page 32: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

AW Bench

• Compares the performance of major (industrial) AOP implementations

– AspectJ, AspectWerkz, JBoss AOP, JAsCo, Spring, dynaop, cglib proxy

• Currently tests:– All advice types (not ITDs)– Different kinds of context exposure (reflective and static)– execution pointcuts and singleton aspects only

• Please contribute – with tests and/or new frameworks

• More info: – http://docs.codehaus.org/display/AW/AOP+Benchmark

Micro-benchmark suite

Page 33: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Links

• Project home pages:– http://aspectwerkz.codehaus.org/– http://eclipse.org/aspectj/

• Articles:– http://www.theserverside.com/articles/article.tss?l=AspectWerkzP1– http://www.theserverside.com/articles/article.tss?l=AspectWerkzP2

• Benchmark:– http://docs.codehaus.org/display/AW/AOP+Benchmark

• AspectJ™ 5 FAQ:– http://dev.eclipse.org/viewcvs/indextech.cgi/~checkout~/aspectj-home/

aj5announce.html

• JRockit™ home page:– http://commerce.bea.com/products/weblogicjrockit/5.0/jr_50.jsp

Page 34: AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems

Questions...?

Thanks for listening