Wed, 16 Jun 2010 16:23:27 -0700
6956638: JavacTask.generate does not generate all required files
Reviewed-by: darcy
Contributed-by: joshuamaurice@gmail.com
test/tools/javac/T6956638.java | file | annotate | diff | comparison | revisions |
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/T6956638.java Wed Jun 16 16:23:27 2010 -0700 1.3 @@ -0,0 +1,156 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.BufferedWriter; 1.28 +import java.io.File; 1.29 +import java.io.FileWriter; 1.30 +import java.io.IOException; 1.31 +import java.io.StringWriter; 1.32 +import java.util.Arrays; 1.33 +import java.util.Iterator; 1.34 +import java.util.List; 1.35 + 1.36 +import javax.lang.model.element.Element; 1.37 +import javax.tools.DiagnosticCollector; 1.38 +import javax.tools.JavaCompiler; 1.39 +import javax.tools.JavaFileObject; 1.40 +import javax.tools.StandardJavaFileManager; 1.41 +import javax.tools.ToolProvider; 1.42 + 1.43 +import com.sun.source.tree.CompilationUnitTree; 1.44 +import com.sun.source.util.JavacTask; 1.45 + 1.46 +/** 1.47 + * @test 1.48 + * @bug 6956638 1.49 + * @summary JavacTask.generate does not generate all required files 1.50 + */ 1.51 +public class T6956638 { 1.52 + public static void main(String[] args) throws Exception { 1.53 + new T6956638().run(); 1.54 + } 1.55 + 1.56 + void run() throws Exception { 1.57 + File srcDir = new File("src"); 1.58 + 1.59 + File[] files = { 1.60 + writeFile(new File(srcDir, "T1.java"), 1.61 + "public class T1 extends T2 {}\n"), 1.62 + writeFile(new File(srcDir, "T2.java"), 1.63 + "public class T2 extends T3 {}\n"), 1.64 + writeFile(new File(srcDir, "T3.java"), 1.65 + "public class T3 { public static final int C = 1; }\n"), 1.66 + writeFile(new File(srcDir, "Test.java"), 1.67 + "public class Test { public static final int D = T1.C; }\n") 1.68 + }; 1.69 + 1.70 + for (File f1: files) { 1.71 + for (File f2: files) { 1.72 + if (f2 == f1) continue; 1.73 + for (File f3: files) { 1.74 + if (f3 == f2 || f3 == f1) continue; 1.75 + for (File f4: files) { 1.76 + if (f4 == f3 || f4 == f2 || f4 == f1) continue; 1.77 + try { 1.78 + test(f1, f2, f3, f4); 1.79 + } catch (Exception e) { 1.80 + error(e); 1.81 + } 1.82 + } 1.83 + } 1.84 + } 1.85 + } 1.86 + 1.87 + if (errors > 0) 1.88 + throw new Exception(errors + " tests failed"); 1.89 + } 1.90 + 1.91 + void test(File... sourceFiles) throws Exception { 1.92 + System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles)); 1.93 + 1.94 + File classesDir = new File("classes" + count); 1.95 + classesDir.mkdirs(); 1.96 + 1.97 + StringWriter compilerOutputStream = new StringWriter(); 1.98 + 1.99 + List<String> compileOptions = Arrays.asList("-d", classesDir.getPath()); 1.100 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.101 + DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>(); 1.102 + StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null); 1.103 + Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles); 1.104 + System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects); 1.105 + JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects); 1.106 + JavacTask javacTask = (JavacTask) task; 1.107 + 1.108 + Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse(); 1.109 + Iterable<? extends Element> analyzedTrees = javacTask.analyze(); 1.110 + Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate(); 1.111 + 1.112 + System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles)); 1.113 + 1.114 + System.err.print("3-"); 1.115 + for (JavaFileObject f : generatedFiles) 1.116 + System.err.print(" " + f); 1.117 + System.err.println(""); 1.118 + 1.119 + System.err.print("5-"); 1.120 + for (File f : classesDir.listFiles()) 1.121 + System.err.print(" " + f); 1.122 + System.err.println(""); 1.123 + 1.124 + System.err.println("----"); 1.125 + System.err.println(compilerOutputStream.toString()); 1.126 + 1.127 + if (size(generatedFiles) != size(parsedTrees)) { 1.128 + throw new Exception("wrong number of files generated: " + size(generatedFiles) 1.129 + + " expected: " + size(parsedTrees)); 1.130 + } 1.131 + } 1.132 + 1.133 + private void error(Throwable t) { 1.134 + t.printStackTrace(); 1.135 + errors++; 1.136 + } 1.137 + 1.138 + int count; 1.139 + int errors; 1.140 + 1.141 + private static <E> int size(Iterable<E> x) { 1.142 + int n = 0; 1.143 + for (Iterator<E> iter = x.iterator(); iter.hasNext(); ++n) 1.144 + iter.next(); 1.145 + return n; 1.146 + } 1.147 + 1.148 + private static File writeFile(File f, String str) throws IOException { 1.149 + f.getParentFile().mkdirs(); 1.150 + BufferedWriter fout = new BufferedWriter(new FileWriter(f)); 1.151 + try { 1.152 + fout.write(str); 1.153 + fout.flush(); 1.154 + } finally { 1.155 + fout.close(); 1.156 + } 1.157 + return f; 1.158 + } 1.159 +}