langtools/test/tools/javac/diags/README.examples.txt
author mcimadamore
Mon, 07 Mar 2011 14:31:50 +0000
changeset 8635 383a416a2bdf
parent 6149 48de3564aa13
child 27852 2e6ad0e4fe20
permissions -rw-r--r--
7020044: Project Coin: diamond erroneous allowed on some anonymous inner classes Summary: Disallow diamond on anonymous innner class creation expression (as per JSR 334's EDR) Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     1
Diagnostics Examples.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     2
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     3
The "examples/ directory contains a collection of examples of Java code, each of
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     4
which is designed to illustrate one or more diagnostics that can be generated by
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     5
the JDK Java compiler, javac. These examples are represented by files or
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     6
directories of files, each of which is designed to illustrate a specific
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     7
diagnostic. Sometimes it is unavoidable that creating one issue will lead to
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     8
downstream issues: this is especially true for lex errors, where the error
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     9
recovery is fragile at best. Each example declares the diagnostics that it is
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    10
expected to generate -- this allows the examples to be verified and facilitates
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    11
searching for examples for specific messages.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    12
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    13
Normally, tests for javac errors avoid checking the actual error messages that
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    14
get generated. Older tests simply verify that one or more warnings or errors
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    15
are generated; more recent tests verify that specific messages are generated,
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    16
but these tests typically avoid checking the localized text by using the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    17
-XDrawDiagnostics mechanism. In addition, the focus of such tests is often on
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    18
completeness instead of simplicity.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    19
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    20
By contrast, the intent of these examples is to provide simple and easy to
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    21
understand examples of the situations in which a diagnostic can arise, and the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    22
messages that may be displayed. This will aid in reviewing the output generated
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    23
by javac and in localizing the resource bundle to other locales. In addition,
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    24
the examples include simple meta-information so that the collection as a whole
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    25
can be audited for coverage, thus encouraging new examples to be added when new
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    26
diagnostics are added to javac.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    27
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    28
There are two utilities for processing these examples.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    29
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    30
The first utility is "CheckExamples" which checks various conditions for the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    31
examples:
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    32
-- each example must generate exactly the set of keys that it is declared to
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    33
   generate
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    34
-- together, the examples must generate all the resource keys coming from javac
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    35
   (except for resource keys that are registered in a "not yet" list)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    36
-- the "not yet" list should only contain those resource keys from javac that
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    37
   are not covered by any example
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    38
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    39
CheckExamples can be run standalone, and as a jtreg test, and will fail if any
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    40
anomalous conditions are found.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    41
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    42
The second utility is "RunExamples" which runs selected examples or all of them.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    43
The examples can be run with -XDrawDiagnostics or without.   Examples can be
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    44
selected by name or by resource key.   Most examples are simple to run directly
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    45
anyway, but some use annotation processors or sourcepath or other options, and
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    46
the framework handles all these requirements.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    47
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    48
RunExamples can be run standalone and as a jtreg test, in which case it
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    49
generates a simple plain text report. In addition, the langtools/make/build.xml
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    50
file has a target "diags-examples" that uses RunExamples to create an HTML
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    51
report containing the output from all the examples.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    52
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    53
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    54
Adding examples.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    55
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    56
When new diagnostics are added to javac, CheckExamples will probably initially
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    57
fail because the diagnostic will not have a corresponding example, nor will the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    58
resource key be registered in the "not yet" list. Avoid the temptation to
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    59
simply add the resource key to the "not yet" list, except as a last resort.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    60
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    61
Examples should be as simple as possible to illustrate a diagnostic.  An example
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    62
that is a single file is to be preferred over multiple files. An example that
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    63
generates just the one intended diagnostic is to be preferred over one that
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    64
generates multiple diagnostics. Examples should be a simple illustration of the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    65
conditions that give rise to the diagnostic and should be easy to understand by
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    66
the reviewer and, potentially, by the localization folk, who have to understand
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    67
the context in which these new messages can appear.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    68
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    69
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    70
Specification for writing examples.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    71
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    72
An example may a single file or a directory of files directly in the "examples"
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    73
directory. One file within an example must contain meta-information such as the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    74
keys that it generates, any javac options that may be required, and additional
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    75
info about how to run the test, if needed.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    76
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    77
If an example is represented by a directory of files, by default all files
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    78
within that directory will be compiled together, putting all the files on the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    79
command line. However, some subdirectories are special:
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    80
-- processors/
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    81
    Files within this directory will be treated as annotation processors and
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    82
    compiled ahead of time. Currently, annotation processors are made available
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    83
    to javac using the -classpath option (not -processorpath). This is to avoid
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    84
    explicit use of annotation processing options on the javac command line.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    85
    Any annotation processors found will be registered for access by the JDK
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    86
    service loaded. Currently, annotation processors are assumed to be in the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    87
    unnamed package.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    88
-- sourcepath/
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    89
    If present, this directory will be put passed to javac using the -sourcepath
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    90
    option.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    91
-- classpath/
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    92
    This name is reserved for future use. It is expected that this directory
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    93
    will be used to provide classes to be compiled and passes to javac via the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    94
    -classpath option.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    95
-- support/
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    96
    This name is reserved for future use. It is expected that this directory
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    97
    will be used to provide classes that setup non-standard conditions for a
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    98
    test, such as very large source files, or illegal class files.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    99
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   100
Meta-information is represented by simple comment lines in exactly one of the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   101
source files of the example.  The file must not be in one of the special
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   102
subdirectories (processors/, sourcepath/, classpath/ or support/). Three
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   103
different types of information may be given:
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   104
// key: <resource-key>
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   105
    One or more such lines must be provided, declaring the full resource keys
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   106
    that will be used by javac when this example is run.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   107
// options: <javac-options>
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   108
    This line may be given at most once, providing one or more options to be
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   109
    passed to javac. It is not possible to use this to specify options that
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   110
    require filenames or directories.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   111
// run: <mode> <optional-args>
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   112
    This line may be given at most once, providing infomation about how to
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   113
    run the example. Three different kinds are supported:
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   114
    jsr199 -- The example will be run using the JSR 199 Compiler API.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   115
              This is the default if the tag is omitted. Messages generated
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   116
              by the "rich diagnostic formatter" can not be accessed in this
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   117
              way.  However, this mode does provide additional options for
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   118
              simulating errors in the filesystem. (See the options below.)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   119
    simple -- The example will be run using the simple com.sun.tools.javac.Main
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   120
              API. This mode is most like the normal command line invocation.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   121
    backdoor -- The example will be run using an internal "backdoor" API, that
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   122
              interposes access to the main compiler message bundle. This mode
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   123
              is required to detect and track messages that bypass the normal
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   124
              diagnostic mechanisms, such as output generated by the -verbose
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   125
              option.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   126
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   127
The "jsr199" run mode accepts the following options:
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   128
    -cantRead:pattern
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   129
    -cantWrite:pattern
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   130
In both cases, the pattern is a standard Java regular expression (See the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   131
javadoc for java.util.regex.Pattern for a complete specification.) Attempts to
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   132
read or write from files matching the corresponding pattern will cause an
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   133
IOException to occur within javac.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   134