File: i18n-format-checker.tex

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (434 lines) | stat: -rw-r--r-- 18,057 bytes parent folder | download | duplicates (3)
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
\htmlhr
\chapterAndLabel{Internationalization Format String Checker (I18n Format String Checker)}{i18n-formatter-checker}

The Internationalization Format String Checker, or I18n Format String Checker,
prevents use of incorrect i18n format strings.

If the I18n Format String Checker issues no warnings or errors, then
\sunjavadoc{java.base/java/text/MessageFormat.html\#format(java.lang.String,java.lang.Object...)}{MessageFormat.format}
will raise no error at run time.
``I18n'' is short for
``internationalization'' because there are 18 characters between the ``i'' and
the ``n''.

Here are the examples of errors that the
I18n Format Checker
detects at compile time.

\begin{Verbatim}
  // Warning: the second argument is missing.
  MessageFormat.format("{0} {1}", 3.1415);
  // String argument cannot be formatted as Time type.
  MessageFormat.format("{0, time}", "my string");
  // Invalid format string: unknown format type: thyme.
  MessageFormat.format("{0, thyme}", new Date());
  // Invalid format string: missing the right brace.
  MessageFormat.format("{0", new Date());
  // Invalid format string: the argument index is not an integer.
  MessageFormat.format("{0.2, time}", new Date());
  // Invalid format string: "#.#.#" subformat is invalid.
  MessageFormat.format("{0, number, #.#.#}", 3.1415);
\end{Verbatim}

For instructions on how to run the Internationalization Format String
Checker, see Section~\ref{i18n-format-running}.

The Internationalization Checker or I18n Checker (\chapterpageref{i18n-checker})
has a different purpose.  It verifies that your code is properly
internationalized: any user-visible text should be obtained from a
localization resource and all keys exist in that resource.


\sectionAndLabel{Internationalization Format String Checker annotations}{i18n-format-annotation}


\begin{figure}
\includeimage{i18n-format-type-hierarchy}{3cm}
\caption{The
  Internationalization
  Format String Checker type qualifier hierarchy.
  The type qualifiers are applicable te \<CharSequence> and its subtypes.
  The figure does not show the subtyping rules among different
  \refqualclass{checker/i18nformatter/qual}{I18nFormat}\code{(...)}
  qualifiers; see
  Section~\ref{i18n-format-conversion-catgeories}.
  All \refqualclass{checker/i18nformatter/qual}{I18nFormatFor} annotations are unrelated by subtyping.
  The qualifiers in gray are used internally by
  the checker and should never be written by a programmer.
}
\label{i18n-format-type-hierarchy}
\end{figure}

The \sunjavadoc{java.base/java/text/MessageFormat.html}{MessageFormat} documentation
specifies the syntax of the i18n format string.

These are the qualifiers that make up the I18n Format String type system.
Figure~\ref{i18n-format-type-hierarchy} shows their subtyping relationships.

\begin{description}

\item[\refqualclass{checker/i18nformatter/qual}{I18nFormat}]
  represents a valid i18n format string. For example,
  \code{@I18nFormat(\{GENERAL, NUMBER, UNUSED, DATE\})} is a legal type for
  \code{"\{0\}\{1, number\} \{3, date\}"}, indicating that when the format
  string is used,
  the first argument should be of \code{GENERAL} conversion category,
  the second argument should be of \code{NUMBER} conversion category, and so on.
  Conversion categories such as \code{GENERAL} are described in
  Section~\ref{i18n-format-conversion-catgeories}.

\item[\refqualclass{checker/i18nformatter/qual}{I18nFormatFor}]
  indicates that the qualified type is a valid i18n format string for use
  with some array of values.  For example,
  \code{@I18nFormatFor("\#2")} indicates that the string can be used to
  format the contents of the second parameter array.
  The argument is a Java expression whose syntax
  is explained in Section~\ref{java-expressions-as-arguments}.
  An example of its use is:

\begin{Verbatim}
  static void method(@I18nFormatFor("#2") String format, Object... args) {
    // the body may use the parameters like this:
    MessageFormat.format(format, args);
  }

  method("{0, number} {1}", 3.1415, "A string");  // OK
  // error: The string "hello" cannot be formatted as a Number.
  method("{0, number} {1}", "hello", "goodbye");
\end{Verbatim}

\item[\refqualclass{checker/i18nformatter/qual}{I18nInvalidFormat}]
  represents an invalid i18n format string. Programmers are not allowed to
  write this annotation. It is only used internally by the type checker.

\item[\refqualclass{checker/i18nformatter/qual}{I18nUnknownFormat}]
  represents any string.  The string might or might not be a valid i18n
  format string.  Programmers are not allowed to write this annotation.

\item[\refqualclass{checker/i18nformatter/qual}{I18nFormatBottom}]
  indicates that the value is definitely \<null>. Programmers are not allowed
  to write this annotation.
\end{description}

\sectionAndLabel{Conversion categories}{i18n-format-conversion-catgeories}

In a message string, the optional second element within the curly braces is
called a \emph{format type} and must be one of \<number>, \<date>,
\<time>, and \<choice>. These four format types correspoond to different
conversion categories. \<date> and \<time> correspond to \emph{DATE} in the
conversion categories figure. \<choice> corresponds to \emph{NUMBER}.
The format type restricts what arguments are legal.
For example, a date argument is not compatible with
the \<number> format type, i.e., \code{MessageFormat.format("\{0, number\}",
new Date())} will throw an exception.

The I18n Checker represents the possible arguments via \emph{conversion
  categories}.  A conversion category defines a set of restrictions or a
subtyping rule.

Figure~\ref{i18n-format-category} summarizes the subset
relationship among all conversion categories.

\begin{figure}
    \includeimage{i18n-format-category}{5cm}
    \caption{The subset relationship among
        i18n
        conversion categories.}
    \label{i18n-format-category}
\end{figure}


\sectionAndLabel{Subtyping rules for \<@I18nFormat>}{i18n-formatter-format-subtyping}

Here are the subtyping rules among different
\code{@I18nFormat}
qualifiers.
It is legal to:

\begin{itemize}
\item use a format string with a weaker (less restrictive) conversion category than required.
\item use a format string with fewer format specifiers than required.
  Although this is legal a warning is issued because most occurrences of
  this are due to programmer error.
\end{itemize}

The following example shows the subtyping rules in action:

\begin{Verbatim}
  @I18nFormat({NUMBER, DATE}) String f;

  f = "{0, number, #.#} {1, date}"; // OK
  f = "{0, number} {1}";            // OK, GENERAL is weaker (less restrictive) than DATE
  f = "{0} {1, date}";              // OK, GENERAL is weaker (less restrictive) than NUMBER
  f = "{0, number}";                // warning: last argument is ignored
  f = "{0}";                        // warning: last argument is ignored
  f = "{0, number} {1, number}";    // error: NUMBER is stronger (more restrictive) than DATE
  f = "{0} {1} {2}";                // error: too many arguments
\end{Verbatim}

The conversion categories are:

\begin{description}

\item[\refenum{checker/i18nformatter/qual}{I18nConversionCategory}{UNUSED}{}]
indicates an unused argument. For example, in
\code{MessageFormat.format("\{0, number\} \{2, number\}", 3.14, "Hello", 2.718)}
, the second argument \code{Hello} is unused. Thus, the conversion
categories for the format, \code{{0, number} {2, number}}, is
\code{(NUMBER, UNUSED, NUMBER)}.

\item[\refenum{checker/i18nformatter/qual}{I18nConversionCategory}{GENERAL}{}]
means that any value can be supplied as an argument.

\item[\refenum{checker/i18nformatter/qual}{I18nConversionCategory}{DATE}{}]
is applicable for date, time, and number types. An argument needs to be
of \sunjavadoc{java.sql/java/sql/Date.html}{Date},
\sunjavadoc{java.sql/java/sql/Time.html}{Time}, or
\sunjavadoc{java.base/java/lang/Number.html}{Number} type or a subclass of them,
including \sunjavadoc{java.sql/java/sql/Timestamp.html}{Timestamp} and the classes
listed immediately below.

\item[\refenum{checker/i18nformatter/qual}{I18nConversionCategory}{NUMBER}{}]
means that the argument needs to be of \code{Number}
type or a subclass:
\sunjavadoc{java.base/java/lang/Number.html}{Number},
\sunjavadoc{java.base/java/util/concurrent/atomic/AtomicInteger.html}{AtomicInteger},
\sunjavadoc{java.base/java/util/concurrent/atomic/AtomicLong.html}{AtomicLong},
\sunjavadoc{java.base/java/math/BigDecimal.html}{BigDecimal},
\sunjavadoc{java.base/java/math/BigInteger.html}{BigInteger},
\sunjavadoc{java.base/java/lang/Byte.html}{Byte},
\sunjavadoc{java.base/java/lang/Double.html}{Double},
\sunjavadoc{java.base/java/lang/Float.html}{Float},
\sunjavadoc{java.base/java/lang/Integer.html}{Integer},
\sunjavadoc{java.base/java/lang/Long.html}{Long},
\sunjavadoc{java.base/java/lang/Short.html}{Short}.

\end{description}

\sectionAndLabel{What the Internationalization Format String Checker checks}{i18n-format-checks}

The Internationalization Format String Checker checks calls to the i18n
formatting method \sunjavadoc{java.base/java/text/MessageFormat.html\#format(java.lang.String,java.lang.Object...)}{MessageFormat.format}
and guarantees the following:

\begin{enumerate}
  \item{The checker issues a warning for the following cases:}
    \begin{enumerate}
      \item There are missing arguments from what is required by the format string.

      \code{MessageFormat.format("\{0, number\} \{1, number\}", 3.14); // Output: 3.14 \{1\}}

      \item More arguments are passed than what is required by the format string.

      \code{MessageFormat.format("\{0, number\}", 1, new Date());}

      \code{MessageFormat.format("\{0, number\} \{0, number\}", 3.14, 3.14);}

      This does not cause an error at run time, but it often indicates a
      programmer mistake.  If it is intentional, then you should suppress
      the warning (see Chapter~\ref{suppressing-warnings}).

      \item Some argument is an array of objects.

      \code{MessageFormat.format("\{0, number\} \{1\}", array);}

      The checker cannot verify whether the format string is valid, so
      the checker conservatively issues a warning.  This is a limitation of
      the Internationalization Format String Checker.

    \end{enumerate}
  \item The checker issues an error for the following cases:
    \begin{enumerate}
      \item The format string is invalid.

        \begin{itemize}
          \item Unmatched braces.

          \code{MessageFormat.format("\{0, time", new Date());}

          \item The argument index is not an integer or is negative.

          \code{MessageFormat.format("\{0.2, time\}", new Date());}

          \code{MessageFormat.format("\{-1, time\}", new Date());}

          \item Unknown format type.

          \code{MessageFormat.format("\{0, foo\}", 3.14);}

          \item Missing a format style required for \<choice> format.

          \code{MessageFormat.format("\{0, choice\}", 3.14);}

          \item Wrong format style.

          \code{MessageFormat.format("\{0, time, number\}", 3.14);}

          \item Invalid subformats.

          \code{MessageFormat.format("\{0, number, \#.\#.\#\}", 3.14)}
        \end{itemize}

      \item Some argument's type doesn't satisfy its conversion category.

      \code{MessageFormat.format("\{0, number\}", new Date());}
    \end{enumerate}
\end{enumerate}

The Checker also detects illegal assignments: assigning a non-format-string
or an incompatible format string to a variable declared as containing a
specific type of format string. For example,

\begin{Verbatim}
  @I18nFormat({GENERAL, NUMBER}) String format;
  // OK.
  format = "{0} {1, number}";
  // OK, GENERAL is weaker (less restrictive) than NUMBER.
  format = "{0} {1}";
  // OK, it is legal to have fewer arguments than required (less restrictive).
  // But the warning will be issued instead.
  format = "{0}";

  // Error, the format string is stronger (more restrictive) than the specifiers.
  format = "{0} {1} {2}";
  // Error, the format string is more restrictive. NUMBER is a subtype of GENERAL.
  format = "{0, number} {1, number}";
\end{Verbatim}

\sectionAndLabel{Resource files}{i18n-format-resource-files}

A programmer rarely writes an i18n format string literally. (The examples
in this chapter show that for simplicity.) Rather, the i18n format strings are
read from a resource file.  The program chooses a resource file at run time
depending on the locale (for example, different resource files for English
and Spanish users).

\noindent For example, suppose that the \<resource1.properties> file contains

\begin{Verbatim}
  key1 = The number is {0, number}.
\end{Verbatim}

\noindent Then code such as the following:

\begin{Verbatim}
  String formatPattern = ResourceBundle.getBundle("resource1").getString("key1");
  System.out.println(MessageFormat.format(formatPattern, 2.2361));
\end{Verbatim}

\noindent will output ``The number is 2.2361.''  A different resource file would contain
\code{key1 = El n\'{u}mero es \{0, number\}.}

When you run the I18n Format String Checker, you need to indicate which resource file it
should check. If you change the resource file or use a different resource
file, you should re-run the checker
to ensure that you did not make an error. The I18n Format String Checker supports two types of
resource files: ResourceBundles and property files. The example above shows use of
resource bundles.
For more about checking property files, see \chapterpageref{propkey-checker}.


\sectionAndLabel{Running the Internationalization Format Checker}{i18n-format-running}

The checker can be invoked by running one of the following commands (with
the whole command on one line).

\begin{itemize}
  \item Using ResourceBundles:

    \begin{smaller}
    \code{javac -processor
      org.checkerframework.checker.i18nformatter.I18nFormatterChecker
    -Abundlenames=MyResource MyFile.java}
    \end{smaller}

  \item Using property files:

    \begin{smaller}
    \code{javac -processor
      org.checkerframework.checker.i18nformatter.I18nFormatterChecker
    -Apropfiles=MyResource.properties MyFile.java}
    \end{smaller}

  \item Not using a property file.  Use this if the programmer hard-coded the
  format patterns without loading them from a property file.

    \begin{smaller}
    \code{javac -processor
      org.checkerframework.checker.i18nformatter.I18nFormatterChecker MyFile.java}
    \end{smaller}
\end{itemize}


\sectionAndLabel{Testing whether a string has an i18n format type}{i18n-format-testing}

In the case that the checker cannot infer the i18n format type of a string,
you can use the \refmethod{checker/i18nformatter}{I18nFormatUtil}{hasFormat}{-java.lang.String-org.checkerframework.checker.i18nformatter.qual.I18nConversionCategory...-}
method to define the type of the string in the scope of a conditional statement.

\begin{description}

\item[\refmethod{checker/i18nformatter}{I18nFormatUtil}{hasFormat}{-java.lang.String-org.checkerframework.checker.i18nformatter.qual.I18nConversionCategory...-}]
  returns \<true> if the given string has the given i18n format type.

\end{description}

\noindent For an example, see Section~\ref{i18n-format-examples}.


\sectionAndLabel{Examples of using the Internationalization Format Checker}{i18n-format-examples}

\begin{itemize}
  \item Using \sunjavadoc{java.base/java/text/MessageFormat.html\#format(java.lang.String,java.lang.Object...)}{MessageFormat.format}.
\begin{Verbatim}
      // suppose the bundle "MyResource" contains:  key1={0, number} {1, date}
      String value = ResourceBundle.getBundle("MyResource").getString("key1");
      MessageFormat.format(value, 3.14, new Date());  // OK
      // error: incompatible types in argument; found String, expected number
      MessageFormat.format(value, "Text", new Date());
\end{Verbatim}
  \item Using the
    \refmethod{checker/i18nformatter}{I18nFormatUtil}{hasFormat}{-java.lang.String-org.checkerframework.checker.i18nformatter.qual.I18nConversionCategory...-}
    method to check whether a format
    string has particular conversion categories.
\begin{Verbatim}
      void test1(String format) {
        if (I18nFormatUtil.hasFormat(format, I18nConversionCategory.GENERAL,
                                             I18nConversionCategory.NUMBER)) {
          MessageFormat.format(format, "Hello", 3.14);  // OK
          // error: incompatible types in argument; found String, expected number
          MessageFormat.format(format, "Hello", "Bye");
          // error: missing arguments; expected 2 but 1 given
          MessageFormat.format(format, "Bye");
          // error: too many arguments; expected 2 but 3 given
          MessageFormat.format(format, "A String", 3.14, 3.14);
        }
      }
\end{Verbatim}
  \item Using \refqualclass{checker/i18nformatter/qual}{I18nFormatFor}
    to ensure that an argument is a particular type of format string.
\begin{Verbatim}
      static void method(@I18nFormatFor("#2") String f, Object... args) {...}

      // OK, MessageFormat.format(...) would return "3.14 Hello greater than one"
      method("{0, number} {1} {2, choice,0#zero|1#one|1<greater than one}",
             3.14, "Hello", 100);

      // error: incompatible types in argument; found String, expected number
      method("{0, number} {1}", "Bye", "Bye");
\end{Verbatim}
  \item Annotating a string with
    \refqualclass{checker/i18nformatter/qual}{I18nFormat}.
\begin{Verbatim}
      @I18nFormat({I18nConversionCategory.DATE}) String;
      s1 = "{0}";
      s1 = "{0, number}";        // error: incompatible types in assignment
\end{Verbatim}
\end{itemize}

%%  LocalWords:  I18n i18n java MessageFormat I18nFormat I18nFormatFor
%%  LocalWords:  arg I18nInvalid I18nUnknownFormat I18nFormatBottom
%%  LocalWords:  Timestamp AtomicInteger AtomicLong BigDecimal BigInteger
%%  LocalWords:  number' foo subformats resource1 key1 mero Abundlenames
%%  LocalWords:  ResourceBundles MyResource MyFile Apropfiles hasFormat
%%  LocalWords:  I18nFormatUtil