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