langtools/test/tools/javac/6917288/T6917288.java
author mcimadamore
Thu, 03 Feb 2011 09:38:19 +0000
changeset 8239 d2c934e951e2
parent 6029 89f2c9b62d31
child 30730 d3ce7619db2c
permissions -rw-r--r--
7014715: javac returns different error code for certain failure(s) Summary: javac silently crashes when emitting certain kinds of resolution diagnostics Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6029
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     1
/*
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     4
 *
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     8
 *
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    13
 * accompanied this code).
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    14
 *
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    18
 *
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    21
 * questions.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    22
 */
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    23
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    24
/* @test
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    25
 * @bug 6917288
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    26
 * @summary Unnamed nested class is not generated
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    27
 */
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    28
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    29
import java.io.*;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    30
import java.util.*;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    31
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    32
public class T6917288 {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    33
    // refers to kind of reference to an anon inner class that may be generated
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    34
    enum Kind { NONE, FALSE, TRUE, ALWAYS };
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    35
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    36
    public static void main(String... args) throws Exception {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    37
        new T6917288().run();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    38
    }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    39
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    40
    void run() throws Exception {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    41
        for (Kind k: Kind.values()) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    42
            test(k);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    43
        }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    44
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    45
        if (errors > 0)
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    46
            throw new Exception(errors + " errors occurred");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    47
    }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    48
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    49
    /**
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    50
     *  Run a test case for Kind k.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    51
     */
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    52
    void test(Kind k) throws Exception {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    53
        System.err.println("Test " + (++count) + ": " + k);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    54
        File testDir = new File("test" + count);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    55
        File srcDir = new File(testDir, "src");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    56
        srcDir.mkdirs();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    57
        File classesDir = new File(testDir, "classes");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    58
        classesDir.mkdirs();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    59
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    60
        List<String> opts = new ArrayList<String>();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    61
        opts.add("-d");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    62
        opts.add(classesDir.getPath());
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    63
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    64
        File f = writeFile(srcDir, k);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    65
        int rc = compile(opts, f);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    66
        if (rc != 0) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    67
            error("compilation failed: rc=" + rc);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    68
            return;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    69
        }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    70
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    71
        check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    72
    }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    73
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    74
    /**
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    75
     *  Compile files with given options.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    76
     *  Display any output from compiler, and return javac return code.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    77
     */
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    78
    int compile(List<String> opts, File... files) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    79
        List<String> args = new ArrayList<String>();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    80
        args.addAll(opts);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    81
        for (File f: files)
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    82
            args.add(f.getPath());
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    83
        StringWriter sw = new StringWriter();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    84
        PrintWriter pw = new PrintWriter(sw);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    85
        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    86
        pw.close();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    87
        String out = sw.toString();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    88
        if (out.length() > 0)
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    89
            System.err.println(out);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    90
        return rc;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    91
    }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    92
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    93
    /**
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    94
     *  Check that a directory contains the expected files.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    95
     */
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    96
    void check(File dir, String... paths) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    97
        Set<String> found = new TreeSet<String>(Arrays.asList(dir.list()));
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    98
        Set<String> expect = new TreeSet<String>(Arrays.asList(paths));
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
    99
        if (found.equals(expect))
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   100
            return;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   101
        for (String f: found) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   102
            if (!expect.contains(f))
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   103
                error("Unexpected file found: " + f);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   104
        }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   105
        for (String e: expect) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   106
            if (!found.contains(e))
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   107
                error("Expected file not found: " + e);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   108
        }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   109
    }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   110
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   111
    /**
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   112
     *  Write source file for test case k.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   113
     */
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   114
    File writeFile(File dir, Kind k) throws Exception {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   115
        StringBuilder sb = new StringBuilder();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   116
        sb.append("public class Test {\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   117
        sb.append("    private Inner inner;\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   118
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   119
        // generate different cases of an anon inner class
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   120
        if (k != Kind.NONE) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   121
            sb.append("    private void m() {\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   122
            sb.append("        ");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   123
            switch (k) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   124
                case FALSE: case TRUE:
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   125
                    sb.append("if (" + k.toString().toLowerCase() + ") ");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   126
            }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   127
            sb.append("new Runnable() { public void run() { } };\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   128
            sb.append("    }\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   129
        }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   130
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   131
        sb.append("    private void init() {\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   132
        sb.append("        inner = new Inner();\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   133
        sb.append("    }\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   134
        sb.append("\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   135
        sb.append("    private static class Inner {\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   136
        sb.append("        private Inner() {\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   137
        sb.append("        }\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   138
        sb.append("    }\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   139
        sb.append("}\n");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   140
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   141
        File f = new File(dir, "Test.java");
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   142
        FileWriter w = new FileWriter(f);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   143
        w.write(sb.toString());
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   144
        w.close();
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   145
        return f;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   146
    }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   147
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   148
    /**
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   149
     *  Record an error message.
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   150
     */
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   151
    void error(String msg) {
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   152
        System.err.println(msg);
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   153
        errors++;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   154
    }
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   155
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   156
    int count;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   157
    int errors;
89f2c9b62d31 6917288: Unnamed nested class is not generated
jjg
parents:
diff changeset
   158
}