1 /*
2 * Copyright (c) 2020, 2022, 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 * @enablePreview
27 * @summary Test behavior of Pretty
28 * @modules jdk.compiler
29 */
30
31 import java.io.IOException;
32 import java.io.StringWriter;
33 import java.net.URI;
34 import java.util.Arrays;
35 import java.util.List;
36
37 import javax.tools.*;
38
39 import com.sun.source.tree.CompilationUnitTree;
40 import com.sun.source.util.JavacTask;
41
42 public class PrettyTest {
43
44 public static void main(String[] args) throws Exception {
45 new PrettyTest().run();
46 }
47
48 void run() throws Exception {
49 String code = "class Test {\n" +
50 " boolean t(Object o) {\n" +
51 " boolean b;\n" +
52 " boolean _ = true;\n" +
53 " b = o instanceof String s;\n" +
54 " b = o instanceof R(String s);\n" +
55 " b = o instanceof R(var s);\n" +
56 " b = o instanceof R2(R(var s), String t);\n" +
57 " b = o instanceof R2(R(var s), var t);\n" +
58 " b = o instanceof R(String _);\n" +
59 " b = o instanceof R2(R(var _), var _);\n" +
60 " b = o instanceof R2(R(_), var t);\n" +
61 " }\n" +
62 " record R(String s) {}\n" +
63 " record R2(R r, String s) {}\n" +
64 "}\n";
65 String pretty = parse(code).toString().replaceAll("\\R", "\n");
66 String expected = """
67 \n\
68 class Test {
69 \n\
70 boolean t(Object o) {
71 boolean b;
72 boolean _ = true;
73 b = o instanceof String s;
74 b = o instanceof R(String s);
75 b = o instanceof R(var s);
76 b = o instanceof R2(R(var s), String t);
77 b = o instanceof R2(R(var s), var t);
78 b = o instanceof R(String _);
79 b = o instanceof R2(R(var _), var _);
80 b = o instanceof R2(R(_), var t);
81 }
82 \n\
83 class R {
84 private final String s;
85 }
86 \n\
87 class R2 {
88 private final R r;
89 private final String s;
90 }
91 }""";
92 if (!expected.equals(pretty)) {
93 throw new AssertionError("Actual prettified source: " + pretty);
94 }
95 }
96
97 private CompilationUnitTree parse(String code) throws IOException {
98 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
99 assert tool != null;
100 DiagnosticListener<JavaFileObject> noErrors = d -> {};
101
102 StringWriter out = new StringWriter();
103 JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
104 List.of(), null,
105 Arrays.asList(new MyFileObject(code)));
106 return ct.parse().iterator().next();
107 }
108
109 static class MyFileObject extends SimpleJavaFileObject {
110 private String text;
111
112 public MyFileObject(String text) {
113 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
114 this.text = text;
115 }
116
117 @Override
118 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
119 return text;
120 }
121 }
122 }
--- EOF ---