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