|
1 /* |
|
2 * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 import java.io.*; |
|
25 import java.net.*; |
|
26 import javax.tools.*; |
|
27 import java.util.*; |
|
28 |
|
29 import com.sun.source.tree.CompilationUnitTree; |
|
30 import com.sun.source.util.JavacTask; |
|
31 import com.sun.tools.javac.api.JavacTool; |
|
32 import com.sun.tools.javac.tree.JCTree; |
|
33 import com.sun.tools.javac.tree.Pretty; |
|
34 |
|
35 /** |
|
36 * @test |
|
37 * @bug 6902720 |
|
38 * @summary javac pretty printer does not handle enums correctly |
|
39 */ |
|
40 |
|
41 public class Test { |
|
42 |
|
43 public static void main(String[] args) throws Exception { |
|
44 Test t = new Test(); |
|
45 t.run("E1.java", "E2.java"); |
|
46 } |
|
47 |
|
48 void run(String... args) throws Exception { |
|
49 File testSrcDir = new File(System.getProperty("test.src")); |
|
50 for (String arg: args) { |
|
51 test(new File(testSrcDir, arg)); |
|
52 } |
|
53 } |
|
54 |
|
55 void test(File test) throws Exception { |
|
56 JavacTool tool1 = JavacTool.create(); |
|
57 StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null); |
|
58 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test); |
|
59 |
|
60 // parse test file into a tree, and write it out to a stringbuffer using Pretty |
|
61 JavacTask t1 = tool1.getTask(null, fm, null, null, null, files); |
|
62 StringWriter sw = new StringWriter(); |
|
63 PrintWriter pw = new PrintWriter(sw); |
|
64 Iterable<? extends CompilationUnitTree> trees = t1.parse(); |
|
65 for (CompilationUnitTree tree: trees) { |
|
66 new Pretty(pw, true).printExpr((JCTree) tree); |
|
67 } |
|
68 pw.close(); |
|
69 |
|
70 final String out = sw.toString(); |
|
71 System.err.println("generated code:\n" + out + "\n"); |
|
72 |
|
73 // verify the generated code is valid Java by compiling it |
|
74 JavacTool tool2 = JavacTool.create(); |
|
75 JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) { |
|
76 @Override |
|
77 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
78 return out; |
|
79 } |
|
80 }; |
|
81 JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo)); |
|
82 boolean ok = t2.call(); |
|
83 if (!ok) |
|
84 throw new Exception("compilation of generated code failed"); |
|
85 |
|
86 File expectedClass = new File(test.getName().replace(".java", ".class")); |
|
87 if (!expectedClass.exists()) |
|
88 throw new Exception(expectedClass + " not found"); |
|
89 } |
|
90 } |
|
91 |