author | never |
Wed, 07 Jul 2010 12:40:01 -0700 | |
changeset 5915 | 406168b53eb0 |
parent 5520 | 86e4b9a9da40 |
child 27319 | 030080f03e4f |
permissions | -rw-r--r-- |
4413
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
1 |
/* |
5520 | 2 |
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
4413
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
4 |
* |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
8 |
* |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
13 |
* accompanied this code). |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
14 |
* |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
18 |
* |
5520 | 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. |
|
4413
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
22 |
*/ |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
23 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
24 |
import java.io.*; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
25 |
import java.net.*; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
26 |
import javax.tools.*; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
27 |
import java.util.*; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
28 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
29 |
import com.sun.source.tree.CompilationUnitTree; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
30 |
import com.sun.source.util.JavacTask; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
31 |
import com.sun.tools.javac.api.JavacTool; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
32 |
import com.sun.tools.javac.tree.JCTree; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
33 |
import com.sun.tools.javac.tree.Pretty; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
34 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
35 |
/** |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
36 |
* @test |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
37 |
* @bug 6902720 |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
38 |
* @summary javac pretty printer does not handle enums correctly |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
39 |
*/ |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
40 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
41 |
public class Test { |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
42 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
43 |
public static void main(String[] args) throws Exception { |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
44 |
Test t = new Test(); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
45 |
t.run("E1.java", "E2.java"); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
46 |
} |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
47 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
48 |
void run(String... args) throws Exception { |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
49 |
File testSrcDir = new File(System.getProperty("test.src")); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
50 |
for (String arg: args) { |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
51 |
test(new File(testSrcDir, arg)); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
52 |
} |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
53 |
} |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
54 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
55 |
void test(File test) throws Exception { |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
56 |
JavacTool tool1 = JavacTool.create(); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
57 |
StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
58 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
59 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
60 |
// parse test file into a tree, and write it out to a stringbuffer using Pretty |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
61 |
JavacTask t1 = tool1.getTask(null, fm, null, null, null, files); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
62 |
StringWriter sw = new StringWriter(); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
63 |
PrintWriter pw = new PrintWriter(sw); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
64 |
Iterable<? extends CompilationUnitTree> trees = t1.parse(); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
65 |
for (CompilationUnitTree tree: trees) { |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
66 |
new Pretty(pw, true).printExpr((JCTree) tree); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
67 |
} |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
68 |
pw.close(); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
69 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
70 |
final String out = sw.toString(); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
71 |
System.err.println("generated code:\n" + out + "\n"); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
72 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
73 |
// verify the generated code is valid Java by compiling it |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
74 |
JavacTool tool2 = JavacTool.create(); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
75 |
JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) { |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
76 |
@Override |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
77 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
78 |
return out; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
79 |
} |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
80 |
}; |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
81 |
JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo)); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
82 |
boolean ok = t2.call(); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
83 |
if (!ok) |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
84 |
throw new Exception("compilation of generated code failed"); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
85 |
|
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
86 |
File expectedClass = new File(test.getName().replace(".java", ".class")); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
87 |
if (!expectedClass.exists()) |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
88 |
throw new Exception(expectedClass + " not found"); |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
89 |
} |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
90 |
} |
2ae470f0b492
6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff
changeset
|
91 |