Wed, 26 Jan 2011 13:45:25 -0800
7013272: Automatically generate info about how compiler resource keys are used
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2009, 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 */
24 import java.io.*;
25 import java.net.*;
26 import javax.tools.*;
27 import java.util.*;
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;
35 /**
36 * @test
37 * @bug 6902720
38 * @summary javac pretty printer does not handle enums correctly
39 */
41 public class Test {
43 public static void main(String[] args) throws Exception {
44 Test t = new Test();
45 t.run("E1.java", "E2.java");
46 }
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 }
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);
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();
70 final String out = sw.toString();
71 System.err.println("generated code:\n" + out + "\n");
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");
86 File expectedClass = new File(test.getName().replace(".java", ".class"));
87 if (!expectedClass.exists())
88 throw new Exception(expectedClass + " not found");
89 }
90 }