langtools/test/tools/javac/diags/Example.java
author jlahoda
Wed, 09 Oct 2013 13:09:31 +0200
changeset 21006 534673718919
parent 17798 abe47e71498b
child 27388 d694da45bd7a
permissions -rw-r--r--
8025087: Annotation processing api returns default modifier for interface static method Summary: ClassReader must not set Flags.DEFAULT for interface static methods Reviewed-by: vromero, 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
/*
17798
abe47e71498b 7060779: test/tools/javac/diags/Example.java leaves directories in tempdir
vromero
parents: 14541
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     4
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     8
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    13
 * accompanied this code).
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    14
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    18
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    21
 * questions.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    22
 */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    23
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    24
import java.io.*;
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
    25
import java.net.URL;
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
    26
import java.net.URLClassLoader;
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    27
import java.util.*;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    28
import java.util.regex.*;
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
    29
import javax.annotation.processing.Processor;
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    30
import javax.tools.Diagnostic;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    31
import javax.tools.DiagnosticCollector;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    32
import javax.tools.JavaCompiler;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    33
import javax.tools.JavaCompiler.CompilationTask;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    34
import javax.tools.JavaFileObject;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    35
import javax.tools.StandardJavaFileManager;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    36
import javax.tools.ToolProvider;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    37
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    38
// The following two classes are both used, but cannot be imported directly
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    39
// import com.sun.tools.javac.Main
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    40
// import com.sun.tools.javac.main.Main
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    41
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
    42
import com.sun.tools.javac.api.ClientCodeWrapper;
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
    43
import com.sun.tools.javac.file.JavacFileManager;
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10454
diff changeset
    44
import com.sun.tools.javac.main.Main;
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    45
import com.sun.tools.javac.util.Context;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    46
import com.sun.tools.javac.util.JavacMessages;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    47
import com.sun.tools.javac.util.JCDiagnostic;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    48
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    49
/**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    50
 * Class to handle example code designed to illustrate javac diagnostic messages.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    51
 */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    52
