T6956638.java revision 2687:56f8be952a5c
1/* 2 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.io.BufferedWriter; 25import java.io.File; 26import java.io.FileWriter; 27import java.io.IOException; 28import java.io.StringWriter; 29import java.util.Arrays; 30import java.util.Iterator; 31import java.util.List; 32 33import javax.lang.model.element.Element; 34import javax.tools.DiagnosticCollector; 35import javax.tools.JavaCompiler; 36import javax.tools.JavaFileObject; 37import javax.tools.StandardJavaFileManager; 38import javax.tools.ToolProvider; 39 40import com.sun.source.tree.CompilationUnitTree; 41import com.sun.source.util.JavacTask; 42 43/** 44 * @test 45 * @bug 6956638 46 * @summary JavacTask.generate does not generate all required files 47 */ 48public class T6956638 { 49 public static void main(String[] args) throws Exception { 50 new T6956638().run(); 51 } 52 53 void run() throws Exception { 54 File srcDir = new File("src"); 55 56 File[] files = { 57 writeFile(new File(srcDir, "T1.java"), 58 "public class T1 extends T2 {}\n"), 59 writeFile(new File(srcDir, "T2.java"), 60 "public class T2 extends T3 {}\n"), 61 writeFile(new File(srcDir, "T3.java"), 62 "public class T3 { public static final int C = 1; }\n"), 63 writeFile(new File(srcDir, "Test.java"), 64 "public class Test { public static final int D = T1.C; }\n") 65 }; 66 67 for (File f1: files) { 68 for (File f2: files) { 69 if (f2 == f1) continue; 70 for (File f3: files) { 71 if (f3 == f2 || f3 == f1) continue; 72 for (File f4: files) { 73 if (f4 == f3 || f4 == f2 || f4 == f1) continue; 74 try { 75 test(f1, f2, f3, f4); 76 } catch (Exception e) { 77 error(e); 78 } 79 } 80 } 81 } 82 } 83 84 if (errors > 0) 85 throw new Exception(errors + " tests failed"); 86 } 87 88 void test(File... sourceFiles) throws Exception { 89 System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles)); 90 91 File classesDir = new File("classes" + count); 92 classesDir.mkdirs(); 93 94 StringWriter compilerOutputStream = new StringWriter(); 95 96 List<String> compileOptions = Arrays.asList("-d", classesDir.getPath()); 97 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 98 DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>(); 99 try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null)) { 100 Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles); 101 System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects); 102 JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects); 103 JavacTask javacTask = (JavacTask) task; 104 105 Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse(); 106 Iterable<? extends Element> analyzedTrees = javacTask.analyze(); 107 Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate(); 108 109 System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles)); 110 111 System.err.print("3-"); 112 for (JavaFileObject f : generatedFiles) 113 System.err.print(" " + f); 114 System.err.println(""); 115 116 System.err.print("5-"); 117 for (File f : classesDir.listFiles()) 118 System.err.print(" " + f); 119 System.err.println(""); 120 121 System.err.println("----"); 122 System.err.println(compilerOutputStream.toString()); 123 124 if (size(generatedFiles) != size(parsedTrees)) { 125 throw new Exception("wrong number of files generated: " + size(generatedFiles) 126 + " expected: " + size(parsedTrees)); 127 } 128 } 129 } 130 131 private void error(Throwable t) { 132 t.printStackTrace(); 133 errors++; 134 } 135 136 int count; 137 int errors; 138 139 private static <E> int size(Iterable<E> x) { 140 int n = 0; 141 for (Iterator<E> iter = x.iterator(); iter.hasNext(); ++n) 142 iter.next(); 143 return n; 144 } 145 146 private static File writeFile(File f, String str) throws IOException { 147 f.getParentFile().mkdirs(); 148 BufferedWriter fout = new BufferedWriter(new FileWriter(f)); 149 try { 150 fout.write(str); 151 fout.flush(); 152 } finally { 153 fout.close(); 154 } 155 return f; 156 } 157} 158