test/tools/javac/tree/NewArrayPretty.java

changeset 0
959103a6100f
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2013, 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 /*
25 * @test
26 * @bug 8014826
27 * @summary test Pretty print of NewArray
28 * @author ksrini
29 */
30 import com.sun.source.tree.ClassTree;
31 import com.sun.source.tree.CompilationUnitTree;
32 import com.sun.tools.javac.api.JavacTaskImpl;
33 import com.sun.tools.javac.tree.JCTree;
34 import java.io.IOException;
35 import java.net.URI;
36 import java.util.Arrays;
37 import javax.tools.JavaCompiler;
38 import javax.tools.JavaFileObject;
39 import javax.tools.SimpleJavaFileObject;
40 import javax.tools.ToolProvider;
41
42 public class NewArrayPretty {
43 private final JavaCompiler tool;
44 NewArrayPretty() {
45 tool = ToolProvider.getSystemJavaCompiler();
46 }
47 public static void main(String... args) throws Exception {
48 NewArrayPretty nap = new NewArrayPretty();
49 nap.run("Class[] cls = null");
50 nap.run("Class[] cls = new Class[]{Object.class}");
51 }
52 void run(String code) throws IOException {
53 String src = "public class Test {" + code + ";}";
54
55 JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
56 null, Arrays.asList(new MyFileObject(src)));
57
58 for (CompilationUnitTree cut : ct.parse()) {
59 JCTree.JCVariableDecl var =
60 (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
61
62 if (!code.equals(var.toString())) {
63 System.err.println("Expected: " + code);
64 System.err.println("Obtained: " + var.toString());
65 throw new RuntimeException("strings do not match!");
66 }
67 }
68 }
69 }
70 class MyFileObject extends SimpleJavaFileObject {
71
72 private String text;
73
74 public MyFileObject(String text) {
75 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
76 this.text = text;
77 }
78
79 @Override
80 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
81 return text;
82 }
83 }

mercurial