Path: blob/master/test/langtools/tools/javac/6902720/Test.java
41149 views
/*1* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;24import java.net.*;25import javax.tools.*;26import java.util.*;2728import com.sun.source.tree.CompilationUnitTree;29import com.sun.source.util.JavacTask;30import com.sun.tools.javac.api.JavacTool;31import com.sun.tools.javac.tree.JCTree;32import com.sun.tools.javac.tree.Pretty;3334/**35* @test36* @bug 690272037* @summary javac pretty printer does not handle enums correctly38* @modules jdk.compiler/com.sun.tools.javac.api39* jdk.compiler/com.sun.tools.javac.file40* jdk.compiler/com.sun.tools.javac.tree41*/4243public class Test {4445public static void main(String[] args) throws Exception {46Test t = new Test();47t.run("E1.java", "E2.java");48}4950void run(String... args) throws Exception {51File testSrcDir = new File(System.getProperty("test.src"));52for (String arg: args) {53test(new File(testSrcDir, arg));54}55}5657void test(File test) throws Exception {58JavacTool tool1 = JavacTool.create();59try (StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null)) {60Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);6162// parse test file into a tree, and write it out to a stringbuffer using Pretty63JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);64StringWriter sw = new StringWriter();65PrintWriter pw = new PrintWriter(sw);66Iterable<? extends CompilationUnitTree> trees = t1.parse();67for (CompilationUnitTree tree: trees) {68new Pretty(pw, true).printExpr((JCTree) tree);69}70pw.close();7172final String out = sw.toString();73System.err.println("generated code:\n" + out + "\n");7475// verify the generated code is valid Java by compiling it76JavacTool tool2 = JavacTool.create();77JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {78@Override79public CharSequence getCharContent(boolean ignoreEncodingErrors) {80return out;81}82};83JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));84boolean ok = t2.call();85if (!ok)86throw new Exception("compilation of generated code failed");8788File expectedClass = new File(test.getName().replace(".java", ".class"));89if (!expectedClass.exists())90throw new Exception(expectedClass + " not found");91}92}93}94959697