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