class Example implements Comparable<Example> {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    53
    /* Create an Example from the files found at path.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    54
     * The head of the file, up to the first Java code, is scanned
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    55
     * for information about the test, such as what resource keys it
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    56
     * generates when run, what options are required to run it, and so on.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    57
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    58
    Example(File file) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    59
        this.file = file;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    60
        declaredKeys = new TreeSet<String>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    61
        srcFiles = new ArrayList<File>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    62
        procFiles = new ArrayList<File>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    63
        supportFiles = new ArrayList<File>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    64
        srcPathFiles = new ArrayList<File>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    65
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    66
        findFiles(file, srcFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    67
        for (File f: srcFiles) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    68
            parse(f);
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
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    71
        if (infoFile == null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    72
            throw new Error("Example " + file + " has no info file");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    73
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    74
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    75
    private void findFiles(File f, List<File> files) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    76
        if (f.isDirectory()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    77
            for (File c: f.listFiles()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    78
                if (files == srcFiles && c.getName().equals("processors"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    79
                    findFiles(c, procFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    80
                else if (files == srcFiles && c.getName().equals("sourcepath")) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    81
                    srcPathDir = c;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    82
                    findFiles(c, srcPathFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    83
                } else if (files == srcFiles && c.getName().equals("support"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    84
                    findFiles(c, supportFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    85
                else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    86
                    findFiles(c, files);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    87
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    88
        } else if (f.isFile() && f.getName().endsWith(".java")) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    89
            files.add(f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    90
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    91
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    92
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    93
    private void parse(File f) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    94
        Pattern keyPat = Pattern.compile(" *// *key: *([^ ]+) *");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    95
        Pattern optPat = Pattern.compile(" *// *options: *(.*)");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    96
        Pattern runPat = Pattern.compile(" *// *run: *(.*)");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    97
        Pattern javaPat = Pattern.compile(" *@?[A-Za-z].*");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    98
        try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    99
            String[] lines = read(f).split("[\r\n]+");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   100
            for (String line: lines) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   101
                Matcher keyMatch = keyPat.matcher(line);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   102
                if (keyMatch.matches()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   103
                    foundInfo(f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   104
                    declaredKeys.add(keyMatch.group(1));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   105
                    continue;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   106
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   107
                Matcher optMatch = optPat.matcher(line);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   108
                if (optMatch.matches()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   109
                    foundInfo(f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   110
                    options = Arrays.asList(optMatch.group(1).trim().split(" +"));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   111
                    continue;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   112
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   113
                Matcher runMatch = runPat.matcher(line);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   114
                if (runMatch.matches()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   115
                    foundInfo(f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   116
                    runOpts = Arrays.asList(runMatch.group(1).trim().split(" +"));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   117
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   118
                if (javaPat.matcher(line).matches())
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   119
                    break;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   120
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   121
        } catch (IOException e) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   122
            throw new Error(e);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   123
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   124
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   125
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   126
    private void foundInfo(File file) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   127
        if (infoFile != null && !infoFile.equals(file))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   128
            throw new Error("multiple info files found: " + infoFile + ", " + file);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   129
        infoFile = file;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   130
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   131
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   132
    String getName() {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   133
        return file.getName();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   134
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   135
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   136
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   137
     * Get the set of resource keys that this test declares it will generate
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   138
     * when it is run.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   139
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   140
    Set<String> getDeclaredKeys() {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   141
        return declaredKeys;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   142
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   143
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   144
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   145
     * Get the set of resource keys that this test generates when it is run.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   146
     * The test will be run if it has not already been run.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   147
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   148
    Set<String> getActualKeys() {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   149
        if (actualKeys == null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   150
            actualKeys = run(false);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   151
        return actualKeys;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   152
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   153
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   154
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   155
     * Run the test.  Information in the test header is used to determine
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   156
     * how to run the test.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   157
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   158
    void run(PrintWriter out, boolean raw, boolean verbose) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   159
        if (out == null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   160
            throw new NullPointerException();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   161
        try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   162
            run(out, null, raw, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   163
        } catch (IOException e) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   164
            e.printStackTrace(out);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   165
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   166
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   167
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   168
    Set<String> run(boolean verbose) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   169
        Set<String> keys = new TreeSet<String>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   170
        try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   171
            run(null, keys, true, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   172
        } catch (IOException e) {
8226
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   173
            e.printStackTrace(System.err);
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   174
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   175
        return keys;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   176
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   177
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   178
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   179
     * Run the test.  Information in the test header is used to determine
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   180
     * how to run the test.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   181
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   182
    private void run(PrintWriter out, Set<String> keys, boolean raw, boolean verbose)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   183
            throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   184
        ClassLoader loader = getClass().getClassLoader();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   185
        if (supportFiles.size() > 0) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   186
            File supportDir = new File(tempDir, "support");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   187
            supportDir.mkdirs();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   188
            clean(supportDir);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   189
            List<String> sOpts = Arrays.asList("-d", supportDir.getPath());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   190
            new Jsr199Compiler(verbose).run(null, null, false, sOpts, procFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   191
            URLClassLoader ucl =
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   192
                    new URLClassLoader(new URL[] { supportDir.toURI().toURL() }, loader);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   193
            loader = ucl;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   194
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   195
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   196
        File classesDir = new File(tempDir, "classes");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   197
        classesDir.mkdirs();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   198
        clean(classesDir);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   199
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   200
        List<String> opts = new ArrayList<String>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   201
        opts.add("-d");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   202
        opts.add(classesDir.getPath());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   203
        if (options != null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   204
            opts.addAll(options);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   205
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   206
        if (procFiles.size() > 0) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   207
            List<String> pOpts = Arrays.asList("-d", classesDir.getPath());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   208
            new Jsr199Compiler(verbose).run(null, null, false, pOpts, procFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   209
            opts.add("-classpath"); // avoid using -processorpath for now
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   210
            opts.add(classesDir.getPath());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   211
            createAnnotationServicesFile(classesDir, procFiles);
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 10638
diff changeset
   212
        } else if (options != null) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 10638
diff changeset
   213
            int i = options.indexOf("-processor");
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 10638
diff changeset
   214
            // check for built-in anno-processor(s)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 10638
diff changeset
   215
            if (i != -1 && options.get(i + 1).equals("DocCommentProcessor")) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 10638
diff changeset
   216
                opts.add("-classpath");
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 10638
diff changeset
   217
                opts.add(System.getProperty("test.classes"));
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 10638
diff changeset
   218
            }
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   219
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   220
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   221
        if (srcPathDir != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   222
            opts.add("-sourcepath");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   223
            opts.add(srcPathDir.getPath());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   224
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   225
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   226
        try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   227
            Compiler c = Compiler.getCompiler(runOpts, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   228
            c.run(out, keys, raw, opts, srcFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   229
        } catch (IllegalArgumentException e) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   230
            if (out != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   231
                out.println("Invalid value for run tag: " + runOpts);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   232
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   233
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   234
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   235
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   236
    void createAnnotationServicesFile(File dir, List<File> procFiles) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   237
        File servicesDir = new File(new File(dir, "META-INF"), "services");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   238
        servicesDir.mkdirs();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   239
        File annoServices = new File(servicesDir, Processor.class.getName());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   240
        Writer out = new FileWriter(annoServices);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   241
        try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   242
            for (File f: procFiles) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   243
                out.write(f.getName().toString().replace(".java", ""));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   244
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   245
        } finally {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   246
            out.close();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   247
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   248
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   249
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   250
    @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   251
    public int compareTo(Example e) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   252
        return file.compareTo(e.file);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   253
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   254
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   255
    @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   256
    public String toString() {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   257
        return file.getPath();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   258
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   259
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   260
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   261
     * Read the contents of a file.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   262
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   263
    private String read(File f) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   264
        byte[] bytes = new byte[(int) f.length()];
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   265
        DataInputStream in = new DataInputStream(new FileInputStream(f));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   266
        try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   267
            in.readFully(bytes);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   268
        } finally {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   269
            in.close();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   270
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   271
        return new String(bytes);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   272
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   273
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   274
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   275
     * Clean the contents of a directory.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   276
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   277
    boolean clean(File dir) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   278
        boolean ok = true;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   279
        for (File f: dir.listFiles()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   280
            if (f.isDirectory())
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   281
                ok &= clean(f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   282
            ok &= f.delete();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   283
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   284
        return ok;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   285
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   286
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   287
    File file;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   288
    List<File> srcFiles;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   289
    List<File> procFiles;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   290
    File srcPathDir;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   291
    List<File> srcPathFiles;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   292
    List<File> supportFiles;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   293
    File infoFile;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   294
    private List<String> runOpts;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   295
    private List<String> options;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   296
    private Set<String> actualKeys;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   297
    private Set<String> declaredKeys;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   298
17798
abe47e71498b 7060779: test/tools/javac/diags/Example.java leaves directories in tempdir
vromero
parents: 14541
diff changeset
   299
    static File tempDir = (System.getProperty("test.src") != null) ?
abe47e71498b 7060779: test/tools/javac/diags/Example.java leaves directories in tempdir
vromero
parents: 14541
diff changeset
   300
            new File(System.getProperty("user.dir")):
abe47e71498b 7060779: test/tools/javac/diags/Example.java leaves directories in tempdir
vromero
parents: 14541
diff changeset
   301
            new File(System.getProperty("java.io.tmpdir"));
abe47e71498b 7060779: test/tools/javac/diags/Example.java leaves directories in tempdir
vromero
parents: 14541
diff changeset
   302
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   303
    static void setTempDir(File tempDir) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   304
        Example.tempDir = tempDir;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   305
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   306
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   307
    abstract static class Compiler {
8226
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   308
        interface Factory {
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   309
            Compiler getCompiler(List<String> opts, boolean verbose);
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   310
        }
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   311
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   312
        static class DefaultFactory implements Factory {
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   313
            public Compiler getCompiler(List<String> opts, boolean verbose) {
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   314
            String first;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   315
            String[] rest;
8226
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   316
                if (opts == null || opts.isEmpty()) {
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   317
                first = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   318
                rest = new String[0];
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   319
            } else {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   320
                first = opts.get(0);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   321
                rest = opts.subList(1, opts.size()).toArray(new String[opts.size() - 1]);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   322
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   323
            if (first == null || first.equals("jsr199"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   324
                return new Jsr199Compiler(verbose, rest);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   325
            else if (first.equals("simple"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   326
                return new SimpleCompiler(verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   327
            else if (first.equals("backdoor"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   328
                return new BackdoorCompiler(verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   329
            else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   330
                throw new IllegalArgumentException(first);
8226
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   331
                }
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   332
        }
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   333
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   334
        static Factory factory;
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   335
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   336
        static Compiler getCompiler(List<String> opts, boolean verbose) {
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   337
            if (factory == null)
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   338
                factory = new DefaultFactory();
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   339
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 6710
diff changeset
   340
            return factory.getCompiler(opts, verbose);
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   341
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   342
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   343
        protected Compiler(boolean verbose) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   344
            this.verbose = verbose;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   345
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   346
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   347
        abstract boolean run(PrintWriter out, Set<String> keys, boolean raw,
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   348
                List<String> opts,  List<File> files);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   349
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   350
        void setSupportClassLoader(ClassLoader cl) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   351
            loader = cl;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   352
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   353
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   354
        protected ClassLoader loader;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   355
        protected boolean verbose;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   356
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   357
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   358
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   359
     * Compile using the JSR 199 API.  The diagnostics generated are
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   360
     * scanned for resource keys.   Not all diagnostic keys are generated
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   361
     * via the JSR 199 API -- for example, rich diagnostics are not directly
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   362
     * accessible, and some diagnostics generated by the file manager may
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   363
     * not be generated (for example, the JSR 199 file manager does not see
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   364
     * -Xlint:path).
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   365
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   366
    static class Jsr199Compiler extends Compiler {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   367
        List<String> fmOpts;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   368
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   369
        Jsr199Compiler(boolean verbose, String... args) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   370
            super(verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   371
            for (int i = 0; i < args.length; i++) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   372
                String arg = args[i];
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   373
                if (arg.equals("-filemanager") && (i + 1 < args.length)) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   374
                    fmOpts = Arrays.asList(args[++i].split(","));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   375
                } else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   376
                    throw new IllegalArgumentException(arg);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   377
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   378
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   379
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   380
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   381
        boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   382
            if (out != null && keys != null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   383
                throw new IllegalArgumentException();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   384
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   385
            if (verbose)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   386
                System.err.println("run_jsr199: " + opts + " " + files);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   387
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   388
            DiagnosticCollector<JavaFileObject> dc = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   389
            if (keys != null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   390
                dc = new DiagnosticCollector<JavaFileObject>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   391
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   392
            if (raw) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   393
                List<String> newOpts = new ArrayList<String>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   394
                newOpts.add("-XDrawDiagnostics");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   395
                newOpts.addAll(opts);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   396
                opts = newOpts;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   397
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   398
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   399
            JavaCompiler c = ToolProvider.getSystemJavaCompiler();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   400
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   401
            StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   402
            if (fmOpts != null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   403
                fm = new FileManager(fm, fmOpts);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   404
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   405
            Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   406
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   407
            CompilationTask t = c.getTask(out, fm, dc, opts, null, fos);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   408
            Boolean ok = t.call();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   409
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   410
            if (keys != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   411
                for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   412
                    scanForKeys(unwrap(d), keys);
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   413
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   414
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   415
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   416
            return ok;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   417
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   418
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   419
        /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   420
         * Scan a diagnostic for resource keys.  This will not detect additional
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   421
         * sub diagnostics that might be generated by a rich diagnostic formatter.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   422
         */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   423
        private static void scanForKeys(JCDiagnostic d, Set<String> keys) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   424
            keys.add(d.getCode());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   425
            for (Object o: d.getArgs()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   426
                if (o instanceof JCDiagnostic) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   427
                    scanForKeys((JCDiagnostic) o, keys);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   428
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   429
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   430
            for (JCDiagnostic sd: d.getSubdiagnostics())
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6349
diff changeset
   431
                scanForKeys(sd, keys);
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   432
        }
10454
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   433
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   434
        private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   435
            if (diagnostic instanceof JCDiagnostic)
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   436
                return (JCDiagnostic) diagnostic;
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   437
            if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   438
                return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   439
            throw new IllegalArgumentException();
9d5584396849 7074416: Regression: JSR199: javac doesn't unwrap clientcodewrapper objects
jjg
parents: 8615
diff changeset
   440
        }
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   441
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   442
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   443
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   444
     * Run the test using the standard simple entry point.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   445
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   446
    static class SimpleCompiler extends Compiler {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   447
        SimpleCompiler(boolean verbose) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   448
            super(verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   449
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   450
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   451
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   452
        boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   453
            if (out != null && keys != null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   454
                throw new IllegalArgumentException();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   455
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   456
            if (verbose)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   457
                System.err.println("run_simple: " + opts + " " + files);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   458
6349
f80dfbda8f27 6976833: options included twice in Example SimpleCompiler
jjg
parents: 6155
diff changeset
   459
            List<String> args = new ArrayList<String>();
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   460
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   461
            if (keys != null || raw)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   462
                args.add("-XDrawDiagnostics");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   463
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   464
            args.addAll(opts);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   465
            for (File f: files)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   466
                args.add(f.getPath());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   467
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   468
            StringWriter sw = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   469
            PrintWriter pw;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   470
            if (keys != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   471
                sw = new StringWriter();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   472
                pw = new PrintWriter(sw);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   473
            } else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   474
                pw = out;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   475
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   476
            int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   477
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   478
            if (keys != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   479
                pw.close();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   480
                scanForKeys(sw.toString(), keys);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   481
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   482
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   483
            return (rc == 0);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   484
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   485
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   486
        private static void scanForKeys(String text, Set<String> keys) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   487
            StringTokenizer st = new StringTokenizer(text, " ,\r\n():");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   488
            while (st.hasMoreElements()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   489
                String t = st.nextToken();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   490
                if (t.startsWith("compiler."))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   491
                    keys.add(t);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   492
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   493
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   494
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   495
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   496
    static class BackdoorCompiler extends Compiler {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   497
        BackdoorCompiler(boolean verbose) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   498
            super(verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   499
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   500
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   501
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   502
        boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   503
            if (out != null && keys != null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   504
                throw new IllegalArgumentException();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   505
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   506
            if (verbose)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   507
                System.err.println("run_simple: " + opts + " " + files);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   508
8615
11d80b55b0ed 7022310: test/tools/javac/diags/Example: args added twice
jjg
parents: 8614
diff changeset
   509
            List<String> args = new ArrayList<String>();
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   510
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   511
            if (out != null && raw)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   512
                args.add("-XDrawDiagnostics");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   513
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   514
            args.addAll(opts);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   515
            for (File f: files)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   516
                args.add(f.getPath());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   517
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   518
            StringWriter sw = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   519
            PrintWriter pw;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   520
            if (keys != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   521
                sw = new StringWriter();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   522
                pw = new PrintWriter(sw);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   523
            } else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   524
                pw = out;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   525
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   526
            Context c = new Context();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   527
            JavacFileManager.preRegister(c); // can't create it until Log has been set up
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   528
            MessageTracker.preRegister(c, keys);
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10454
diff changeset
   529
            Main m = new Main("javac", pw);
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10454
diff changeset
   530
            Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   531
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   532
            if (keys != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   533
                pw.close();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   534
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   535
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10454
diff changeset
   536
            return rc.isOK();
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   537
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   538
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   539
        static class MessageTracker extends JavacMessages {
6155
214f78bb8006 6972747: CheckExamples fail when assertions are enabled
mcimadamore
parents: 6149
diff changeset
   540
214f78bb8006 6972747: CheckExamples fail when assertions are enabled
mcimadamore
parents: 6149
diff changeset
   541
            MessageTracker(Context context) {
214f78bb8006 6972747: CheckExamples fail when assertions are enabled
mcimadamore
parents: 6149
diff changeset
   542
                super(context);
214f78bb8006 6972747: CheckExamples fail when assertions are enabled
mcimadamore
parents: 6149
diff changeset
   543
            }
214f78bb8006 6972747: CheckExamples fail when assertions are enabled
mcimadamore
parents: 6149
diff changeset
   544
8614
06e42328ddab 7021650: fix Context issues
jjg
parents: 8226
diff changeset
   545
            static void preRegister(Context c, final Set<String> keys) {
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   546
                if (keys != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   547
                    c.put(JavacMessages.messagesKey, new Context.Factory<JavacMessages>() {
8614
06e42328ddab 7021650: fix Context issues
jjg
parents: 8226
diff changeset
   548
                        public JavacMessages make(Context c) {
6155
214f78bb8006 6972747: CheckExamples fail when assertions are enabled
mcimadamore
parents: 6149
diff changeset
   549
                            return new MessageTracker(c) {
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   550
                                @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   551
                                public String getLocalizedString(Locale l, String key, Object... args) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   552
                                    keys.add(key);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   553
                                    return super.getLocalizedString(l, key, args);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   554
                                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   555
                            };
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   556
                        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   557
                    });
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   558
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   559
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   560
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   561
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   562
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   563
}