| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Infinite loops</title><link rel="stylesheet" href="aspectj-docs.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.44"><link rel="home" href="index.html" title="The AspectJTM Programming Guide"><link rel="up" href="pitfalls.html" title="Chapter 5. Pitfalls"><link rel="previous" href="pitfalls.html" title="Chapter 5. Pitfalls"><link rel="next" href="quick.html" title="Appendix A. AspectJ Quick Reference"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Infinite loops</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="pitfalls.html">Prev</a> </td><th width="60%" align="center">Chapter 5. Pitfalls</th><td width="20%" align="right"> <a accesskey="n" href="quick.html">Next</a></td></tr></table><hr></div><div class="sect1"><a name="pitfalls-infiniteLoops"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="pitfalls-infiniteLoops"></a>Infinite loops</h2></div></div><p> |
| Here is a Java program with peculiar behavior |
| </p><pre class="programlisting"> |
| public class Main { |
| public static void main(String[] args) { |
| foo(); |
| System.out.println("done with call to foo"); |
| } |
| |
| static void foo() { |
| try { |
| foo(); |
| } finally { |
| foo(); |
| } |
| } |
| } |
| </pre><p> |
| This program will never reach the println call, but when it aborts |
| may have no stack trace. |
| </p><p> |
| This silence is caused by multiple StackOverflowExceptions. First |
| the infinite loop in the body of the method generates one, which the |
| finally clause tries to handle. But this finally clause also |
| generates an infinite loop which the current JVMs can't handle |
| gracefully leading to the completely silent abort. |
| </p><p> |
| The following short aspect will also generate this behavior: |
| </p><pre class="programlisting"> |
| aspect A { |
| before(): call(* *(..)) { System.out.println("before"); } |
| after(): call(* *(..)) { System.out.println("after"); } |
| } |
| </pre><p> |
| Why? Because the call to println is also a call matched by the |
| pointcut <tt>call (* *(..))</tt>. We get no output because |
| we used simple after() advice. If the aspect were changed to |
| </p><pre class="programlisting"> |
| aspect A { |
| before(): call(* *(..)) { System.out.println("before"); } |
| after() returning: call(* *(..)) { System.out.println("after"); } |
| } |
| </pre><p> |
| Then at least a StackOverflowException with a stack trace would be |
| seen. In both cases, though, the overall problem is advice applying |
| within its own body. |
| </p><p> |
| There's a simple idiom to use if you ever have a worry that your |
| advice might apply in this way. Just restrict the advice from occurring in |
| join points caused within the aspect. So: |
| </p><pre class="programlisting"> |
| aspect A { |
| before(): call(* *(..)) && !within(A) { System.out.println("before"); } |
| after() returning: call(* *(..)) && !within(A) { System.out.println("after"); } |
| } |
| </pre><p> |
| Other solutions might be to more closely restrict the pointcut in |
| other ways, for example: |
| </p><pre class="programlisting"> |
| aspect A { |
| before(): call(* MyObject.*(..)) { System.out.println("before"); } |
| after() returning: call(* MyObject.*(..)) { System.out.println("after"); } |
| } |
| </pre><p> |
| The moral of the story is that unrestricted generic pointcuts can |
| pick out more join points than intended. |
| </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pitfalls.html">Prev</a> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right"> <a accesskey="n" href="quick.html">Next</a></td></tr><tr><td width="40%" align="left">Chapter 5. Pitfalls </td><td width="20%" align="center"><a accesskey="u" href="pitfalls.html">Up</a></td><td width="40%" align="right"> Appendix A. AspectJ Quick Reference</td></tr></table></div></body></html> |