/20@yegor256 1
Practical Example of AOP
with AspectJ
Yegor Bugayenko
/20@yegor256 2
Aspect Oriented Programming (AOP)
AspectJ in 2000

by Gregor Kiczales
in Xerox PARC
/20@yegor256 3
class Page {
public String html() {
// load HTML and return
}
}
class Directory {
public void delete() {
//...
/20@yegor256 4
while (true) {
try {
return load_html();
} catch (Exception ex) {
// nothing, just ignore
}
}
/20@yegor256 5
code duplication
/20@yegor256 6
class Page {
@Retry
public String html() {
// load HTML and return
}
}
/20@yegor256 7
@Retention(value=RUNTIME)
@Target(value=METHOD)
public @interface Retry {
}
/20@yegor256 8
@Aspect
class Robustness {
@Around("execution(* *(..)) && @annotation(Retry)")
public Object around(Proceed...
/20@yegor256 9
class Page {
@Retry
public String html() {
// load HTML and return
}
}
@Aspect
class Robustness {
@Around(“...
/20@yegor256 10
binary aspect weaving
javac + ajc
/20@yegor256 11
class Page {
private Robustness r;
public String html() {
return this.r.around(point);
}
public String htm...
/20@yegor256 12
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<executions>
<execution>...
/20@yegor256 13
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency...
/20@yegor256 14
no code duplication
/20@yegor256 15
procedural and clumsy
/20@yegor256 16
@Aspect
class Robustness {
@Around("execution(* *(..)) && @annotation(Retry)")
public Object around(Procee...
/20@yegor256 17
class RobustPage {
private final Page page;
public String html() {
while (true) {
try {
return this.page.ht...
/20@yegor256 18
new RobustPage(new Page());
/20@yegor256 19
a better Java with AOP?
/20@yegor256 20
@yegor256
/20@yegor256 21
class Page {
public String html() around retry() {
// load HTML and return
}
advice Object retry() {
while...
/20@yegor256 22
class Robust<T> decorates T {
advice Object retry() {
while (true) {
try {
return proceed;
} catch (Except...
/20@yegor256 23
page = new Robust<Page>(new Page()) {
@Override
public String html() around retry() {
return origin.html()...
Upcoming SlideShare
Loading in …5
×

Practical Example of AOP with AspectJ

395 views

Published on

JavaDayKyiv 2016; 14 October 2016; Kyiv, Ukraine. Video is here: https://www.youtube.com/watch?v=4SRoLYxvIQ8 (in Russian, with English subtitles)

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
395
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Practical Example of AOP with AspectJ

  1. 1. /20@yegor256 1 Practical Example of AOP with AspectJ Yegor Bugayenko
  2. 2. /20@yegor256 2 Aspect Oriented Programming (AOP) AspectJ in 2000
 by Gregor Kiczales in Xerox PARC
  3. 3. /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 } }
  4. 4. /20@yegor256 4 while (true) { try { return load_html(); } catch (Exception ex) { // nothing, just ignore } }
  5. 5. /20@yegor256 5 code duplication
  6. 6. /20@yegor256 6 class Page { @Retry public String html() { // load HTML and return } }
  7. 7. /20@yegor256 7 @Retention(value=RUNTIME) @Target(value=METHOD) public @interface Retry { }
  8. 8. /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 } } } }
  9. 9. /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
  10. 10. /20@yegor256 10 binary aspect weaving javac + ajc
  11. 11. /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
  12. 12. /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
  13. 13. /20@yegor256 13 <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> </dependency> </dependencies>
  14. 14. /20@yegor256 14 no code duplication
  15. 15. /20@yegor256 15 procedural and clumsy
  16. 16. /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 } } } }
  17. 17. /20@yegor256 17 class RobustPage { private final Page page; public String html() { while (true) { try { return this.page.html(); } catch (Exception ex) { // ignore } } } }
  18. 18. /20@yegor256 18 new RobustPage(new Page());
  19. 19. /20@yegor256 19 a better Java with AOP?
  20. 20. /20@yegor256 20 @yegor256
  21. 21. /20@yegor256 21 class Page { public String html() around retry() { // load HTML and return } advice Object retry() { while (true) { try { return proceed; } catch (Exception ex) { // ignore } } } }
  22. 22. /20@yegor256 22 class Robust<T> decorates T { advice Object retry() { while (true) { try { return proceed; } catch (Exception ex) { // ignore } } } }
  23. 23. /20@yegor256 23 page = new Robust<Page>(new Page()) { @Override public String html() around retry() { return origin.html(); } }

×