PrettyPrintingForLoopsTest.java revision 3478:293579e318b9
1/* 2 * Copyright (c) 2016, 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 8068460 27 * @summary Pretty printing for loops 28 * @library /tools/javac/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.file 31 * jdk.compiler/com.sun.tools.javac.tree 32 * jdk.compiler/com.sun.tools.javac.util 33 * @build JavacTestingAbstractProcessor 34 * @run main PrettyPrintingForLoopsTest 35 */ 36 37import java.io.IOException; 38import java.io.StringWriter; 39import java.net.URI; 40import java.util.Arrays; 41import java.util.List; 42 43import javax.tools.JavaFileObject; 44import javax.tools.SimpleJavaFileObject; 45import javax.tools.StandardJavaFileManager; 46 47import com.sun.source.tree.CompilationUnitTree; 48import com.sun.source.util.JavacTask; 49import com.sun.tools.javac.api.JavacTool; 50import com.sun.tools.javac.tree.JCTree; 51import com.sun.tools.javac.tree.Pretty; 52import com.sun.tools.javac.util.Assert; 53 54public class PrettyPrintingForLoopsTest { 55 56 public static void main(String... args) throws IOException { 57 new PrettyPrintingForLoopsTest().testForLoop(); 58 } 59 60 public void testForLoop() throws IOException { 61 List files = Arrays.asList(new JavaSource("")); 62 JavacTool tool = JavacTool.create(); 63 try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { 64 JavacTask task = tool.getTask(null, fm, null, null, null, files); 65 Iterable trees = task.parse(); 66 CompilationUnitTree thisTree = (CompilationUnitTree)trees.iterator().next(); 67 String thisSrc = prettyPrint((JCTree) thisTree); 68 Assert.check(thisSrc.equals(JavaSource.source)); 69 } 70 } 71 72 private static String prettyPrint(JCTree tree) throws IOException { 73 StringWriter sw = new StringWriter(); 74 new Pretty(sw, true).printExpr(tree); 75 return sw.toString().replaceAll(System.getProperty("line.separator"), "\n"); 76 } 77 78 static class JavaSource extends SimpleJavaFileObject { 79 static String source = "\n" + 80 "class Test {\n" + 81 " \n" + 82 " void m() {\n" + 83 " for (int i; true; i++) {\n" + 84 " i = 0;\n" + 85 " }\n" + 86 " for (int i = 0, j, k = 23, l; i < 42; i++) {\n" + 87 " }\n" + 88 " }\n" + 89 "}"; 90 91 public JavaSource(String stmt) { 92 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 93 } 94 95 @Override 96 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 97 return source; 98 } 99 } 100} 101