jjg@439: /* ohair@554: * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. jjg@439: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@439: * jjg@439: * This code is free software; you can redistribute it and/or modify it jjg@439: * under the terms of the GNU General Public License version 2 only, as jjg@439: * published by the Free Software Foundation. jjg@439: * jjg@439: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@439: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@439: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@439: * version 2 for more details (a copy is included in the LICENSE file that jjg@439: * accompanied this code). jjg@439: * jjg@439: * You should have received a copy of the GNU General Public License version jjg@439: * 2 along with this work; if not, write to the Free Software Foundation, jjg@439: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@439: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@439: */ jjg@439: jjg@439: import java.io.*; jjg@439: import java.net.*; jjg@439: import javax.tools.*; jjg@439: import java.util.*; jjg@439: jjg@439: import com.sun.source.tree.CompilationUnitTree; jjg@439: import com.sun.source.util.JavacTask; jjg@439: import com.sun.tools.javac.api.JavacTool; jjg@439: import com.sun.tools.javac.tree.JCTree; jjg@439: import com.sun.tools.javac.tree.Pretty; jjg@439: jjg@439: /** jjg@439: * @test jjg@439: * @bug 6902720 jjg@439: * @summary javac pretty printer does not handle enums correctly jjg@439: */ jjg@439: jjg@439: public class Test { jjg@439: jjg@439: public static void main(String[] args) throws Exception { jjg@439: Test t = new Test(); jjg@439: t.run("E1.java", "E2.java"); jjg@439: } jjg@439: jjg@439: void run(String... args) throws Exception { jjg@439: File testSrcDir = new File(System.getProperty("test.src")); jjg@439: for (String arg: args) { jjg@439: test(new File(testSrcDir, arg)); jjg@439: } jjg@439: } jjg@439: jjg@439: void test(File test) throws Exception { jjg@439: JavacTool tool1 = JavacTool.create(); jjg@439: StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null); jjg@439: Iterable files = fm.getJavaFileObjects(test); jjg@439: jjg@439: // parse test file into a tree, and write it out to a stringbuffer using Pretty jjg@439: JavacTask t1 = tool1.getTask(null, fm, null, null, null, files); jjg@439: StringWriter sw = new StringWriter(); jjg@439: PrintWriter pw = new PrintWriter(sw); jjg@439: Iterable trees = t1.parse(); jjg@439: for (CompilationUnitTree tree: trees) { jjg@439: new Pretty(pw, true).printExpr((JCTree) tree); jjg@439: } jjg@439: pw.close(); jjg@439: jjg@439: final String out = sw.toString(); jjg@439: System.err.println("generated code:\n" + out + "\n"); jjg@439: jjg@439: // verify the generated code is valid Java by compiling it jjg@439: JavacTool tool2 = JavacTool.create(); jjg@439: JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) { jjg@439: @Override jjg@439: public CharSequence getCharContent(boolean ignoreEncodingErrors) { jjg@439: return out; jjg@439: } jjg@439: }; jjg@439: JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo)); jjg@439: boolean ok = t2.call(); jjg@439: if (!ok) jjg@439: throw new Exception("compilation of generated code failed"); jjg@439: jjg@439: File expectedClass = new File(test.getName().replace(".java", ".class")); jjg@439: if (!expectedClass.exists()) jjg@439: throw new Exception(expectedClass + " not found"); jjg@439: } jjg@439: } jjg@439: