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.

readme-sandbox.adoc 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. = AspectJ sandbox for sample code and instructions
  2. This directory is a place to put scraps that might end up in the AspectJ
  3. documentation or on the web site:
  4. * sample code for AspectJ programs,
  5. * sample code for extensions to AspectJ tools and API's,
  6. * sample scripts for invoking AspectJ tools, and
  7. * documentation trails showing how to do given tasks using AspectJ,
  8. AJDT or various IDE or deployment environments.
  9. == General
  10. In the past, we found it tedious to keep and verify sample code used in
  11. documentation because it involves copying the code from the
  12. documentation into a form that can be tested (or vice versa). Further,
  13. there are a lot of tips and sample code contributed on the mailing
  14. lists, but they can be difficult to find when needed. This aims to be a
  15. place where such contributions can be gathered, kept in good order, and
  16. extracted for use in docs, without too much overhead.
  17. Code in the sandbox is kept in a running state; it's tested by running
  18. the harness on the sandbox test suite file sandbox-test.xml or
  19. sandbox-api-test.xml. To extract the code for documentation, we use a
  20. tool that recognizes a sample within a source file if it has comments
  21. signalling the start and end of the sample. Keeping sample code in sync
  22. with the documentation and with the test suite specification only takes
  23. a bit of care with formatting and comments. The rest of this document
  24. tells how.
  25. == Documenting samples
  26. `org.aspectj.internal.tools.build.SampleGatherer` (in the build module)
  27. extracts samples of the following form from any "source" file (currently
  28. source, html, text, and shell scripts):
  29. [source, text]
  30. ....
  31. ... some text, possibly including @author tags
  32. {comment} START-SAMPLE [anchorName] [anchor title] {end-comment}
  33. ... sample code ...
  34. {comment} END-SAMPLE [anchorName] {end-comment}
  35. ... more text ...
  36. ....
  37. Each sample extracted consists of the code and associated attributes:
  38. the source location, the anchor name and title, any text flagged with
  39. XXX, and the author pulled from the closest-preceding `@author` tag. (If
  40. there is no author, the AspectJ team is presumed.) `SampleGatherer` can
  41. render the collected samples back out to HTML (todo: or DocBook) for
  42. inclusion in the FAQ, the online samples, the Programming Guide, etc. An
  43. editor might reorder or annotate the samples but the code should not be
  44. edited to avoid introducing mistakes.
  45. To help keep the sample code in sync with the docs...
  46. * Use comments in the sample to explain the code, rather than describing
  47. it in the documentation.
  48. * Preformat samples to be included without modification in docs:
  49. ** Use 4 spaces rather than a tab for each indent.
  50. ** Keep lines short - 60 characters or less.
  51. ** In Java, code taken out of context of the defining type can be
  52. indented only once in the source code, even though they might normally
  53. be indented more.
  54. ** In AspectJ, indent advice pointcuts beyond the block code:
  55. +
  56. [source, java]
  57. ....
  58. before() : call(!public * com.company.library..*.*(String,..))
  59. && within(Runnable+) { // indent once more than code
  60. // code here
  61. }
  62. ....
  63. Where you put the sample code depends on how big it is and what it's
  64. for. Any code intended as an extension to the AspectJ tools goes in the
  65. link:api-clients[api-clients/] directory. Most code will instead be
  66. samples of AspectJ programs. Subdirectories of this directory should be
  67. the base directories of different source sets. The link:common[common/]
  68. directory should work for most code snippets, but standalone,
  69. self-consistent code belongs in its own directory, as do sets pertaining
  70. to particular publications or tutorials. An example of this are the
  71. sources for the "Test Inoculated" article in the
  72. link:inoculated[inoculated/] directory. Finally, the
  73. link:testsrc[testsrc/] directory is reserved for code used to test the
  74. other code in the sandbox. There should be no samples under testsrc.
  75. == Testing samples
  76. We try to make sure that the samples we present to people actually work
  77. by testing each kind differently:
  78. * Most Java and AspectJ programs are tested using sandbox-test.xml.
  79. * API clients are tested using sandbox-api-test.xml, which requires
  80. building `aspectjtools.jar`.
  81. * Shell and Ant scripts should be run per instructions.
  82. * HTML and text files must be manually reviewed.
  83. When adding Java or AspectJ code, add a corresponding test case in
  84. sandbox-test.xml. This file has the same format as other harness test
  85. suites; for more information, see
  86. ../../tests/readme-writing-compiler-tests.html. The test suite should
  87. run and pass after new code is added and before samples are extracted.
  88. To keep Java/AspectJ code in sync with the tests:
  89. * The test title should be prefixed with the anchor name and have any
  90. suffixes necessary for clarity and to make sure there are unique titles
  91. for each test. E.g., for a sample with the anchor
  92. "`language-initialization`",
  93. +
  94. [source, xml]
  95. ....
  96. <ajc-test
  97. dir="common"
  98. title="language-initialization constructor-call example">
  99. ...
  100. </ajc-test>
  101. ....
  102. +
  103. (Someday we'll be able to compare the test titles with the anchor names
  104. to verify that the titles contain all the anchor names.)
  105. * Avoid mixing compiler-error tests with ordinary code, so others can
  106. reuse the target code in their samples. Most of the sample code should
  107. compile.
  108. * Any code that is supposed to trigger a compiler error should be tested
  109. by verifying that the error message is produced, checking either or both
  110. of the line number and the message text. E.g.,
  111. +
  112. [source, xml]
  113. ....
  114. <compile files="declares/Declares.java, {others}"
  115. <message kind="error" line="15" text="Factory"/>
  116. </compile>
  117. ....
  118. +
  119. Where a test case refers to a line number, we have to keep the expected
  120. message and the target code in sync. You can help with this by adding a
  121. comment in the target code so people editing the code know not to fix or
  122. move the code. E.g.,
  123. +
  124. [source, java]
  125. ....
  126. void spawn() {
  127. new Thread(this, toString()).start(); // KEEP CE 15 declares-factory
  128. }
  129. ....
  130. +
  131. Any good comment could work, but here are some optional conventions:
  132. ** Use "CE" or "CW" for compiler error or warning, respectively.
  133. ** Specify the line number, if one is expected.
  134. ** Specify the affected test title(s) or sample code anchor label to
  135. make it easier to find the test that will break if the code is modified.
  136. (The editor can also run the tests to find any broken ones.)
  137. If the code is broken (e.g., if it no longer works in the latest version
  138. of AspectJ), then prefix SAMPLE with BROKEN in the tag:
  139. [source, text]
  140. ....
  141. {comment} START-BROKEN-SAMPLE ...
  142. ... sample code ...
  143. {comment} END-BROKEN-SAMPLE ...
  144. ... more text ...
  145. ....
  146. It will no longer be gathered, but can be fixed and reinstated.
  147. Happy coding!