Home | History | Annotate | Download | only in annotation-file-utilities
      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4   <title>Annotation File Utilities</title>
      5 </head>
      6 <body>
      7 <h1>Annotation File Utilities</h1>
      8 
      9 <p>Contents:</p>
     10 <!-- start toc.  do not edit; run html-update-toc instead -->
     11     <ul>
     12       <li><a href="#motivation">Motivation</a>
     13         <ul>
     14           <li><a href="#jaif-file">External storage of annotations</a></li>
     15           <li><a href="#annotation-file-utilities-description">Annotation File Utilities</a></li>
     16         </ul></li>
     17       <li><a href="#installation">Installation</a>
     18         <ul>
     19           <li><a href="#viewing-source">Building from source</a></li>
     20         </ul></li>
     21       <li><a href="#using">Using the Annotation File Utilities</a>
     22         <ul>
     23           <li><a href="#insert-annotations">Insert-annotations</a></li>
     24           <li><a href="#extract-annotations">Extract-annotations</a></li>
     25           <li><a href="#insert-annotations-to-source">Insert-annotations-to-source</a></li>
     26         </ul></li>
     27       <li><a href="#implementation">Design and Implementation Details</a>
     28         <ul>
     29           <li><a href="#asmx">Asmx</a></li>
     30           <li><a href="#scene-lib">Scene-lib</a>
     31             <ul>
     32               <li><a href="#bytecode-insertion">Bytecode Insertion</a></li>
     33               <li><a href="#bytecode-extraction">Bytecode Extraction</a></li>
     34             </ul></li>
     35           <li><a href="#source-insertion">Annotation-file-utilities</a></li>
     36         </ul></li>
     37       <li><a href="#feedback">Feedback and bug reports</a>
     38         <ul>
     39           <li><a href="#changelog">Changelog</a></li>
     40         </ul></li>
     41     </ul>
     42 <!-- end toc -->
     43 
     44 
     45 
     46 <hr />
     47 <h2 id="motivation">Motivation</h2>
     48 
     49 <p>
     50 Java annotations are meta-data about Java program elements, as in
     51 &ldquo;<code><mark>@Deprecated</mark> class Date
     52 {&nbsp;...&nbsp;}</code>&rdquo; or &ldquo;<code>List&lt;<mark>@NonNull</mark> String&gt;</code>&rdquo;.
     53 Ordinarily, Java annotations are
     54 written in the
     55 source code of a <code>.java</code> Java source file.  When
     56 <code>javac</code> compiles the source code, it inserts the annotations in
     57 the resulting <code>.class</code> file (as
     58 &ldquo;attributes&rdquo;).
     59 </p>
     60 
     61 <!--
     62   I've moved text from here to the annotation-file-format document, but feel
     63   free to improve that document.
     64 -->
     65 
     66 <h3 id="jaif-file">External storage of annotations</h3>
     67 
     68 <p>
     69 Sometimes, it is convenient to specify
     70 the annotations outside the source code or the <code>.class</code> file.
     71 The document
     72 &ldquo;Annotation File Format Specification&rdquo; (<a
     73     href="annotation-file-format.pdf">PDF</a>, <a
     74     href="annotation-file-format.html">HTML</a>)
     75 defines a textual format for annotations,
     76 and it also motivates reasons why such a file format is necessary in
     77 addition to the <code>.java</code> and <code>.class</code> formats.  The
     78 file format supports both the declaration annotations and type annotations.
     79 </p>
     80 
     81 <p>
     82 An annotation file
     83 conventionally has the extension <code>.jaif</code> (for Java Annotation Index
     84 File).
     85 The <a href="#scene-lib"><code>scene-lib</code></a> sub-project provides
     86 API methods for building and manipulating annotation files.
     87 </p>
     88 
     89 
     90 <h3 id="annotation-file-utilities-description">Annotation File Utilities</h3>
     91 
     92 <p>
     93 Programmers need to be able to transfer annotations
     94 between the three possible locations for annotations &mdash; source code, class files,
     95 and annotation files.   Programmers will want to extract
     96 annotations from source and class files to an annotation file in order to easily
     97 read annotations, while various tools will only read annotations from
     98 source and class files.  The Annotation File Utilities provide three tools
     99 to read and write annotation files.
    100 </p>
    101 
    102 <ul>
    103   <li> <a href="#insert-annotations"><code>insert-annotations</code></a> reads annotations from an annotation file
    104        and inserts them into a class file</li>
    105   <li> <a href="#extract-annotations"><code>extract-annotations</code></a> reads annotations from a class file
    106        and writes them out to an annotation file</li>
    107   <li> <a href="#insert-annotations-to-source"><code>insert-annotations-to-source</code></a> reads annotations from an
    108        annotation file and inserts them into a Java source file</li>
    109 </ul>
    110 
    111 <p>
    112 A diagram showing the interactions betweeen these tools is below.
    113 </p>
    114 
    115 <div>
    116 <img src="figures/tool-relations.svg" alt="Relationships between AFU tools" />
    117 </div>
    118 
    119 <p>
    120 There is no
    121 <code>extract-annotations-from-source</code> tool:  one can
    122 compile the source code and then use
    123 <code>extract-annotations</code> to read the annotations from the class
    124 file.
    125 </p>
    126 
    127 
    128 <hr />
    129 <h2 id="installation">Installation</h2>
    130 
    131 <p>
    132 The following instructions assume either a Linux or Windows system using a command-line environment.
    133 </p>
    134 
    135 <p>
    136 The current release is Annotation File Utilities version
    137 <!-- afu-version -->3.6.44, 03 Aug 2017<!-- /afu-version -->.
    138 </p>
    139 
    140 <ol>
    141 <li>
    142   Download
    143   <a href="annotation-tools-3.6.44.zip"><!-- annotation-tools-zip-version -->annotation-tools-3.6.44.zip<!-- /annotation-tools-zip-version --></a>.
    144 </li>
    145 
    146 <li>
    147   Creade a directory named
    148   <code>annotation-tools</code> by
    149   unpacking the distribution zipfile (a standard place to do this is in a
    150   directory <code>~/jsr308/</code>):
    151 
    152   <pre><code>unzip annotation-tools-3.6.44.zip</code></pre>
    153 
    154   The <code>annotation-tools</code> directory must be a sibling of the <code>jsr308-langtools</code>
    155   directory (available at <a href="https://checkerframework.org/jsr308/">https://checkerframework.org/jsr308/</a>).
    156   Alternatively, Unix (including Linux and MacOS) users may set the <code>LANGTOOLS</code> environment variable to
    157   the location of the <code>jsr308-langtools</code> directory:
    158 
    159   <pre><code>export LANGTOOLS=<em>/path/to</em>/jsr308-langtools</code></pre>
    160 </li>
    161 
    162 <li>
    163 Add the <code>annotation-file-utilities</code> directory to your path.
    164 
    165 <ul>
    166 <li>
    167 For <b>Unix</b> (including Linux and MacOS), add the directory to your PATH
    168 environment variable.  If your shell is sh or bash, add to your
    169 <code>~/.bashrc</code> or <code>~/.bash_profile</code> file:
    170 <pre><code>export PATH=${PATH}:<em>/path/to</em>/annotation-tools/annotation-file-utilities/scripts</code></pre>
    171 <!-- Omitted to save space and simplify instructions
    172 For csh/tcsh, add to ~/.tcshrc or ~/.cshrc or ~/.login:
    173 <pre><code>setenv PATH=${PATH}:/path/to/annotation-file-utilities/</code></pre>
    174 -->
    175 </li>
    176 <li>
    177 For <b>Windows</b>, add the directory to your
    178 <code>PATH</code> system
    179 variable by going to
    180 
    181 <pre><code> Control Panel -> System -> Advanced -> Environment Variables </code></pre>
    182 
    183 From there, find the <code>PATH</code> variable under &ldquo;System variables&rdquo;
    184 and append to it the directory <code><em>path\to</em>\annotatation-tools\annotation-file-utilities\scripts</code>.
    185 </li>
    186 </ul>
    187 </li>
    188 </ol>
    189 
    190 
    191 <!-- I think this is obvious, so I have commented it out -MDE. -->
    192 <!--
    193 <p>
    194 To update the annotation file utilities, simply download the most recent <code>annotation-tools.zip</code> file from this website and replace the existing <code>annotation-tools.zip</code>, then extract the file just as when you first installed it.  As long as you followed the above instructions, no further work needs to be done.
    195 </p>
    196 -->
    197 
    198 <h3 id="viewing-source">Building from source</h3>
    199 
    200 <p>
    201 The annotation file utilities are pre-compiled (a jar file is included in
    202 the distribution), so most users do not need to compile it themselves.
    203 </p>
    204 
    205 <p>
    206 There are two ways to obtain the source code.
    207 Source code is provided in the
    208 <a href="https://github.com/typetools/annotation-tools/releases">distribution</a>.
    209 Alternately, see the source code repository at
    210 <a href="https://github.com/typetools/annotation-tools">https://github.com/typetools/annotation-tools</a>.
    211 </p>
    212 
    213 <p>
    214 To compile, run
    215 <code>ant jarfile</code> from the <code>annotation-file-utilities</code>
    216 subdirectory.  If the <code>javac</code> and <code>java</code> programs are
    217 not on your execution path, see the notes near the top of
    218 <code>annotation-file-utilities/tests/Makefile</code>.
    219 </p>
    220 
    221 
    222 <hr />
    223 <h2 id="using">Using the Annotation File Utilities</h2>
    224 
    225 <p>
    226 To use the tools, simply run them from the command-line with the
    227 appropriate arguments.  The following instructions are for running the
    228 tools on a Linux/Unix/MacOS machine.
    229 The tools work identically on Windows, except
    230 the extension <code>.bat</code> needs to be appended to the tool name (for
    231 example, Windows users would execute <code>insert-annotations.bat</code>
    232 instead of <code>insert-annotations</code>).
    233 </p>
    234 
    235 <p>
    236 For all the tools, arguments starting with a single
    237 &lsquo;<code>@</code>&rsquo; are recognized as argument files
    238 (<code>argfiles</code>), the contents of which get expanded into the
    239 command line.  (Initial <code>@@</code> represents a literal
    240 <code>@</code> in the argument text.)  For additional details of argfile
    241 processing, refer to Oracle's
    242 <a href="https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html"><code>javac</code></a>
    243 documentation.
    244 </p>
    245 
    246 <h3 id="insert-annotations"> Insert-annotations </h3>
    247 
    248 <p>
    249 To insert annotations specified by an annotation file into a class file, use the
    250 insert-annotations tool.  Running:
    251 </p>
    252 
    253 <pre><code>insert-annotations mypackage.MyClass indexFile.jaif</code></pre>
    254 
    255 <p>
    256 will read in all the annotations from the annotation file
    257 <code>indexFile.jaif</code> and insert those annotations pertaining to
    258 <code>mypackage.myClass</code> into the class file for
    259 <code>mypackage.MyClass</code>, outputting the final class file to
    260 <code>mypackage.MyClass.class</code> in the present working directory.
    261 Note that the class file for <code>mypackage.MyClass</code> must be located
    262 on your classpath.
    263 </p>
    264 
    265 <p>
    266 Multiple pairs of class and index files (in that order) can be specified
    267 on a single command line; if the program exits normally, the results
    268 are the same as if the program were run once for each pair of arguments
    269 in sequence.
    270 Run:
    271 </p>
    272 
    273 <pre><code>insert-annotations --help</code></pre>
    274 
    275 <p>
    276 for full usage information.
    277 </p>
    278 
    279 
    280 <h3 id="extract-annotations"> Extract-annotations </h3>
    281 
    282 <p>
    283 To extract annotations from a class file and write them to an annotation file,
    284 use the extract-annotations tool.  Running:
    285 </p>
    286 
    287 <pre><code>extract-annotations mypackage.MyClass</code></pre>
    288 
    289 <p>
    290 will locate the class file for <code>mypackage.MyClass</code>, read all
    291 annotations from it, and write the results in annotation file format to
    292 <code>mypackage.MyClass.jaif</code> in the present working directory.  Note
    293 that <code>mypackage.MyClass</code> must be located on your classpath.
    294 </p>
    295 
    296 <p>
    297 Multiple classes can be specified on a single command line; if the
    298 program exits normally, the results are the same as if the program were
    299 run once for each class in sequence.
    300 Run:
    301 </p>
    302 
    303 <pre><code>extract-annotations --help</code></pre>
    304 
    305 <p>
    306 for full usage information.
    307 </p>
    308 
    309 
    310 <h3 id="insert-annotations-to-source">Insert-annotations-to-source </h3>
    311 
    312 <p>
    313 To insert annotations specified by an annotation file into a Java source file,
    314 use the insert-annotations-to-source tool.  Running:
    315 </p>
    316 
    317 <pre><code>insert-annotations-to-source index1.jaif index2.jaif mypackage/MyClass.java yourpackage/YourClass.java</code></pre>
    318 
    319 <p>
    320 will read all the annotations from <code>index1.jaif</code> and
    321 <code>index2.jaif</code>, insert them (when applicable) into their
    322 appropriate locations in <code>mypackage/MyClass.java</code> and
    323 <code>yourpackage/YourClass.java</code>, and write the results to
    324 <code>annotated/mypackage/MyClass.java</code> and
    325 <code>annotated/mypackage/MyClass.java</code>, respectively.
    326 </p>
    327 
    328 <p>
    329 Index and source files can be specified in any order, mixing the two
    330 file types freely; if the source files have no overlapping definitions
    331 and the program exits normally, the results are the same as if the
    332 program were run once for each source file, with <em>all</em> JAIFs
    333 given for each run.
    334 Run:
    335 </p>
    336 
    337 <pre><code>insert-annotations-to-source --help</code></pre>
    338 
    339 <p>
    340 for full usage information.
    341 </p>
    342 
    343 <p>
    344 If you wish to insert annotations into method bodies, you must have the
    345 associated class <code>mypackage.MyClass.class</code> on your classpath.
    346 You can insert annotations on class/field/method declarations and
    347 signatures without the class on your classpath.
    348 </p>
    349 
    350 <p>
    351 In general (but see below for exceptions), your source code needs to
    352 contain the locations that your
    353 <code>.jaif</code> file references.  In particular, if the
    354 <code>.jaif</code> file contains annotations for a type parameter, but the
    355 source code uses a raw type, then you will get an error such as
    356 </p>
    357 <pre>
    358 Found class Edge, but unable to insert @checkers.nullness.quals.Nullable:
    359   @checkers.nullness.quals.Nullable (nl=true) @ [GenericArrayLocationCriterion at ( [TYPE_ARGUMENT(0)] ), ...
    360 </pre>
    361 <p>
    362 In this case, you should add type arguments, such as changing
    363 </p>
    364 <pre>  public void pushNonezeroRing(Stack stack, Hashtable seen) {</pre>
    365 <p>to</p>
    366 <pre>  public void pushNonezeroRing(Stack&lt;Edge&gt; stack, Hashtable&lt;Edge, ?&gt; seen) {</pre>
    367 <p>
    368 In a few cases, insert-annotations-to-source will generate "default"
    369 code to provide a location for an annotation.  These include
    370 </p>
    371 <ul>
    372 <li>method and constructor <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.1-220">receivers;</a></li>
    373 <li>type parameter bounds (<code>extends Object</code>);</li>
    374 <li>type casts for expressions</li>
    375 <li>nullary constructor definitions; and</li>
    376 <li>explicit constructor invocations for literal arrays (e.g., <code>{"a", "b"}</code> becomes <code>new String[] {"a", "b"}</code>).</li>
    377 </ul>
    378 
    379 <p>
    380 The command-line options are as follows:
    381 </p>
    382 
    383 <!-- start options doc (DO NOT EDIT BY HAND) -->
    384 <ul>
    385   <li id="optiongroup:General-options">General options
    386     <ul>
    387       <li id="option:outdir"><b>-d</b> <b>--outdir=</b><i>directory</i>. Directory in which output files are written. [default annotated/]</li>
    388       <li id="option:in-place"><b>-i</b> <b>--in-place=</b><i>boolean</i>. If true, overwrite original source files (making a backup first).
    389  Furthermore, if the backup files already exist, they are used instead
    390  of the .java files.  This behavior permits a user to tweak the .jaif
    391  file and re-run the annotator.
    392  <p>
    393 
    394  Note that if the user runs the annotator with --in-place, makes edits,
    395  and then re-runs the annotator with this --in-place option, those
    396  edits are lost.  Similarly, if the user runs the annotator twice in a
    397  row with --in-place, only the last set of annotations will appear in
    398  the codebase at the end.
    399  <p>
    400 
    401  To preserve changes when using the --in-place option, first remove the
    402  backup files.  Or, use the <code>-d .</code> option, which makes (and
    403  reads) no backup, instead of --in-place. [default false]</li>
    404       <li id="option:abbreviate"><b>-a</b> <b>--abbreviate=</b><i>boolean</i>. Abbreviate annotation names [default true]</li>
    405       <li id="option:comments"><b>-c</b> <b>--comments=</b><i>boolean</i>. Insert annotations in comments [default false]</li>
    406       <li id="option:omit-annotation"><b>-o</b> <b>--omit-annotation=</b><i>string</i>. Omit given annotation</li>
    407       <li id="option:nowarn"><b>--nowarn=</b><i>boolean</i>. Suppress warnings about disallowed insertions [default false]</li>
    408       <li id="option:convert-jaifs"><b>--convert-jaifs=</b><i>boolean</i>. Convert JAIFs to new format [default false]</li>
    409       <li id="option:help"><b>-h</b> <b>--help=</b><i>boolean</i>. Print usage information and exit [default false]</li>
    410     </ul>
    411   </li>
    412   <li id="optiongroup:Debugging-options">Debugging options
    413     <ul>
    414       <li id="option:verbose"><b>-v</b> <b>--verbose=</b><i>boolean</i>. Verbose (print progress information) [default false]</li>
    415       <li id="option:debug"><b>--debug=</b><i>boolean</i>. Debug (print debug information) [default false]</li>
    416       <li id="option:print-error-stack"><b>--print-error-stack=</b><i>boolean</i>. Print error stack [default false]</li>
    417     </ul>
    418   </li>
    419 </ul>
    420 <!-- end options doc -->
    421 
    422 
    423 <hr />
    424 <h2 id="implementation">Design and Implementation Details</h2>
    425 
    426 <p>This section describes some high level-design and implementation
    427 details of the Annotation File Utilities, including the different
    428 components of the Annotation File Utilities and how they fit together.
    429 It is intended for someone who is beginning work on the Annotation File
    430 Utilities or is curious about how the Annotation File Utilities work.</p>
    431 
    432 <p>The Annotation File Utilities is composed of three sub-projects:
    433 <code>asmx</code>, <code>scene-lib</code>, and
    434 <code>annotation-file-utilities</code>.  The <code>asmx</code>
    435 sub-project provides methods for reading and writing class files.  The
    436 <code>scene-lib</code> sub-project represents a <code>.jaif</code> file
    437 and inserts and extracts annotations to/from bytecode.  The
    438 <code>annotation-file-utilities</code> sub-project inserts annotations
    439 into source code.</p>
    440 
    441 <h3 id="asmx" class="subsection">Asmx</h3>
    442 
    443 <p>The <code>asmx</code> sub-project provides methods for reading and
    444 writing class files.  <code>asmx</code> is based on an old version
    445 (2.2.2) of the <a href="http://asm.ow2.org/">ASM Framework</a>. It has been modified to
    446 allow it to read and write <a href="https://checkerframework.org/jsr308/specification/java-annotation-design.html">JSR 308</a>
    447 annotations in bytecode.  However, it is far behind the current ASM
    448 version (5.0), which provides support for type annotations. We should
    449 discard the custom <code>asmx</code> in the Annotation File Utilities
    450 and adapt the Annotation File Utilities to use the official, supported
    451 version of ASM.</p>
    452 
    453 <h3 id="scene-lib" class="subsection">Scene-lib</h3>
    454 
    455 <p><code>scene-lib</code> is an interface to a <code>.jaif</code> file.
    456 It reads in and writes out <code>.jaif</code> files and provides an
    457 internal representation of a <code>.jaif</code> file to access and
    458 manipulate.</p>
    459 
    460 <p>Internally, a <code>.jaif</code> file is represented by the
    461 <code>scene-lib/src/annotations/el/AScene.java</code> class. The
    462 <code>AScene</code> class (or &ldquo;annotated scene&rdquo;) roughly
    463 parallels the root of an abstract syntax tree. An <code>AScene</code>
    464 has a number of classes (<code>AClass</code>) as children. Each class
    465 has a number of methods (<code>AMethod</code>), fields
    466 (<code>AElement</code>), etc. as children. All of these classes are
    467 related in the type hierarchy shown below.</p>
    468 
    469 <div>
    470 <img src="figures/scene-lib-type-hierarchy.svg" alt="scene-lib type hierarchy">
    471 </div>
    472 
    473 <p>Each class in the type hierarchy has one or more fields to hold
    474 annotations for the different components of the class. For example, the
    475 <code>AMethod</code> class has the following fields: bounds, return
    476 type, receiver parameters, and throws clause. Each of these fields
    477 holds the annotations stored on that part of the method. For details on
    478 the remainder of the classes in the type hierarchy, and their
    479 respective fields, see the documentation for each file in
    480 <code>scene-lib/src/annotations/el/</code>.</p>
    481 
    482 <p>An <code>AScene</code> instance can be created in two ways. An empty
    483 <code>AScene</code> can be created by calling the <code>AScene</code>
    484 constructor, or an <code>AScene</code> can be created by parsing an
    485 existing <code>.jaif</code> file. Once an <code>AScene</code> is
    486 created, annotations can be added to it by adding them to the correct
    487 fields of the children. An <code>AScene</code> can also be output to
    488 create a new <code>.jaif</code> file.</p>
    489 
    490 <h4 id="bytecode-insertion" class="subsubsection">Bytecode Insertion</h4>
    491 
    492 <p>Annotations can be inserted into bytecode by executing the
    493 <code>annotation-file-utilities/scripts/insert-annotations</code>
    494 script. This script takes one or more &langle;class name,
    495 <code>.jaif</code> file&rangle; pairs as arguments. The annotations
    496 specified in the <code>.jaif</code> file are inserted into the
    497 classfile directly before the <code>.jaif</code> file in the argument
    498 list.</p><p>First, each <code>.jaif</code> file is parsed into an
    499 <code>AScene</code> (as described in
    500 <a href="#scene-lib">Scene-lib</a>). Then,
    501 <code>asmx/src/org/objectweb/asm/ClassReader.java</code> parses the
    502 classfile.  As it is parsing the classfile, it passes the parsed
    503 bytecode off to the
    504 <code>scene-lib/src/annotations/io/classfile/ClassAnnotationSceneWriter.java</code>
    505 class. This class has a reference to the <code>AScene</code> parsed
    506 from the <code>.jaif</code> file. As this class receives the parsed
    507 bytecode it inserts the relevant annotations from the
    508 <code>AScene</code> in the bytecode and then writes the bytecode back
    509 out.</p>
    510 
    511 <h4 id="bytecode-extraction" class="subsubsection">Bytecode Extraction</h4>
    512 
    513 <p>Annotations can be extracted from bytecode by executing the
    514 <code>annotation-file-utilities/scripts/extract-annotations</code>
    515 script. This script takes one or more class names as arguments and
    516 outputs the annotations found in those classes to <code>.jaif</code>
    517 files.</p><p>First, an empty <code>AScene</code> is constructed to store
    518 the annotations.
    519 <code>asmx/src/org/objectweb/asm/ClassReader.java</code> parses the
    520 classfile and passes the parsed bytecode off to the
    521 <code>scene-lib/src/annotations/io/classfile/ClassAnnotationSceneReader.java</code>
    522 class. This class filters out the annotations in the bytecode and adds
    523 them to the correct part of the <code>AScene</code>. After this, the
    524 <code>AScene</code> is output to a <code>.jaif</code> file.</p>
    525 
    526 <h3 id="source-insertion" class="subsection">Annotation-file-utilities</h3>
    527 
    528 <p>The <code>annotation-file-utilities</code> sub-project inserts annotations into source
    529 code. It can be run by executing the
    530 <code>annotation-file-utilities/scripts/insert-annotations-to-source</code>
    531 script.  The script takes one or more <code>.jaif</code> files, followed by one or more <code>.java</code> source files as arguments. The annotations in the <code>.jaif</code> files are inserted into the <code>.java</code> source files.</p><p>First, an instance of
    532 <code>annotation-file-utilities/src/annotator/specification/IndexFileSpecification.java</code>
    533 is created. Its <code>parse</code> method parses the <code>.jaif</code>
    534 file into an <code>AScene</code> (as described in
    535 <a href="#scene-lib">Scene-lib</a>).  The <code>parse</code> method
    536 calls the <code>parseScene</code> method, which traverses through the
    537 <code>AScene</code> and creates an
    538 <code>annotation-file-utilities/src/annotator/specification/CriterionList.java.</code>
    539 A <code>CriterionList</code> identifies a unique AST node that is the
    540 location of an insertion. It contains objects that implement the
    541 <code>annotation-file-utilities/src/annotator/find/Criterion.java</code>
    542 interface.
    543 Each <code>Criterion</code> has an <code>isSatisifiedBy</code> method 
    544 a predicate that takes an AST node and returns <code>true</code> if the
    545 AST node satisfies the <code>Criterion</code> and <code>false</code>
    546 otherwise. To determine if a given node matches a
    547 <code>CriterionList</code>, the node is passed to all of the
    548 <code>Criterion</code>s in the <code>CriteriaList</code>. If every
    549 <code>Criterion</code> returns <code>true</code> then it is match. If
    550 one or more <code>Criterion</code>s return <code>false</code> then it is
    551 not a match.  The various <code>Criterion</code> classes are in the
    552 <code>annotation-file-utilities/src/annotator/find/</code> directory.
    553 For example, take the following source code:</p>
    554 
    555 <pre class="verbatim">package afu.example;
    556 
    557 public class Test {
    558     public void m(boolean b, int i) {
    559       // ...
    560     }
    561 }
    562 </pre>
    563 
    564 <p>The <code>CriterionList</code> to specify the location of the
    565 <code>i</code> parameter contains the following
    566 <code>Criterion</code>s:</p>
    567 
    568 <ul class="itemize"><li class="li-itemize">
    569 <code>InPackageCriterion("afu.example")</code>
    570 </li><li class="li-itemize"><code>InClassCriterion("Test")</code>
    571 </li><li class="li-itemize"><code>InMethodCriterion("m(ZI)V")</code>
    572 </li><li class="li-itemize"><code>ParamCriterion(1)</code>
    573 </li></ul>
    574 
    575 <p>After this <code>CriterionList</code> is built up an
    576 <code>annotation-file-utilities/src/annotator/find/Insertion.java</code>
    577 is created.  An <code>Insertion</code> stores an
    578 <code>annotation-file-utilities/src/annotator/find/Criteria.java</code>
    579 (which is created from a <code>CriterionList</code>) and the text to be
    580 inserted. All of these <code>Insertion</code>s are then added to a
    581 list. The Java compiler then is called to parse the Java source into an
    582 abstract syntax tree. This is followed by a call to the
    583 <code>getPositions</code> method of
    584 <code>annotation-file-utilities/src/annotator/find/TreeFinder.java</code>,
    585 which scans through each node of the abstract syntax trees. For each node,
    586 it runs through the <code>Criteria</code> for each un-matched
    587 <code>Insertion</code>. If at least one of the <code>Criteria</code>
    588 does not match, then this is not the correct place for the
    589 <code>Insertion</code> and the <code>Insertion</code> will be checked
    590 at the remaining nodes of the tree. If all of the <code>Criteria</code>
    591 match, then this node is the correct place for the
    592 <code>Insertion</code>. It is removed from the list of un-matched
    593 <code>Insertion</code>s and the position where to insert the
    594 <code>Insertion</code> is determined. This position is the integer
    595 index in the file where the <code>Insertion</code> should be inserted.
    596 After the positions are found for all of the <code>Insertion</code>s,
    597 the <code>Insertion</code> text is inserted into the file. This happens
    598 backwards, with <code>Insertion</code>s at the end of the file (i.e.
    599 with higher positions) being inserted first. If <code>Insertion</code>s
    600 were instead inserted from the beginning of the file then a single
    601 <code>Insertion</code> would invalidate all of the positions for the
    602 following <code>Insertion</code>s.</p>
    603 
    604 <p>If there are remaining <code>Insertion</code>s that were not matched
    605 to a node in the abstract syntax tree then an error message is
    606 displayed.</p>
    607 
    608 
    609 <hr />
    610 <h2 id="feedback"> Feedback and bug reports </h2>
    611 
    612 <p>
    613 To submit a bug report or request a new feature, use the
    614 <a href="https://github.com/typetools/annotation-tools/issues">issue
    615   tracker</a>.  When reporting a bug, please include exact instructions in
    616 how to reproduce it, and please also attach relevant input files.  This
    617 will let us resolve the issue quickly.
    618 </p>
    619 
    620 <p>
    621 You can also reach the developers at
    622 <a href="mailto:annotation-tools-dev (a] googlegroups.com">annotation-tools-dev (a] googlegroups.com</a>.
    623 But please use the
    624   <a href="https://github.com/typetools/annotation-tools/issues">issue
    625   tracker</a> for bug reports and feature requests.
    626 </p>
    627 
    628 
    629 <h3 id="changelog">Changelog</h3>
    630 
    631 <p>
    632 The <a href="changelog.html">changelog</a> describes what is new in each release.
    633 </p>
    634 
    635 
    636 <hr/>
    637 
    638 </body>
    639 </html>
    640 
    641 <!--  LocalWords:  utils bashrc tcsh tcshrc cshrc classpath
    642  -->
    643