aspectwerkz dynamic aop for java david rabinowitz
Post on 19-Dec-2015
215 views
TRANSCRIPT
![Page 1: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/1.jpg)
AspectWerkzDynamic AOP For Java
David Rabinowitz
![Page 2: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/2.jpg)
March 3rd, 2004 Object Oriented Design Course 2
Aspect Oriented Programming
Cross-cutting concerns•System wide common handling
•Security, logging, persistence, synchronization Advices
•The additional code applied to the existing model
Point-cuts•Where the advice should be applied
Aspects•Combinations of point-cuts and advices
![Page 3: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/3.jpg)
March 3rd, 2004 Object Oriented Design Course 3
Current AOP Implementations
Focusing on Java•Other implementations for .NET, C++,
Ruby, Smalltalk and more AspectJ
•Extend the Java language itself
•Introduce the notion of an aspect is another type of class
![Page 4: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/4.jpg)
March 3rd, 2004 Object Oriented Design Course 4
Java AOP Implementations
Other implementations•Work within Java
•Dynamic proxies, reflection, bytecode manipulation, custom class loading
•Aspect, advice and point-cut are plain Java classes
Several implementations:•AspectWerkz
• JBossAOP
•Spring framework
•And more
![Page 5: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/5.jpg)
March 3rd, 2004 Object Oriented Design Course 5
AspectWerkz
A dynamic, lightweight AOP framework for Java
Allows adding aspects to new code as well to existing applications
Advices can be plain Java objects (POJO)
http://aspectwerkz.codehaus.org/
![Page 6: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/6.jpg)
March 3rd, 2004 Object Oriented Design Course 6
Example Advice
public Object myAroundAdvice(JoinPoint joinPoint) throws Throwable {
// do stuff Object result = joinPoint.proceed(); // do more stuff return result;}
![Page 7: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/7.jpg)
March 3rd, 2004 Object Oriented Design Course 7
JoinPoint
A well-defined point in the program flow
Provides information on:•The signature of the code
(method/field/…)
•RTTI informationJoinPoint.proceed() – telling the
program to continue
![Page 8: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/8.jpg)
March 3rd, 2004 Object Oriented Design Course 8
Advice types
Around advice • Invoked "around" the join point
After advice• Invoked after the join point
Before advice• Invoked before the join point
JoinPoint.proceed() can only be called in around advices, otherwise you have infinite loops
![Page 9: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/9.jpg)
March 3rd, 2004 Object Oriented Design Course 9
Definitions
There are two aspect definition models•External XML file
•Javadoc-like attributes Using XML
•… is using well known and supported format
•… is more complex, no refactoring
![Page 10: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/10.jpg)
March 3rd, 2004 Object Oriented Design Course 10
Using Attributes
Aspect are self contained Puts the definition and
implementation in single class
•Makes it easy to build reusable aspect libraries
![Page 11: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/11.jpg)
March 3rd, 2004 Object Oriented Design Course 11
Point-cut Types (1) execution(<method or constructor pattern>)
• picks out join points defining method (static or member) or constructor execution
call(<method or constructor pattern>)• picks out join points defining method (static or
member) or constructor call get/set(<field pattern>)
• picks out join points defining field access/modification.
Valid advice for these pointcuts are around , before and after
![Page 12: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/12.jpg)
March 3rd, 2004 Object Oriented Design Course 12
Point-cut Types (II)
handler(<exception class pattern>)•picks out join points definining a catch
clause. Valid advice for this pointcut is before
![Page 13: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/13.jpg)
March 3rd, 2004 Object Oriented Design Course 13
Point-cut selection pattern language
Defines where To Add the Advice * wildcard
•One package level or one method parameter
•At least one character (package name)
•Zero or more characters (otherwise) .. Wildcard
•Any sequence of characters that start and end with a ".",
•org.codehaus..* will match all classes in all subpackages starting from org.codehaus
![Page 14: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/14.jpg)
March 3rd, 2004 Object Oriented Design Course 14
How To Choose Classes? foo.bar.* - will match foo.bar.FooBar2 as
well as foo.bar.FooBear
foo.*.FooBar - will match foo.bar.FooBar2 as well as foo.bear.FooBar but not foo.bear.FooBear
foo.*.FooB* - will match foo.bar.FooBar2 as well as foo.bear.FooBear as well as foo.bear.FooB
![Page 15: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/15.jpg)
March 3rd, 2004 Object Oriented Design Course 15
How To Choose Fields?
int foo.*.Bar.m_foo - will match int m_foo but not int s_foo or long m_foo
* foo.*.Bar.m_foo - will match int m_foo as well as java.lang.String m_foo
java.lang.* foo.*.Bar.m_foo - will match java.lang.String m_foo as well as java.lang.StringBuffer m_foo
![Page 16: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/16.jpg)
March 3rd, 2004 Object Oriented Design Course 16
How To Choose Methods? (I)
int foo.*.Bar.method() - will match int method() but not int method(int i)
int *.method(*) - will match int Foo.method(int i) but not int Foo.method()
![Page 17: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/17.jpg)
March 3rd, 2004 Object Oriented Design Course 17
How To Choose Methods? (II)
int foo.*.*.method(*,int) - will match int method(String s, int i) as well as int method(int i1, int i2)
int foo.*.Bar.method(..) - will match int method() as well as int method(String s, int i) as well as int method(int i, double d, String s, Object o)
![Page 18: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/18.jpg)
March 3rd, 2004 Object Oriented Design Course 18
How To Choose Methods? (III)
There are many more examples Variation of return type, method
name and parameters Check
http://aspectwerkz.codehaus.org/ definition_issues.html
![Page 19: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/19.jpg)
March 3rd, 2004 Object Oriented Design Course 19
What About Constructors?
Use “new” as method name foo.*.Bar.new() - will match new Bar() but not new Bar(int i)
*.new(String) - will match new Foo(String name) and new Bar(String name) but not new Foo()
![Page 20: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/20.jpg)
March 3rd, 2004 Object Oriented Design Course 20
Subtyping
As we saw, the pattern * foo.Bar.*(..) will match any method declared in foo.Bar
What about foo.Bar2 who extend foo.Bar?
The pattern is * foo.Bar+.*(..)
![Page 21: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/21.jpg)
March 3rd, 2004 Object Oriented Design Course 21
Point-cut composition
Using the logical operators &&, ||, ! and ()
execution(* foo.bar.Baz.*(..)) || call(* foo.bar.Baz.*(..))
(set(* foo.bar.*) || get(* foo.bar.*)) && cflow(* foo.bar.Buzz.(..))
handler(java.lang.Exception+) && !cflow(* foo.bar.Buzz.(..))
![Page 22: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/22.jpg)
March 3rd, 2004 Object Oriented Design Course 22
Example Code/** @Aspect perInstance */public class LoggingAspect extends Aspect {
}
/** @Call * foo.bar.*.*(..) */Pointcut logMethodCall;
/** @Execution * foo.baz.*.*(..) */Pointcut logMethodExecution;
/** @Before logMethodCall */public void logEntry(JoinPoint joinPoint) { ... } /** @After logMethodCall */public void logExit(JoinPoint joinPoint) { ... } /** @Around logMethodExecution */public Object logExecution(JoinPoint joinPoint) { ... }
![Page 23: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/23.jpg)
March 3rd, 2004 Object Oriented Design Course 23
Equivalent XML (I)
<aspectwerkz> <system id="sample"> <aspect class="examples.LoggingAspect"> <pointcut name="logMethodCall" expression="call(* foo.bar.*.*(..))"/> <pointcut name="logMethodExecution" expression="execution(* foo.baz.*.*(..))"/>
![Page 24: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/24.jpg)
March 3rd, 2004 Object Oriented Design Course 24
Equivalent XML (II) <advice name="logEntry"
type="before" bind-to="logMethodCall"/> <advice name="logExit" type="after" bind-to="logMethodCall"/> <advice name="logExecution" type="around" bind-to="logMethodExecution"/> </aspect> </system></aspectwerkz>
![Page 25: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/25.jpg)
March 3rd, 2004 Object Oriented Design Course 25
Caching Example/** * @Around execution(int
examples.caching.Pi.getPiDecimal(int)) */public Object cache(final JoinPoint joinPoint) throws Throwable { MethodRtti rtti = (MethodRtti)joinPoint.getRtti(); final Long hash = new Long(calculateHash(rtti)); final Object cachedResult = m_cache.get(hash); if (cachedResult != null) { return cachedResult; } final Object result = joinPoint.proceed(); m_cache.put(hash, result); return result;}
![Page 26: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/26.jpg)
March 3rd, 2004 Object Oriented Design Course 26
Exception Handling Examplepublic class ExceptionHandlingAspect {
/** * @Before handler(java.lang.Exception) */ public void logEntry(final JoinPoint joinPoint) throws Throwable { CatchClauseRtti rtti = (CatchClauseRtti)joinPoint.getRtti(); Exception e = (Exception)rtti.getParameterValue(); System.out.println( "[From advice] exception catched:" + e.toString()); }}
![Page 27: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/27.jpg)
March 3rd, 2004 Object Oriented Design Course 27
How To Use It?
How do we add aspect to existing code?
There are two modes:•Online – “using” AspectWerkz as the
virtual machine (instead of java)
•Offline – the class file are post-processed and the aspects are added
![Page 28: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/28.jpg)
March 3rd, 2004 Object Oriented Design Course 28
Offline mode (I)
Command line tool:aspectwerkz -offline <definition file> <options> [-cp <classpath>]* <target to transform>+
![Page 29: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/29.jpg)
March 3rd, 2004 Object Oriented Design Course 29
Offline mode (II)
Instead of aspectwerkz you can write “java org.codehaus.aspectwerkz. compiler.AspectWerkzC”
Why this is important?•Ant task
•More about it in the “introduction to ant” tirgul
![Page 30: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/30.jpg)
March 3rd, 2004 Object Oriented Design Course 30
Where can you find it?
~ood/aspectwerkz Set the ASPECTWERKZ_HOME
environment variable Add $ASPECTWERKZ_HOME/lib to the
classpath Add $ASPECTWERKZ_HOME/bin to PATH
![Page 31: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/31.jpg)
March 3rd, 2004 Object Oriented Design Course 31
Start Early
It is a complex framework Don’t wait to exercise 5 to learn it There many examples in ~ood/aspectwerkz/src/samples •run them and see that you understand
them Next week
•How do we easily add the post process to the build process?
![Page 32: AspectWerkz Dynamic AOP For Java David Rabinowitz](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d3b5503460f94a15fee/html5/thumbnails/32.jpg)
March 3rd, 2004 Object Oriented Design Course 32
More Information
http://aspectwerkz.codehaus.org/ http://www.onjava.com/pub/a/
onjava/2004/01/14/aop.html