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.

faq.adoc 140KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385
  1. [[faq]]
  2. = AspectJ FAQ
  3. _Copyright (c) 1997-2001 Xerox Corporation, 2002 Palo Alto Research
  4. Center, Incorporated, 2003-2006 Contributors. All rights reserved._
  5. _Last updated November 3, 2006_
  6. For a list of recently-updated FAQ entries, see #q:faqchanges[Q:What has
  7. changed since the last FAQ version?]
  8. == Overview
  9. *Q:* What is AspectJ?
  10. *A:* AspectJ(tm) is a simple and practical extension to the Java(tm)
  11. programming language that adds to Java aspect-oriented programming (AOP)
  12. capabilities. AOP allows developers to reap the benefits of modularity
  13. for concerns that cut across the natural units of modularity. In
  14. object-oriented programs like Java, the natural unit of modularity is
  15. the class. In AspectJ, aspects modularize concerns that affect more than
  16. one class.
  17. You compile your program using the AspectJ compiler (perhaps using the
  18. supported development environments) and then run it, supplying a small
  19. (< 100K) runtime library.
  20. The AspectJ technologies include a compiler (`ajc`), a debugger
  21. (`ajdb`), a documentation generator (`ajdoc`) and integration with Eclipse and
  22. Ant.
  23. *Q:* What are the benefits of using AspectJ?
  24. *A:* AspectJ can be used to improve the modularity of software systems.
  25. Using ordinary Java, it can be difficult to modularize design concerns
  26. such as
  27. * system-wide error-handling
  28. * contract enforcement
  29. * distribution concerns
  30. * feature variations
  31. * context-sensitive behavior
  32. * persistence
  33. * testing
  34. The code for these concerns tends to be spread out across the system.
  35. Because these concerns won't stay inside of any one module boundary, we
  36. say that they _crosscut_ the system's modularity.
  37. AspectJ adds constructs to Java that enable the modular implementation
  38. of crosscutting concerns. This ability is particularly valuable because
  39. crosscutting concerns tend to be both complex and poorly localized,
  40. making them hard to deal with.
  41. *Q:* Can AspectJ work with any Java program?
  42. *A:* AspectJ has been designed as a _compatible_ extension to Java. By
  43. compatible, we mean
  44. [cols=",",]
  45. |===
  46. |_upward compatible_ |All legal Java programs are legal AspectJ
  47. programs.
  48. |_platform compatible_ |All legal AspectJ programs run on standard Java
  49. virtual machines.
  50. |_tool compatible_ |Existing tools can be extended to work with AspectJ.
  51. |_programmer compatible_ |Programming in AspectJ feels natural to Java
  52. programmers.
  53. |===
  54. The AspectJ tools run on any Java 2 Platform compatible platform. The
  55. AspectJ compiler produces classes that run on any Java 1.1 (or later)
  56. compatible platform.
  57. *Q:* How is AspectJ licensed?
  58. *A:* Since AspectJ 1.9.7, source code and documentation is available
  59. under the
  60. https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt[Eclipse Public
  61. License v 2.0].
  62. AspectJ 1.5.2 through 1.9.6 source code and documentation is available
  63. under the https://www.eclipse.org/org/documents/epl-v10.php[Eclipse
  64. Public License v 1.0].
  65. AspectJ 1.1 through 1.5.1 source code and documentation is available
  66. under the https://eclipse.org/legal/cpl-v10.html[Common Public License
  67. 1.0].
  68. The AspectJ 1.0 tools are open-source software available under the
  69. https://www.opensource.org/licenses/mozilla1.1[Mozilla Public License
  70. 1.1]. That documentation is available under a separate license that
  71. precludes for-profit or commercial redistribution.
  72. The runtime jar aspectjrt.jar and its distribution are also covered by
  73. the https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt[Eclipse
  74. Public License].
  75. For answers to common licensing questions, see the
  76. https://www.eclipse.org/legal/eplfaq.php[Eclipse Public License FAQ].
  77. *Q:* What is the AspectJ Project?
  78. *A:* AspectJ is based on over ten years of research at
  79. https://www.parc.xerox.com[Xerox Palo Alto Research Center] as funded by
  80. Xerox, a U.S. Government grant (NISTATP), and a DARPA contract.
  81. It has evolved through open-source releases to a strong user community
  82. and now operates as an open source project at https://eclipse.org/aspectj
  83. The AspectJ team works closely with the community to ensure AspectJ
  84. continues to evolve as an effective aspect-oriented programming language
  85. and tool set.
  86. The latest release is 1.2 which can be downloaded from the
  87. https://eclipse.org/aspectj[AspectJ project page], including sources as
  88. described xref:#buildingsource[Q:How do I get and compile the source code
  89. for AspectJ?]. Development is focused on supporting applications,
  90. improving quality and performance, enhancing integration with IDE's, and
  91. building the next generations of the language.
  92. [[quickstart]]
  93. == Quick Start
  94. *Q:* What Java versions does AspectJ require and support?
  95. *A:* The AspectJ compiler produces programs for any released version of
  96. the Java platform (jdk1.1 and later). When running, your program classes
  97. must be able to reach classes in the small (< 100K) runtime library
  98. (aspectjrt.jar) from the distribution. The tools themselves require J2SE
  99. 1.3 or later to run, but the compiler can produce classes for any
  100. 1.1-compliant version of the Java platform.
  101. *Q:* How do I download and install AspectJ?
  102. *A:* From AspectJ's https://eclipse.org/aspectj[web page] , download the
  103. AspectJ distribution. The `jar` file is installed by executing
  104. [source, text]
  105. ....
  106. java -jar jar file name
  107. ....
  108. Do *not* try to extract the `jar` file contents and then attempt to
  109. execute `java org.aspectj.tools.Main`. (A `NoClassDefFoundError`
  110. exception will be thrown.) The AspectJ distribution is not designed to
  111. be installed this way. Use the `java -jar` form shown above.
  112. To uninstall, remove the files the installer wrote in your file system.
  113. In most cases, you can delete the top-level install directory (and all
  114. contained files), after you remove any new or updated files you want to
  115. keep. On Windows, no registry settings were added or changed, so nothing
  116. needs to be undone. Do not install over prior versions, which might have
  117. different files. Delete the prior version first.
  118. [[howToStartUsing]]
  119. *Q:* How should I start using AspectJ?
  120. *A:* Many users adopt AspectJ in stages, first using it to understand
  121. and validate their systems (relying on it only in development) and then
  122. using it to implement crosscutting concerns in production systems.
  123. AspectJ has been designed to make each step discrete and beneficial.
  124. In order of increasing reliance, you may use AspectJ:
  125. * *In the development process* Use AspectJ to trace or log interesting
  126. information. You can do this by adding simple AspectJ code that performs
  127. logging or tracing. This kind of addition may be removed ("unplugged")
  128. for the final build since it does not implement a design requirement;
  129. the functionality of the system is unaffected by the aspect.
  130. * *As an ancillary part of your system* Use AspectJ to more completely
  131. and accurately test the system. Add sophisticated code that can check
  132. contracts, provide debugging support, or implement test strategies. Like
  133. pure development aspects, this code may also be unplugged from
  134. production builds. However, the same code can often be helpful in
  135. diagnosing failures in deployed production systems, so you may design
  136. the functionality to be deployed but disabled, and enable it when
  137. debugging.
  138. * *As an essential part of your system* Use AspectJ to modularize
  139. crosscutting concerns in your system by design. This uses AspectJ to
  140. implement logic integral to a system and is delivered in production
  141. builds.
  142. This adoption sequence works well in practice and has been followed by
  143. many projects.
  144. [[integrateWithDevTools]]
  145. *Q:* How does AspectJ integrate with existing Java development tools?
  146. *A:* AspectJ products are designed to make it easy to integrate AspectJ
  147. into an existing development process. Each release includes Ant tasks
  148. for building programs, the AspectJ Development Environment (AJDE) for
  149. writing aspects inside popular IDE's, and command-line tools for
  150. compiling and documenting Java and AspectJ code.
  151. AspectJ provides replacements for standard Java tools:
  152. * `ajc`, the AspectJ compiler, runs on any Java 2 compatible platform,
  153. and produces classes that run on any Java 1.1 (or later) compatible
  154. platform.
  155. * `ajdoc` produces API documentation like javadoc, with additional
  156. crosscutting links. For example, it shows advice affecting a particular
  157. method or all code affected by a given aspect. At present, `ajdoc` is
  158. only supported in AspectJ 1.0.
  159. For debugging, AspectJ supports JSR-45, which provides a mechanism for
  160. debugging .class files that have multiple source files. Debugger clients
  161. and VM's are beginning to support this; see Sun's J2SE 1.4.1 VM and jdb
  162. debugger and recent versions of JBuilder.
  163. The AspectJ Development Environment (AJDE) enables programmers to view
  164. and navigate the crosscutting structures in their programs, integrated
  165. with existing support in popular Java IDE's for viewing and navigating
  166. object-oriented structures. For many programmers this provides a deeper
  167. understanding of how aspects work to modularize their concerns and
  168. permits them to extend some of their development practices without
  169. having to abandon their existing tools.
  170. AJDE is a set of API's providing the basis for the following development
  171. tool integrations:
  172. * Eclipse (version 2.0) in the Eclipse AspectJ Development Tools project
  173. https://eclipse.org/ajdt
  174. * Emacs (GNU version 20.3) and XEmacs (version 21.1 on Unix and 21.4 on
  175. Windows), in the SourceForge AspectJ for Emacs project
  176. https://aspectj4emacs.sourceforge.net
  177. * JBuilder (versions 4 through 7) from Borland in the SourceForge
  178. AspectJ for JBuilder project https://aspectj4jbuildr.sourceforge.net
  179. * Netbeans up to 3.4 (and Sun Microsystems' Forte for Java (versions 2
  180. and 3), Sun/One) in the SourceForge AspectJ for NetBeans project
  181. https://aspectj4netbean.sourceforge.net
  182. Finally, as mentioned above, AspectJ also supports building with Ant by
  183. providing task interfaces to the ajc and ajdoc tools.
  184. [[typicalprograms]]
  185. == Typical AspectJ programs
  186. *Q:* Are aspects always optional or non-functional parts of a program?
  187. *A:* No. Although AspectJ can be used in a way that allows AspectJ code
  188. to be removed for the final build, aspect-oriented code is not _always_
  189. optional or non-functional. Consider what AOP really does: it makes the
  190. modules in a program correspond to modules in the design. In any given
  191. design, some modules are optional, and some are not.
  192. The examples directory included in the AspectJ distribution contains
  193. some examples of the use aspects that are not optional. Without aspects,
  194. [cols=",",]
  195. |===
  196. |*bean* |Point objects would not be JavaBeans.
  197. |*introduction* |Point objects would not be cloneable, comparable or
  198. serializable.
  199. |*spacewar* |Nothing would be displayed.
  200. |*telecom* |No calls would be billed.
  201. |===
  202. *Q:* What is the difference between development and production aspects?
  203. *A:* Production aspects are delivered with the finished product, while
  204. development aspects are used during the development process. Often
  205. production aspects are also used during development.
  206. *Q:* What are some common development aspects?
  207. *A:* Aspects for logging, tracing, debugging, profiling or performance
  208. monitoring, or testing.
  209. *Q:* What are some common production aspects?
  210. *A:* Aspects for performance monitoring and diagnostic systems, display
  211. updating or notifications generally, security, context passing, and
  212. error handling.
  213. [[concepts]]
  214. == Basic AOP and AspectJ Concepts
  215. *Q:* What are scattering, tangling, and crosscutting?
  216. *A:* "Scattering" is when similar code is distributed throughout many
  217. program modules. This differs from a component being used by many other
  218. components since it involves the risk of misuse at each point and of
  219. inconsistencies across all points. Changes to the implementation may
  220. require finding and editing all affected code.
  221. "Tangling" is when two or more concerns are implemented in the same body
  222. of code or component, making it more difficult to understand. Changes to
  223. one implementation may cause unintended changes to other tangled
  224. concerns.
  225. "Crosscutting" is how to characterize a concern than spans multiple
  226. units of OO modularity - classes and objects. Crosscutting concerns
  227. resist modularization using normal OO constructs, but aspect-oriented
  228. programs can modularize crosscutting concerns.
  229. *Q:* What are join points?
  230. *A:* Join points are well-defined points in the execution of a program.
  231. Not every execution point is a join point: only those points that can be
  232. used in a disciplined and principled manner are. So, in AspectJ, the
  233. execution of a method call is a join point, but "the execution of the
  234. expression at line 37 in file Foo.java" is not.
  235. The rationale for restricting join points is similar to the rationale
  236. for restricting access to memory (pointers) or restricting control flow
  237. expressions (`goto`) in Java: programs are easier to understand,
  238. maintain and extend without the full power of the feature.
  239. AspectJ join points include reading or writing a field; calling or
  240. executing an exception handler, method or constructor.
  241. *Q:* What is a pointcut?
  242. *A:* A pointcut picks out #q:joinpoints[ join points ]. These join
  243. points are described by the pointcut declaration. Pointcuts can be
  244. defined in classes or in aspects, and can be named or be anonymous.
  245. *Q:* What is advice?
  246. *A:* Advice is code that executes at each #q:joinpoints[join point]
  247. picked out by a #q:pointcut[pointcut]. There are three kinds of advice:
  248. before advice, around advice and after advice. As their names suggest,
  249. before advice runs before the join point executes; around advice
  250. executes before and after the join point; and after advice executes
  251. after the join point. The power of advice comes from the advice being
  252. able to access values in the execution context of a pointcut.
  253. *Q:* What are inter-type declarations?
  254. *A:* AspectJ enables you to declare members and supertypes of another
  255. class in an aspect, subject to Java's type-safety and access rules.
  256. These are visible to other classes only if you declare them as
  257. accessible. You can also declare compile-time errors and warnings based
  258. on pointcuts.
  259. *Q:* What is an aspect?
  260. *A:* Aspects are a new class-like language element that has been added
  261. to Java by AspectJ. Aspects are how developers encapsulate concerns that
  262. cut across classes, the natural unit of modularity in Java.
  263. Aspects are similar to classes because...
  264. * aspects have type
  265. * aspects can extend classes and other aspects
  266. * aspects can be abstract or concrete
  267. * non-abstract aspects can be instantiated
  268. * aspects can have static and non-static state and behavior
  269. * aspects can have fields, methods, and types as members
  270. * the members of non-privileged aspects follow the same accessibility
  271. rules as those of classes
  272. Aspects are different than classes because...
  273. * aspects can additionally include as members pointcuts, advice, and
  274. inter-type declarations;
  275. * aspects can be qualified by specifying the context in which the
  276. non-static state is available
  277. * aspects can't be used interchangeably with classes
  278. * aspects don't have constructors or finalizers, and they cannot be
  279. created with the new operator; they are automatically available as
  280. needed.
  281. * privileged aspects can access private members of other types
  282. [[whyaop]]
  283. == Why AOP?
  284. *Q:* Are crosscutting concerns induced by flaws in parts of the system
  285. design, programming language, operating system, etc. Or is there
  286. something more fundamental going on?
  287. *A:* AOP's fundamental assumption is that in any sufficiently complex
  288. system, there will inherently be some crosscutting concerns.
  289. So, while there are some cases where you could re-factor a system to
  290. make a concern no longer be crosscutting, the AOP idea is that there are
  291. many cases where that is not possible, or where doing so would damage
  292. the code in other ways.
  293. *Q:* Does it really make sense to define aspects in terms of
  294. crosscutting?
  295. *A:* Yes.
  296. The short summary is that it is right to define AOP in terms of
  297. crosscutting, because well-written AOP programs have clear crosscutting
  298. structure. It would be a mistake to define AOP in terms of "cleaning up
  299. tangling and scattering", because that isn't particular to AOP, and past
  300. programming language innovations also do that, as will future
  301. developments.
  302. (Slides for a long talk on this topic were once available at
  303. https://www.cs.ubc.ca/~gregor/vinst-2-17-01.zip.)
  304. *Q:* Is AOP restricted to domain-specific applications?
  305. *A:* No. Some implementations of AOP are domain-specific, but AspectJ
  306. was specifically designed to be general-purpose.
  307. *Q:* Why do I need AOP if I can use interceptors (or JVMPI or ref
  308. lection)?
  309. *A:* There are many mechanisms people use now to implement some
  310. crosscutting concerns. But they don't have a way to express the actual
  311. structure of the program so you (and your tools) can reason about it.
  312. Using a language enables you to express the crosscutting in first-class
  313. constructs. You can not only avoid the maintenance problems and
  314. structural requirements of some other mechanisms, but also combine forms
  315. of crosscutting so that all the mechanisms for a particular concern are
  316. one piece of code.
  317. [[related]]
  318. == Related Technology
  319. *Q:* How does AspectJ compare to other new forms of programming?
  320. *A:* There are many recent proposals for programming languages that
  321. provide control over crosscutting concerns. Aspect-oriented programming
  322. is an overall framework into which many of these approaches fit. AspectJ
  323. is one particular instance of AOP, distinguished by the fact that it was
  324. designed from the ground up to be compatible with Java.
  325. *Q:* How do you compare the features of AspectJ with reflective systems?
  326. *A:* Reflective and aspect-oriented languages have an important
  327. similarity: both provide programming support for dealing with
  328. crosscutting concerns. In this sense reflective systems proved that
  329. independent programming of crosscutting concerns is possible.
  330. But the control that reflection provides tends to be low-level and
  331. extremely powerful. In contrast, AspectJ provides more carefully
  332. controlled power, drawing on the rules learned from object-oriented
  333. development to encourage a clean and understandable program structure.
  334. *Q:* How do AspectJ features compare with those of mixin-based
  335. inheritance?
  336. *A:* Some features of AspectJ, such as introduction, are related to
  337. _mixin-based inheritance_. But, in order to support crosscutting, a core
  338. goal for AspectJ, AspectJ goes beyond mixin-based inheritance.
  339. Firstly, an aspect imposes behavior on a class, rather than a class
  340. requesting behavior from an aspect. An aspect can modify a class without
  341. needing to edit that class. This property is sometimes called _reverse
  342. inheritance_.
  343. Secondly, a single aspect can affect multiple classes in different ways.
  344. A single paint aspect can add different paint methods to all the classes
  345. that know how to paint, unlike mixin classes.
  346. So mixin-based inheritance doesn't have the reverse inheritance
  347. property, and mixins affect every class that mixes them in the same. If
  348. I want to do something like SubjectObserverProtocol, I need two mixins,
  349. SubjectPartofSubjectObserverProtocol and ObserverPartof... In AspectJ,
  350. both halves of the protocol can be captured in a single aspect.
  351. *Q:* How does AspectJ compare with more dynamic AOP?
  352. *A:* Some AOP techniques are presented as "dynamic" because the weaving
  353. occurs when classes are loaded, because aspects can be configured in a
  354. separate XML file before launch, or because some advice depends on
  355. runtime reflection. They are said to be more flexible than AspectJ.
  356. This is a misconception. First, the AspectJ 1.1 weaver has always
  357. supported weaving at compile-time or class-load-time. Weaving at
  358. compile-time reduces application launch and running time, and it helps
  359. IDE's offer support for tracking down weaving errors and understanding
  360. the impact of aspects on a system. On the other hand, weaving at
  361. load-time simplifies build and deployment. Before AspectJ 1.2, the user
  362. had to write a class loader that used the weaver API to weave at load
  363. time; since 1.2, AspectJ comes with a command-line launcher to support
  364. weaving at class-load-time without any other changes to a build
  365. configuration. In AspectJ 5, we expect to get a similar level of support
  366. as AspectWerkz, and to exploit the class bytecode weaving hook available
  367. in Java 5 VM's.
  368. Second, AspectJ programs, like Java programs generally, can be written
  369. to support any level of XML configuration or to depend on runtime
  370. reflection. There are some benefits to using AspectJ; e.g., the
  371. proceed() form within around advice simplifies a lot of the work that
  372. otherwise would go into writing a generalized interceptor, without
  373. introducing many of the runtime errors that can result from
  374. interceptors. For AspectJ examples of configurable or
  375. reflection-dependent programs, see the sample code linked off the
  376. AspectJ documentation page or the examples discussed on the mailing
  377. list, e.g.,
  378. https://dev.eclipse.org/mhonarc/lists/aspectj-users/msg02151.html[Incremental
  379. and runtime weaving support?].
  380. *Q:* What is the relationship between AOP and XP (extreme programming
  381. AKA agile methods)?
  382. *A:* From a question on the user list:
  383. [source, text]
  384. ....
  385. > Anyone know the connections between AOP and Extreme Programming?
  386. > I am really confused. It seems AOP is a programming paradigm, which
  387. > is the next level of abstraction of OOP. Extreme Programming, however,
  388. > this is a lightweight software development process. One of the common
  389. > motivations of AOP and XP is designed to adopt to the requirement
  390. > changes, so that it can save the cost of software development.
  391. ....
  392. This is Raymond Lee's answer:
  393. You're not really that confused. AOP and XP are orthogonal concepts,
  394. although AOP can be used to help accomplish XP goals. One of the goals
  395. of XP is to respond to changing requirements. Another is to reduce the
  396. overall cost of development. These are not necessarily the same thing.
  397. One of the principles of XP that contribute to meeting those goals is to
  398. maintain clean, simple designs. One of the criteria for clean, simple
  399. designs is to factor out duplication from the code. Benefits of removing
  400. duplication include the code being easier to understand, better
  401. modularity of the design, lower costs of code changes, less chance of
  402. conflicting changes when practicing collective code ownership, etc.
  403. Different types of duplication lend themselves to being addressed by
  404. different design paradigms and language features. Duplicate snippets of
  405. code can be factored out into methods. Duplicate methods can be factored
  406. out to common classes, or pushed up to base classes. Duplicate patterns
  407. of methods and their use can be factored out to mechanisms of classes
  408. and methods (i.e. instantiations of design patterns).
  409. AOP addresses a type of duplication that is very difficult to handle in
  410. the other common paradigms, namely cross-cutting concerns. By factoring
  411. out duplicate cross-cutting code into aspects, the target code becomes
  412. simpler and cleaner, and the cross-cutting code becomes more centralized
  413. and modular.
  414. So, AOP as a paradigm, and the associated tools, gives an XPer, or
  415. anyone wanting to remove duplication from the code base, a powerful way
  416. to remove a form of duplication not easily addressed until now.
  417. *Q:* Will you support C#?
  418. *A:* Not at this time. Although the resemblances between C# and Java
  419. means it would probably be a fairly straightforward matter to take the
  420. AspectJ language design and produce AspectC#, our current focus is only
  421. on supporting effective uses of AspectJ.
  422. [[adoption]]
  423. == Deciding to adopt AspectJ
  424. *Q:* Is it safe to use AspectJ in my product plans?
  425. *A:* You may use AspectJ in your product or project with little risk.
  426. Several factors play a role in reducing the risk of adopting this new
  427. technology:
  428. * AspectJ is an _addition_ to Java, and can be introduced into a project
  429. in a way that limits risk. See #q:startUsingAJ[Q: How should I start
  430. using AspectJ?] for some suggestions on how to do this.
  431. * The AspectJ compiler accepts standard Java as input and produces
  432. standard Java bytecode as output. In 1.0, an optional mode produces
  433. standard Java source code which may then be compiled with any compliant
  434. Java compiler (e.g. Sun's `javac` compiler or IBM's `jikes` compiler).
  435. In 1.1, an optional mode accepts standard Java bytecode from any
  436. compliant Java compiler and weaves in the aspects to produce new
  437. bytecode.
  438. * AspectJ is available under a non-proprietary, open source license, the
  439. https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt[Eclipse Public
  440. License v 2.0]. AspectJ will continue to evolve and be available,
  441. regardless of the fate of any particular organization involved with
  442. AspectJ.
  443. * Removing AspectJ from your program is not difficult, although you will
  444. lose the flexibility and economy that AspectJ provided.
  445. * A number of significant open-source projects and industry products use
  446. AspectJ successfully. A prominent example is the Spring framework which supports
  447. both native AspectJ and its internal "AOP lite" framework Spring AOP. Spring AOP
  448. is based on dynamic proxies, offers a subset of AspectJ features and offers the
  449. corresponding subset of the @AspectJ annotation-based aspect syntax.
  450. * You may also search for search for, e.g., "AspectJ in real world" on the WWW
  451. or in AspectJ mailing list archives, as described in
  452. xref:#searchingsite[How can I search the email archives or the web site?]).
  453. *Q:* What is the effect of using AspectJ on the source code size of
  454. programs?
  455. *A:* Using aspects reduces, as a side effect, the number of source lines
  456. in a program. However, the major benefit of using aspects comes from
  457. _improving_ the modularity of a program, not because the program is
  458. smaller. Aspects gather into a module concerns that would otherwise be
  459. scattered across or duplicated in multiple classes.
  460. *Q:* Does AspectJ add any performance overhead?
  461. *A:* The issue of performance overhead is an important one. It is also
  462. quite subtle, since knowing what to measure is at least as important as
  463. knowing how to measure it, and neither is always apparent.
  464. We aim for the performance of our implementation of AspectJ to be on par
  465. with the same functionality hand-coded in Java. Anything significantly
  466. less should be considered a bug.
  467. There is currently no benchmark suite for AOP languages in general or
  468. for AspectJ in particular. It is probably too early to develop such a
  469. suite because AspectJ needs more maturation of the language and the
  470. coding styles first. Coding styles really drive the development of the
  471. benchmark suites since they suggest what is important to measure.
  472. Though we cannot show it without a benchmark suite, we believe that code
  473. generated by AspectJ has negligible performance overhead. Inter-type
  474. member and parent introductions should have very little overhead, and
  475. advice should only have some indirection which could be optimized away
  476. by modern VM's.
  477. The `ajc` compiler will use static typing information to only insert the
  478. advice and dynamic pointcut tests that are absolutely necessary. Unless
  479. you use 'thisJoinPoint' or 'if', the main dynamic checks will be
  480. 'instanceof' checks which are generally quite fast. These checks will
  481. only be inserted when they can not be inferred from the static type
  482. information.
  483. When measuring performance, write AspectJ code fragments and compare
  484. them to the performance of the corresponding code written without
  485. AspectJ. For example, don't compare a method with before/after advice
  486. that grabs a lock to just the method. That would be comparing apples and
  487. oranges. Also be sure to watch out for JIT effects that come from empty
  488. method bodies and the like. Our experience is that they can be quite
  489. misleading in understanding what you've measured.
  490. *Q:* I've heard that AspectJ leads to modularity violations. Does it?
  491. *A:* Well I haven't yet seen a language in which you can't write bad
  492. code!
  493. But seriously, most AspectJ users find that just like when they learned
  494. OO, it takes a while to really get the hang of it. They tend to start in
  495. the usual way, by copying canonical examples and experimenting with
  496. variations on them.
  497. But users also find that rather than being dangerous, AspectJ helps them
  498. write code that is more clear and has better encapsulation -- once they
  499. understand the kind of modularity AspectJ supports. There are several
  500. good papers that talk about this (see below), but here's a basic point
  501. to keep in mind: when properly used, AspectJ makes it possible program
  502. in a modular way, something that would otherwise be spread throughout
  503. the code. Consider the following code, adapted from the AspectJ
  504. tutorial:
  505. [source, java]
  506. ....
  507. aspect PublicErrorLogging {
  508. Log log = new Log();
  509. pointcut publicInterface(Object o):
  510. call(public * com.xerox.*.*(..)) && target(o);
  511. after(Object o) throwing (Error e): publicInterface(o) {
  512. log.write(o, e);
  513. }
  514. }
  515. ....
  516. The effect of this code is to ensure that whenever any public method of
  517. an interface or class in the `com.xerox` package throws an error, that
  518. error is logged before being thrown to its caller.
  519. Of course in the alternative implementation a large number of methods
  520. have a try/catch around their body.
  521. The AspectJ implementation of this crosscutting concern is clearly
  522. modular, whereas the other implementation is not. As a result, if you
  523. want to change it, its easier in the AspectJ implementation. For
  524. example, if you also want to pass the name of the method, or its
  525. arguments to `log.write`, you only have to edit one place in the AspectJ
  526. code.
  527. This is just a short example, but I hope it shows how what happens with
  528. AOP and AspectJ is that the usual benefits of modularity are achieved
  529. for crosscutting concerns, and that leads to better code, not more
  530. dangerous code.
  531. One paper someone else just reminded me of that talks some more about
  532. this is:
  533. https://www.cs.ubc.ca/~kdvolder/Workshops/OOPSLA2001/submissions/12-nordberg.pdf
  534. *Q:* Why does AspectJ permit aspects to access and add members of
  535. another type? Isn't that violating OO encapsulation?
  536. *A:* In the spirit of Smalltalk, we have decided to give more power to
  537. the language in order to let the user community experiment and discover
  538. what is right. To date this has proven to be a successful strategy
  539. because it has permitted the construction of many useful aspects that
  540. crosscut the internal state of an object, and as such need access the
  541. its private members. However, we are not discounting that some sort of
  542. restrictions are useful, rather, we are seeking input from the community
  543. in order to decide on what these restrictions should be.
  544. In that light, our position on encapsulation is :
  545. * we respect Java's visibility rules
  546. * we also provide open-classes, a mature OO technology
  547. * we provide "privileged" access if you really need it.
  548. Introducing parents or members to classes is a well-studied OO technique
  549. known as open classes.
  550. Open classes have been used in many languages prior to AspectJ,
  551. including CLOS, Python, Smalltalk, Objective-C, and others. Building
  552. from Java, introduction in AspectJ provides better name hygiene and
  553. access control than prior languages. Introduced code obeys all of Java's
  554. normal accessibility rules for its lexical location in the aspect that
  555. it is introduced from. Such code can not even see, much less access,
  556. private members of the class it is introduced into. Further,
  557. introductions can be declared private to the aspect, so they are not
  558. visible to other clients of the class.
  559. Privileged aspects do permit access to private members of another class.
  560. They are a response to the very few cases where developers genuinely
  561. need such access (typically for testing purposes where it access is
  562. necessary), but it would be more risky to open access by putting the
  563. aspect in the same package, adding test code, or changing access in the
  564. target class. We recommend using privileged aspects only as necessary,
  565. and believe that marking them "privileged" makes any potential misuse
  566. apparent.
  567. *Q:* Can I use AspectJ with J2EE?
  568. *A:* Consider the component types in J2EE:
  569. * Servlet: AspectJ works well within servlets
  570. * JSP: It is possible to use AspectJ to affect code in JSPs by
  571. precompiling them into Java sources and compiling these with ajc. This
  572. can be used, e.g., to customize displays by turning on and off custom
  573. JSP taglibs. The mapping from a given jsp source to java package and
  574. class name is not standardized, which means doing this imposes
  575. dependencies on specific container versions.
  576. * EJB: AspectJ supports a wide variety of aspects for EJBs. It can be
  577. used for logging, tracing, debugging, error handling by layers,
  578. correlated method-level interception (e.g., chargebacks), metering,
  579. fine-grained transactions, etc. Indeed, it can be used to enforce
  580. adherence to coding restrictions within an EJB (e.g., not using java.io,
  581. creating a class loader, or listening on sockets) using `declare error`.
  582. The basic limitations are that there is no built-in support for writing
  583. J2EE analogs for AspectJ extensions to Java, like distributed aspects,
  584. distributed cflow, or managing state between invocations. These don't
  585. prevent one from using AspectJ to do useful intra-container
  586. implementation, nor need they prevent one from building distributed
  587. support, state management, and inter-component implementations that
  588. leverage AspectJ. It just takes some work. In more detail:
  589. All AspectJ implementations may define "code the implementation
  590. controls". The AspectJ 1.0 implementation defines this as the files
  591. passed to the compiler (AspectJ 1.1 will also support bytecode weaving).
  592. Some advice on EJB operations will generate methods that confuse ejb
  593. compilers. To avoid this problem, you can use the -XaddSafePrefix flag
  594. when compiling with ajc.
  595. EJB components may be invoked remotely, and containers may passivate and
  596. pool EJB's. Servlets have similar limitations, and in both cases the
  597. lifespan of the defining class loader is implementation-dependent
  598. (though it must span the operation of a particular request).
  599. Being limited by lifecycle and namespace, the AspectJ 1.0 implementation
  600. supports aspects that operate through non-remote invocations during the
  601. lifetime of the namespace for a particular deployment unit compiled in
  602. its entirety by the ajc compiler. This means AspectJ supports common
  603. aspects only within a single local runtime namespace (usually
  604. implemented as a class loader hierarchy).
  605. Further, AspectJ recognizes language-level join points (object
  606. initialization, method calls, etc.), not their EJB analogs (ejb find or
  607. create methods...). These lead to the following consequences:
  608. * Issingleton aspects (the default) are limited to the lifetime of the
  609. defining class loader, which in some implementations may not span
  610. multiple invocations of the same application or EJB component.
  611. * EJB lifecycles are different from object lifecycles, so perthis and
  612. pertarget aspects will make little sense. They do not work in the
  613. current implementation, which uses synchronized methods to ensure a
  614. correct association in threaded environments (EJB's may not have
  615. synchronized methods).
  616. * Percflow or percflowbelow aspects are restricted to a chain of
  617. non-remote invocations. While EJB 2.0 permits declaring an interface
  618. local, this information is not available to the AspectJ compiler today.
  619. For same reasons as stated above fore perthis, these will not work even
  620. in the EJB container.
  621. * Evaluation of cflow or cflowbelow pointcuts will be valid only with
  622. respect to a chain of non-remote invocations.
  623. In addition, any AspectJ code should respect EJB operations:
  624. * The EJB container accesses EJB component fields directly, i.e., in
  625. code outside the control of the compiler. There is no join point for
  626. these accesses, and hence no way to write a pointcut to advise that
  627. access.
  628. * The EJB container may pool EJB components, so any initialization join
  629. points may run once per component constructed, not once per component
  630. initialized for purposes of a client call.
  631. * The EJB container is permitted to change class loaders, even between
  632. invocations of a particular EJB component (by passivating and activating
  633. with a new class loader). In this case, instances of singleton aspects
  634. will not operate over multiple invocations of the component, or that
  635. static initialization join point recur for a given class as it is
  636. re-loaded. This behavior depends on the container implementation.
  637. *Q:* Can I use AspectJ with Generic Java?
  638. *A:* We plan to support Generics when Java 1.5 is available.
  639. But at this time, unfortunately not. The two compilers are just not at
  640. all compatible. In an ideal world, there would be a wonderful Open
  641. Source extensible compiler framework for Java that both GJ and AspectJ
  642. would be built on top of, and they would seamlessly interoperate along
  643. with all other extensions to Java that you might be interested in, but
  644. that's not the case (yet?).
  645. However, on 09 October 2000, the Java Community Process approved a
  646. proposal to add generic types to Java that is largely based on GJ (JSR
  647. 14). A draft specification was submitted for public review, which closed
  648. on 01 August 2001, and a prototype implementation has been released by
  649. Sun.
  650. We are committed to moving very rapidly to add support for generic types
  651. in AspectJ when generic types become part of the Java language
  652. specification. Everyone on the AspectJ team is looking forward to this,
  653. because we too would really like to be able to write code that includes
  654. both aspects and generic types.
  655. *Q:* Can I use AspectJ with J2ME?
  656. *A:* The J2ME platform has several different components. The diagram
  657. below shows how the different profiles build on top of the two
  658. configurations CDC (Connected Device Configuration) and CLDC (Connected
  659. Limited Device Configuration):
  660. [source, text]
  661. ....
  662. --------------
  663. | Personal |
  664. -------------- --------
  665. | Foundation | | MIDP |
  666. ------------------ ------------------
  667. | CDC | | CLDC |
  668. ------------------------------------------
  669. | Java |
  670. ------------------------------------------
  671. ....
  672. Which configuration you have dictates the restrictions when running
  673. AspectJ compiled programs.
  674. If you're running with a profile which sits on top of CDC then there are
  675. not, as far as we are aware, any restrictions when running AspectJ
  676. compiled code on this flavour of J2ME.
  677. If you're running with a profile sitting on top of CLDC 1.1 you are
  678. currently unable to use the `thisJoinPoint,
  679. thisJoinPointStaticPart` and `
  680. thisEnclosingJoinPointStaticPart` variables, the `cflow` and
  681. `cflowbelow` pointcuts and the `percflow` and `
  682. percflowbelow` perClauses.
  683. Finally, if you're running with a profile which sits on top of CLDC 1.0
  684. you have all the restrictions of CLDC 1.1. There may be further
  685. restrictions due to the lack of types corresponding to the primitive
  686. types (e.g. Integer.TYPE), however, at the time of writing we have been
  687. unable to do any extensive testing on this.
  688. Note that the aspectj runtime jar is now (as of AspectJ5) quite large
  689. but only a small subset is required for executing code in J2ME
  690. environments. We plan to ship a second aspectjrt.jar built for the J2ME
  691. environment at some point.
  692. For more discussion and to raise any issues you have with AspectJ and
  693. J2ME, refer to
  694. https://bugs.eclipse.org/bugs/show_bug.cgi?id=92933[bugzilla entry
  695. 92933].
  696. *Q:* Are you working to put AOP into Java? It seems that every AOP
  697. toolset currently uses proprietary mechanisms to describe point-cuts,
  698. etc.
  699. *A:* We are working on standardization, but it's a question of
  700. timing/ripeness (imagine going from thousands of users to millions).
  701. (See #q:standardization[Q:What are your plans to make AspectJ a general
  702. feature of Java supported by Sun and the other key-players in the Java
  703. Industry?].) We believe AspectJ addresses this question in the best way
  704. possible now:
  705. * It's open-source. Rather than being proprietary or controlled by a
  706. vendor, it's available for anybody to use and build upon, forever.
  707. * AspectJ is not a set of mechanisms, it's a language. It is currently
  708. implemented using certain techniques, but there's nothing that prevents
  709. it from being implemented with other techniques. That means users can
  710. adopt the language with confidence that implementations will get better.
  711. * There is no engineering need to change Java. The AspectJ language uses
  712. the join point model already in Java, so there is no need to extend the
  713. programming model. Our implementation produces valid Java bytecode,
  714. which runs in any compliant J2SE VM and supports standard debuggers for
  715. those VM's that support JSR-45 (debugging support for
  716. multi-language/multi-file sources). This is a huge benefit to Sun since
  717. Sun must be extremely cautious about extensions to the language or VM;
  718. before adopting AOP, Sun should demand the kind of actual-proof that
  719. AspectJ implementations offer.
  720. * On the issue of "proprietary mechanisms to describe pointcuts, etc.":
  721. Any AOP has to have some language to describe pointcuts and the like
  722. ("pointcuts" of course being the AspectJ term). Users would like to have
  723. one language (to avoid having to learn or transform between many
  724. languages) and the choice of multiple implementations (tailored for a
  725. configuration, subject to competitive pressure, etc.). That's what
  726. AspectJ offers.
  727. * That said, we believe the AspectJ extensions to Java could form the
  728. basis for bringing AOP to Java; when that happens, there will be
  729. engineering opportunities to make the implementation and tool support
  730. better.
  731. *Q:* What kind of support is available?
  732. *A:* The mailing lists provide the primary support for everyone in the
  733. community (See #q:mailingLists[Q: What mailing lists are there?]). To
  734. request commercial support, tutorials, or presentations, use the
  735. developer mailing list, `aspectj-dev@eclipse.org`.
  736. To find out about known issues, see the
  737. link:progguide/implementation.html[AspectJ Programming Guide Appendix,
  738. "Implementation Notes"] and the AspectJ bugs in the database at
  739. https://bugs.eclipse.org/bugs (using the product `AspectJ`). Here are
  740. direct links to
  741. https://bugs.eclipse.org/bugs/buglist.cgi?product=AspectJ&component=Compiler&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED[view
  742. open compiler bugs],
  743. https://bugs.eclipse.org/bugs/buglist.cgi?product=AspectJ[view all
  744. Aspectj bugs (open or closed)], or
  745. https://bugs.eclipse.org/bugs/enter_bug.cgi?product=AspectJ[add new
  746. bugs].
  747. *Q:* What mailing lists are there?
  748. *A:* The AspectJ users mailing list (`aspectj-users@eclipse.org`)
  749. provides an informal network of AspectJ language users who can answer
  750. usage questions about AspectJ programs and the AspectJ tools. This is
  751. the place to ask how to code something in AspectJ or how to write Ant or
  752. shell scripts to invoke the tools.
  753. The AspectJ developers mailing list (`aspectj-dev@eclipse.org`) provides
  754. an informal network of AspectJ technology experts who aim to understand
  755. the technology behind AspectJ. The committers to the AspectJ project use
  756. this list for open technical and planning discussions. Developers can
  757. answer questions about what's possible and about integrating AspectJ
  758. technology with other technologies.
  759. For both mailing lists, only subscribed members may post messages. To
  760. subscribe, visit the https://eclipse.org/aspectj[AspectJ web site].
  761. There you can also subscribe to `aspectj-announce@eclipse.org`, a
  762. low-traffic list containing only announcements about significant AspectJ
  763. events and product releases.
  764. [[compiler]]
  765. == Using the AspectJ compiler
  766. *Q:* Do I have to use the AspectJ compiler?
  767. *A:* The AspectJ compiler or weaver is required at some point, but many
  768. people can use AspectJ without changing their build or deployment
  769. process significantly. For aspects that are not required to compile, you
  770. can use the AspectJ binary weaver, run at build-time or class-load-time.
  771. You can write aspects using the original code style (which must be
  772. compiled with the AspectJ compiler) or using the annotation style new in
  773. AspectJ 5 (which may be compiled with Javac or the AspectJ compiler).
  774. For more information, see #q:codeversusannotationstyles[Q:Should I use
  775. code- or annotation-style aspects?].
  776. *Q:* What files do I need to include when compiling AspectJ programs?
  777. *A:* You need to specify to the compiler the files that contain your
  778. aspects and the files that contain the types affected by your aspects.
  779. See #q:knowWhenAspectsAffectClasses[Q: How do I know which aspects
  780. affect a class when looking at that class's source code?]. The AspectJ
  781. compiler will not search the source path for types that may be affected
  782. (unlike Javac and Jikes). In AspectJ 1.0, ajc requires all code to be in
  783. source form; in AspectJ 1.1, Java and AspectJ code may be in either
  784. source or binary form.
  785. In some cases you should compile your entire system all at once. If this
  786. is too slow, then you can try to make reasonable divisions between sets
  787. of source files whose aspects do not interact to achieve a shorter
  788. compile cycle (particularly for development aspects). If you have
  789. aspects that apply to different modules, you can try compiling them into
  790. a binary form and using them to weave each module. However, if you get
  791. any problems or if you wish to run tests or do a release, you should
  792. recompile the entire system.
  793. For more information, see the link:devguide/index.html[Development
  794. Environment Guide] link:devguide/ajc-ref.html[Reference for ajc].
  795. *Q:* I have to list many files in the command line to compile with
  796. `ajc`. Is there any other way to provide the file names to `ajc`?
  797. *A:* Yes, use the argfile option to ajc. List source files in a
  798. line-delimited text file and direct ajc to that file using `-argfile` or
  799. `@`:
  800. [source, text]
  801. ....
  802. ajc @sources.lst
  803. ajc -argfile sources.lst
  804. ....
  805. Another way in AspectJ 1.1 is to use the `-sourceroots` options, which
  806. reads all source files in a given set of directories:
  807. [source, text]
  808. ....
  809. ajc -sourceroots "src;testsrc"
  810. ....
  811. For more information, see the link:devguide/index.html[Development
  812. Environment Guide] link:devguide/ajc-ref.html[Reference for ajc].
  813. *Q:* What Java virtual machine (JVM) do I use to run the AspectJ
  814. compiler?
  815. *A:* Use the latest, greatest, fastest JVM you can get your hands on for
  816. your platform. The compiler's performance is dependent on the
  817. performance of the JVM it is running on, so the faster a JVM you can
  818. find to run it on, the shorter your compile times will be. At a minimum
  819. you need to use a Java 2 or later JVM to run the compiler (J2SE 1.3 for
  820. AspectJ 1.1). We realize that this constraint can be a problem for users
  821. who don't currently have a Java 2 JVM available. We're sorry for the
  822. inconvenience, but we had to make the hard decision that the advantages
  823. of being able to rely on Java 2 were worth the cost of losing a number
  824. of developers who are working on platforms without Java 2 support. Here
  825. is a list of starting places where you might find support for your
  826. system.
  827. * https://java.sun.com/j2se/[Java 2 Platform, Standard Edition]
  828. * https://www-106.ibm.com/developerworks/java/jdk/[developerWorks : Java
  829. technology : Tools and products - Developer kits]
  830. * https://www-124.ibm.com/developerworks/oss/jikes/[developerWorks : Open
  831. Source - Jikes Project]
  832. * https://java.sun.com/cgi-bin/java-ports.cgi[Java Platform Ports]
  833. The requirement of Java 2 support is only for _running_ the AspectJ
  834. compiler. The AspectJ compiler can be used to build programs that will
  835. run on Java 1.1 (or probably even on Java 1.0) systems. This means that
  836. it can build programs that will run on Macintosh, FreeBSD, and applets
  837. that will run in Internet Explorer and Netscape Navigator that are still
  838. not yet Java 2 compliant.
  839. *Q:* How can I use `ajc` to compile programs for a JVM that is different
  840. from the one used to run it?
  841. *A:* `ajc` can be used to develop programs that are targeted at the Java
  842. 1.1 platform, even though the `ajc` compiler won't run on that platform.
  843. Here's an example of using `ajc` in this sort of cross-compilation mode
  844. (assuming a Windows platform with all the default installation
  845. directories):
  846. [source, text]
  847. ....
  848. ajc -target 1.1 -bootclasspath c:\jdk1.1.7\lib\classes.zip \
  849. -classpath c:\aspectj1.0\lib\aspectjrt.jar -extdirs "" \
  850. -argfile jdk11system.lst
  851. ....
  852. This same technique can be used if you want to run `ajc` on a JDK 1.3
  853. JVM (highly recommended) but need to generate code for JDK 1.2. That
  854. would look something like:
  855. [source, text]
  856. ....
  857. ajc -bootclasspath c:\jdk1.2\jre\lib\rt.jar \
  858. -classpath c:\aspectj1.0\lib\aspectjrt.jar \
  859. -extdirs c:\jdk1.2\jre\lib\ext
  860. -argfile jdk12system.lst
  861. ....
  862. *Q:* Does the `ajc` compiler support the `assert` keyword in Java 1.4?
  863. *A:* Yes. As with `Javac`, use the `-source 1.4` option as described in
  864. the link:devguide/index.html[Development Environment Guide]
  865. link:devguide/ajc-ref.html[Reference for ajc].
  866. *Q:* Does the `ajc` compiler support generics and the other new language
  867. features of Java 5?
  868. *A:* Yes. As with `Javac`, use the `-1.5` option as described in the
  869. link:devguide/index.html[Development Environment Guide]
  870. link:devguide/ajc-ref.html[Reference for ajc].
  871. *Q:* Will aspects work with different versions of the compiler/weaver
  872. and runtime?
  873. *A:* Yes. Both `ajc` and `aspectjrt.jar` should work with versions of
  874. aspect code and libraries back to AspectJ 1.2.1. Any aspects should be
  875. deployed with the same version of `aspectjrt.jar` they were compiled
  876. with. For more information, see the link:devguide/index.html[Development
  877. Environment Guide] link:devguide/ajc-ref.html[Reference for ajc] and
  878. link:devguide/deployment.html[Deployment notes] section on
  879. link:devguide/versionCompatibility.html[Version compatibility].
  880. *Q:* Are there any issues using AspectJ with the Microsoft JVM?
  881. *A:* Since AspectJ requires Java 2 or later, it will not run on the
  882. Microsoft JVM, which does not support Java 2.
  883. *Q:* Does `ajc` rely on `javac` for generating Java bytecode (`.class`)
  884. files?
  885. *A:* No. Some previous versions of AspectJ had this requirement. In
  886. AspectJ 1.0, `javac` can still be used as `ajc` back end by using the
  887. `-usejavac` flag. You can also run `ajc` in preprocessor mode to
  888. generate Java source (`.java`) files to be compiled using `javac` or
  889. another java compiler. Neither option is supported in AspectJ 1.1.
  890. *Q:* I noticed the AspectJ compiler doesn't use a parser generator. Why
  891. is that?
  892. *A:* In AspectJ 1.0, the PARSER for ajc is written by hand. This choice
  893. was made with full awareness of the generator tools out there. (Jim had
  894. for example used the excellent javacc tool for building the parser for
  895. JPython (now Jython)). One of the reasons that AspectJ uses a
  896. hand-written parser is that using javacc taught Jim about the LL-k
  897. design for parsers (pioneered by antlr). As opposed to the state-machine
  898. parsers produced by yacc, these parsers are very readable and writable
  899. by humans.
  900. Antlr and javacc did not really suit the project:
  901. * Antlr's support for unicode in the lexer is still immature and this
  902. makes using it with Java challenging. This was an even bigger issue 3
  903. years ago when we started on the Java implementation of ajc.
  904. * While javacc is freely available, it is not Open Source. Depending on
  905. a closed-source tool to build an Open Source compiler would reduce some
  906. of the transparency and control of open-source.
  907. There were also several things that were easier to implement with a
  908. hand-written parser than with any of the exiting tools.
  909. * Semi-keywords -- it's important to us that "every legal Java program
  910. is also a legal AspectJ program." This wouldn't be true if we made
  911. 'before' and 'call' full keywords in AspectJ. It is easier to support
  912. these sorts of semi-keywords with a hand-written parser. (Note:
  913. ajc-1.0.x handles 'aspect' and 'pointcut' slightly specially which can
  914. break a few unusual pure Java programs. This is a compiler limitation
  915. that will be fixed in a future release.)
  916. * Deprecated syntax warnings -- the syntax of AspectJ changed many times
  917. from version 0.2 to the 1.0 release. It was easier to provide helpful
  918. warning messages for these changes with our hand-written parser.
  919. * Grammar modularity -- We like being able to have AspectJParser extend
  920. JavaParser.
  921. * Part of the grammar for AspectJ is extremely hard for existing tools
  922. to capture. This is the type pattern syntax, i.e. "com.xerox..*.*(..)".
  923. The sort of case that gives standard parser generators fits is something
  924. like "*1.f(..)" which no one would ever write, but which must be
  925. supported for a consistent language.
  926. +
  927. In AspectJ 1.1, the parser was written as it is for the underlying
  928. Eclipse compiler, with some hand-coding of the sort that avoids adding
  929. keywords to the language.
  930. *Q:* How does incremental mode work?
  931. *A:* In incremental mode, ajc minimizes the files that need to be
  932. recompiled after another file has changed. In Java, only the changed
  933. files need to be recompiled, but in AspectJ, other files might also need
  934. to be recompiled or re-woven.
  935. Depending on what is modified, we may need to re-weave code. If you
  936. change a pointcut and save it, we currently have to check everywhere in
  937. case a new match is occurring or an old match is no longer correct.
  938. However, if you simply change the body of an advice in an aspect, there
  939. is (usually) no need to reweave as the affected classes call the advice
  940. and the advice (by design) maintains its name in the recompiled aspect.
  941. If you make a change to a class (as opposed to an aspect) and save it,
  942. we usually can get away with merely having to compile that class then
  943. weave the existing aspects with it - rather than doing a full recompile
  944. of the entire system.
  945. There are a lot of possible optimizations to the algorithms we use, by
  946. performing more complete analysis of the change made to a file that will
  947. enable us to know more accurately whether we need to reweave and if we
  948. do then what we need to reweave - we just haven't gotten around to
  949. implementing them yet.
  950. [[devtools]]
  951. == Integrating AspectJ into your development environment
  952. *Q:* How do I know which aspects affect a class when looking at that
  953. class's source code?
  954. *A:* When you are working with the IDE support, you can get an
  955. understanding of which aspects affect any class. This enables AspectJ
  956. programmers to get the benefits of modularizing crosscutting concerns
  957. while still having immediate access to what aspects affect a class.
  958. For example, the link:devguide/index.html[Development Environment Guide]
  959. shows that you can list
  960. or navigate between method and advice affecting that method and between
  961. a type and declarations in an aspect on that type. (The IDE support may
  962. have more features than that, depending on the IDE. See
  963. #q:integrateWithDevTools[Q: How well does AspectJ integrate with
  964. existing Java development tools?] for more information on which Java
  965. development environments are supported.)
  966. When you are looking at documentation for AspectJ 1.0 programs, `ajdoc`
  967. will provide links from aspects and advice to the affected code, but it
  968. provides less information than the IDE support because it only parses
  969. declarations.
  970. When you are compiling your program, pointcuts that are
  971. statically-determinable can be used in declare statements to identify
  972. the code picked out by the pointcut. (A pointcut is statically
  973. determinable if it only uses the pointcut designators `within`,
  974. `withincode`, `execution`, `call`, `get`, `set`, `initialiation`, and
  975. `staticinitialiation`.) The compiler will list the static code points
  976. which will be affected by any advice specifying the same pointcut. For
  977. example, the following will print a warning whereever some code in class
  978. Bar gets a field value from Foo:
  979. [source, java]
  980. ....
  981. declare warning: get(* Foo.*) && within(Bar)
  982. : "reading Foo state from Bar";
  983. ....
  984. When you are running your program, you can trace advice as it executes.
  985. This enables you to identify advice on join points picked out
  986. dynamically, which cannot be reflected precisely by IDE support. For a
  987. related tracing question, see #q:seeingjoinpoints[Q:I don't understand
  988. what join points exist. How can I see them?]
  989. *Q:* What kind of IDE support is available for developing AspectJ
  990. programs?
  991. *A:* See #q:integrateWithDevTools[Q: How well does AspectJ integrate
  992. with existing Java development tools?]
  993. *Q:* What plans are there to support my IDE?
  994. *A:* The AspectJ team directly provided components for JBuilder, Forte,
  995. and Emacs and supported the open-source AspectJ plugin project at
  996. https://eclipse.org/ajdt which uses the AJDE API support for IDE's.
  997. Supporting new IDE's is a matter of building on the AJDE API's, mostly
  998. likely adopting one of the existing open-source IDE extensions as a
  999. design template. Here are the IDE's where we know people have expressed
  1000. interest, so interested developer may want to join with others in their
  1001. developer communities to build the integration.
  1002. * IDEA/IntelliJ has an enthusiastic community and the developers are
  1003. working on an extensibility API - https://intellij.com
  1004. * jEdit comes from a very active open-source community.
  1005. * Some have suggested Codeguide from Omnicore
  1006. https://www.omnicore.com[https://www.omnicore.com/]
  1007. For questions on AJDE, join the developer's list
  1008. `aspectj-dev@eclipse.org`. For questions on the current IDE
  1009. integrations, contact those projects.
  1010. *Q:* Can I port AJDE support to my development environment?
  1011. *A:* Yes. The core AJDE API is extensible and the source code is
  1012. available for download. Start by studying the sources for the existing
  1013. IDE support linked off the AspectJ site https://eclipse.org/aspectj.
  1014. *Q:* I want the aspects for development builds but remove them for
  1015. production builds. How can I set up the build system so they are
  1016. unpluggable? And so I use `javac` in my production build?
  1017. *A:* If you are using development-time-only aspects - aspects that only
  1018. exist when you are developing the code, not when you ship it - you can
  1019. use implement a hybrid build process by listing the production source
  1020. files into a javac-compliant argfile, and the development source files
  1021. in another ajc argfiles:
  1022. [source, text]
  1023. ....
  1024. -- file "production.lst":
  1025. One.java
  1026. two/Three.java
  1027. ...
  1028. -- file "tracing.lst":
  1029. trace/Library.java
  1030. Trace.java
  1031. -- file "development.lst":
  1032. @production.lst
  1033. @tracing.lst
  1034. ....
  1035. Then your development build can use `ajc`:
  1036. [source, text]
  1037. ....
  1038. ajc @development.lst
  1039. ....
  1040. And your development build can use `ajc` or `javac` or `jikes`:
  1041. [source, text]
  1042. ....
  1043. jikes @production.lst
  1044. ....
  1045. *Q:* We compile module jars and then assemble them. Can we continue this
  1046. with AspectJ?
  1047. *A:* Aspects apply to everything in a namespace, as if everything is
  1048. compiled together. Sometimes you can break the build down into separate
  1049. steps without breaking this model, but we haven't stated exactly where
  1050. it could break because it depends on the interactions between all types.
  1051. You can try the approaches below, but remember to rebuild everything in
  1052. one go if there are problems.
  1053. The simplest scenario is when the aspects apply to all modules and the
  1054. modules compile without the aspects. In that case, weaving in the
  1055. aspects is just the final assembly step for the build.
  1056. Next is the case where the aspects make changes to a common library that
  1057. are visible to other clients, which themselves are otherwise unaffected
  1058. by the aspects. In this case, the common library can be built using ajc,
  1059. and used on the classpath for the module builds:
  1060. [source, text]
  1061. ....
  1062. ajc -outjar common.jar -sourceroots "aspectj-src:src" ...
  1063. cd ../otherProject
  1064. javac -classpath "../common/common.jar:${aspectjrt.jar}" {src}
  1065. ....
  1066. Combining these last two, there's the case where a common set of aspects
  1067. should affect two or more modules that are in a dependency relationship
  1068. to one another. It should work to reuse the aspects in binary form for
  1069. each compile, in dependency order:
  1070. [source, text]
  1071. ....
  1072. ajc -outjar common-aspects.jar
  1073. -sourceroots "aspectj-src" ...
  1074. ajc -outjar common.jar
  1075. -sourceroots "src"
  1076. -aspectpath common-aspects.jar ...
  1077. cd ../module1
  1078. ajc -outjar module1.jar
  1079. -sourceroots "src"
  1080. -classpath common.jar
  1081. -aspectpath ../common-aspects.jar ...
  1082. cd ../module2
  1083. ajc -outjar module2.jar
  1084. -sourceroots "src"
  1085. -classpath "common.jar;../module1.jar"
  1086. -aspectpath ../common-aspects.jar ...
  1087. ....
  1088. If two modules are visibly affected by aspects and mutually-dependent,
  1089. the only thing to do is compile them together.
  1090. It's safest to assume that all aspects can affect all types in a
  1091. namespace; using build boundaries to effect crosscutting limits causes a
  1092. dangerous dependency on the build process and might cause problems.
  1093. *Q:* We use modules and would like to use incremental compilation. Is
  1094. that possible?
  1095. *A:* Just incrementally-compile the whole system. Specify to ajc the
  1096. modules as multiple source roots (or input jars if you are weaving
  1097. libraries).
  1098. In Eclipse's AJDT, you can create a top-level project with symbolic
  1099. links out to the sources:
  1100. [source, text]
  1101. ....
  1102. app-assembly/
  1103. {link common/aspects}
  1104. {link common/src}
  1105. {link module1/src}
  1106. ...
  1107. ....
  1108. Then everything is part of one huge incremental compile. Also, you can
  1109. close this master project and work the others using the Java compiler or
  1110. AJDT.
  1111. The links make incremental development possible without affecting the
  1112. modularized Ant builds. (Our practice runs along those lines.)
  1113. [[notes]]
  1114. == Programming notes and tips
  1115. *Q:* Is it possible to change methods by introducing keywords (like
  1116. `synchronized`), adding parameters, or changing the "throws" clause?
  1117. *A:* AspectJ does not enable you to change the signature of a method,
  1118. but you can (by express declaration) work around some limits imposed by
  1119. the signature. You can convert a checked exception to unchecked using
  1120. `declare soft`, privileged aspects have access to private methods, and
  1121. you can use a percflow aspect to ferry additional state to a callee
  1122. without changing intervening signatures. For more details, see
  1123. link:progguide/index.html[The AspectJ Programming Guide]. In the case of
  1124. `synchronized`, we have what we consider a better solution that uses
  1125. around advice instead of introduction. This solution is described in
  1126. https://aspectj.org/pipermail/users/2000/000534.html[this thread (no
  1127. longer available)] on the AspectJ users list, with some
  1128. https://aspectj.org/pipermail/users/2000/000536.html[additional comments
  1129. (no longer available)] .
  1130. *Q:* I don't understand what join points exist. How can I see them?
  1131. *A:* You can trace them using using an aspect. For example, you can
  1132. start logging at a particular method call and see what join points occur
  1133. after the call and before it returns.
  1134. Here's some code Jim Hugunin wrote to trace join points and posted to
  1135. the users list. To reuse the aspect, define a subaspect and implement
  1136. the pointcuts, for example:
  1137. [source, java]
  1138. ....
  1139. aspect JoinPointSampleAspect extends aj.TraceJoinPoints {
  1140. protected pointcut entry() :
  1141. execution(static void JoinPointSample.main(String[]));
  1142. protected pointcut exit() :
  1143. call(static void JoinPointSampleAspect.exit());
  1144. public static void main (String[] args) {
  1145. JoinPointSample.main(args);
  1146. JoinPointSampleAspect.exit();
  1147. }
  1148. public static void exit() {}
  1149. }
  1150. class JoinPointSample {
  1151. public static void main(String[] args) {}
  1152. }
  1153. ....
  1154. Here's the aspect:
  1155. [source, java]
  1156. ....
  1157. /* TraceJoinPoints.java */
  1158. package aj;
  1159. import org.aspectj.lang.*;
  1160. import org.aspectj.lang.reflect.*;
  1161. import java.io.*;
  1162. public abstract aspect TraceJoinPoints {
  1163. protected abstract pointcut entry();
  1164. protected pointcut exit(): call(* java..*.*(..));
  1165. // this line is for AspectJ 1.1; for 1.0, use "dominates"
  1166. declare precedence : TraceJoinPoints, *;
  1167. final pointcut start(): entry() && !cflowbelow(entry());
  1168. final pointcut trace():
  1169. cflow(entry()) && !cflowbelow(exit()) && !within(TraceJoinPoints+);
  1170. before(): start() { makeLogStream(); }
  1171. before(): trace() { logEnter(thisJoinPointStaticPart); }
  1172. after(): trace() { logExit(thisJoinPointStaticPart); }
  1173. after(): start() { closeLogStream(); }
  1174. //------------ added
  1175. /**
  1176. * Emit a message in the log, e.g.,
  1177. * <pre>TraceJoinPoints tjp = TraceJoinPoints.aspectOf();
  1178. * if (null != tjp) tjp.message("Hello, World!");</pre>
  1179. */
  1180. public void message(String s) {
  1181. out.println("<message>" + prepareMessage(s) + "</message>");
  1182. }
  1183. public void message(String sink, String s) {
  1184. if (null == sink) {
  1185. message(s);
  1186. } else {
  1187. out.println("<message sink=" + quoteXml(sink)
  1188. + " >" + prepareMessage(s) + "</message>");
  1189. }
  1190. }
  1191. protected String prepareMessage(String s) { return s; } // XXX implement
  1192. //--------- end of added
  1193. PrintStream out;
  1194. int logs = 0;
  1195. protected void makeLogStream() {
  1196. try {
  1197. out = new PrintStream(new FileOutputStream("log" + logs++ + ".xml"));
  1198. } catch (IOException ioe) {
  1199. out = System.err;
  1200. }
  1201. }
  1202. protected void closeLogStream() {
  1203. out.close();
  1204. }
  1205. int depth = 0;
  1206. boolean terminal = false;
  1207. protected void logEnter(JoinPoint.StaticPart jp) {
  1208. if (terminal) out.println(">");
  1209. indent(depth);
  1210. out.print("<" + jp.getKind());
  1211. writeSig(jp);
  1212. writePos(jp);
  1213. depth += 1;
  1214. terminal = true;
  1215. }
  1216. void writeSig(JoinPoint.StaticPart jp) {
  1217. out.print(" sig=");
  1218. out.print(quoteXml(jp.getSignature().toShortString()));
  1219. }
  1220. void writePos(JoinPoint.StaticPart jp) {
  1221. SourceLocation loc = jp.getSourceLocation();
  1222. if (loc == null) return;
  1223. out.print(" pos=");
  1224. out.print(quoteXml(loc.getFileName() +
  1225. ":" + loc.getLine() +
  1226. ":" + loc.getColumn()));
  1227. }
  1228. String quoteXml(String s) {
  1229. return "\"" + s.replace('<', '_').replace('>', '_') + "\"";
  1230. }
  1231. protected void logExit(JoinPoint.StaticPart jp) {
  1232. depth -= 1;
  1233. if (terminal) {
  1234. out.println("/>");
  1235. } else {
  1236. indent(depth);
  1237. out.println("</" + jp.getKind() + ">");
  1238. }
  1239. terminal = false;
  1240. }
  1241. void indent(int i) {
  1242. while (i-- > 0) out.print(" ");
  1243. }
  1244. }
  1245. ....
  1246. Note that if you are using AspectJ 1.0, the line starting with
  1247. `declare precedence` would be removed, and the aspect declaration would
  1248. look like `aspect TraceMyJoinPoints dominates *`.
  1249. *Q:* What is the difference between call and execution join points?
  1250. *A:* Briefly, there are two interesting times when a constructor or
  1251. method is run. Those times are when it is called, and when it actually
  1252. executes.
  1253. The main difference is that a call join point happens outside of the
  1254. target object (for non-static methods) or class (for static methods and
  1255. constructors), and that an execution join point happens inside the
  1256. object or class. This means that the `within` and `withincode` pointcuts
  1257. pick them out differently: A call join point is picked out within the
  1258. caller, while an execution join point is picked out where it is actually
  1259. defined.
  1260. A call join point is the ``outermost'' join point for a particular call.
  1261. Once a call join point proceeds, then a number of different things
  1262. happen. For non-static methods, for example, method dispatch happens,
  1263. which will cause one method execution join point -- perhaps more, if
  1264. there are super calls. For constructors, the super constructor is
  1265. called, and fields are initialized, and then various constructor
  1266. execution join points will occur.
  1267. A call join point matches only the ``external'' calls of a method or
  1268. constructor, based on a signature, and it does not pick out calls made
  1269. with `super`, or `this` constructor calls.
  1270. Here's more detail:
  1271. Consider method execution in Java as (1) the initial call from this
  1272. object to some method on the target object with a particular signature;
  1273. and (2) the execution of the actual code in the particular method
  1274. dispatched in the target object. The call join point starts with the
  1275. initial call and ends when control returns to the call (by return or
  1276. perhaps thrown exception). The execution join point starts with the
  1277. method body and ends when the body completes (again by return or
  1278. throwing an exception), so the execution join point always happens
  1279. within the bounds of the corresponding call join point. You can see this
  1280. if you use the join-point tracing aspect in see #q:seeingjoinpoints[Q:I
  1281. don't understand what join points exist. How can I see them?].
  1282. As you would expect, the context differs in advice on pointcuts picking
  1283. out execution and call join points; for call, `this` refers to the
  1284. caller, whereas for execution `this` refers to the called (executing)
  1285. object.
  1286. There are some subtle interactions with other AspectJ semantics. First,
  1287. the meaning of the signature in the `execution()` and `call()` pointcut
  1288. designators (PCD's) differ: the call type depends upon the type of the
  1289. reference making the call, while the execution type depends on the
  1290. enclosing class. Second, you may choose one over another if you cannot
  1291. bring all your sources within the code the compiler controls (described
  1292. in the link:progguide/semantics.html[appendix] to the
  1293. `Programming Guide`). For example, to trace calls into a method from
  1294. classes which are outside the code the compiler controls at compile
  1295. time, then using `execution()` will work while using `call()`may not.
  1296. Finally, since `super` invocations are not considered method calls, to
  1297. trace `super.foo()` would require using `execution`.
  1298. Because of differences in the way AspectJ 1.0 and 1.1 are implemented,
  1299. in 1.0 you should use the `call()` pointcut designator unless you have a
  1300. good reason to use `execution()`; in AspectJ 1.1, the reverse is true.
  1301. *Q:* What is the difference between cflow and cflowbelow?
  1302. *A:* Both pick out all the join points in the control flow of the
  1303. specified join points. They differ only in that the `cflowbelow()`
  1304. pointcut designator does not pick out the join points specified, while
  1305. `cflow()` does.
  1306. *Q:* How do I say that I want the topmost entrypoint in a recursive
  1307. call? How about the most-recent prior entrypoint?
  1308. *A:* This is best seen by way of example. Given a recursive call to
  1309. `int factorial(int)` you can print the arguments for (a) the current and
  1310. most-recent recursive call or (b) the current and original recursive
  1311. call:
  1312. [source, java]
  1313. ....
  1314. aspect LogFactorial {
  1315. pointcut f(int i) : call(int factorial(int)) && args(i);
  1316. // most-recent
  1317. before(int i, final int j) : f(i) && cflowbelow(f(j)) {
  1318. System.err.println(i + "-" + j);
  1319. }
  1320. // original
  1321. before(int i, final int j) : f(i)
  1322. && cflowbelow(cflow(f(j)) && !cflowbelow(f(int))) {
  1323. System.err.println(i + "@" + j);
  1324. }
  1325. }
  1326. ....
  1327. *Q:* What is the difference between constructor call, constructor
  1328. execution, initialization, and static initialization join points?
  1329. *A:* Static initialization pertains to initialization of a class or
  1330. interface type. Constructor call and execution are akin to method call,
  1331. and initialization generalizes this and picks out the first constructor
  1332. called.
  1333. Their relations are best demonstrated by tracing the join points. Below
  1334. is the class Test which implements an interface and extends a class
  1335. along with a trace of the join points below and including the
  1336. constructor call obtained using `TraceJointPoints.java` from
  1337. #q:seeingjoinpoints[Q:I don't understand what join points exist. How can
  1338. I see them?].
  1339. [source, java]
  1340. ....
  1341. public class Init {
  1342. public static void main (String[] args) {
  1343. new Test();
  1344. end();
  1345. }
  1346. static void end() {}
  1347. }
  1348. class Super {}
  1349. interface I {}
  1350. class Test extends Super implements I {
  1351. Test() {}
  1352. }
  1353. ....
  1354. For a program compiled with AspectJ 1.0, the result is this:
  1355. [source, xml]
  1356. ....
  1357. <constructor-call sig="Test()" >
  1358. <staticinitialization sig="Super._init_" />
  1359. <staticinitialization sig="Test._init_" />
  1360. <initialization sig="Super()" >
  1361. <instanceinitializer-execution sig="Super._init_" />
  1362. <constructor-execution sig="Super()" />
  1363. </initialization>
  1364. <initialization sig="I()" >
  1365. <instanceinitializer-execution sig="I._init_" />
  1366. <constructor-execution sig="I()" />
  1367. </initialization>
  1368. <initialization sig="Test()" >
  1369. <instanceinitializer-execution sig="Test._init_" />
  1370. <constructor-execution sig="Test()" />
  1371. </initialization>
  1372. </constructor-call>
  1373. ....
  1374. Ordinarily, using a `call` pointcut designator is best because the call
  1375. join point surrounds the others, but in the case of constructors there
  1376. is no target object for the call (because it has not been constructed
  1377. yet), so you might prefer to use the `initialization` pointcut
  1378. designator.
  1379. *Q:* How do I work with an object right when it is created?
  1380. *A:* You can advise some form of constructor join point. Constructors
  1381. are tricky in Java, and that's exposed in AspectJ. Here are some rules
  1382. of thumb:
  1383. * If you want the join point on the "outside" of object creation, use
  1384. after returning from call to the constructor:
  1385. +
  1386. [source, java]
  1387. ....
  1388. after() returning (Foo newlyCreatedObject): call(Foo.new(..)) { ... }
  1389. ....
  1390. +
  1391. You might be tempted to use "this" or "target" to expose the new object,
  1392. but remember that if you're on the "outside" of object creation, the
  1393. object itself might not be created yet... it only exists "on the way
  1394. out", when you return the object.
  1395. * If you want the join point inside a particular constructor, use:
  1396. +
  1397. [source, java]
  1398. ....
  1399. after(Foo newlyCreatedObject) returning: this(newlyCreatedObject) && execution(Foo.new(..)) { ... }
  1400. ....
  1401. +
  1402. Remember, though, that if you use "before" advice here, the body of the
  1403. constructor will not have run, and so the object may be somewhat
  1404. uninitialized.
  1405. * In the rare case that there are all sorts of constructors for the
  1406. object that call each other with `this(...)` and you want exactly one
  1407. join point for each initialization of `Foo`, regardless of the path of
  1408. constructors it takes, then use:
  1409. +
  1410. [source, java]
  1411. ....
  1412. after(Foo f) returning: this(f) && initialization(Foo.new(..)) { ... }
  1413. ....
  1414. *Q:* I want advice to run at two join points, but it doesn't run at all.
  1415. What gives?
  1416. *A:* This usually reflects both a conceptual error and a programming
  1417. mistake. Most likely you want to do something like "run the advice for
  1418. all public and private calls," and the code looks something like this:
  1419. [source, java]
  1420. ....
  1421. within(com.xerox.printing..*) && call(public * *(..)) && call(private * *(..))
  1422. ....
  1423. But a pointcut is evaluated at *each* join point. The expression above
  1424. would never pick out any call join point, because no method signature
  1425. has both public and private access. In a pointcut, `pc1() && pc2()`
  1426. means both must be true at a given join point for advice to run at that
  1427. join point. The correct pointcut would use `||` as follows:
  1428. [source, java]
  1429. ....
  1430. within(com.xerox.printing..*) && (call(public * *(..)) || call(private * *(..)))
  1431. ....
  1432. Then the advice will run at the join point.
  1433. *Q:* How do I refer to a static field when my advice crosscuts multiple
  1434. classes?
  1435. *A:* There is no way in advice to refer to the type of the code
  1436. executing in a static context except by specification. This makes it
  1437. impossible to refer to static members using runtime information.
  1438. However, AspectJ can determine the class for something in the join point
  1439. context, which you can use as a per-class key. Then you can actually
  1440. declare an instance field to contain the per-class value (see the next
  1441. question). This comes at the cost of an extra reference, but the field
  1442. can be final.
  1443. *Q:* I would like to reuse a type pattern, e.g., to write advice that is
  1444. limited to a certain set of classes. Do I have to retype it each time?
  1445. *A:* No. You can declare that all the types implement an interface you
  1446. define, and then use the interface type in your program. For example:
  1447. [source, java]
  1448. ....
  1449. /**
  1450. * Example of using an interface to represent a type pattern.
  1451. * sub-aspects use declare parents to add to traced types, e.g.,
  1452. * declare parents: com.mycompany.whatever..* implements Marked;
  1453. */
  1454. abstract aspect MarkerExample {
  1455. /** marker interface for types that we want to trace */
  1456. interface Marked {}
  1457. /** calls to an instance of Marked not from an instance of Marked */
  1458. pointcut dynamicCallsIn(): call(* *(..)) && target(Marked) && !this(Marked);
  1459. /** calls to methods defined by a subtype of Marked
  1460. * that don't come from the body of a subtype of Marked
  1461. */
  1462. pointcut staticCallsIn(): call(* Marked+.*(..)) && !within(Marked+);
  1463. /** print dynamic calls */
  1464. before(): dynamicCallsIn() { System.out.println("before " + thisJoinPoint); }
  1465. }
  1466. aspect MyMarker extends MarkerExample {
  1467. declare parents: com.mycompany.whatever..* implements Marked;
  1468. }
  1469. ....
  1470. *Q:* Where do I find example programs and how-to's?
  1471. *A:* There are a number of places to find sample code and instructions
  1472. for using AspectJ with other programming tools.
  1473. [arabic]
  1474. . The AspectJ release includes examples in its `doc` directory.
  1475. . There is a community repository of sample code and tutorials in the
  1476. AspectJ CVS tree `docs` module `sandbox` directory. These are extracted
  1477. and published (online only)
  1478. https://dev.eclipse.org/viewcvs/indextech.cgi/~checkout~/aspectj-home/sample-code.html[here]
  1479. .
  1480. . The `teaching` directory of the `docs` module contains public
  1481. materials the AspectJ committers use for presentations, some of which
  1482. include example code. To access CVS, see xref:#buildingsource[Q:How do I
  1483. get and compile the source code for AspectJ?].
  1484. . The archives for the user and developer mailing lists contain many
  1485. good examples. To search the archives, see #q:searchingsite[Q:How can I
  1486. search the email archives or the web site?].
  1487. This code can vary in quality. Code that we publish or include with
  1488. AspectJ is generally correct. However, code found in our CVS tree might
  1489. not have been tested thoroughly, and code from the mailing lists might
  1490. be untested or use older versions of the language.
  1491. *Q:* Are aspect libraries available?
  1492. *A:* Some libraries are distributed in the release under the examples
  1493. folder in the distribution. These are "libraries" in the sense that they
  1494. are reusable, but they are delivered in source form. Similarly, some of
  1495. the sample code is reusable; for that, see #q:exampleprograms[Q:Where do
  1496. I find example programs and how-to's?]. If you develop such a library
  1497. and want to make it available to other users, feel to send it to the
  1498. users mailing list `aspectj-users@eclipse.org`.
  1499. In AspectJ 1.1, ajc supports binary aspects, so you can distribute
  1500. aspect libraries without distributing the source. For more information,
  1501. see the `-aspectpath` option in the link:devguide/ajc-ref.html[Reference
  1502. for ajc].
  1503. *Q:* How does `ajc` interact with the `serialVersionUID`?
  1504. *A:* The current version of `ajc` can change the `serialVersionUID` of
  1505. generated `.class` files as a result of weaving in advice. This is an
  1506. important fact that developers using both aspects and serialization
  1507. should be aware of. It is likely that a future version of the compiler
  1508. will be better behaved regarding the `serialVersionUID`.
  1509. However, changes to the `serialVersionUID` attribute are typically only
  1510. important when using serialization for the long-term persistence of
  1511. objects. Using standard Java serialization for long-term persistence has
  1512. a number of drawbacks and many developers already use alternative
  1513. solutions. For one possibly standard solution, see
  1514. https://jcp.org/jsr/detail/057.jsp[Long-Term Persistence for JavaBeans
  1515. Specification] .
  1516. *Q:* How can I use AspectJ with applets?
  1517. *A:* Just include the aspectjrt.jar as a required archive. For example,
  1518. here is the HTML code for an HTML editor applet that contains some
  1519. debugging aspects:
  1520. [source, xml]
  1521. ....
  1522. <APPLET
  1523. CODE='com.company.swing.applets.EditorApplet'
  1524. WIDTH='700'
  1525. HEIGHT='525'>
  1526. <PARAM NAME="CODE" VALUE="com.company.swing.applets.EditorApplet" >
  1527. <PARAM NAME="ARCHIVE"
  1528. VALUE ="../company-applets.jar,../aspectjrt.jar,../xmlrpc-applet.jar" >
  1529. <PARAM NAME="type" VALUE="application/x-java-applet;version=1.4">
  1530. <PARAM NAME="scriptable" VALUE="false">
  1531. </APPLET>
  1532. ....
  1533. The above markup has worked reliably with the Java Plugin (included in
  1534. the JRE 1.4.x) in IE 6, Mozilla 1.1 (Win32), and Mozilla 1.0.1 (Red Hat
  1535. Linux 8.0). The following link describes how to configure
  1536. Mozilla/Netscape 6.x/7.x to use the Java Plugin from a JRE/SDK
  1537. installation: https://java.sun.com/j2se/1.4.1/manual_install_linux.html.
  1538. (Thanks to Chris Bartling for this answer.)
  1539. *Q:* How can I specify types for advice that captures primitives, void,
  1540. etc.?
  1541. *A:* In some cases, AspectJ allows conversion from values of primitive
  1542. types to Object, so that highly polymorphic advice may be written. This
  1543. works if an advice parameter or the return type for around is typed to
  1544. Object. So:
  1545. [source, java]
  1546. ....
  1547. class Test {
  1548. static int i;
  1549. public static void main(String[] args) {
  1550. i = 37;
  1551. }
  1552. }
  1553. aspect TraceSet {
  1554. before(Object val): set(* Test.*) && args(val) {
  1555. System.err.println(val);
  1556. System.err.println(val.class);
  1557. }
  1558. }
  1559. ....
  1560. will print out
  1561. [source, text]
  1562. ....
  1563. 37
  1564. java.lang.Integer
  1565. ....
  1566. For more information, see the Programming Guide
  1567. link:progguide/semantics-pointcuts.html[semantics section "Context
  1568. Exposure"] .
  1569. *Q:* How do I detect which version I am running?
  1570. *A:* The `ajc` compiler emits the version when passed the `-version`
  1571. flag as an argument.
  1572. To programmatically detect the version of the AspectJ runtime while
  1573. running under Java 1.4 or later, get the version from the package:
  1574. [source, java]
  1575. ....
  1576. Package lang = org.aspectj.lang.JoinPoint.class.getPackage();
  1577. String version = lang.getImplementationVersion();
  1578. ....
  1579. When running under Java 1.3 or earlier, read the manifest directly. For
  1580. example code, see the source for
  1581. `AjBuildManager.checkRtJar(AjBuildConfig)` in the
  1582. `org.aspectj.ajdt.internal.core.builder` package of the
  1583. `org.aspectj.ajdt.core` module, available as described in
  1584. xref:#buildingsource[Q:How do I get and compile the source code for
  1585. AspectJ?].
  1586. Note that the version of AspectJ for the tools in `aspectjtools.jar` is
  1587. in `org.aspectj.bridge.Version`.
  1588. *Q:* How do I write synchronized advice?
  1589. *A:* The only modifier advice can take is `strictfp`. However, you can
  1590. enclose the body of the advice in a synchronized clause:
  1591. [source, java]
  1592. ....
  1593. before() : pc() {
  1594. synchronized (this) {
  1595. // advice code here
  1596. }
  1597. }
  1598. ....
  1599. It should not be necessary to synchronize a percflow aspect, but you
  1600. might do this for perthis, pertarget, or issingleton (default) aspects.
  1601. To serialize advice in multiple aspects, synchronize on a lock object
  1602. available (only) to the aspects.
  1603. [[problems]]
  1604. == Common Problems
  1605. *Q:* When I run, I get a `StackOverflowError` (or a long stack trace or
  1606. no output whatsoever)
  1607. *A:* Most likely this is a case of infinite recursion, where advice is
  1608. advising itself. It presents as a `StackOverflowError` or silence as the
  1609. VM exhausts itself in the recursion.
  1610. Of course, infinite recursion is possible in Java:
  1611. [source, java]
  1612. ....
  1613. public class Main {
  1614. public static void main(String[] args) {
  1615. try {
  1616. main(args);
  1617. } finally {
  1618. main(args);
  1619. }
  1620. }
  1621. }
  1622. ....
  1623. If you compile and run this program, and it will fail silently, trying
  1624. to process the finally clause even after throwing the
  1625. StackOverflowError.
  1626. Here's a similar AspectJ program where the recursion is not so obvious:
  1627. [source, java]
  1628. ....
  1629. aspect A {
  1630. after(): call(* *(..)) { System.out.println("after " + thisJoinPoint); }
  1631. }
  1632. ....
  1633. This re-invokes itself because it advises any call. It invokes itself
  1634. even after an exception is thrown, since `after` advice, like a finally
  1635. clause, runs even after exceptions are thrown. You can fix this by
  1636. following two practices:
  1637. In AspectJ 1.1, the String concatenation operator (+) is advised in its
  1638. StringBuffer form, so if your advise uses String + in a way that is
  1639. picked out by your pointcut, you will get infinite recursion.
  1640. {empty}(1) Use `after returning` to advise normal completions or
  1641. `after throwing` to advise abrupt completions. If you use `after` or
  1642. `after throwing`, write the advice with the same care you would a
  1643. finally clause, understanding that it may run after some failure.
  1644. {empty}(2) Avoid writing advice that advises itself. One simple way to
  1645. do so is to exclude the code within the current aspect:
  1646. [source, java]
  1647. ....
  1648. aspect A {
  1649. after() returning: !within(A) && call(* *(..)) {
  1650. System.out.println("after " + thisJoinPoint);
  1651. }
  1652. }
  1653. ....
  1654. A better way is often to re-write the pointcut. If the advice is
  1655. advising itself accidentally, that's a sign that the pointcut is not
  1656. saying what you mean.
  1657. [source, java]
  1658. ....
  1659. aspect A {
  1660. pointcut withinTargetClasses() : within(A+) || within(B+);
  1661. after() returning: withinTargetClasses() && call(* *(..)) {
  1662. System.out.println("after " + thisJoinPoint);
  1663. }
  1664. }
  1665. ....
  1666. *Q:* I've declared a field on every class in my package; how do I use it
  1667. in advice?
  1668. [source, java]
  1669. ....
  1670. aspect A {
  1671. boolean com.xerox..*.dirtyFlag;
  1672. after (Object target) returning
  1673. : target(target) && call(* com.xerox..*.set*(..)) {
  1674. target.dirtyFlag = true; // compile fails here
  1675. }
  1676. }
  1677. ....
  1678. *A:* You need a type to refer to any member, field or method. It's
  1679. generally better to introduce onto an interface and declare classes to
  1680. implement the interface, which permits you to use the interface type in
  1681. advice formals.
  1682. [source, java]
  1683. ....
  1684. aspect A {
  1685. interface TrackingSets {}
  1686. boolean TrackingSets.dirtyFlag;
  1687. declare parents : com.xerox..* implements TrackingSets;
  1688. after (TrackingSets target) returning
  1689. : target(target) && call(* com.xerox..*.set*(..)) {
  1690. target.dirtyFlag = true;
  1691. }
  1692. }
  1693. ....
  1694. *Q:* The AspectJ compiler aborts with an OutOfMemoryError when compiling
  1695. many classes. How can I fix this?
  1696. *A:* `ajc` can use more memory than a javac compile of the corresponding
  1697. pure-java sources when aspects are added to the mix. You'll need to
  1698. increase the memory available.
  1699. The command `ajc` is actually a script that launches a Java virtual
  1700. machine with the correct classpath. You should make a copy of this
  1701. script, rename it, and then edit it. Change the -Xmx option, size of
  1702. memory allocation pool (heap). You might try `-Xmx128M` or even
  1703. `-Xmx256M`.
  1704. When running under Ant, give Ant more memory or use the `fork` option
  1705. together with the `Xmaxmem` option.
  1706. When running under an IDE, look to the documentation for the IDE to
  1707. determine how to increase available memory.
  1708. In either case, doing incremental compilations can hold on to more
  1709. memory than a one-shot compile process, as the compiler trades space for
  1710. time in recompiles.
  1711. *Q:* Why do I get a message that my class is already defined?
  1712. *A:* Most commonly, a source file was specified twice on the command
  1713. line (e.g., directly and by a *.java entry in a .lst file). However,
  1714. sometimes you have defined a class in two files in the same package, and
  1715. you need to rename the class or change its scope. You should get this
  1716. message from any Java compiler.
  1717. *Q:* `ajc` recompiles all files every time. How can I make it recompile
  1718. only the files that have changed?
  1719. *A:* `ajc` 1.0 does not support incremental compilation, but since 1.1
  1720. `ajc` does when passed the `-incremental` option. It may still recompile
  1721. files that have not changed, if they could be affected by aspects in
  1722. particular ways, but the files compiled should be fewer and result in
  1723. faster compiles. Further, the 1.1 release supports binary weaving, so
  1724. you need not recompile if you already have .class files.
  1725. *Q:* `ajc` is using the wrong JVM. How do I fix it?
  1726. *A:* The easiest way to fix this is to re-install `ajc` (using the same
  1727. `.class` or `.exe` file that you originally downloaded) and this time
  1728. make sure to tell it to use the desired JDK (typically the JDK versions
  1729. 1.2 or 1.3 from Sun).
  1730. If you are familiar with DOS batch files or shell programming, you could
  1731. also fix this by simply editing the `bin\ajc.bat` or `bin/ajc` script.
  1732. *Q:* My IDE is trying to parse AspectJ files which makes my project
  1733. unusable. What can I do?
  1734. *A:* When working with an unsupported IDE that objects to the syntax of
  1735. AspectJ source files (and, e.g., automatically gathers them in a source
  1736. tree as Java files based on the .java extension), you can use the .aj
  1737. extension for your AspectJ files. The ajc compiler accepts both .java
  1738. and .aj files, and you can set up your build scripts to include the
  1739. correct list of source files. (You will have to find another editor for
  1740. editing AspectJ files and possible viewing crosscutting structure.)
  1741. *Q:* I used to be able to compile my program in my IDE, but when I use
  1742. AJDE, I run out of memory (or it goes really slow).
  1743. *A:* The ajc compiler does more analysis than (e.g.,) javac, and AJDE
  1744. may in some IDE's hold a copy of the structure tree until the next tree
  1745. is available from the compile process. Both mean that you may need extra
  1746. memory to compile the same program. However, increasing available memory
  1747. to the point that you are swapping to disk can slow the process
  1748. considerably.
  1749. If you are having problems and would like to find the optimal memory
  1750. allocation, iteratively decrease the amount of memory available until
  1751. AJDE or ajc signals out-of-memory errors, and then increase that amount
  1752. by 5-10%.
  1753. To increase memory for the ajc compiler, see #q:ajcoom[Q:The AspectJ
  1754. compiler aborts with an OutOfMemoryError when compiling many classes.
  1755. How can I fix this?]. For your IDE, do something similar or follow the
  1756. provider's instructions. For example, to increase memory, add something like
  1757. this to your IDE's config file or start script:
  1758. [source, text]
  1759. ....
  1760. vmparam -Xmx384m
  1761. ....
  1762. If it turns out that your project is too big to use with AJDE, your IDE
  1763. may nonetheless support external commands or Ant build processes, which
  1764. run outside the IDE memory space. For a JBuilder Ant plugin, some people
  1765. have directed us to https://antrunner.sourceforge.net[].
  1766. *Q:* When I run, I get a `NoAspectBoundException` or a ClassNotFound
  1767. message for `NoAspectBoundException`.
  1768. *A:* This happens when an aspect is not associated with an object that
  1769. is being advised. We have seen this happen two ways:
  1770. * You get a ClassNotFound message for `NoAspectBoundException` when
  1771. loading a class affected by aspects if `aspectjrt.jar` classes are not
  1772. on the runtime classpath. To fix this, put the classes on the classpath.
  1773. * You can get a `NoAspectBoundException` when there is a cycle in aspect
  1774. initialization or static initialization, most commonly when an aspect
  1775. advises its own initializer. To fix this, first find the class that
  1776. fails to load by running java in debug mode or looking at the
  1777. `NoAspectBoundException` trace, and then fix the offending (probably
  1778. unintended) dependency. Most often, it comes from a pointcut like
  1779. `staticinitialization(com.company..*)` or `within(com.company..*)`,
  1780. which can include any aspects in the same subpackages. You can avoid
  1781. advising most join points associated with the aspect `TheAspect` by
  1782. adding `&& !within(TheAspect)` to your pointcut.
  1783. *Q:* My stack traces don't make sense. What gives?
  1784. *A:* In 1.0, unless you are using the `ajdb` debugger, stack traces may
  1785. have synthetic methods in the stack, and the line numbers may not track
  1786. your source code. The link:devguide/index.html[Development Environment
  1787. Guide] discusses how to interpret stack at the end of the
  1788. link:devguide/ajc-ref.html[Reference for ajc].
  1789. In 1.1, line numbers should work correctly. The only difference from a
  1790. normal stack might be the addition of extra stack frames for call-backs.
  1791. *Q:* My advice is not running (or running twice), and I don't know why.
  1792. *A:* When advice is not running, there is probably a problem in the
  1793. pointcut. Sometimes users specify pointcuts that do not mean what they
  1794. intend - most often when they misspell a type name. Run the compiler in
  1795. `-Xlint` mode, which will flag some likely mistakes, like the type name.
  1796. If that does not work, and your pointcut is staticly-determinable, use a
  1797. declare statement to identify affected code. (For more information, see
  1798. #q:knowWhenAspectsAffectClasses[Q: How do I know which aspects affect a
  1799. class when looking at that class's source code?].) If that does not work
  1800. and your pointcut is dynamically determined, see if your join points are
  1801. executing at all by using TraceJoinPoints.java from
  1802. #q:seeingjoinpoints[Q:I don't understand what join points exist. How can
  1803. I see them?].
  1804. When advice is running more than it should, either (1) your advice is in
  1805. an abstract aspect and the pointcut picks out the same join point for
  1806. more than one concrete instantiation of the aspect, or (2) your pointcut
  1807. picks out more join points than you intend.
  1808. In the case of advice in abstract aspects, the advice will run once for
  1809. each concrete instance of the aspect. If the pointcut for that advice
  1810. picks out the same join point for two concrete aspects, then the correct
  1811. behavior is for the advice to run the advice twice at that join point.
  1812. To see if your pointcut picks out the join points you intend, you can
  1813. use IDE support, logging, or declare-warnings. If you are using IDE
  1814. support, you should be able to trace back from the pointcut or advice to
  1815. the join points which can be statically determined to be affected.
  1816. Without IDE support, you can write declare-warning statements to
  1817. identify code affected by staticly- determinable pointcuts. To identify
  1818. advised dynamic join points, you can try using `TraceJoinPoints.java` as
  1819. above, or update the advice to print the source location of the join
  1820. point. Doing any of these should show if the advice applies to code that
  1821. you did not expect.
  1822. If you've done this and convinced yourself it's not working, it may be a
  1823. bug. See #q:bugreports[Q:How do I submit a bug report?].
  1824. *Q:* My advice runs for each overridden method!
  1825. *A:* Most likely you are advising the method execution join point and
  1826. specifying the defining signature. Since all overriding methods share
  1827. this signature, the advice runs for each method executed. (This happens,
  1828. e.g., when one method invokes the same method in the superclass using
  1829. `super.\{method}(..)`). This is the correct behavior.
  1830. To avoid this, use the `call(..)` pointcut designator, or use
  1831. `!cflow(..)` to pick out only the initial method-execution.
  1832. *Q:* I don't understand when thisEnclosingJoinPointStaticPart is
  1833. available.
  1834. *A:* `thisEnclosingJoinPointStaticPart` is a special variable available
  1835. in the context of advice to refer to the join point, if any, lexically
  1836. enclosing the current join point:
  1837. .thisEnclosingJoinPointStaticPart
  1838. [cols=",",]
  1839. |===
  1840. |One of these... |will be tEJSP for each of these:
  1841. |constructor-execution, method-execution, advice execution,
  1842. initialization, pre-initialization, static initialization
  1843. |constructor-call, method-call, handler, field-set, field-get
  1844. |===
  1845. Expressions in the body of handlers have the same
  1846. `thisEnclosingJoinPointStaticPart` as the handler itself.
  1847. *Q:* I declared a member on a class with package access, but other
  1848. classes in the package cannot see it.
  1849. *A:* When declaring parents on other types from an aspect, package
  1850. access only applies to code the implementation controls. For AspectJ
  1851. 1.0, that is the set of files passed to the compiler. That means other
  1852. classes not compiled with the aspect will not be able to access the
  1853. aspect-declared members even if they are in the same package. The only
  1854. way for classes outside the control of the implementation to access
  1855. aspect-declared members is to declare them public.
  1856. *Q:* I declared a member on a interface, but javac does not see it.
  1857. *A:* You have to compile all the top-level implementating classes of the
  1858. interface using `ajc`. From an email by Jim Hugunin on the requirements
  1859. for AspectJ 1.1 to implement members declared by an aspect on an
  1860. interface:
  1861. If you introduce non-static fields or non-abstract methods on an
  1862. interface from an aspect, then all of the top-most implementors of that
  1863. interface must be woven by that same aspect. (A class C is a top-most
  1864. implementor of an interface I if C implements I and the superclass of C
  1865. does not implement I.)
  1866. *Q:* `ajc` 1.0 complains that it can't find `javac`. What's wrong?
  1867. *A:* `ajc` 1.0 does not try to locate `javac` in your path: it uses the
  1868. `javac` classes directly. In JDK 1.2 and 1.3 these classes are found in
  1869. `tools.jar` (in the `lib` directory of the JDK distribution), which must
  1870. be on your classpath to make `ajc` work with `javac`. Inspect the java
  1871. command that launches ajc to make sure that `tools.jar` is on the
  1872. classpath for ajc; the -classpath option only applies to the sources
  1873. compiled.
  1874. *Q:* I'm running under 1.4, but `ajdoc` asks for 1.3 (or throws
  1875. IllegalAccessError for HtmlWriter.configuration)
  1876. *A:* The 1.0 implementation of `ajdoc` uses specific javadoc classes in
  1877. the J2SE 1.3 tools.jar. We are working on addressing this limitation,
  1878. but in the interim it is best to run ajdoc under 1.3.
  1879. When running from the command-line scripts, edit the scripts directly to
  1880. put the 1.3 tools.jar first on the classpath. (The installer does not
  1881. know about this limitation of ajdoc.)
  1882. When running from Ant, users often have tools.jar in $\{ant.classpath}
  1883. (to make javac, et al work). That makes it impossible to run the ajdoc
  1884. taskdef (which does not currently support forking), so you'll need to
  1885. run a separate ant process, either from the command-line or via Ant's
  1886. exec task (the Ant task will propagate the classpath). If the wrong
  1887. tools.jar is not on the ant classpath, then it should work to put the
  1888. 1.3 tools.jar in the taskdef classpath.
  1889. *Q:* I set up different files to my compiles to change what the aspects
  1890. see, but now I don't understand how the aspects are working.
  1891. *A:* It is a bad practice to use the compilation unit to control
  1892. crosscutting. Aspects and pointcuts especially should be written to
  1893. specify crosscutting precisely. Aspects will behave the same when you
  1894. add files if you initially included all files affected by your aspects.
  1895. If you use the compilation unit, then your code will behave differently
  1896. in AspectJ implementations that do not limit themselves to specified
  1897. files.
  1898. *Q:* I'm reading the code generated by `ajc` 1.0 in `-preprocess` mode,
  1899. and it seems like it would not work (or "like it works this way").
  1900. *A:* The generated code can be difficult for a human to read and
  1901. understand. The compiler uses implementation techniques which might not
  1902. be apparent. To determine if the code is behaving correctly, you should
  1903. write and run a program that attempts to provoke the error you suspect.
  1904. Similarly, you should not rely on invariants you infer from the
  1905. generated code (especially naming conventions for generated members).
  1906. Please rely only on the semantics stated in the appendix of the AspectJ
  1907. link:progguide/index.html[Programming Guide].
  1908. *Q:* I've heard AspectJ can generate or inject code into my code. Is
  1909. this true?
  1910. *A:* This is a misconception spawned from the early implementation.
  1911. AspectJ does not "inject" or "generate" code. In AspectJ the pointcut
  1912. constructs allow the programmer to identify join points, and the advice
  1913. constructs define additional code to run at those join points.
  1914. So the semantic model of advice is like the semantic model of a method
  1915. -- it says "when any of these things happen, do this".
  1916. People who worked with earlier versions of AspectJ, in which ajc was
  1917. very explicitly a pre-processor, sometimes thought of AspectJ as
  1918. injecting code. But that was an artifact of the implementation, not the
  1919. underlying language semantics.
  1920. This distinction is important for two reasons. One is that thinking
  1921. about it this way will make more sense at the implementation continues
  1922. to evolve towards load-time or runtime weaving. The other is that it
  1923. makes it much easier to understand the semantics of advice on cflow
  1924. pointcuts.
  1925. *Q:* Why can't AspectJ pick out local variables (or array elements or
  1926. ...)?
  1927. *A:* Users have sometimes wanted AspectJ to pick out many more join
  1928. points, including
  1929. * method-local field access
  1930. * array-element access
  1931. * loop iteration
  1932. * method parameter evaluation
  1933. Most of these have turned out not to make sense, for a variety of
  1934. reasons:
  1935. * it is not a commonly-understood unit for Java programmers
  1936. * there are very few use-cases for advice on the join point
  1937. * a seemingly-insignificant change to the underlying program causes a
  1938. change in the join point
  1939. * pointcuts can't really distinguish the join point in question
  1940. * the join point would differ too much for different implementations of
  1941. AspectJ, or would only be implementable in one way
  1942. We prefer to be very conservative in the join point model for the
  1943. language, so a new join point would have to be useful, sensible, and
  1944. implementable. The most promising of the new join points proposed are
  1945. for exception throws clauses and for synchronized blocks.
  1946. *Q:* Why doesn't AspectJ pick out reflective calls? The pointcut
  1947. `call(void run())` won't pick out a call using reflection, like
  1948. `((Method)run).invoke(null, args)`.
  1949. *A:* The pointcut `execution(void run())` will work. The call pointcut
  1950. doesn't work because `Method.invoke(..)` is the Java method-call, and
  1951. AspectJ cannot delve into the Java reflection library to implement call
  1952. semantics. To advise a reflective call (e.g., because the compiler does
  1953. not control the code for the method execution), test the context for
  1954. `invoke(..)`. Here's a pointcut that tests only if the method name is
  1955. correct:
  1956. [source, java]
  1957. ....
  1958. aspect A {
  1959. pointcut runReflectiveCall(Method run) : target(run) &&
  1960. call(Object Method.invoke(..)) && if("run".equals(run.getName()));
  1961. before() : runReflectiveCall(Method) {
  1962. System.out.println("before reflective call " + thisJoinPoint);
  1963. }
  1964. }
  1965. ....
  1966. *Q:* What are the bugs now most affecting users?
  1967. *A:* The bugs affecting the semantics of the language are marked with
  1968. the "info" keyword. Find them with the query
  1969. https://bugs.eclipse.org/bugs/buglist.cgi?product=AspectJ&keywords=info
  1970. *Q:* What extra memory is required at runtime?
  1971. *A:* When running classes produced by the AspectJ weaver or compiler,
  1972. there are no significant hidden uses of memory. As would be expected,
  1973. each aspect is instantiated. The per-object aspects (like `pertarget` or
  1974. `perthis`) in some implementations use a map to link aspects and the
  1975. associated object. When using `cflow`-related pointcuts, a `ThreadLocal`
  1976. is used to track control flow for each affected thread.
  1977. Of course, the size and code in an aspect can require memory. Aside from
  1978. normal Java practices, take care with join point references. When
  1979. referencing the static part of a join point (e.g.,
  1980. `thisJoinPointStaticPart`), only one object is created. However, if you
  1981. reference the join point itself (e.g., `thisJoinPoint`), then one
  1982. `JoinPoint` object will be created for each join point running advice.
  1983. Aspect instances will be garbage collected just like regular objects
  1984. after there are no more strong references to them. For the default
  1985. aspect instantiation model, `issingleton`, the aspect class retains a
  1986. reference to the singleton instance, in order to implement
  1987. `static \{AspectClass} aspectOf()`, so singleton instances will not be
  1988. garbage collected until the class is. For long-running or
  1989. memory-critical programs, consider using weak references in singleton
  1990. aspects for state that should be garbage collected.
  1991. Finally, when using load-time weaving, the weaver can require memory in
  1992. its own right. Because the class loader never can know when it is done
  1993. loading classes, the weaver can hold on to the aspects required to weave
  1994. for some time. There are strategies for minimizing this (with different
  1995. trade-off's), so the time and memory required for load-time weaving will
  1996. vary as load-time weaving evolves.
  1997. *Q:* I get a VerifyError when running CGLIB generated code that has been
  1998. woven by AspectJ. Why is this?
  1999. *A:* When weaving after advice into any piece of code, the AspectJ
  2000. strategy is to make all exit points from that code jump to a single exit
  2001. point that executes the advice before returning. There is a verifier
  2002. rule in the JVM specification that specifies that all routes to a jump
  2003. destination must have the same height stack when they get there,
  2004. regardless of the route taken to get there through the bytecode. The
  2005. CGLIB generated code has different stack heights at the various exit
  2006. points. This is not a problem with the CGLIB generated code, it is
  2007. perfectly valid - it is just unusual and the AspectJ weaving strategy
  2008. causes the verify error to trigger when it makes all exits jump to a
  2009. single destination.
  2010. AspectJ could cope with this and instead implement after advice by
  2011. calling the advice and returning at each exit point. However, it is
  2012. unlikely that the user actually meant to weave the CGLIB generated code
  2013. in the first place - and so usually the right thing to do is to exclude
  2014. CGLIB generate code from the weaving process by appropriate use of the
  2015. exclude element in the aop.xml. A typical clause in the aop.xml might
  2016. look as follows:
  2017. [source, xml]
  2018. ....
  2019. <weaver>
  2020. <exclude within="*CGLIB*" />
  2021. </weaver>
  2022. ....
  2023. [[aj11]]
  2024. == AspectJ 1.1 and eclipse.org
  2025. *Q:* Why did the AspectJ project move to eclipse.org?
  2026. *A:* From the message sent to users:
  2027. AspectJ has come a long way -- the language has stabilized; there are a
  2028. rapidly growing number of commercial users; the 1.1 release is imminent
  2029. and will include byte-code weaving and incremental compilation; and the
  2030. tool support is now well integrated with several major IDEs.
  2031. This growth of the community and the technology means that the original
  2032. research and prototype development of AspectJ is complete. As such it is
  2033. time for ongoing development and support of AspectJ to move outside of
  2034. PARC. This has already started to happen; the Eclipse AJDT plug-in and
  2035. the several books in preparation are examples.
  2036. To encourage the growth of the AspectJ technology and community, PARC is
  2037. transferring AspectJ to an openly-developed eclipse.org project. This
  2038. project will include documentation, web site, mailing lists, bug
  2039. database, and sources for the compiler. The command-line AspectJ
  2040. compiler is still the primary tool produced by this project, in addition
  2041. to APIs that support integration with a variety of IDEs. The Eclipse
  2042. plug-in will remain at eclipse.org, while the NetBeans, JBuilder and
  2043. Emacs support will move to SourceForge.net projects. We look forward to
  2044. your involvement with and contribution to those projects.
  2045. We see Eclipse as an excellent new home for core AspectJ technology
  2046. development -- it is an active community of Open Source development and
  2047. innovation in the Java space. Once development moves to Eclipse.org,
  2048. others will be able to contribute more easily.
  2049. *Q:* Do I have to download Eclipse to use AspectJ?
  2050. *A:* No. The AspectJ tools download is completely self-contained and
  2051. does not require that you work in Eclipse. For information on IDE
  2052. support, see #q:integrateWithDevTools[Q: How well does AspectJ integrate
  2053. with existing Java development tools?].
  2054. *Q:* What are the relationships between AspectJ, JDT, Eclipse, AJDT, and
  2055. IDE support generally?
  2056. *A:* Eclipse is a software platform.
  2057. JDT is an eclipse project to support Java development. JDT has a Java
  2058. compiler.
  2059. AspectJ 1.1 is built on Eclipse/JDT's Java compiler but is distributed
  2060. standalone and can run standalone. With the AspectJ distribution, you
  2061. can compile and run AspectJ programs and use the AspectJ structure
  2062. browser.
  2063. AJDT is an eclipse project to integrate AspectJ into Eclipse/JDT so you
  2064. can use Eclipse to develop AspectJ programs. AJDT aims to support the
  2065. full Eclipse experience - searching, compiler-error tasks, etc. AJDT
  2066. will use the AspectJ Development Environment (AJDE) API's for creating
  2067. IDE integrations, as well as hooking in to the model underlying the Java
  2068. compiler.
  2069. Similarly, Sourceforge has projects integrating AspectJ into other
  2070. development environments using the AJDE API's:
  2071. https://aspectj4emacs.sourceforge.net[AspectJ for Emacs],
  2072. https://aspectj4jbuildr.sourceforge.net[AspectJ for JBuilder], and
  2073. https://aspectj4netbean.sourceforge.net[AspectJ for NetBeans].
  2074. This is the right level of separation/integration. AspectJ is available
  2075. standalone, leverages an existing open-source compliant Java compiler,
  2076. and supports external projects doing IDE integrations in Eclipse, Emacs,
  2077. JBuilder, and NetBeans through a common API, AJDE.
  2078. [[AspectJ5]]
  2079. == AspectJ 5 and Java 5
  2080. *Q:* What are the new features of AspectJ 5?
  2081. *A:* All the new features are documented in the
  2082. xref:../adk15notebook/index.adoc[AspectJ 5 Developer's Notebook] and the
  2083. xref:../devguide/index.adoc[AspectJ Development Environment Guide]. To
  2084. summarize:
  2085. * Java 5 support: as an extension to Java, AspectJ supports all the new
  2086. language features of Java 5, including generics (parameterized types),
  2087. autoboxing, covariant return types, enhanced for-loops, enums, varargs,
  2088. and of course annotations.
  2089. * Java 5 extensions: the AspectJ language has been extended to make use
  2090. of Java 5 language features.
  2091. ** Generic aspects: an abstract aspect can be declared with a generic
  2092. type parameter which can be used in pointcuts and when declaring members
  2093. on the aspect (but not when declaring members on other types).
  2094. ** Annotations: pointcuts can now pick out join points based on the
  2095. associated annotations, annotation values can be bound in the same way
  2096. that other context variables are bound at the join point, and
  2097. annotations may be declared on other types in an aspect.
  2098. * Annotation-style aspects: AspectJ 5 integrates AspectWerkz-style
  2099. aspects declared in annotations. This permits aspects to be written and
  2100. compiled in pure-java code and woven using build-time or load-time
  2101. weaving with the AspectJ weaver. (The original AspectJ language aspects
  2102. are distinguished as "code-style" aspects.)
  2103. * AspectWerkz load-time weaving: Load-time weaving is greatly improved
  2104. for all versions of Java, and now supports an XML configuration file
  2105. which can declare concrete aspects. This means developers can deploy
  2106. binary abstract aspects that deployers configure using only XML.
  2107. * pertypewithin instantiation model: aspects may now be instantiated on
  2108. a per-class basis.
  2109. * Reflection and runtime support: AspectJ 5 supports reflection on
  2110. aspects using the Aspect class, and also support runtime evaluation of
  2111. pointcuts using a pointcut parser.
  2112. *Q:* Should I use code- or annotation-style aspects?
  2113. *A:* To use AspectJ, you can use the original code-style aspects or the
  2114. annotation-style aspects new in AspectJ 5.
  2115. The original code-style is a small extension of the Java language
  2116. designed to express crosscutting as clearly as possible in ways familiar
  2117. to most Java programmers. To use the original code-style aspects,
  2118. compile them with the AspectJ compiler or weave pre-compiled binary
  2119. aspects using the AspectJ binary (.class) weaver, either at build-time
  2120. or at class-load-time. Code-style aspects have excellent IDE support,
  2121. allowing you to navigate to and from affected source code.
  2122. Annotation-style aspects are written (not surprisingly) using
  2123. annotations. They use the subset of the AspectJ language that works when
  2124. aspects are woven after the code is compiled. The source files are
  2125. compiled with Javac, which simply saves the annotations in the .class
  2126. files. The resulting .class files must be woven using the AspectJ
  2127. weaver, which reads the annotations from the .class file and uses them
  2128. to define aspects. Annotation-style aspects have the benefit of being
  2129. compilable by Javac, but you can't use the full AspectJ language, and
  2130. you don't enjoy the same level of IDE support for viewing crosscutting
  2131. structure.
  2132. [[ltw]]
  2133. *Q:* What's new about the load-time weaving support in AspectJ 5?
  2134. *A:* While the AspectJ weaver could be used at load-time in previous
  2135. releases, the AspectJ 5 release supports much better integration with
  2136. the Java 5 VM and the BEA JRocket JVM. It also supports an XML file for
  2137. configuration that allows deployers to declare concrete aspects using
  2138. only XML. This means aspect developers can write abstract aspects, and
  2139. deployers need only configure `aop.xml` and run using the AspectJ weaver
  2140. in Java 5. For example, to run Java 5 VM with load-time weaving,
  2141. [source, text]
  2142. ....
  2143. java -javaagent:aspectjweaver.jar -classpath "aspects.jar:${CLASSPATH}" ..
  2144. ....
  2145. To declare a concrete aspect, add a a concrete-aspect XML entity to
  2146. `META-INF/aop.xml`. This example extends a tracing aspect to apply to
  2147. every type in the application:
  2148. [source, xml]
  2149. ....
  2150. <concrete-aspect
  2151. name="com.company.tracing.ConcreteTracing"
  2152. extends="tracing.AbstractTracing">
  2153. <pointcut
  2154. name="tracingScope"
  2155. expression="within(com.company.app..*)"/>
  2156. </concrete-aspect>
  2157. ....
  2158. For more information, see the link:devguide/index.html[AspectJ
  2159. Development Environment Guide].
  2160. [[Technology]]
  2161. == Understanding AspectJ Technology
  2162. *Q:* Do I need to know how the compiler or weaver works?
  2163. *A:* Writing AspectJ programs only requires understanding the
  2164. link:progguide/index.html[Programming Guide]. However, current
  2165. implementations do not control everything in a system, so AspectJ
  2166. program semantics may be limited to code the implementation controls.
  2167. For our implementation, these limitations are stated in
  2168. link:progguide/implementation.html[Programming Guide Appendix:
  2169. Implementation Notes]. Aside from understanding the use and limitations
  2170. of the implementation, there is no need to understand the underlying
  2171. technology when writing AspectJ programs.
  2172. The technology that implements AspectJ interests some academic
  2173. researchers and some developers who want new features or new ways to
  2174. weave. These extensions are not discussed in the documentation. Some are
  2175. being developed already, others are on the drawing board (or perhaps
  2176. were left off long ago), and still others haven't been considered. If
  2177. you are interested in a certain extension, check the bug database for
  2178. feature requests and the mailing list archives for any past discussions.
  2179. Then email the list to see if it's been considered. For more
  2180. information, see xref:#Developers[AspectJ Project Development].
  2181. *Q:* How does the compiler/weaver work? Are there any white papers?
  2182. *A:* There are currently no documents describing this process in detail.
  2183. You can compile programs and inspect the generated source or bytecode,
  2184. or view the source code (see xref:#Developers[AspectJ Project
  2185. Development]). We hope to write papers on the bytecode weaving model
  2186. used in AspectJ-1.1 if we can find the time. Erik Hilsdale and Jim
  2187. Hugunin did draft a paper for AOSD 2004, now available on Jim's web
  2188. site: https://hugunin.net/papers.html Jim summarized advice weaving in
  2189. the AspectJ 1.1 implementation in the
  2190. https://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg00519.html[following
  2191. mailing-list reply]:
  2192. Each piece of advice in an aspect is associated with a pointcut. This
  2193. pointcut is stored in an attribute on the methods corresponding to each
  2194. piece of advice. Before weaving, all of these pieces of advice are
  2195. gathered into one large list.
  2196. Each .class file is woven independently. A .class file is woven by the
  2197. following steps:
  2198. * Collect all of the joinpoint shadows in the .class file. For every
  2199. dynamic joinpoint in the AspectJ language model, there is a
  2200. corresponding static shadow of that joinpoint in the bytecode. For
  2201. example, every method call joinpoint has an INVOKE bytecode as its
  2202. static shadow. Some joinpoints (such as initialization) have much more
  2203. complicated static shadows.
  2204. * Each piece of advice is matched to each static shadow. There are three
  2205. results possible from this match.
  2206. ** Never matches, in which case nothing is done to the shadow
  2207. ** Always matches, in which case the advice is woven into this joinpoint
  2208. shadow
  2209. ** Sometimes matches, in which case the advice is woven into the shadow
  2210. along with the minimal dynamic tests to determine if any particular
  2211. joinpoint in the actual running program matches the advice. The simplest
  2212. example of sometimes matches is when the pointcut uses if(test()).
  2213. * If any advice matched any static shadows in the .class file, then the
  2214. transformed .class file is written out, otherwise it is left unchanged.
  2215. See `BcelClassWeaver` and `BcelShadow` in the `org.aspectj.weaver.bcel`
  2216. package for the two primary classes involved in this process.
  2217. Note: This explanation ignores the implementations of inter-type
  2218. declarations completely. It also ignores performance optimizations such
  2219. as fast-match or pipelining that speed up the process.
  2220. *Q:* How do I get load-time weaving to work in my chosen application
  2221. server?
  2222. *A:* You have two choices based on how wide you want the weaving to take
  2223. effect: application-server wide and application-specific weaving. You
  2224. choose between the two by loading aspect artifacts--aspects, associated
  2225. types, and aop.xml--through the right classloader. The aop.xml must be
  2226. in the META-INF directory on the classpath for the chosen classloader.
  2227. In either case, you modify the startup script to specify the
  2228. -javaagent:path-to/aspectjweaver.jar option to the Java virtual machine.
  2229. Note that it is not essential that all the artifacts be placed in a
  2230. single jar.
  2231. For application-server wide weaving, you make aspect artifacts
  2232. accessible to the server's classloader. Typically, you achieve such
  2233. access by putting these artifacts in the server's lib directory. For
  2234. example, for Tomcat, you will place the aspect artifacts in the
  2235. TOMCAT_HOME/lib directory.
  2236. For application-specific weaving, you make aspect artifacts accessible
  2237. to application classloader by bundling them along with application's
  2238. classes. For example, for a web application, you will place the aspect
  2239. artifacts in the MY_APP/WEB-INF/lib and/or MY_APP/WEB-INF/classes
  2240. directory.
  2241. We recommend that you start with application-specific weaving. Note that
  2242. you have an additional option if your application is based on the Spring
  2243. framework. If you deploy in one of the supported web servers or
  2244. application servers, you can avoid modifications to the startup script.
  2245. Please see
  2246. https://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-aj-ltw-spring
  2247. for more details.
  2248. *Q:* Does AspectJ use reflection at runtime?
  2249. *A:* The only time that reflection is used during run-time is when the
  2250. special thisJoinPoint object is used to discover reflective information
  2251. about the join point. If you don't use thisJoinPoint then no reflection
  2252. will be used.
  2253. *Q:* What about load-time weaving? Can I weave aspects at runtime?
  2254. *A:* Since the 1.1 release, AspectJ can weave binary aspects into
  2255. classes in bytecode form. Hooked up to a class loader, this can weave
  2256. class bytecodes after they are read in, before the class is defined by
  2257. the VM. (This means load-time weaving only works were aspects are not
  2258. required to compile the pure-java classes. If the aspects are required,
  2259. then the Java classes have to be compiled with the aspects using the
  2260. AspectJ compiler.) The AspectJ 1.2 release had the
  2261. WeavingURLClassLoader, and the 1.2.1 release introduced the aj.bat
  2262. script for Java 1.4. The AspectJ 5 release introduces much better
  2263. support for load-time weaving, including declaring concrete aspects in
  2264. XML files and integrating with Java 5 and BEA JRocket JVM's. See
  2265. #q:aspectj5ltw[Q:What's new about the load-time weaving support in
  2266. AspectJ 5?].
  2267. Some have asked about only weaving particular classes specified at
  2268. run-time. Aspects should work across an entire namespace, and problems
  2269. will likely result from weaving some classes but not others. Also, it's
  2270. confusing to specify crosscutting both in the aspect and in the list of
  2271. runtime classes; the crosscutting specification should be in the aspect
  2272. itself, where it can be processed by tools.
  2273. And just to state the obvious: do not use bytecode weaving, at load-time
  2274. or otherwise, to modify .class files protected by license, without
  2275. permission from the licensor.
  2276. [[Developers]]
  2277. == AspectJ Project Development
  2278. *Q:* I'm interested in the code implementing AspectJ.
  2279. *A:* Most people do not need to see the code for AspectJ; they can
  2280. download the binary distribution for documentation and tools for writing
  2281. AspectJ programs.
  2282. For people who want to know how the AspectJ technology works, the source
  2283. code is the best resource, until we write some proper white papers (see
  2284. #q:implementation[Q:Do I need to know how the compiler works?]). To get
  2285. and compile the Java source code for the AspectJ distribution, see
  2286. xref:#buildingsource[Q:How do I get and compile the source code for
  2287. AspectJ?].
  2288. Bear in mind when looking at the code that there are many ways to
  2289. implement the AspectJ language, and the code inspected might be an
  2290. initial version of a new architecture (e.g., bytecode weaving).
  2291. *Q:* How can I get involved with developing the AspectJ project?
  2292. *A:* For those who want to contribute to the project, here's a general
  2293. list of ways to do so, in no particular order:
  2294. * Participate effectively in the mailing lists. The quality of the
  2295. mailing lists makes a huge difference in the ability of new and
  2296. experienced AspectJ users to write good code. For guidance on effective
  2297. participation, see #q:talktousers[Q:How do I communicate with other
  2298. AspectJ users?] and #q:writingbugsandemails[Q:How should I write email
  2299. queries?]. Also, the time that experienced users take in answering
  2300. emails can directly translate to time developers can use (instead) for
  2301. fixing bugs or adding features.
  2302. * Write bugs. Good bugs, especially with test cases, are always
  2303. appreciated. We especially like proposals for new `XLint` messages,
  2304. since they are sometimes easy to implement and help users learn AspectJ,
  2305. and for other implementable features grounded in a compelling use-case.
  2306. * Write test cases for compiler bugs without test cases. Compiler bugs
  2307. without test cases are much less likely to be fixed; until they are
  2308. rendered in code, they might be user mistakes, and they might duplicate
  2309. another bug or actually cover many bugs.
  2310. +
  2311. Find them by searching open compiler bugs and picking out any which do
  2312. not have test case attachments or a comment that a test case has been
  2313. written. Here is a query for open compiler bugs:
  2314. https://bugs.eclipse.org/bugs/buglist.cgi?product=AspectJ&component=Compiler&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED
  2315. +
  2316. For how to write test cases, see #q:harnesstestcases[Q:How should I
  2317. submit test cases for bugs?].
  2318. * Write patches to fix bugs. If you particularly need a bug to be fixed,
  2319. or if you're interested in learning about the system, then get the
  2320. source code and try to fix the bug. Most likely you'll want to email
  2321. aspectj-dev@eclipse.org to declare your intentions and the approach you
  2322. propose (based on having looked at the code). Mailing the list gives
  2323. those experienced with the code a chance to guide you away from
  2324. pitfalls. To submit the patch, attach it to the bug. (When creating
  2325. patches, do so on a per-module basis; that means if fixing the bug
  2326. involves changes to three modules, submit three patches.)
  2327. * Write patches for other reasons. Often documentation needs to be
  2328. fixed, or there may be a small new feature you'd like to see. You can
  2329. just do it and then submit it as a patch to a bug you create. As with
  2330. bugs, in some cases you might want to declare your intentions on the
  2331. mailing list to avoid wasting time on something that's been fixed but
  2332. not committed or on an approach that will be fruitless.
  2333. [[buildingsource]]
  2334. *Q:* How do I get and compile the source code for AspectJ?
  2335. *A:* The AspectJ source code is available on the https://github.com/eclipse/org.aspectj[AspectJ GitHub page].
  2336. Read-me section https://github.com/eclipse/org.aspectj#building[Building] briefly explains how to compile AspectJ
  2337. from scratch.
  2338. Read-me section https://github.com/eclipse/org.aspectj#documentation-for-aspectj-developers[Documentation for AspectJ developers]
  2339. links to more detailed resources about build options, how to contribute to the project and how to release it.
  2340. For the latest AspectJ Development Tools (AJDT) for Eclipse release, please visit the
  2341. https://www.eclipse.org/ajdt/downloads/[AJDT Downloads] page.
  2342. *Q:* How do I build AspectJ and integrate it into AJDT?
  2343. *A:* *[TODO: outdated, update!]* To build AspectJ, first get the source tree as described in
  2344. xref:#buildingsource[Q:How do I get and compile the source code for
  2345. AspectJ?]. Once you have a development environment set up, copy the
  2346. `build/sample-local.properties` file to `build/local.properties` and
  2347. within this file point the `java14.home` and `java15.home` to the
  2348. corresponding places on your machine.
  2349. To build AspectJ on the command line:
  2350. * Open a command prompt
  2351. * Navigate to the `build` directory within your AspectJ workspace (to
  2352. find out where your workspace is go to `File >
  2353. Switch Workspace` within Eclipse).
  2354. * Run `ant clean` to remove the files from previously built AspectJ
  2355. versions.
  2356. * Run `ant` to build AspectJ. The built files are created in
  2357. `your_eclipse_installation_directory/aspectj_development_workspace/aj-build`.
  2358. To import a locally built AspectJ into AJDT first follow the
  2359. instructions on https://www.eclipse.org/ajdt/faq.php#q:develop[How do I
  2360. setup an AJDT development environment in Eclipse?] for setting up an
  2361. AJDT development environment and running the correctness tests. Then:
  2362. * Create a file `aspectjlib.properties` within the `org.aspectj.ajde`
  2363. project and add the following two lines
  2364. +
  2365. [source, text]
  2366. ....
  2367. aspectj.lib.dir=C:/eclipse/aspectj-workspace/aj-build/dist/tools/lib
  2368. aspectj.doc.dir=C:/eclipse/aspectj-workspace/aj-build/dist/ide/eclipse/org.aspectj.ajde.doc/doc
  2369. ....
  2370. +
  2371. making sure to change the path to correspond to your set up.
  2372. * Run the `build.xml` file in `org.aspectj.ajde` with the `plugin jars`
  2373. target:
  2374. ** Right click on the `build.xml` file in the `org.aspectj.ajde` plugin
  2375. ** Select `Run As > Ant build...`
  2376. ** In the resultant dialog navigate to the `Targets` tab
  2377. ** Ensure `plugin jars` is the only selected target
  2378. ** Click `Run`
  2379. * Refresh the `org.aspectj.ajde, org.aspectj.runtime` and
  2380. `org.aspectj.weaver` plugins.
  2381. *Q:* Where do I find developer documentation on building and testing
  2382. AspectJ source code?
  2383. *A:* Find the developer documentation in HTML files in the CVS tree,
  2384. inside the `build` and `testing` modules (i.e., in
  2385. `org.aspectj/modules/build/...`). Most pertinant:
  2386. * `../build/readme-build-and-test-aspectj.html` describes how to build
  2387. the AspectJ distribution in Eclipse and in Ant.
  2388. * `../build/readme-docs-module.html` describes the AspectJ documentation
  2389. sources and how to build the documentation using Ant.
  2390. * `../build/readme-tests-module.html` describes the all the tests in the
  2391. `tests` module.
  2392. * `../build/readme-writing-compiler-tests.html` describes how to write
  2393. compiler tests that can be run by the AspectJ test harness.
  2394. * `../build/readme-testing-drivers-module.html` describes the test
  2395. harness used to run the compiler tests in the `tests` module.
  2396. * `../build/readme-testing-drivers-module.html` describes the test
  2397. harness used to run the compiler tests in the `testing` module.
  2398. *Q:* How should I submit test cases for bugs?
  2399. *A:* You can attach files to a bug after it has been created. The code
  2400. of course should replicate the actual behavior described in the bug when
  2401. run on the target version. If you have a single source file, you can
  2402. attach it directly, describing in the comments the expected result
  2403. (e.g., error on line 14, or successful compile/run). The most helpful
  2404. form for describing the test scenario and the expected results are the
  2405. test definitions described next.
  2406. For more complex bugs requiring many files, create a zip file of a
  2407. directory containing all the files and an XML test definition file. The
  2408. XML test definition file contains specifications for how to compile,
  2409. recompile, or run the test sources. Complete documentation is available
  2410. in the CVS tree at `tests/readme-writing-compiler-tests.html` but here
  2411. is a sample file with some example definitions, preceded by comments
  2412. showing the directory layout of the files referred to in the test
  2413. definitions.
  2414. [source, xml]
  2415. ....
  2416. <!DOCTYPE suite SYSTEM "../tests/ajcTestSuite.dtd">
  2417. <suite>
  2418. <!-- Compile and run
  2419. using the following files:
  2420. {testDefinitions}.xml
  2421. one/
  2422. pack1/
  2423. Main.java
  2424. p2/
  2425. BeforeConstructor.java
  2426. Note the bug number goes in the pr attribute.
  2427. ("pr" stands for "problem report")
  2428. -->
  2429. <ajc-test dir="one" pr="234" title="before constructor call">
  2430. <compile files="pack1/Main.java,p2/BeforeConstructor.java"/>
  2431. <run class="pack1.Main"/>
  2432. </ajc-test>
  2433. <!-- Check that compiler warning was emitted
  2434. using the following files:
  2435. {testDefinitions}.xml
  2436. two/
  2437. UsesDeprecated.java
  2438. -->
  2439. <ajc-test dir="two" pr="244" title="deprecated, noImportError">
  2440. <compile options="-warn:deprecated,-noImportError"
  2441. files="UsesDeprecated.java">
  2442. <message kind="warning" line="20"/>
  2443. </compile>
  2444. </ajc-test>
  2445. <!-- Cooked example that uses all compiler attributes
  2446. and the following files:
  2447. {testDefinitions}.xml
  2448. testCaseDir/
  2449. jars/
  2450. injar.jar
  2451. required.jar
  2452. requiredAspects.jar
  2453. pack/
  2454. Main.java
  2455. providedClassesDir/
  2456. ClassInDefaultPackage.class
  2457. org/
  2458. foo/
  2459. AnotherRequired.class
  2460. -->
  2461. <ajc-test dir="testCaseDir" title="attributes test">
  2462. <compile files="pack/Main.java,jars/injar.jar"
  2463. staging="true"
  2464. options="-Xlint,-g:none"
  2465. argfiles="debug.lst,aspects/test.lst"
  2466. aspectpath="jars/requiredAspects.jar"
  2467. classpath="providedClassesDir,jars/required.jar"/>
  2468. <run class="Main"/>
  2469. </ajc-test>
  2470. <!-- Compiler errors, recompile after changing files, and run
  2471. using the following files:
  2472. {testDefinitions}.xml
  2473. three/
  2474. pack/
  2475. IncCompileFix.java
  2476. IncCompileFix.20.java
  2477. Before compiling, IncCompileFix.java is copied to a staging
  2478. directory. Before recompiling, IncCompileFix.20.java
  2479. replaces it, so the compiler treats file as updated.
  2480. -->
  2481. <ajc-test dir="three" pr="622" title="incremental fix">
  2482. <compile staging="true" files="pack/IncCompileFix.java">
  2483. <message kind="error" line="20"/>
  2484. <message kind="error" line="42"/>
  2485. </compile>
  2486. <inc-compile tag="20"/>
  2487. <run class="pack.IncCompileFix"/>
  2488. </ajc-test>
  2489. </suite>
  2490. ....
  2491. *Q:* I'd like to run my test case. How do I get the test harness?
  2492. *A:* The test harness is not distributed. To build it, get the source
  2493. tree as described in xref:#buildingsource[Q:How do I get and compile the
  2494. source code for AspectJ?] and then build the `build-testing-drivers`
  2495. target:
  2496. [source, text]
  2497. ....
  2498. cd build
  2499. ../lib/ant/bin/ant -f build.xml build-testing-drivers
  2500. ....
  2501. This produces `../aj-build/jars/testing-drivers-all.jar` which you can
  2502. run as described in `tests/readme-tests-module.html`.
  2503. *Q:* BCEL is used by AspectJ but it's not actively developed. Will you
  2504. change?
  2505. *A:* The AspectJ bytecode weaver has used BCEL for bytecode manipulation
  2506. since its first release. We have upgraded it extensively, to improve
  2507. performance, support Java 5, etc. The BCEL developers have not
  2508. incorporated our patches, so we continue to maintain our own version.
  2509. Ours has been optimized for the AspectJ weaver and battle-hardened over
  2510. years of development and use. At some point in the future, the AspectJ
  2511. weaver might be restructured to make it easy to see whether another
  2512. bytecode package offers the same stability, functionality, and
  2513. performance, but for now we prefer using something that we know works
  2514. well.
  2515. In the AspectJ 5 release, the weaver has been restructured to use
  2516. reflection where possible. Otherwise, it continues to use BCEL, but does
  2517. not hold BCEL structures in memory after our evaluation completes.
  2518. [[help]]
  2519. == Getting Help
  2520. *Q:* How do I find out more about AspectJ?
  2521. *A:* Visit the AspectJ project web site: https://eclipse.org/aspectj.
  2522. [[ajcbugs]]
  2523. *Q:* How do I submit a bug report?
  2524. *A:* You can submit a bug from
  2525. https://bugs.eclipse.org/bugs/enter_bug.cgi?product=AspectJ . If it seems
  2526. to be a bug in the compiler, please attach a small test case (source
  2527. code) to reproduce the problem. For more information on writing compiler
  2528. test cases, see #q:ajcbugs[Q:How do I write bugs for the AspectJ
  2529. compiler?]. If you are unable to submit a test case, consider submitting
  2530. traces, ajcore files, and/or .class dump files, as described in the
  2531. link:pdguide/index.html[AspectJ Problem Diagnosis Guide].
  2532. *Q:* How do I communicate with other AspectJ users?
  2533. *A:* You can reach other AspectJ users by using the aspectj-users
  2534. mailing list. You can subscribe to the list or view the list archives
  2535. from the AspectJ home page https://eclipse.org/aspectj .
  2536. [[searchingsite]]
  2537. *Q:* How can I search the email archives or the web site?
  2538. *A:* It is very effective to do a google search of the form,
  2539. https://www.google.com/search?q=site:eclipse.org+cflowbelow , and you can
  2540. use the eclipse.org search at https://www.eclipse.org/search/search.cgi .
  2541. You can also check the old archives available for download from the
  2542. AspectJ home page https://eclipse.org/aspectj .
  2543. *Q:* How should I write email queries?
  2544. *A:* Here's the general form of a good email:
  2545. [arabic]
  2546. . Describe the big picture of what you are trying to do...
  2547. . Describe what you think it takes, in AspectJ terms (concepts, syntax,
  2548. and semantics) from the link:progguide/index.html[Programming Guide]...
  2549. . Show the AspectJ code you are using, what output it produces when run,
  2550. and what output you expect...
  2551. The big picture helps others redirect you to other approaches. Using
  2552. AspectJ terms helps others correct mistakes in thinking about the
  2553. problem (the most common being to confuse join points and pointcuts).
  2554. The code is key to clarifying your question and getting a good response.
  2555. On the mail list, someone can reply by fixing your code. In bugs, the
  2556. developers can reproduce the problem immediately and start analyzing the
  2557. fix. The code should not be incomplete; it should run (or fail) as-is,
  2558. without additional libraries or source files.
  2559. For the mail lists, we try to follow the conventions for open-source
  2560. discussions that help avoid "the tragedy of the commons." For example
  2561. conventions, see https://jakarta.apache.org/site/mail.html and
  2562. https://www.tuxedo.org/%7Eesr/faqs/smart-questions.html .
  2563. *Q:* How do I write bugs for IDE support?
  2564. *A:* Bugs appearing in the IDE's may apply to the affected IDE or to the
  2565. compiler. Compiler stack traces in IDE message windows are prefixed
  2566. "Internal Compiler Error" and should be written up as compiler bugs. If
  2567. you are unsure, try redoing the compile from the command line.
  2568. Bug report for the IDE extensions go to their respective projects,
  2569. listed in #q:integrateWithDevTools[Q: How well does AspectJ integrate
  2570. with existing Java development tools?] (including bug reports for the
  2571. AJDE Eclipse support, which you can submit at
  2572. https://bugs.eclipse.org/bugs/enter_bug.cgi?product=AJDT ).
  2573. One of the benefits of open-source is that you can find and fix the bug
  2574. for yourself; when you submit the fix back to us, we can validate the
  2575. fix for you and incorporate it into the next release. You can submit a
  2576. patch by attaching it to the bug.
  2577. *Q:* How do I write bugs for the AspectJ compiler?
  2578. *A:* The best compiler bug report is a reproducible test case,
  2579. standalone code that demonstrates the problem. Sometimes with aspects, a
  2580. test case requires several files, if not some way to capture the
  2581. behavior. Here's how we recommend submitting test cases:
  2582. [arabic]
  2583. . Write the test case so that when the compiler bug is fixed, the test
  2584. completes normally without output (e.g., expected compiler errors are
  2585. issued, or classes produced run correctly). This usually means writing
  2586. one or more source files.
  2587. . In the bug report, briefly summarize the bug. If it is not obvious, be
  2588. sure to specify the expected output/behavior (e.g., compiler error on
  2589. line 32) and, if the compile should complete, the main class to run.
  2590. . Submit the bugs via the web form
  2591. https://bugs.eclipse.org/bugs/enter_bug.cgi?product=AspectJ .
  2592. . Attach the test case to the bug. The test case may be a single file or
  2593. it may be multiple files in a single zip archive, of the form discussed
  2594. in #q:harnesstestcases[Q:How should I submit test cases for bugs?].
  2595. *Q:* Can you recommend reading or teaching material for AspectJ?
  2596. *A:* The documentation available in the distribution is the best source
  2597. for language and usage questions.
  2598. *Q:* Where can our group get consulting and support?
  2599. *A:* The best thing to do is join and email the
  2600. `aspectj-dev@eclipse.org` mailing list.
  2601. *Q:* What has changed since the last FAQ version?
  2602. *A:* Entries changed recently:
  2603. * #q:license[Q:How is AspectJ licensed?]
  2604. * #q:productplans[Q:Is it safe to use AspectJ in my product plans??]
  2605. * #q:whitepapers[Q:How does the compiler/weaver work? Are there any
  2606. white papers?]
  2607. * #q:bugreports[Q:How do I submit a bug report?]
  2608. [[project]]
  2609. == About the AspectJ Project
  2610. [[opensource]]
  2611. *Q:* What does the fact that AspectJ is an Open Source Project mean to
  2612. me?
  2613. *A:* Open source protects your interest in a correct, long-lived,
  2614. up-to-date, and widely-accepted implementation of AspectJ.
  2615. * With the source code, you control your own destiny in perpetuity. You
  2616. can continue to use the implementation and update it as necessary to fix
  2617. bugs and add things you need.
  2618. * Because the code is available to all, anyone can find and fix bugs.
  2619. There is no need to hope for it to be fixed in the next product release.
  2620. Those who encounter the bugs are motivated to fix them, and there are
  2621. more eyeballs on the code than in closed-source, so the quality tends to
  2622. be high. This can be particularly true for the AspectJ community, which
  2623. tends to be highly skilled.
  2624. * The same is true of new features or behavior, so the implementation
  2625. should be up-to-date. This is important as the field of AOP develops, to
  2626. capture the latest solutions.
  2627. * For a programming language which forms the basis of an entire solution
  2628. stack, open source facilitates the kind of adoption -- tool integrations
  2629. and significant projects -- that develop and prove the technology for
  2630. wider adoption. This limits delays caused by waiting for the completion
  2631. of standards process or promulgation by industry leaders, and also
  2632. provides the proofs necessary for such adoption.
  2633. *Q:* What are your plans to make AspectJ a general feature of Java
  2634. supported by Sun and the other key players in the Java Industry?
  2635. *A:* Although we are committed to making AspectJ available to a wide
  2636. range of users, it is too early to decide on a strategy. Some options
  2637. include continuing AspectJ as a stand-alone product, integrating it into
  2638. IDEs, or possibly incorporating it into standard Java with Sun's
  2639. blessing.
  2640. We currently focus on developing for the 1.1 implementation which
  2641. improves AspectJ in key areas: rapid incremental compilation, bytecode
  2642. weaving, and IDE integration.
  2643. Through all of this our goal is to make AspectJ integrate as seamlessly
  2644. as possible with the Java programming language. The AspectJ language
  2645. design is becoming more integrated, the compiler is becoming faster and
  2646. more integrated, the IDE extensions are becoming more integrated. All of
  2647. this is designed to help users really use AspectJ and give us feedback
  2648. on it.
  2649. As the system is improved and we work more closely with users, we will
  2650. be in good position to explore the best path for AspectJ in the long
  2651. term.
  2652. *Q:* When will AspectJ work from class files? When will it work at
  2653. class-loading time?
  2654. *A:* Bytecode weaving is in AspectJ 1.1. We believe it works as
  2655. described in an email to the users list by Jim Hugugin:
  2656. The AspectJ language was designed to support weaving at many different
  2657. times: compile, load, or even run-time in the JVM. Weaving into
  2658. bytecodes at both compile and load-time will definitely be provided in a
  2659. future release. This will allow weaving at compile-time into libraries
  2660. for which source code is not available. It will also support
  2661. aspect-aware class loaders that can perform weaving at load time on
  2662. arbitrary classes. One advantage of a language like AspectJ, rather than
  2663. an explicit meta-tool like jiapi, is that it separates the specification
  2664. of a crosscutting concern from any particular implementation strategy
  2665. for weaving.
  2666. ...AspectJ provides a language that can cleanly capture crosscutting
  2667. concerns while preserving the static type checking, modularity, and
  2668. composability of Java.
  2669. If you have an application for using aspects and bytecode, please let
  2670. the AspectJ team know of your requirements. We expect to have a
  2671. demonstration classloader available in the 1.1 release or soon
  2672. thereafter.
  2673. *Q:* What are the differences between the current and previously
  2674. released versions of AspectJ?
  2675. *A:* The AspectJ team aims to keep the implementation bug-free and
  2676. up-to-date with the Java language, to limit AspectJ language changes to
  2677. those that are carefully considered, compelling, and
  2678. backwards-compatible, and to deliver those language changes only in
  2679. significant releases (1.0, 1.1).
  2680. [cols=",",]
  2681. |===
  2682. |Version |Description
  2683. |AspectJ 1.5 |Upgrade to support Java 5 language and much better
  2684. load-time weaving. See README-1.5.0.html for more details.
  2685. |AspectJ 1.1 |A few language changes and clarifications; bytecode
  2686. weaving and incremental compilation. See README-1.1.html for more detail.
  2687. |AspectJ 1.0 |Many language changes, fixes, cleanup and clarifications,
  2688. some significant.
  2689. |AspectJ 0.8 |More cleanup of the syntax and semantics.
  2690. |AspectJ 0.7 |Clean up of the semantics, 0.7 beta 4 is the first open
  2691. source release.
  2692. |AspectJ 0.6 |Advice and crosscuts get explicit type signatures which
  2693. describe the values that are available to advice at a crosscut.
  2694. |AspectJ 0.5 |Improved tool support: better Emacs environment support
  2695. and `ajdoc` to parallel `javadoc`. around advice is added, and the
  2696. `aspect` keyword is removed and replaced by the Java keyword class.
  2697. |AspectJ 0.4 |Clear separation of crosscuts and crosscut actions makes
  2698. it possible to define extensible library aspects.
  2699. |AspectJ 0.3 |First all Java implementation, also includes many small
  2700. language improvements.
  2701. |AspectJ 0.2 |General-purpose support for crosscutting. Users could
  2702. program any kind of aspects, not just coordination. This release dropped
  2703. COOL.
  2704. |AspectJ 0.1 |A single domain-specific aspect language, called COOL, for
  2705. programming coordination in multi-threaded programs.
  2706. |===
  2707. More details for 1.0 and earlier releases are available in changes.html.
  2708. *Q:* What is the AspectJ development schedule?
  2709. *A:* Below is a table describing the goals for the major releases. For
  2710. information about specific features, search the bug database for ``RFE``'s
  2711. ("requests for enhancement") by
  2712. https://bugs.eclipse.org/bugs/buglist.cgi?product=AspectJ&bug_severity=enhancement[selecting
  2713. severity of "enhancement"]. Like many open-source projects, we don't
  2714. make or promise schedules, but we do follow a pattern of issuing preview
  2715. releases which can give observers an idea of when a particular release
  2716. might be available.
  2717. .The AspectJ Development Schedule
  2718. [cols=",",]
  2719. |===
  2720. |Version |Description
  2721. |1.0 |Final syntax and semantic changes. Standalone structure browser.
  2722. Complete documentation.
  2723. |1.1 |Faster incremental compilation, bytecode weaving, and a small
  2724. number of language changes.
  2725. |1.2 |Faster weaving, -inpath option, better error messages, better
  2726. handling of binary input and resources during incremental compilation,
  2727. faster runtime
  2728. |1.5 (AspectJ 5) |Support for Java 1.5, generic aspects, annotations,
  2729. etc. Integrates AspectWerkz-style load-time weaving.
  2730. |===
  2731. *Q:* Will AspectJ support Java 5?
  2732. *A:* Yes. Java 5 is supported in AspectJ 5.
  2733. AspectJ is a registered trademark of Palo Alto Research Center,
  2734. Incorporated (PARC), used with permission. Java and all Java-based marks
  2735. are trademarks or registered trademarks of Sun Microsystems, Inc. in the
  2736. United States and other countries. All other trademarks are the property
  2737. of their respective owners.