practical example of aop with aspectj
TRANSCRIPT
![Page 1: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/1.jpg)
/20@yegor256 1
Practical Example of AOP with AspectJ
Yegor Bugayenko
![Page 2: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/2.jpg)
/20@yegor256 2
Aspect Oriented Programming (AOP)
AspectJ in 2000by Gregor Kiczales
in Xerox PARC
![Page 3: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/3.jpg)
/20@yegor256 3
class Page { public String html() { // load HTML and return } }
class Directory { public void delete() { // remove all files } }
class File { public long length() { // return file size } }
![Page 4: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/4.jpg)
/20@yegor256 4
while (true) { try { return load_html(); } catch (Exception ex) { // nothing, just ignore } }
![Page 5: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/5.jpg)
/20@yegor256 5
code duplication
![Page 6: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/6.jpg)
/20@yegor256 6
class Page { @Retry public String html() { // load HTML and return } }
![Page 7: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/7.jpg)
/20@yegor256 7
@Retention(value=RUNTIME) @Target(value=METHOD) public @interface Retry { }
![Page 8: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/8.jpg)
/20@yegor256 8
@Aspect class Robustness { @Around("execution(* *(..)) && @annotation(Retry)") public Object around(ProceedingJoinPoint point) { while (true) { try { return point.proceed(); } catch (Exception ex) { // just ignore it } } } }
![Page 9: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/9.jpg)
/20@yegor256 9
class Page { @Retry public String html() { // load HTML and return } }
@Aspect class Robustness { @Around(“…”) public Object around(point) { while (true) { try { return point.proceed(); } catch (Exception ex) { // just ignore it } } } }
Page.class
Robustness.class
![Page 10: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/10.jpg)
/20@yegor256 10
binary aspect weaving
javac + ajc
![Page 11: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/11.jpg)
/20@yegor256 11
class Page { private Robustness r; public String html() { return this.r.around(point); } public String html_aroundBody() { // load HTML and return } }
@Aspect class Robustness { @Around(“…”) public Object around(point) { while (true) { try { return point.proceed(); } catch (Exception ex) { // just ignore it } } } }
Page.class
Robustness.class
![Page 12: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/12.jpg)
/20@yegor256 12
<plugin> <groupId>com.jcabi</groupId> <artifactId>jcabi-maven-plugin</artifactId> <executions> <execution> <goals> <goal>ajc</goal> </goals> </execution> </executions> </plugin>
jcabi-maven-plugin
![Page 13: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/13.jpg)
/20@yegor256 13
<dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> </dependency> </dependencies>
![Page 14: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/14.jpg)
/20@yegor256 14
no code duplication
![Page 15: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/15.jpg)
/20@yegor256 15
procedural and clumsy
![Page 16: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/16.jpg)
/20@yegor256 16
@Aspect class Robustness { @Around("execution(* *(..)) && @annotation(Retry)") public Object around(ProceedingJoinPoint point) { while (true) { try { return point.proceed(); } catch (Exception ex) { // just ignore it } } } }
![Page 17: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/17.jpg)
/20@yegor256 17
class RobustPage { private final Page page; public String html() { while (true) { try { return this.page.html(); } catch (Exception ex) { // ignore } } } }
![Page 18: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/18.jpg)
/20@yegor256 18
new RobustPage(new Page());
![Page 19: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/19.jpg)
/20@yegor256 19
a better Java with AOP?
![Page 20: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/20.jpg)
/20@yegor256 20
@yegor256
![Page 21: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/21.jpg)
/20@yegor256 21class Page { public String html() around retry() { // load HTML and return } advice Object retry() { while (true) { try { return proceed; } catch (Exception ex) { // ignore } } } }
![Page 22: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/22.jpg)
/20@yegor256 22
class Robust<T> decorates T { advice Object retry() { while (true) { try { return proceed; } catch (Exception ex) { // ignore } } } }
![Page 23: Practical Example of AOP with AspectJ](https://reader031.vdocument.in/reader031/viewer/2022022202/587c16c21a28abb5068b4703/html5/thumbnails/23.jpg)
/20@yegor256 23
page = new Robust<Page>(new Page()) { @Override public String html() around retry() { return origin.html(); } }