You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

implementation.adoc 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. [[implementation]]
  2. = Implementation Notes
  3. == Compiler Notes
  4. The initial implementations of AspectJ have all been compiler-based
  5. implementations. Certain elements of AspectJ's semantics are difficult
  6. to implement without making modifications to the virtual machine, which
  7. a compiler-based implementation cannot do. One way to deal with this
  8. problem would be to specify only the behavior that is easiest to
  9. implement. We have chosen a somewhat different approach, which is to
  10. specify an ideal language semantics, as well as a clearly defined way in
  11. which implementations are allowed to deviate from that semantics. This
  12. makes it possible to develop conforming AspectJ implementations today,
  13. while still making it clear what later, and presumably better,
  14. implementations should do tomorrow.
  15. According to the AspectJ language semantics, the declaration
  16. [source, java]
  17. ....
  18. before(): get(int Point.x) { System.out.println("got x"); }
  19. ....
  20. should advise all accesses of a field of type `int` and name `x` from
  21. instances of type (or subtype of) `Point`. It should do this regardless of
  22. whether all the source code performing the access was available at the
  23. time the aspect containing this advice was compiled, whether changes
  24. were made later, etc.
  25. But AspectJ implementations are permitted to deviate from this in a
  26. well-defined way -- they are permitted to advise only accesses in _code
  27. the implementation controls_. Each implementation is free within certain
  28. bounds to provide its own definition of what it means to control code.
  29. In the current AspectJ compiler, _ajc_, control of the code means having
  30. bytecode for any aspects and all the code they should affect available
  31. during the compile. This means that if some class `Client` contains code
  32. with the expression `new Point().x` (which results in a field get join point at runtime), the
  33. current AspectJ compiler will fail to advise that access, unless
  34. `Client.java` or `Client.class` is compiled as well. It also means that join
  35. points associated with code in native methods (including their execution
  36. join points) cannot be advised.
  37. Different join points have different requirements. Method and
  38. constructor call join points can be advised only if _ajc_ controls the
  39. bytecode for the caller. Field reference or assignment join points can
  40. be advised only if _ajc_ controls the bytecode for the "caller", the code
  41. actually making the reference or assignment. Initialization join points
  42. can be advised only if _ajc_ controls the bytecode of the type being
  43. initialized, and execution join points can be advised only if _ajc_
  44. controls the bytecode for the method or constructor body in question.
  45. The end of an exception handler is underdetermined in bytecode, so _ajc_
  46. will not implement after or around advice on handler join points.
  47. Similarly, _ajc_ cannot implement `around` advice on `initialization` or
  48. `preinitialization` join points. In cases where _ajc_ cannot implement
  49. advice, it will emit a compile-time error noting this as a compiler
  50. limitation.
  51. Aspects that are defined `perthis` or `pertarget` also have restrictions
  52. based on control of the code. In particular, at a join point where the
  53. bytecode for the currently executing object is not available, an aspect
  54. defined `perthis` of that join point will not be associated. So aspects
  55. defined `perthis(Object)` will not create aspect instances for every
  56. object unless `Object` is part of the compile. Similar restrictions apply
  57. to `pertarget` aspects.
  58. Inter-type declarations such as `declare parents` also have restrictions
  59. based on control of the code. If the bytecode for the target of an
  60. inter-type declaration is not available, then the inter-type declaration
  61. is not made on that target. So, `declare parents : String implements MyInterface`
  62. will not work for `java.lang.String`, unless `java.lang.String` is part of the compile.
  63. When declaring members on interfaces, the implementation must control
  64. both the interface and the top-level implementors of that interface (the
  65. classes that implement the interface but do not have a superclass that
  66. implements the interface). You may weave these separately, but be aware
  67. that you will get runtime exceptions if you run the affected top-level
  68. classes without the interface as produced by the same _ajc_
  69. implementation. Any intertype declaration of an `abstract` method on an
  70. interface must be specified as `public`, you will get a compile time error
  71. message indicating this is a compiler limitation if you do not specify
  72. `public`. A non-`abstract` method declared on an interface can use any
  73. access modifier except protected. Note that this is different to normal
  74. Java rules where all members declared in an interface are implicitly
  75. `public`. Finally, note that one cannot define `static` fields or methods on
  76. interfaces.
  77. When declaring methods on target types, only methods declared `public` are
  78. recognizable in the bytecode, so methods must be declared `public` to be
  79. overridden in any subtype or to be called from code in a later compile
  80. using the target type as a library.
  81. Other AspectJ implementations, indeed, future versions of _ajc_, may
  82. define _code the implementation controls_ more liberally or
  83. restrictively, so long as they comport with the Java language. For
  84. example, the `call` pointcut does not pick out reflective calls to a
  85. method implemented in
  86. `java.lang.reflect.Method.invoke(Object, Object[])`. Some suggest that
  87. the call "happens" and the call pointcut should pick it out, but the
  88. AspectJ language shouldn't anticipate what happens in code outside the
  89. control of the implementation, even when it is a well-defined API in a
  90. Java standard library.
  91. The important thing to remember is that core concepts of AspectJ, such
  92. as the join point, are unchanged, regardless of which implementation is
  93. used. During your development, you will have to be aware of the
  94. limitations of the _ajc_ compiler you're using, but these limitations
  95. should not drive the design of your aspects.
  96. == Bytecode Notes
  97. [[the-class-expression-and-string-plus]]
  98. === The `.class` expression and `String` `+`
  99. The java language form `Foo.class` is implemented in bytecode with a
  100. call to `Class.forName` guarded by an exception handler catching a
  101. `ClassNotFoundException`.
  102. The java language `+` operator, when applied to `String` arguments, is
  103. implemented in bytecode by calls to `StringBuffer.append`.
  104. In both of these cases, the current AspectJ compiler operates on the
  105. bytecode implementation of these language features; in short, it
  106. operates on what is really happening rather than what was written in
  107. source code. This means that there may be call join points to
  108. `Class.forName` or `StringBuffer.append` from programs that do not, at
  109. first glance, appear to contain such calls:
  110. [source, java]
  111. ....
  112. class Test {
  113. void main(String[] args) {
  114. System.out.println(Test.class); // calls Class.forName
  115. System.out.println(args[0] + args[1]); // calls StringBuffer.append
  116. }
  117. }
  118. ....
  119. In short, the join point model of the current AspectJ compiler considers
  120. these as valid join points.
  121. === The `handler()` join point
  122. The end of exception handlers cannot reliably be found in Java bytecode.
  123. Instead of removing the `handler` join point entirely, the current AspectJ
  124. compiler restricts what can be done with the `handler` join point:
  125. * `after` and `around` advice cannot apply to `handler` join points.
  126. * The control flow of a `handler` join point cannot be detected.
  127. The first of these is relatively straightforward. If any piece of `after`
  128. advice (returning, throwing, or "finally") would normally apply to a
  129. `handler` join point, it will not in code output by the current AspectJ
  130. compiler. A compiler warning is generated, whenever this is detected to
  131. be the case. `before` advice is allowed.
  132. The second is that the control flow of a `handler` join point is not
  133. picked out. For example, the following pointcut
  134. [source, java]
  135. ....
  136. cflow(call(void foo()) || handler(java.io.IOException))
  137. ....
  138. will capture all join points in the control flow of a call to
  139. `void foo()`, but it will _not_ capture those in the control flow of an
  140. `IOException` handler. It is equivalent to `cflow(call(void foo()))`. In
  141. general, `cflow(handler(Type))` will not pick out any join points, the
  142. one exception to this is join points that occur during the execution of
  143. any before advice on the handler.
  144. This does not restrict programs from placing before advice on handlers
  145. inside _other_ control flows. This advice, for example, is perfectly
  146. fine:
  147. [source, java]
  148. ....
  149. before(): handler(java.io.IOException) && cflow(void parse()) {
  150. System.out.println("about to handle an exception while parsing");
  151. }
  152. ....
  153. A source-code implementation of AspectJ (such as AspectJ 1.0.6) is able
  154. to detect the endpoint of a handler join point, and as such will likely
  155. have fewer such restrictions.
  156. === Initializers and Inter-type Constructors
  157. The code for Java initializers, such as the assignment to the field `d` in
  158. [source, java]
  159. ....
  160. class C {
  161. double d = Math.sqrt(2);
  162. }
  163. ....
  164. are considered part of constructors by the time AspectJ gets ahold of
  165. bytecode. That is, the assignment of `d` to the square root of two happens
  166. _inside_ the default constructor of `C`.
  167. Thus inter-type constructors will not necessarily run a target type's
  168. initialization code. In particular, if the inter-type constructor calls
  169. a super-constructor (as opposed to a `this` constructor), the target
  170. type's initialization code will _not_ be run when that inter-type
  171. constructor is called.
  172. [source, java]
  173. ....
  174. aspect A {
  175. C.new(Object o) {} // implicitly calls super()
  176. public static void main(String[] args) {
  177. System.out.println((new C() ).d); // prints 1.414...
  178. System.out.println((new C(null)).d); // prints 0.0
  179. }
  180. }
  181. ....
  182. It is the job of an inter-type constructor to do all the required
  183. initialization, or to delegate to a `this` constructor if necessary.
  184. == Annotation-style Notes
  185. Writing aspects in annotation-style is subject to the same bytecode
  186. limitations since the binary aspects take the same form and are woven in
  187. the same way. However, the implementation differences (e.g., the
  188. mechanism for implementing `around` advice) may be apparent at runtime.
  189. See the documentation on annotation-style for more information.
  190. == Summary of implementation requirements
  191. This summarizes the requirements of our implementation of AspectJ. For
  192. more details, see the relevant sections of this guide.
  193. * The invoking code must be under the control of _ajc_ for the following
  194. join points:
  195. ** `call` join point
  196. ** `get` join point
  197. ** `set` join point
  198. * The declaring/target code must be under the control of _ajc_ for the
  199. following join points and inter-type declarations:
  200. ** `execution` join point
  201. ** `adviceexecution` join point
  202. ** `handler` join point
  203. ** `initialization` join point
  204. ** `preinitialiaztion` join point
  205. ** `staticinitialization` join point
  206. ** `perthis` aspect
  207. ** `pertarget` aspect
  208. ** `declare _parents_`
  209. ** `declare _method_ | _field_` (see interface caveats below)
  210. * Implementation Caveats
  211. ** The `initialization` and `preinitialization` join points do not support
  212. `around` advice
  213. ** The `handler` join point does not support...
  214. *** `after advice`
  215. *** `around` advice
  216. *** `cflow(handler(..))`
  217. ** Declaring members on an interface in an aspect affects only the
  218. topmost implementing classes the implementation controls.
  219. ** `cflow` and `cflowbelow` pointcuts work within a single thread.
  220. ** Runtime `ClassCastException` may result from supplying a supertype of
  221. the actual type as an argument to `proceed(..)` in `around` advice.