1/* 2 * Copyright (c) 2009, 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 24import java.io.*; 25import java.net.*; 26import javax.tools.*; 27import java.util.*; 28 29import com.sun.source.tree.CompilationUnitTree; 30import com.sun.source.util.JavacTask; 31import com.sun.tools.javac.api.JavacTool; 32import com.sun.tools.javac.tree.JCTree; 33import com.sun.tools.javac.tree.Pretty; 34 35/** 36 * @test 37 * @bug 6902720 38 * @summary javac pretty printer does not handle enums correctly 39 * @modules jdk.compiler/com.sun.tools.javac.api 40 * jdk.compiler/com.sun.tools.javac.file 41 * jdk.compiler/com.sun.tools.javac.tree 42 */ 43 44public class Test { 45 46 public static void main(String[] args) throws Exception { 47 Test t = new Test(); 48 t.run("E1.java", "E2.java"); 49 } 50 51 void run(String... args) throws Exception { 52 File testSrcDir = new File(System.getProperty("test.src")); 53 for (String arg: args) { 54 test(new File(testSrcDir, arg)); 55 } 56 } 57 58 void test(File test) throws Exception { 59 JavacTool tool1 = JavacTool.create(); 60 try (StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null)) { 61 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test); 62 63 // parse test file into a tree, and write it out to a stringbuffer using Pretty 64 JavacTask t1 = tool1.getTask(null, fm, null, null, null, files); 65 StringWriter sw = new StringWriter(); 66 PrintWriter pw = new PrintWriter(sw); 67 Iterable<? extends CompilationUnitTree> trees = t1.parse(); 68 for (CompilationUnitTree tree: trees) { 69 new Pretty(pw, true).printExpr((JCTree) tree); 70 } 71 pw.close(); 72 73 final String out = sw.toString(); 74 System.err.println("generated code:\n" + out + "\n"); 75 76 // verify the generated code is valid Java by compiling it 77 JavacTool tool2 = JavacTool.create(); 78 JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) { 79 @Override 80 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 81 return out; 82 } 83 }; 84 JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo)); 85 boolean ok = t2.call(); 86 if (!ok) 87 throw new Exception("compilation of generated code failed"); 88 89 File expectedClass = new File(test.getName().replace(".java", ".class")); 90 if (!expectedClass.exists()) 91 throw new Exception(expectedClass + " not found"); 92 } 93 } 94} 95 96