Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6902720/Test.java
38813 views
/*1* Copyright (c) 2009, 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*/3940public class Test {4142public static void main(String[] args) throws Exception {43Test t = new Test();44t.run("E1.java", "E2.java");45}4647void run(String... args) throws Exception {48File testSrcDir = new File(System.getProperty("test.src"));49for (String arg: args) {50test(new File(testSrcDir, arg));51}52}5354void test(File test) throws Exception {55JavacTool tool1 = JavacTool.create();56StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);57Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);5859// parse test file into a tree, and write it out to a stringbuffer using Pretty60JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);61StringWriter sw = new StringWriter();62PrintWriter pw = new PrintWriter(sw);63Iterable<? extends CompilationUnitTree> trees = t1.parse();64for (CompilationUnitTree tree: trees) {65new Pretty(pw, true).printExpr((JCTree) tree);66}67pw.close();6869final String out = sw.toString();70System.err.println("generated code:\n" + out + "\n");7172// verify the generated code is valid Java by compiling it73JavacTool tool2 = JavacTool.create();74JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {75@Override76public CharSequence getCharContent(boolean ignoreEncodingErrors) {77return out;78}79};80JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));81boolean ok = t2.call();82if (!ok)83throw new Exception("compilation of generated code failed");8485File expectedClass = new File(test.getName().replace(".java", ".class"));86if (!expectedClass.exists())87throw new Exception(expectedClass + " not found");88}89}90919293