forked from typetools/annotation-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
annotation-file-format.tex
1460 lines (1233 loc) · 57.9 KB
/
annotation-file-format.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage{fullpage}
\usepackage{relsize}
\usepackage{url}
\usepackage{hevea}
\usepackage[shortcuts]{extdash}
\usepackage{textcomp}
% \usepackage{verbdef}
\def\topfraction{.9}
\def\dbltopfraction{\topfraction}
\def\floatpagefraction{\topfraction} % default .5
\def\dblfloatpagefraction{\topfraction} % default .5
\def\textfraction{.1}
%HEVEA \footerfalse % Disable hevea advertisement in footer
\newcommand{\code}[1]{\ifmmode{\mbox{\relax\ttfamily{#1}}}\else{\relax\ttfamily #1}\fi}
%% Hevea version omits "\smaller"
%HEVEA \renewcommand{\code}[1]{\ifmmode{\mbox{\ttfamily{#1}}}\else{\ttfamily #1}\fi}
\newcommand{\includeimage}[2]{
\begin{center}
\ifhevea\imgsrc{#1.png}\else
\resizebox{!}{#2}{\includegraphics{figures/#1}}
\vspace{-1.5\baselineskip}
\fi
\end{center}}
% Add line between figure and text
\makeatletter
\def\topfigrule{\kern3\p@ \hrule \kern -3.4\p@} % the \hrule is .4pt high
\def\botfigrule{\kern-3\p@ \hrule \kern 2.6\p@} % the \hrule is .4pt high
\def\dblfigrule{\kern3\p@ \hrule \kern -3.4\p@} % the \hrule is .4pt high
\makeatother
\title{Annotation File Format Specification}
% Hevea ignores \date, so move the date into \author
\author{\url{https://checkerframework.org/annotation-file-utilities/} \\
\today}
\date{}
\begin{document}
\maketitle
%HEVEA \setcounter{tocdepth}{2}
\tableofcontents
\section{Purpose: External storage of annotations\label{purpose}}
Java annotations are meta-data about Java program elements, as in
``\code{@Deprecated class Date \{ \ldots\ \}}''.
Ordinarily, Java annotations are written in the source code of a
\code{.java} Java source file. When \code{javac} compiles the source code,
it inserts the annotations in the resulting \code{.class} file (as
``attributes'').
Sometimes, it is convenient to specify the annotations outside the source
code or the \code{.class} file.
\begin{itemize}
%BEGIN LATEX
\itemsep 0pt \parskip 0pt
%END LATEX
\item
When source code is not available, a textual file provides a format for
writing and storing annotations that is much easier to read and modify
than a \code{.class} file. Even if the eventual purpose is to insert the
annotations in the \code{.class} file, the annotations must be specified
in some textual format first.
\item
Even when source code is available, sometimes it should not be changed,
yet annotations must be stored somewhere for use by tools.
\item
A textual file for annotations can eliminate code clutter. A developer
performing some specialized task (such as code verification,
parallelization, etc.)\ can store annotations in an annotation file without
changing the main version of the source code. (The developer's private
version of the code could contain the annotations, but the developer
could move them to the separate file before committing changes.)
\item
Tool writers may find it more convenient to use a textual file, rather
than writing a Java or \code{.class} file parser.
\item
When debugging annotation-processing tools, a textual file format
(extracted from the Java or \code{.class} files) is easier to read and
is easier for use in testing.
\end{itemize}
All of these uses require an external, textual file format for Java annotations.
The external file format should be easy for people to create, read, and
modify.
%
An ``annotation file'' serves this purpose by specifying a set of
Java annotations.
The Annotation File Utilities
(\url{https://checkerframework.org/annotation-file-utilities/}) are a set
of tools that process annotation files.
The file format discussed in this document supports both standard Java SE 5
declaration annotations and also the type annotations that are introduced by Java SE 8.
The file format provides a simple syntax to represent the structure of a Java
program. For annotations in method bodies of \code{.class} files the annotation
file closely follows
section ``Class File Format Extensions'' of the JSR 308 design document~\cite{JSR308-webpage-201310},
which explains how the annotations are stored in the \code{.class}
file.
In that sense, the current design is extremely low-level, and users
probably would not want to write the files by hand (but they might fill in a
template that a tool generated automatically). As future work, we should
design a more
user-friendly format that permits Java signatures to be directly specified.
For \code{.java} source files, the file format provides a separate, higher-level
syntax for annotations in method bodies.
%% I don't like this, as it may force distributing logically connected
%% elements all over a file system. Users should be permitted, but not
%% forced, to adopt such a file structure. -MDE
% Each file corresponds to exactly one
% ``.class'' file, so (for instance) inner classes are written in
% separate annotation files, named in the same ``{\tt
% OuterClass\$InnerClass}'' pattern as the ``.class'' file.
By convention, an annotation file ends with ``\code{.jaif}'' (for ``Java
annotation index file''), but this is not required.
% \verbdef\lineend|"\n"|
%BEGIN LATEX
\DefineShortVerb{\|}
\SaveVerb{newline}|\n|
\UndefineShortVerb{\|}
\newcommand{\lineend}{\bnflit{\UseVerb{newline}}}
%END LATEX
%HEVEA \newcommand{\bs}{\char"5C}
%HEVEA \newcommand{\lineend}{\bnflit{\bs{}n}}
% literal
\newcommand{\bnflit}[1]{\textrm{``}\textbf{#1}\textrm{''}}
% non-terminal
\newcommand{\bnfnt}[1]{\textsf{\emph{#1}}}
% comment
\newcommand{\bnfcmt}{\rm \# }
% alternative
\newcommand{\bnfor}{\ensuremath{|}}
\section{Grammar\label{grammar}}
This section describes the annotation file format in detail by presenting it in
the form of a grammar. Section~\ref{grammar-conventions} details the conventions
of the grammar. Section~\ref{java-file-grammar} shows how to represent the
basic structure of a Java program (classes, methods, etc.) in an annotation
file. Section~\ref{annotations-grammar} shows how to add annotations to an
annotation file.
\subsection{Grammar conventions\label{grammar-conventions}}
Throughout this document, ``name'' is any valid Java simple name or
binary name, ``type'' is any valid type, and ``value'' is any
valid Java constant, and quoted strings are literal values.
%
The Kleene qualifiers ``*'' (zero or more), ``?'' (zero or one), and ``+''
(one or more) denote plurality of a grammar element.
%
A vertical bar (``\bnfor'') separates alternatives.
Parentheses (``()'') denote grouping, and square brackets (``[]'')
denote optional syntax, which is equivalent to ``( \ldots\ )\ ?''\ but more concise.
We use the hash/pound/octothorpe symbol (``\#'') for comments within the grammar.
In the annotation file,
besides its use as token separator,
whitespace (excluding
newlines) is optional with one exception: no space is permitted
between an ``@'' character and a subsequent name. Indentation is
ignored, but is encouraged to maintain readability of the hierarchy of
program elements in the class (see the example in Section~\ref{example}).
Comments can be written throughout the annotation file using the double-slash
syntax employed by Java for single-line comments: anything following
two adjacent slashes (``//'') until the first newline is a comment.
This is omitted from the grammar for simplicity.
Block comments (``/* \ldots\ */'') are not allowed.
The line end symbol \lineend{} is used for all the different line end
conventions, that is, Windows- and Unix-style newlines are supported.
\subsection{Java file grammar\label{java-file-grammar}}
This section shows how to represent the basic structure of a Java program
(classes, methods, etc.) in an annotation file. For Java elements that can
contain annotations, this section will reference grammar productions contained
in Section~\ref{annotations-grammar}, which describes how annotations are used
in an annotation file.
An annotation file has the same basic structure as a Java program. That is,
there are packages, classes, fields and methods.
The annotation file may omit certain program elements --- for instance, it
may mention only some of the packages in a program, or only some of the
classes in a package, or only some of the fields or methods of a class.
Program elements that do not appear in the annotation file are treated as
unannotated.
\subsubsection{Package definitions\label{package-definitions}}
At the root of an annotation file is one or more package definitions.
A package definition describes a package containing a list of annotation
definitions and classes. A package definition also contains any
annotations on the package (such as those from a
\code{package-info.java} file).
\begin{tabbing}
\qquad \= \kill
\bnfnt{annotation-file} ::= \\
\qquad \bnfnt{package-definition}+ \\
\\
\bnfnt{package-definition} ::= \\
\qquad \bnflit{package} ( \bnflit{:} ) \bnfor{} ( \bnfnt{name} \bnflit{:} \bnfnt{decl-annotation}* ) \lineend \\
\qquad ( \bnfnt{annotation-definition} \bnfor{} \bnfnt{class-definition} ) *
\end{tabbing}
\noindent
Use a package line of \code{package:} for the default package. Note that
annotations on the default package are not allowed.
\subsubsection{Class definitions\label{class-definitions}}
A class definition describes the annotations present on a class declaration,
as well as fields and methods of the class. It is organized according to
the hierarchy of fields and methods in the class.
Note that we use \bnfnt{class-definition} also for interfaces, enums, and
annotation types (to specify annotations in an existing annotation type, not to
be confused with \bnfnt{annotation-definition}s described in
Section~\ref{annotation-definitions}, which defines annotations to be used
throughout an annotation file); for syntactic simplicity, we use \bnflit{class}
for
all such definitions.
% TODO: add test cases for this.
Inner classes are treated as ordinary classes whose names happen to contain
\code{\$} signs and must be defined at the top level of a class definition file.
(To change this, the grammar would have to be extended with a closing
delimiter for classes; otherwise, it would be ambiguous whether a
field or method appearing after an inner class definition belonged to the
inner class or the outer class.) The syntax for inner class names is the same as
is used by the \code{javac} compiler. A good way to get an idea of the inner
class names for a class is to compile the class and look at the filenames of the
\code{.class} files that are produced.
\begin{tabbing}
\qquad \= \kill
\bnfnt{class-definition} ::= \\
\qquad \bnflit{class} \bnfnt{name} \bnflit{:} \bnfnt{decl-annotation}* \lineend \\
% TODO: is the order really important? eg. can fields and methods not
% be mixed?
\qquad \bnfnt{typeparam-definition}* \\
\qquad \bnfnt{typeparam-bound}* \\
\qquad \bnfnt{extends}* \\
\qquad \bnfnt{implements}* \\
\qquad \bnfnt{field-definition}* \\
\qquad \bnfnt{staticinit}* \\
\qquad \bnfnt{instanceinit}* \\
\qquad \bnfnt{method-definition}*
\end{tabbing}
\noindent
Annotations on the \bnflit{class} line are annotations on the class declaration,
not the class name.
\paragraph{Type parameter definitions}
The \bnfnt{typeparam-definition} production defines annotations on the
declaration of a type parameter, such as on \code{K} and \code{T} in
\begin{verbatim}
public class Class<K> {
public <T> void m() {
...
}
}
\end{verbatim}
or on the type parameters on the left-hand side of a member reference,
as on \code{String} in \code{List<String>::size}.
\begin{tabbing}
\qquad \= \kill
\bnfnt{typeparam-definition} ::= \\
\qquad \bnfcmt The integer is the zero-based type parameter index. \\
\qquad \bnflit{typeparam} \bnfnt{integer} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}*
\end{tabbing}
\paragraph{Type Parameter Bounds}
The \bnfnt{typeparam-bound} production defines annotations on a bound of a
type variable declaration, such as on \code{Number} and \code{Date} in
\begin{verbatim}
public class Class<K extends Number> {
public <T extends Date> void m() {
...
}
}
\end{verbatim}
\begin{tabbing}
\qquad \= \kill
\bnfnt{typeparam-bound} ::= \\
% The bound should really be a sub-element of the typeparam!
\qquad \bnfcmt The integers are respectively the parameter and bound indexes of \\
\qquad \bnfcmt the type parameter bound~\cite{JSR308-webpage-201310}. \\
\qquad \bnflit{bound} \bnfnt{integer} \bnflit{\&} \bnfnt{integer} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}*
\end{tabbing}
\paragraph{Implements and extends}
The \bnfnt{extends} and \bnfnt{implements} productions
define annotations on the names of classes a class \code{extends} or
\code{implements}.
(Note: For interface declarations, \bnfnt{implements} rather than
\bnfnt{extends} defines annotations on the names of extended
interfaces.)
\begin{tabbing}
\qquad \= \kill
\bnfnt{extends} ::= \\
\qquad \bnflit{extends} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}* \\
\\
\bnfnt{implements} ::= \\
\qquad \bnfcmt The integer is the zero-based index of the implemented interface. \\
\qquad \bnflit{implements} \bnfnt{integer} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}*
\end{tabbing}
\paragraph{Static and instance initializers}
The \bnfnt{staticinit} and \bnfnt{instanceinit} productions
define annotations on code within static or instance initializer blocks.
\begin{tabbing}
\qquad \= \kill
\bnfnt{staticinit} ::= \\
\qquad \bnfcmt The integer is the zero-based index of the implemented interface. \\
\qquad \bnflit{staticinit} \bnflit{*} \bnfnt{integer} \bnflit{:} \lineend \\
\qquad \bnfnt{compound-type}*
\\
\bnfnt{instanceinit} ::= \\
\qquad \bnfcmt The integer is the zero-based index of the implemented interface. \\
\qquad \bnflit{instanceinit} \bnflit{*} \bnfnt{integer} \bnflit{:} \lineend \\
\qquad \bnfnt{compound-type}*
\end{tabbing}
\subsubsection{Field definitions\label{field-definitons}}
A field definition can have annotations on the declaration, the type of the
field, or --- if in source code --- the field's initialization.
\begin{tabbing}
\qquad \= \kill
\bnfnt{field-definition} ::= \\
\qquad \bnflit{field} \bnfnt{name} \bnflit{:} \bnfnt{decl-annotation}* \lineend \\
\qquad \bnfnt{type-annotations}* \\
\qquad \bnfnt{expression-annotations}*
\end{tabbing}
\noindent
Annotations on the \bnflit{field} line are on the field declaration, not the
type of the field.
The \bnfnt{expression-annotations} production specifies annotations on the
initialization expression of a field. If a field is initialized at declaration
then in bytecode the initialization is moved to the constructor when the class
is compiled. Therefore for bytecode, annotations on the initialization
expression go in the constructor (see Section~\ref{method-definitions}), rather
than the field definition. Source code annotations for the field initialization
expression are valid on the field definition.
\subsubsection{Method definitions\label{method-definitions}}
A method definition can have annotations on the method declaration, in the
method signature (return type, parameters, etc.), as well as the method body.
\begin{tabbing}
\qquad \= \kill
\bnfnt{method-definition} ::= \\
\qquad \bnflit{method} \bnfnt{method-key} \bnflit{:} \bnfnt{decl-annotation}* \lineend \\
\qquad \bnfnt{typeparam-definition}* \\
\qquad \bnfnt{typeparam-bound}* \\
\qquad \bnfnt{return-type}? \\
\qquad \bnfnt{receiver-definition}? \\
\qquad \bnfnt{parameter-definition}* \\
% TODO: method throws
\qquad \bnfnt{variable-definition}* \\
\qquad \bnfnt{expression-annotations}*
\end{tabbing}
\urldef\jvmsMethodDescriptors\url|https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html#jvms-4.3.3|
\noindent
The annotations on the \bnflit{method} line are on the method declaration, not
on the return value. The \bnfnt{method-key} consists of the simple name followed
by a method descriptor, which is the signature in JVML format
(see JVMS \S4.3.3, \jvmsMethodDescriptors). For example, the following method
\begin{verbatim}
boolean foo(int[] i, String s) {
...
}
\end{verbatim}
\noindent
has the \bnfnt{method-key}:
\begin{verbatim}
foo([ILjava/lang/String;)Z
\end{verbatim}
Note that the
signature is the erased signature of the method and does not contain generic
type information, but does contain the return type. Using \code{javap -s} makes
it easy to find the signature. The method keys ``\code{<init>}'' and
``\code{<clinit>}'' are used to name instance (constructor) and class (static)
initialization methods. (The name of the constructor---that is, the final
element of the class name---can be used in place of ``\code{<init>}''.)
For both instance and class initializers, the ``return type'' part of the
signature should be \code{V} (for \code{void}).
% TODO: exception types in catch clause
% TODO: .class literals
% TODO: type arguments in constructor and method calls
\paragraph{Return type}
A return type defines the annotations on the return type of a method
declaration. It is also used for the result of a constructor.
\begin{tabbing}
\qquad \= \kill
\bnfnt{return-type} ::= \\
\qquad \bnflit{return:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}*
\end{tabbing}
\paragraph{Receiver definition}
A receiver definition defines the annotations on the type of the receiver
parameter in a method declaration. A method receiver is the implicit formal
parameter, \code{this}, used in non-static methods. For source code insertion,
the receiver parameter will be inserted if it does not already exist.
Only inner classes have a receiver. A top-level constructor does not have
a receiver, though it does have a result. The type of a constructor result
is represented as a return type.
\begin{tabbing}
\qquad \= \kill
\bnfnt{receiver-definition} ::= \\
\qquad \bnflit{receiver:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}*
\end{tabbing}
\paragraph{Parameter definition}
A formal parameter definition defines the annotations on a method formal
parameter declaration and the type of a method formal parameter, but
\emph{not} the receiver formal parameter.
\begin{tabbing}
\qquad \= \kill
\bnfnt{parameter-definition} ::= \\
\qquad \bnfcmt The integer is the zero-based index of the formal parameter in the method. \\
\qquad \bnflit{parameter} \bnfnt{integer} \bnflit{:} \bnfnt{decl-annotation}* \lineend \\
\qquad \bnfnt{type-annotations}*
\end{tabbing}
\noindent
The annotations on the \bnflit{parameter} line are on the formal parameter
declaration, not on the type of the parameter. A parameter index of 0 is the
first formal parameter. The receiver parameter is not index 0. Use the
\bnfnt{receiver-definition} production to annotate the receiver parameter.
\subsection{Bytecode Locations\label{bytecode-locations}}
Certain elements in the body of a method or the initialization expression of a
field can be annotated. The \bnfnt{expression-annotations} rule describes the
annotations that can be added to a method body or a field initialization
expression:
\begin{tabbing}
\qquad \= \kill
\bnfnt{expression-annotations} ::= \\
\qquad \bnfnt{typecast}* \\
\qquad \bnfnt{instanceof}* \\
\qquad \bnfnt{new}* \\
\qquad \bnfnt{call}* \\
\qquad \bnfnt{reference}* \\
\qquad \bnfnt{lambda}* \\
\qquad \bnfnt{source-insert-typecast}* \\
\qquad \bnfnt{source-insert-annotation}*
\end{tabbing}
\noindent
Additionally, a variable declaration in a method body can be annotated with the
\bnfnt{variable-definition} rule, which appears below.
Because of the differences between Java source code and \code{.class} files,
the syntax for specifying code locations is different for \code{.class} files
and source code. For \code{.class} files we use a syntax called ``bytecode
offsets''. For source code we use a different syntax called ``source code
indexes''. These are both described below.
If you wish to be able to insert a given code annotation in both a \code{.class} file and a source
code file, the annotation file must redundantly specify the annotation's bytecode offset and source
code index. This can be done in a single \code{.jaif} file or two separate
\code{.jaif} files. It is not necessary to include
redundant information to insert annotations on signatures in both \code{.class}
files and source code.
Additionally, a new typecast with annotations (rather than an annotation added to an
existing typecast) can be inserted into source code. This uses a third
syntax that is described below under ``AST paths''.
A second way to insert a typecast is by specifying just an annotation, not
a full typecast (\code{insert-annotation} instead of
\code{insert-typecast}). In this case, the source annotation insertion
tool generates a full typecast if Java syntax requires one.
\subsubsection{Bytecode offsets\label{bytecode-offsets}}
For locations in bytecode, the
annotation file uses offsets into the bytecode array of the class file to
indicate the specific expression to which the annotation refers. Because
different compilation strategies yield different \code{.class} files, a
tool that maps such annotations from an annotation file into source code must
have access to the specific \code{.class} file that was used to generate
the annotation file. The
\code{javap -v} command is an effective technique to discover bytecode
offsets. Non-expression annotations such as those on methods,
fields, classes, etc., do not use a bytecode offset.
\subsubsection{Source code indexes\label{source-code-indexes}}
For locations in source code, the annotation file indicates the kind of
expression, plus a zero-based index to indicate which occurrence of that kind of
expression. For example,
\begin{verbatim}
public void method() {
Object o1 = new @A String();
String s = (@B String) o1;
Object o2 = new @C Integer(0);
Integer i = (@D Integer) o2;
}
\end{verbatim}
\noindent
\code{@A} is on new, index 0. \code{@B} is on typecast, index 0. \code{@C} is on
new, index 1. \code{@D} is on typecast, index 1.
Source code indexes only include occurrences in the class that exactly matches
the name of the enclosing \bnfnt{class-definition} rule. Specifically,
occurrences in nested classes are not included. Use a new
\bnfnt{class-definition} rule with the name of the nested class for source code
insertions in a nested class.
\subsubsection{Code locations grammar\label{code-grammar}}
For each kind of expression, the grammar contains a separate location rule.
This location rule contains the bytecode offset syntax followed by the
source code index syntax.
The grammar uses \bnflit{\#} for bytecode offsets and \bnflit{*} for source code indexes.
\begin{tabbing}
\qquad \= \kill
\bnfnt{variable-location} ::= \\
\qquad \bnfcmt Bytecode offset: the integers are respectively the index, start, and length \\
\qquad \bnfcmt fields of the annotations on this variable~\cite{JSR308-webpage-201310}. \\
\qquad (\bnfnt{integer} \bnflit{\#} \bnfnt{integer} \bnflit{+} \bnfnt{integer}) \\
\qquad \bnfcmt Source code index: the \bnfnt{name} is the identifier of the local variable. \\
\qquad \bnfcmt The \bnfnt{integer} is the optional zero-based index of the intended local \\
\qquad \bnfcmt variable within all local variables with the given \bnfnt{name}. \\
\qquad \bnfcmt The default value for the index is zero. \\
\qquad \bnfor{} (\bnfnt{name} [\bnflit{*} \bnfnt{integer}]) \\
\\
\bnfnt{variable-definition} ::= \\
\qquad \bnfcmt The annotations on the \bnflit{local} line are on the variable declaration, \\
\qquad \bnfcmt not the type of the variable. \\
\qquad \bnflit{local} \bnfnt{variable-location} \bnflit{:} \bnfnt{decl-annotation}* \lineend \\
\qquad \bnfnt{type-annotations}* \\
\\
\bnfnt{typecast-location} ::= \\
\qquad \bnfcmt Bytecode offset: the first integer is the offset field and the optional \\
\qquad \bnfcmt second integer is the type index of an intersection type~\cite{JSR308-webpage-201310}. \\
\qquad \bnfcmt The type index defaults to zero if not specified. \\
\qquad (\bnflit{\#} \bnfnt{integer} [ \bnflit{,} \bnfnt{integer} ]) \\
\qquad \bnfcmt Source code index: the first integer is the zero-based index of the typecast \\
\qquad \bnfcmt within the method and the optional second integer is the type index of an \\
\qquad \bnfcmt intersection type~\cite{JSR308-webpage-201310}. The type index defaults to zero if not specified. \\
\qquad \bnfor{} (\bnflit{*} \bnfnt{integer} [ \bnflit{,} \bnfnt{integer} ]) \\
\\
\bnfnt{typecast} ::= \\
\qquad \bnflit{typecast} \bnfnt{typecast-location} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}* \\
\\
\bnfnt{instanceof-location} ::= \\
\qquad \bnfcmt Bytecode offset: the integer is the offset field of the annotation~\cite{JSR308-webpage-201310}. \\
\qquad (\bnflit{\#} \bnfnt{integer}) \\
\qquad \bnfcmt Source code index: the integer is the zero-based index of the \code{instanceof} \\
\qquad \bnfcmt within the method. \\
\qquad \bnfor{} (\bnflit{*} \bnfnt{integer}) \\
\\
\bnfnt{instanceof} ::= \\
\qquad \bnflit{instanceof} \bnfnt{instanceof-location} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}* \\
\\
\bnfnt{new-location} ::= \\
\qquad \bnfcmt Bytecode offset: the integer is the offset field of the annotation~\cite{JSR308-webpage-201310}. \\
\qquad (\bnflit{\#} \bnfnt{integer}) \\
\qquad \bnfcmt Source code index: the integer is the zero-based index of the object or array \\
\qquad \bnfcmt creation within the method. \\
\qquad \bnfor{} (\bnflit{*} \bnfnt{integer}) \\
\\
\bnfnt{new} ::= \\
\qquad \bnflit{new} \bnfnt{new-location} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}*
\\
\bnfnt{call-location} ::= \\
\qquad \bnfcmt Bytecode offset: the integer is the offset field of the annotation~\cite{JSR308-webpage-201310}. \\
\qquad (\bnflit{\#} \bnfnt{integer}) \\
\qquad \bnfcmt Source code index: the integer is the zero-based index of the method call \\
\qquad \bnfcmt within the field or method definition. \\
\qquad \bnfor{} (\bnflit{*} \bnfnt{integer}) \\
\\
\bnfnt{call} ::= \\
\qquad \bnflit{call} \bnfnt{call-location} \bnflit{:} \lineend \\
\qquad \bnfnt{typearg-definition}* \\
\\
\bnfnt{reference-location} ::= \\
\qquad \bnfcmt Bytecode offset: the integer is the offset field of the annotation~\cite{JSR308-webpage-201310}. \\
\qquad (\bnflit{\#} \bnfnt{integer}) \\
\qquad \bnfcmt Source code index: the integer is the zero-based index of the member \\
\qquad \bnfcmt reference~\cite{JSR308-webpage-201310}. \\
\qquad \bnfor{} (\bnflit{*} \bnfnt{integer}) \\
\\
\bnfnt{reference} ::= \\
\qquad \bnflit{reference} \bnfnt{reference-location} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}* \\
\qquad \bnfnt{typearg-definition}* \\
\\
\bnfnt{lambda-location} ::= \\
\qquad \bnfcmt Bytecode offset: the integer is the offset field of the annotation~\cite{JSR308-webpage-201310}. \\
\qquad (\bnflit{\#} \bnfnt{integer}) \\
\qquad \bnfcmt Source code index: the integer is the zero-based index of the lambda \\
\qquad \bnfcmt expression~\cite{JSR308-webpage-201310}. \\
\qquad \bnfor{} (\bnflit{*} \bnfnt{integer}) \\
\\
\bnfnt{lambda} ::= \\
\qquad \bnflit{lambda} \bnfnt{lambda-location} \bnflit{:} \lineend \\
%\qquad \bnfnt{return-type}? \\
\qquad \bnfnt{parameter-definition}* \\
\qquad \bnfnt{variable-definition}* \\
\qquad \bnfnt{expression-annotations}*
\\
\qquad \= \kill
\bnfnt{typearg-definition} ::= \\
\qquad \bnfcmt The integer is the zero-based type argument index. \\
\qquad \bnflit{typearg} \bnfnt{integer} \bnflit{:} \bnfnt{type-annotation}* \lineend \\
\qquad \bnfnt{compound-type}*
\end{tabbing}
\subsubsection{AST paths\label{ast-paths}}
A path through the AST (abstract
syntax tree) specifies an arbitrary expression in source code to modify.
AST paths can be used in the \code{.jaif} file to specify a location to
insert either a bare annotation (\bnflit{insert-annotation}) or a cast
(\bnflit{insert-typecast}).
For a cast insertion, the \code{.jaif} file specifies the type to cast to.
The annotations on the \bnflit{insert-typecast} line will be inserted on
the outermost type of the type to cast to. If the type to cast to is a compound
type then annotations on parts of the compound type are specified with the
\bnfnt{compound-type} rule. If there are no annotations on
the \bnflit{insert-typecast} line then a cast with no annotations will be
inserted or, if compound type annotations are specified, a cast with annotations
only on the compound types will be inserted.
Note that the type specified on the \bnflit{insert-typecast} line cannot contain
any qualified type names. For example, use \code{Entry<String, Object>} instead
of \code{Map.Entry<java.lang.String, java.lang.Object>}.
\begin{tabbing}
\bnfnt{source-insert-typecast} ::= \\
\qquad \bnfcmt \bnfnt{ast-path} is described below. \\
\qquad \bnfcmt \bnfnt{type} is the un-annotated type to cast to. \\
\qquad \bnflit{insert-typecast} \bnfnt{ast-path}\bnflit{:} \bnfnt{type-annotation}* \bnfnt{type} \lineend \\
\qquad \bnfnt{compound-type}*
\end{tabbing}
An AST path represents a traversal through the AST. AST paths can only be
used in \bnfnt{field-definition}s and \bnfnt{method-definition}s.
An AST path starts with the first element under the definition. For
methods this is \code{Block} and for fields this is \code{Variable}.
An AST path is composed of one or more AST entries, separated by commas. Each
AST entry is composed of a tree kind, a child selector, and an optional
argument. An example AST entry is:
\begin{verbatim}
Block.statement 1
\end{verbatim}
The tree kind is \code{Block}, the child selector is \code{statement} and the
argument is \code{1}.
The available tree kinds correspond to the Java AST tree nodes (from the package
\code{com.sun.source.tree}), but with ``Tree'' removed from the name. For
example, the class \code{com.sun.source.tree.BlockTree} is represented as
\code{Block}. The child selectors correspond to the method names of the given
Java AST tree node, with ``get'' removed from the beginning of the method name
and the first letter lowercased. In cases where the child selector method
returns a list, the method name is made singular and the AST entry also contains
an argument to select the index of the list to take. For example, the method
\code{com\-.sun\-.source\-.tree\-.Block\-Tree\-.get\-Statements()} is represented as
\code{Block.statement} and requires an argument to select the statement to take.
The following is an example of an entire AST path:
\begin{verbatim}
Block.statement 1, Switch.case 1, Case.statement 0, ExpressionStatement.expression,
MethodInvocation.argument 0
\end{verbatim}
Since the above example starts with a \code{Block} it belongs in a
\bnfnt{method-definition}. This AST path would select an expression that is in
statement 1 of the method, case 1 of the switch statement, statement 0 of the
case, and argument 0 of a method call (\code{ExpressionStatement} is just a
wrapper around an expression that can also be a statement).
The following is an example of an annotation file with AST paths used to specify
where to insert casts.
\begin{verbatim}
package p:
annotation @A:
class ASTPathExample:
field a:
insert-typecast Variable.initializer, Binary.rightOperand: @A Integer
method m()V:
insert-typecast Block.statement 0, Variable.initializer: @A Integer
insert-typecast Block.statement 1, Switch.case 1, Case.statement 0,
ExpressionStatement.expression, MethodInvocation.argument 0: @A Integer
\end{verbatim}
And the matching source code:
\begin{verbatim}
package p;
public class ASTPathExample {
private int a = 12 + 13;
public void m() {
int x = 1;
switch (x + 2) {
case 1:
System.out.println(1);
break;
case 2:
System.out.println(2 + x);
break;
default:
System.out.println(-1);
}
}
}
\end{verbatim}
The following is the output, with the casts inserted.
\begin{verbatim}
package p;
import p.A;
public class ASTPathExample {
private int a = 12 + ((@A Integer) (13));
public void m() {
int x = ((@A Integer) (1));
switch (x + 2) {
case 1:
System.out.println(1);
break;
case 2:
System.out.println(((@A Integer) (2 + x)));
break;
default:
System.out.println(-1);
}
}
}
\end{verbatim}
Using \code{insert-annotation} instead of \code{insert-typecast} yields
almost the same result --- it also inserts a cast. The sole difference
is the inability to specify the type in the cast expression. If you use
\code{insert-annotation}, then the annotation inserter infers the type,
which is \code{int} in this case.
Note that a cast can be inserted on any expression, not
just the deepest expression in the AST. For example, a cast could be inserted on
the expression \code{i + j}, the identifier \code{i}, and/or the identifier \code{j}.
To help create correct AST paths it may be useful to view the AST of a class.
The Checker Framework has a processor to do this. The following command will
output indented AST nodes for the entire input program.
\begin{verbatim}
javac -processor org.checkerframework.common.util.debug.TreeDebug ASTPathExample.java
\end{verbatim}
The following is the grammar for AST paths.
\begin{tabbing}
\qquad \= \kill
\bnfnt{ast-path} ::= \\
\qquad \bnfnt{ast-entry} [ \bnflit{,} \bnfnt{ast-entry} ]+ \\
\\
\bnfnt{ast-entry} ::= \\
\qquad \bnfnt{annotated-type} \\
\qquad \bnfor{} \bnfnt{annotation} \\
\qquad \bnfor{} \bnfnt{array-access} \\
\qquad \bnfor{} \bnfnt{array-type} \\
\qquad \bnfor{} \bnfnt{assert} \\
\qquad \bnfor{} \bnfnt{assignment} \\
\qquad \bnfor{} \bnfnt{binary} \\
\qquad \bnfor{} \bnfnt{block} \\
\qquad \bnfor{} \bnfnt{case} \\
\qquad \bnfor{} \bnfnt{catch} \\
\qquad \bnfor{} \bnfnt{compound-assignment} \\
\qquad \bnfor{} \bnfnt{conditional-expression} \\
\qquad \bnfor{} \bnfnt{do-while-loop} \\
\qquad \bnfor{} \bnfnt{enhanced-for-loop} \\
\qquad \bnfor{} \bnfnt{expression-statement} \\
\qquad \bnfor{} \bnfnt{for-loop} \\
\qquad \bnfor{} \bnfnt{if} \\
\qquad \bnfor{} \bnfnt{instance-of} \\
\qquad \bnfor{} \bnfnt{intersection-type} \\
\qquad \bnfor{} \bnfnt{labeled-statement} \\
\qquad \bnfor{} \bnfnt{lambda-expression} \\
\qquad \bnfor{} \bnfnt{member-reference} \\
\qquad \bnfor{} \bnfnt{member-select} \\
\qquad \bnfor{} \bnfnt{method-invocation} \\
\qquad \bnfor{} \bnfnt{new-array} \\
\qquad \bnfor{} \bnfnt{new-class} \\
\qquad \bnfor{} \bnfnt{parameterized-type} \\
\qquad \bnfor{} \bnfnt{parenthesized} \\
\qquad \bnfor{} \bnfnt{return} \\
\qquad \bnfor{} \bnfnt{switch} \\
\qquad \bnfor{} \bnfnt{synchronized} \\
\qquad \bnfor{} \bnfnt{throw} \\
\qquad \bnfor{} \bnfnt{try} \\
\qquad \bnfor{} \bnfnt{type-cast} \\
\qquad \bnfor{} \bnfnt{type-parameter} \\
\qquad \bnfor{} \bnfnt{unary} \\
\qquad \bnfor{} \bnfnt{union-type} \\
\qquad \bnfor{} \bnfnt{variable-type} \\
\qquad \bnfor{} \bnfnt{while-loop} \\
\qquad \bnfor{} \bnfnt{wildcard-tree} \\
\\
\bnfnt{annotated-type} :: = \\
\qquad \bnflit{AnnotatedType} \bnflit{.} ( ( \bnflit{annotation} \bnfnt{integer} ) \bnfor{} \bnflit{underlyingType} ) \\
\\
\bnfnt{annotation} ::= \\
\qquad \bnflit{Annotation} \bnflit{.} ( \bnflit{type} \bnfor{} \bnflit{argument} \bnfnt{integer} ) \\
\\
\bnfnt{array-access} ::= \\
\qquad \bnflit{ArrayAccess} \bnflit{.} ( \bnflit{expression} \bnfor{} \bnflit{index} ) \\
\\
\bnfnt{array-type} ::= \\
\qquad \bnflit{ArrayType} \bnflit{.} \bnflit{type} \\
\\
\bnfnt{assert} ::= \\
\qquad \bnflit{Assert} \bnflit{.} ( \bnflit{condition} \bnfor{} \bnflit{detail} ) \\
\\
\bnfnt{assignment} ::= \\
\qquad \bnflit{Assignment} \bnflit{.} ( \bnflit{variable} \bnfor{} \bnflit{expression} ) \\
\\
\bnfnt{binary} ::= \\
\qquad \bnflit{Binary} \bnflit{.} ( \bnflit{leftOperand} \bnfor{} \bnflit{rightOperand} ) \\
\\
\bnfnt{block} ::= \\
\qquad \bnflit{Block} \bnflit{.} \bnflit{statement} \bnfnt{integer} \\
\\
\bnfnt{case} ::= \\
\qquad \bnflit{Case} \bnflit{.} ( \bnflit{expression} \bnfor{} ( \bnflit{statement} \bnfnt{integer} ) ) \\
\\
\bnfnt{catch} ::= \\
\qquad \bnflit{Catch} \bnflit{.} ( \bnflit{parameter} \bnfor{} \bnflit{block} ) \\
\\
\bnfnt{compound-assignment} ::= \\
\qquad \bnflit{CompoundAssignment} \bnflit{.} ( \bnflit{variable} \bnfor{} \bnflit{expression} ) \\
\\
\bnfnt{conditional-expression} ::= \\
\qquad \bnflit{ConditionalExpression} \bnflit{.} ( \bnflit{condition} \bnfor{} \bnflit{trueExpression} \bnfor{} \bnflit{falseExpression} ) \\
\\
\bnfnt{do-while-loop} ::= \\
\qquad \bnflit{DoWhileLoop} \bnflit{.} ( \bnflit{condition} \bnfor{} \bnflit{statement} ) \\
\\
\bnfnt{enhanced-for-loop} ::= \\
\qquad \bnflit{EnhancedForLoop} \bnflit{.} ( \bnflit{variable} \bnfor{} \bnflit{expression} \bnfor{} \bnflit{statement} ) \\
\\
\bnfnt{expression-statement} ::= \\
\qquad \bnflit{ExpressionStatement} \bnflit{.} \bnflit{expression} \\
\\
\bnfnt{for-loop} ::= \\
\qquad \bnflit{ForLoop} \bnflit{.} ( ( \bnflit{initializer} \bnfnt{integer} ) \bnfor{} \bnflit{condition} \bnfor{} ( \bnflit{update} \bnfnt{integer} ) \bnfor{} \bnflit{statement} ) \\
\\
\bnfnt{if} ::= \\
\qquad \bnflit{If} \bnflit{.} ( \bnflit{condition} \bnfor{} \bnflit{thenStatement} \bnfor{} \bnflit{elseStatement} ) \\
\\
\bnfnt{instance-of} ::= \\
\qquad \bnflit{InstanceOf} \bnflit{.} ( \bnflit{expression} \bnfor{} \bnflit{type} ) \\
\\
\bnfnt{intersection-type} ::= \\
\qquad \bnflit{IntersectionType} \bnflit{.} \bnflit{bound} \bnfnt{integer} \\
\\
\bnfnt{labeled-statement} ::= \\
\qquad \bnflit{LabeledStatement} \bnflit{.} \bnflit{statement} \\
\\
\bnfnt{lambda-expression} ::= \\
\qquad \bnflit{LambdaExpression} \bnflit{.} ( ( \bnflit{parameter} \bnfnt{integer} ) \bnfor{} \bnflit{body} ) \\
\\
\bnfnt{member-reference} ::= \\
\qquad \bnflit{MemberReference} \bnflit{.} ( \bnflit{qualifierExpression} \bnfor{} ( \bnflit{typeArgument} \bnfnt{integer} ) ) \\
\\
\bnfnt{member-select} ::= \\
\qquad \bnflit{MemberSelect} \bnflit{.} \bnflit{expression} \\
\\
\bnfnt{method-invocation} ::= \\
\qquad \bnflit{MethodInvocation} \bnflit{.} ( ( \bnflit{typeArgument} \bnfnt{integer} ) \bnfor{} \bnflit{methodSelect} \\
\qquad \bnfor{} ( \bnflit{argument} \bnfnt{integer} ) ) \\
\\
\bnfnt{new-array} ::= \\
\qquad \bnflit{NewArray} \bnflit{.} ( \bnflit{type} \bnfor{} ( \bnflit{dimension} \bnfor{} \bnflit{initializer} ) \bnfnt{integer} ) \\
\\
\bnfnt{new-class} ::= \\
\qquad \bnflit{NewClass} \bnflit{.} ( \bnflit{enclosingExpression} \bnfor{} ( \bnflit{typeArgument} \bnfnt{integer} ) \bnfor{} \bnflit{identifier} \\
\qquad \bnfor{} ( \bnflit{argument} \bnfnt{integer} ) \bnfor{} \bnflit{classBody} ) \\
\\
\bnfnt{parameterized-type} ::= \\
\qquad \bnflit{ParameterizedType} \bnflit{.} ( \bnflit{type} \bnfor{} ( \bnflit{typeArgument} \bnfnt{integer} ) ) \\
\\
\bnfnt{parenthesized} ::= \\
\qquad \bnflit{Parenthesized} \bnflit{.} \bnflit{expression} \\
\\
\bnfnt{return} ::= \\
\qquad \bnflit{Return} \bnflit{.} \bnflit{expression} \\
\\
\bnfnt{switch} ::= \\
\qquad \bnflit{Switch} \bnflit{.} ( \bnflit{expression} \bnfor{} ( \bnflit{case} \bnfnt{integer} ) ) \\
\\
\bnfnt{synchronized} ::= \\
\qquad \bnflit{Synchronized} \bnflit{.} ( \bnflit{expression} \bnfor{} \bnflit{block} ) \\
\\
\bnfnt{throw} ::= \\
\qquad \bnflit{Throw} \bnflit{.} \bnflit{expression} \\