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