6029
|
1 |
/*
|
|
2 |
* Copyright (c) 2010, 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 |
*/
|
|
28 |
|
|
29 |
import java.io.*;
|
|
30 |
import java.util.*;
|
|
31 |
|
|
32 |
public class T6917288 {
|
|
33 |
// refers to kind of reference to an anon inner class that may be generated
|
|
34 |
enum Kind { NONE, FALSE, TRUE, ALWAYS };
|
|
35 |
|
|
36 |
public static void main(String... args) throws Exception {
|
|
37 |
new T6917288().run();
|
|
38 |
}
|
|
39 |
|
|
40 |
void run() throws Exception {
|
|
41 |
for (Kind k: Kind.values()) {
|
|
42 |
test(k);
|
|
43 |
}
|
|
44 |
|
|
45 |
if (errors > 0)
|
|
46 |
throw new Exception(errors + " errors occurred");
|
|
47 |
}
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Run a test case for Kind k.
|
|
51 |
*/
|
|
52 |
void test(Kind k) throws Exception {
|
|
53 |
System.err.println("Test " + (++count) + ": " + k);
|
|
54 |
File testDir = new File("test" + count);
|
|
55 |
File srcDir = new File(testDir, "src");
|
|
56 |
srcDir.mkdirs();
|
|
57 |
File classesDir = new File(testDir, "classes");
|
|
58 |
classesDir.mkdirs();
|
|
59 |
|
|
60 |
List<String> opts = new ArrayList<String>();
|
|
61 |
opts.add("-d");
|
|
62 |
opts.add(classesDir.getPath());
|
|
63 |
|
|
64 |
File f = writeFile(srcDir, k);
|
|
65 |
int rc = compile(opts, f);
|
|
66 |
if (rc != 0) {
|
|
67 |
error("compilation failed: rc=" + rc);
|
|
68 |
return;
|
|
69 |
}
|
|
70 |
|
|
71 |
check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Compile files with given options.
|
|
76 |
* Display any output from compiler, and return javac return code.
|
|
77 |
*/
|
|
78 |
int compile(List<String> opts, File... files) {
|
|
79 |
List<String> args = new ArrayList<String>();
|
|
80 |
args.addAll(opts);
|
|
81 |
for (File f: files)
|
|
82 |
args.add(f.getPath());
|
|
83 |
StringWriter sw = new StringWriter();
|
|
84 |
PrintWriter pw = new PrintWriter(sw);
|
|
85 |
int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
|
|
86 |
pw.close();
|
|
87 |
String out = sw.toString();
|
|
88 |
if (out.length() > 0)
|
|
89 |
System.err.println(out);
|
|
90 |
return rc;
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Check that a directory contains the expected files.
|
|
95 |
*/
|
|
96 |
void check(File dir, String... paths) {
|
|
97 |
Set<String> found = new TreeSet<String>(Arrays.asList(dir.list()));
|
|
98 |
Set<String> expect = new TreeSet<String>(Arrays.asList(paths));
|
|
99 |
if (found.equals(expect))
|
|
100 |
return;
|
|
101 |
for (String f: found) {
|
|
102 |
if (!expect.contains(f))
|
|
103 |
error("Unexpected file found: " + f);
|
|
104 |
}
|
|
105 |
for (String e: expect) {
|
|
106 |
if (!found.contains(e))
|
|
107 |
error("Expected file not found: " + e);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Write source file for test case k.
|
|
113 |
*/
|
|
114 |
File writeFile(File dir, Kind k) throws Exception {
|
|
115 |
StringBuilder sb = new StringBuilder();
|
|
116 |
sb.append("public class Test {\n");
|
|
117 |
sb.append(" private Inner inner;\n");
|
|
118 |
|
|
119 |
// generate different cases of an anon inner class
|
|
120 |
if (k != Kind.NONE) {
|
|
121 |
sb.append(" private void m() {\n");
|
|
122 |
sb.append(" ");
|
|
123 |
switch (k) {
|
|
124 |
case FALSE: case TRUE:
|
|
125 |
sb.append("if (" + k.toString().toLowerCase() + ") ");
|
|
126 |
}
|
|
127 |
sb.append("new Runnable() { public void run() { } };\n");
|
|
128 |
sb.append(" }\n");
|
|
129 |
}
|
|
130 |
|
|
131 |
sb.append(" private void init() {\n");
|
|
132 |
sb.append(" inner = new Inner();\n");
|
|
133 |
sb.append(" }\n");
|
|
134 |
sb.append("\n");
|
|
135 |
sb.append(" private static class Inner {\n");
|
|
136 |
sb.append(" private Inner() {\n");
|
|
137 |
sb.append(" }\n");
|
|
138 |
sb.append(" }\n");
|
|
139 |
sb.append("}\n");
|
|
140 |
|
|
141 |
File f = new File(dir, "Test.java");
|
|
142 |
FileWriter w = new FileWriter(f);
|
|
143 |
w.write(sb.toString());
|
|
144 |
w.close();
|
|
145 |
return f;
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Record an error message.
|
|
150 |
*/
|
|
151 |
void error(String msg) {
|
|
152 |
System.err.println(msg);
|
|
153 |
errors++;
|
|
154 |
}
|
|
155 |
|
|
156 |
int count;
|
|
157 |
int errors;
|
|
158 |
}
|